Skip to content

Commit

Permalink
Lots of PHPCS styling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonyea committed Nov 7, 2020
1 parent af60000 commit 7f6d132
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 118 deletions.
44 changes: 20 additions & 24 deletions plugins/wp2grav-all.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@



function wp2grav_export_all ( $args, $assoc_args ) {
WP_CLI::line( "Exporting all available content" );
function wp2grav_export_all( $args, $assoc_args ) {
WP_CLI::line( 'Exporting all available content' );

$options = [
'return' => true, // Return 'STDOUT'; use 'all' for full object.
//'parse' => 'json', // Parse captured STDOUT to JSON array.
'launch' => false, // Reuse the current process.
'exit_error' => true, // Halt script execution on error.
];
$options = array(
'return' => TRUE, // Return 'STDOUT'; use 'all' for full object.
'launch' => FALSE, // Reuse the current process.
'exit_error' => TRUE, // Halt script execution on error.
);

// Find all exporter plugins. Assumes filename is the wp-cli command.
$plugin_dir = plugin_dir_path( __FILE__ );
$export_plugins = glob($plugin_dir . "wp2grav-*");
// Find all exporter plugins. Assumes filename is the wp-cli command.
$plugin_dir = plugin_dir_path( __FILE__ );
$export_plugins = glob( $plugin_dir . 'wp2grav-*' );



foreach ($export_plugins as $exporter){
$command = substr( $exporter, strlen( $plugin_dir ) );
// Drops '.php' extension.
$command = substr( $command, 0 , (strlen( $command ) - 4 ) );
foreach ( $export_plugins as $exporter ) {
$command = substr( $exporter, strlen( $plugin_dir ) );
// Drops '.php' extension.
$command = substr( $command, 0, ( strlen( $command ) - 4 ) );

if ($command != "wp2grav-all"){
WP_CLI::runcommand( $command, $options );
}
}
WP_CLI::success( "Completed exporting " . (count($export_plugins) - 1) . " plugins" );

}
if ( $command != 'wp2grav-all' ) {
WP_CLI::runcommand( $command, $options );
}
}
WP_CLI::line( WP_CLI::colorize('%GSuccess:%n Completed exporting ' . ( count( $export_plugins ) - 1 ) . ' plugins' ));
}
81 changes: 81 additions & 0 deletions plugins/wp2grav-post_types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* WP-CLI custom command: Exports WP post types in GravCMS format.
* Syntax: wp wp2grav-users
*/

use Symfony\Component\Yaml\Yaml;

/**
* Exports WP user content as GravCMS account yaml files.
*
* @throws Exception
*/
function wp2grav_export_post_types() {
WP_CLI::line( WP_CLI::colorize( '%YBeginning post types export%n ' ) );
$plugin_dir = plugin_dir_path( __FILE__ );
$export_folder = WP_CONTENT_DIR . '/uploads/wp2grav-exports/' . gmdate( 'Ymd' ) . '/';

$files_export_folder = $export_folder . 'data/files/wp-uploads/';
$theme_export_folder = $export_folder . 'themes/wp-export/';
$templates_export_folder = $export_folder . 'themes/wp-export/templates/';
$blueprints_export_folder = $export_folder . 'themes/wp-export/blueprints/';

if ( ! wp_mkdir_p( $export_folder ) ||
! wp_mkdir_p( $files_export_folder ) ||
! wp_mkdir_p( $theme_export_folder ) ||
! wp_mkdir_p( $blueprints_export_folder )
) {
WP_CLI::error( 'Post Types: Could not create export folders ' );
die();
}

// Find all custom post_types.
$args = array(
'public' => true,
);

$post_types = get_post_types( $args );
unset( $post_types['attachment'] );

if ( ! $post_types ) {
WP_CLI::error( 'No post types found. Stopping export', $exit = true );
}

// Creates a new progress bar.
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating post_type data', count( $post_types ), $interval = 100 );

foreach ( $post_types as $post_type ) {
$progress->tick();
$blueprint_component = dirname( $plugin_dir ) . '/grav_components/contentType_blueprint.yaml';
$blueprint = Yaml::parseFile( $blueprint_component );
$blueprint['title'] = $post_type;

// Reset $new_fields for each content type.
$new_fields = null;

$post_type_object = get_post_type_object( $post_type );

if ( $post_type == 'attachment' ) {
// create custom
continue;
}
$post_type_features = get_all_post_type_supports( $post_type );

// Output post type features to blueprint yaml file for use in admin GUI.
$yaml_output = Yaml::dump( $blueprint, 20, 4 );

try {
if ( ! file_put_contents( $blueprints_export_folder . $post_type . '.yaml', $yaml_output ) ) {
throw new Exception( 'Could not save ' . $post_type . '.yaml blueprint file' );
}
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage(), $exit = true );
}

$template_content = "{% extends 'partials/base.html.twig' %}\n\n{% block content %}\n {{ page.content }}\n{% endblock %}\n";
}
$progress->finish();

WP_CLI::success( 'Saved Complete! ' . count( $post_types ) . " post types exported to $blueprints_export_folder" );
}
114 changes: 52 additions & 62 deletions plugins/wp2grav-roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,55 @@
use Symfony\Component\Yaml\Yaml;


function wp2grav_export_roles ( $args, $assoc_args ) {
WP_CLI::line( WP_CLI::colorize( "%YBeginning role export%n " ) );
$export_folder = WP_CONTENT_DIR . '/uploads/wp2grav-exports/' . date('Ymd') . "/config/";


$roles = get_editable_roles();
$groups = [];
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating roles data', count($roles), $interval = 100 );
foreach ($roles as $key => $role) {



if ( !wp_mkdir_p($export_folder) ) {
WP_CLI::error( "Could not create export folder" );
die();
}

//WP_CLI::line( " |- Exporting role '" . $role["name"] . "' (" . $key. ")" );

$role_name = "wp_" . convert_role_wp_to_grav($key);
$groups[$role_name]['icon'] = "cog";
$groups[$role_name]['readableName'] = convert_role_wp_to_grav($role["name"]);
$groups[$role_name]['description'] = "Exported Wordpress role " . convert_role_wp_to_grav($key);
$groups[$role_name]['access']['site']['login'] = TRUE;

// Grant administrator role further Grav Admin access.
if ($key == 'administrator') {
$groups[$role_name]['access']['admin'] = array(
"login" => TRUE,
"super" => TRUE,
);
}
$progress->tick();
}

// Create new role of authenticated user that will grant basic admin login to all exported users.
if ( !array_key_exists( 'wp_authenticated_user', $groups ) ) {
$groups['wp_authenticated_user']['icon'] = "cog";
$groups['wp_authenticated_user']['readableName'] = convert_role_wp_to_grav($role["name"]);
$groups['wp_authenticated_user']['description'] = "Exported Wordpress role " . convert_role_wp_to_grav($key);
$groups['wp_authenticated_user']['access']['site']['login'] = TRUE;
}

// Finish the progress bar.
$progress->finish();



WP_CLI::line( "Saving role export data to $export_folder/groups.yaml" );
$group_content = Yaml::dump($groups, 20, 4);

try {
if ( !file_put_contents( $export_folder . '/groups.yaml', $group_content) ) {
throw new Exception( 'Could not save groups.yaml export file' );
}

}
catch (Exception $e) {
WP_CLI::error( $e->getMessage(), $exit = TRUE );
}
WP_CLI::success( (count($roles) + 1) . " roles exported" );
}
function wp2grav_export_roles( $args, $assoc_args ) {
WP_CLI::line( WP_CLI::colorize( '%YBeginning role export%n ' ) );
$export_folder = WP_CONTENT_DIR . '/uploads/wp2grav-exports/' . gmdate( 'Ymd' ) . '/config/';

$roles = get_editable_roles();
$groups = array();
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating roles data', count( $roles ), $interval = 100 );
foreach ( $roles as $key => $role ) {
if ( ! wp_mkdir_p( $export_folder ) ) {
WP_CLI::error( 'Could not create export folder' );
die();
}

$role_name = 'wp_' . convert_role_wp_to_grav( $key );
$groups[ $role_name ]['icon'] = 'cog';
$groups[ $role_name ]['readableName'] = convert_role_wp_to_grav( $role['name'] );
$groups[ $role_name ]['description'] = 'Exported WordPress role ' . convert_role_wp_to_grav( $key );
$groups[ $role_name ]['access']['site']['login'] = true;

// Grant administrator role further Grav Admin access.
if ( $key == 'administrator' ) {
$groups[ $role_name ]['access']['admin'] = array(
'login' => true,
'super' => true,
);
}
$progress->tick();
}

// Create new role of authenticated user that will grant basic admin login to all exported users.
if ( ! array_key_exists( 'wp_authenticated_user', $groups ) ) {
$groups['wp_authenticated_user']['icon'] = 'cog';
$groups['wp_authenticated_user']['readableName'] = convert_role_wp_to_grav( $role['name'] );
$groups['wp_authenticated_user']['description'] = 'Exported WordPress role ' . convert_role_wp_to_grav( $key );
$groups['wp_authenticated_user']['access']['site']['login'] = true;
}

// Finish the progress bar.
$progress->finish();

WP_CLI::line( "Saving role export data to $export_folder/groups.yaml" );
$group_content = Yaml::dump( $groups, 20, 4 );

try {
if ( ! file_put_contents( $export_folder . '/groups.yaml', $group_content ) ) {
throw new Exception( 'Could not save groups.yaml export file' );
}
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage(), $exit = true );
}
WP_CLI::success( ( count( $roles ) + 1 ) . ' roles exported' );
}
48 changes: 17 additions & 31 deletions plugins/wp2grav-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ function wp2grav_export_users() {
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating users data', count( $users ), $interval = 100 );

foreach ( $users as $user ) {
$progress->tick();
$progress->tick();

// Reset account.
$account_content = null;

$account_content['email'] = $user->get( 'user_email' );
$account_content['wp']['id'] = $user->get( 'ID' );
$account_content['wp']['user_url'] = $user->get('user_url');
$account_content['wp']['display_name'] = $user->get('display_name');
$account_content['wp']['nickname'] = $user->nickname;
$account_content['fullname'] = $user->first_name . " " . $user->last_name;
$account_content['title'] = null;
$account_content['email'] = $user->get( 'user_email' );
$account_content['wp']['id'] = $user->get( 'ID' );
$account_content['wp']['user_url'] = $user->get( 'user_url' );
$account_content['wp']['display_name'] = $user->get( 'display_name' );
$account_content['wp']['nickname'] = $user->nickname;
$account_content['fullname'] = $user->first_name . ' ' . $user->last_name;
$account_content['title'] = null;
// Default WordPress doesn't have the concept of a disabled user.
$account_content['state'] = 'enabled';

Expand All @@ -58,28 +58,14 @@ function wp2grav_export_users() {
$account = Yaml::dump( $account_content, 20, 4 );
$account .= 'login_attempts: { }';
$filename = convert_username_wp_to_grav( $user );
try {
if ( !file_put_contents( $export_folder . $filename . '.yaml', $account) ) {
throw new Exception( 'Could not save ' . $filename . '.yaml export file' );
}

}
catch (Exception $e) {
WP_CLI::error( $e->getMessage(), $exit = TRUE );
}

try {
if ( ! file_put_contents( $export_folder . $filename . '.yaml', $account ) ) {
throw new Exception( 'Could not save ' . $filename . '.yaml export file' );
}
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage(), $exit = true );
}
}
WP_CLI::success('Saved Complete! ' . count( $users ) . " user accounts exported to $export_folder" );
WP_CLI::success( 'Saved Complete! ' . count( $users ) . " user accounts exported to $export_folder" );
$progress->finish();
}


function random_str( $length = 16 ) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
14 changes: 13 additions & 1 deletion wp2grav_exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
$plugin_dir = plugin_dir_path( __FILE__ );
// Load plugins.
require_once ( $plugin_dir . "plugins/wp2grav-all.php" );
require_once ( $plugin_dir . "plugins/wp2grav-post_types.php" );
require_once ( $plugin_dir . "plugins/wp2grav-roles.php" );
require_once ( $plugin_dir . "plugins/wp2grav-users.php" );



// Register commands with wp-cli.
WP_CLI::add_command( 'wp2grav-all', 'wp2grav_export_all' );
WP_CLI::add_command( 'wp2grav-post_types', 'wp2grav_export_post_types' );
WP_CLI::add_command( 'wp2grav-roles', 'wp2grav_export_roles' );
WP_CLI::add_command( 'wp2grav-users', 'wp2grav_export_users' );
}
Expand Down Expand Up @@ -108,4 +110,14 @@ function connect_fs($url, $method, $context, $fields = null)
}

return true;
}
}

function random_str( $length = 16 ) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

0 comments on commit 7f6d132

Please sign in to comment.