Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Improve migration #119

Merged
merged 1 commit into from Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/customize-migrate.js
Expand Up @@ -3,7 +3,7 @@
'use strict';
var component = {
doingAjax: false,
postMigrationCount: 20
postMigrationCount: 5
};

/**
Expand Down
6 changes: 4 additions & 2 deletions php/class-customize-snapshot-command.php
Expand Up @@ -44,11 +44,13 @@ public function migrate( $arg, $assoc_args ) {
}
$dry_mode = isset( $assoc_args['dry-run'] );
if ( ! $dry_mode ) {
wp_suspend_cache_addition( true );
$post_count = $migrate_obj->changeset_migrate();
\WP_CLI::success( $post_count . ' ' . __( 'posts migrated.', 'customize-snapshots' ) );
} else {
$ids = $migrate_obj->changeset_migrate( - 1, true );
\WP_CLI::success( count( $ids ) . ' ' . __( 'posts migrated:', 'customize-snapshots' ) . ' ' . implode( ',', $ids ) );
$ids = $migrate_obj->changeset_migrate( -1, true );
\WP_CLI::success( __( 'Posts migrated:', 'customize-snapshots' ) . ' ' . implode( ',', $ids ) );
\WP_CLI::success( 'Total ' . count( $ids ) . ' ' . __( 'posts migrated.', 'customize-snapshots' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the string to something like this:

\WP_CLI::success( sprintf( __( 'Total posts migrated: %s', 'customize-snapshots' ), count( $ids ) ) );

}
}
}
Expand Down
34 changes: 25 additions & 9 deletions php/class-migrate.php
Expand Up @@ -102,6 +102,7 @@ public function show_migration_notice() {
* @return int|array migration status or posts.
*/
public function changeset_migrate( $limit = -1, $dry_run = false ) {
$is_doing_cli = defined( 'WP_CLI' ) && WP_CLI;
$query = new \WP_Query();
$arg = array(
'post_type' => 'customize_snapshot',
Expand All @@ -122,6 +123,10 @@ public function changeset_migrate( $limit = -1, $dry_run = false ) {
return $query->posts;
}

if ( $is_doing_cli ) {
\WP_CLI::log( __( 'Migrating', 'customize-snapshots' ) . ' ' . count( $query->posts ) . __( ' snapshots into changeset', 'customize-snapshots' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

\WP_CLI::log( sprintf( __( 'Migrating %s Snapshots into Changeset', 'customize-snapshots' ), count( $query->posts ) ) );

}

if ( ! empty( $query->posts ) ) {
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
if ( $has_kses ) {
Expand All @@ -131,7 +136,14 @@ public function changeset_migrate( $limit = -1, $dry_run = false ) {
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
}
foreach ( $query->posts as $id ) {
$this->migrate_post( $id );
$success = $this->migrate_post( $id );
if ( $is_doing_cli ) {
if ( $success ) {
\WP_CLI::success( __( 'Migrated post', 'customize-snapshots' ) . ' ' . $id . '.' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

\WP_CLI::success( sprintf( __( 'Migrated post %s.', 'customize-snapshots' ), $id ) );

} else {
\WP_CLI::error( __( ' Failed to migrate', 'customize-snapshots' ) . ' ' . $id . '.' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to:

\WP_CLI::error( sprintf( __( 'Failed to migrate post %s.', 'customize-snapshots' ), $id ) );

}
}
}
if ( $has_kses ) {
kses_init_filters();
Expand All @@ -153,7 +165,7 @@ public function changeset_migrate( $limit = -1, $dry_run = false ) {
* @global \WP_Customize_Manager $wp_customize
*/
public function migrate_post( $id ) {
global $wp_customize;
global $wp_customize, $wpdb;

$post = get_post( $id );

Expand All @@ -166,7 +178,7 @@ public function migrate_post( $id ) {
// Get manager instance.
$manager = new \WP_Customize_Manager();
$original_manager = $wp_customize;
$wp_customize = $manager; // Export to global since some filters (like widget_customizer_setting_args) lack as $wp_customize context and need global.
$wp_customize = $manager; // Export to global since some filters (like widget_customizer_setting_args) lack as $wp_customize context and need global. WPCS: override ok.

// Validate data.
foreach ( $data as $setting_id => $setting_params ) {
Expand Down Expand Up @@ -203,13 +215,17 @@ public function migrate_post( $id ) {
$post_data[ $prefixed_setting_id ]['type'] = $setting->type;
}
}
$maybe_updated = wp_update_post( wp_slash( array(
'ID' => $post->ID,
'post_type' => 'customize_changeset',
'post_content' => Customize_Snapshot_Manager::encode_json( $post_data ),
) ), true );
$maybe_updated = $wpdb->update( $wpdb->posts, array(
'post_type' => 'customize_changeset',
'post_content' => Customize_Snapshot_Manager::encode_json( $post_data ),
),
array(
'ID' => $post->ID,
)
);
clean_post_cache( $post );

$wp_customize = $original_manager; // Restore previous manager.
$wp_customize = $original_manager; // Restore previous manager. WPCS: override ok.

return $maybe_updated;
}
Expand Down