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

Commit

Permalink
Merge pull request #119 from xwp/improve/migration
Browse files Browse the repository at this point in the history
Improve migration
  • Loading branch information
westonruter committed Feb 24, 2017
2 parents cd5a18f + ecfd2a5 commit dbee0d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion js/customize-migrate.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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' ) );
}
}
}
Expand Down
34 changes: 25 additions & 9 deletions php/class-migrate.php
Original file line number Diff line number Diff line change
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' ) );
}

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 . '.' );
} else {
\WP_CLI::error( __( ' Failed to migrate', '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

0 comments on commit dbee0d5

Please sign in to comment.