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

Commit dbee0d5

Browse files
authored
Merge pull request #119 from xwp/improve/migration
Improve migration
2 parents cd5a18f + ecfd2a5 commit dbee0d5

3 files changed

+30
-12
lines changed

js/customize-migrate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict';
44
var component = {
55
doingAjax: false,
6-
postMigrationCount: 20
6+
postMigrationCount: 5
77
};
88

99
/**

php/class-customize-snapshot-command.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ public function migrate( $arg, $assoc_args ) {
4444
}
4545
$dry_mode = isset( $assoc_args['dry-run'] );
4646
if ( ! $dry_mode ) {
47+
wp_suspend_cache_addition( true );
4748
$post_count = $migrate_obj->changeset_migrate();
4849
\WP_CLI::success( $post_count . ' ' . __( 'posts migrated.', 'customize-snapshots' ) );
4950
} else {
50-
$ids = $migrate_obj->changeset_migrate( - 1, true );
51-
\WP_CLI::success( count( $ids ) . ' ' . __( 'posts migrated:', 'customize-snapshots' ) . ' ' . implode( ',', $ids ) );
51+
$ids = $migrate_obj->changeset_migrate( -1, true );
52+
\WP_CLI::success( __( 'Posts migrated:', 'customize-snapshots' ) . ' ' . implode( ',', $ids ) );
53+
\WP_CLI::success( 'Total ' . count( $ids ) . ' ' . __( 'posts migrated.', 'customize-snapshots' ) );
5254
}
5355
}
5456
}

php/class-migrate.php

+25-9
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public function show_migration_notice() {
102102
* @return int|array migration status or posts.
103103
*/
104104
public function changeset_migrate( $limit = -1, $dry_run = false ) {
105+
$is_doing_cli = defined( 'WP_CLI' ) && WP_CLI;
105106
$query = new \WP_Query();
106107
$arg = array(
107108
'post_type' => 'customize_snapshot',
@@ -122,6 +123,10 @@ public function changeset_migrate( $limit = -1, $dry_run = false ) {
122123
return $query->posts;
123124
}
124125

126+
if ( $is_doing_cli ) {
127+
\WP_CLI::log( __( 'Migrating', 'customize-snapshots' ) . ' ' . count( $query->posts ) . __( ' snapshots into changeset', 'customize-snapshots' ) );
128+
}
129+
125130
if ( ! empty( $query->posts ) ) {
126131
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
127132
if ( $has_kses ) {
@@ -131,7 +136,14 @@ public function changeset_migrate( $limit = -1, $dry_run = false ) {
131136
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
132137
}
133138
foreach ( $query->posts as $id ) {
134-
$this->migrate_post( $id );
139+
$success = $this->migrate_post( $id );
140+
if ( $is_doing_cli ) {
141+
if ( $success ) {
142+
\WP_CLI::success( __( 'Migrated post', 'customize-snapshots' ) . ' ' . $id . '.' );
143+
} else {
144+
\WP_CLI::error( __( ' Failed to migrate', 'customize-snapshots' ) . ' ' . $id . '.' );
145+
}
146+
}
135147
}
136148
if ( $has_kses ) {
137149
kses_init_filters();
@@ -153,7 +165,7 @@ public function changeset_migrate( $limit = -1, $dry_run = false ) {
153165
* @global \WP_Customize_Manager $wp_customize
154166
*/
155167
public function migrate_post( $id ) {
156-
global $wp_customize;
168+
global $wp_customize, $wpdb;
157169

158170
$post = get_post( $id );
159171

@@ -166,7 +178,7 @@ public function migrate_post( $id ) {
166178
// Get manager instance.
167179
$manager = new \WP_Customize_Manager();
168180
$original_manager = $wp_customize;
169-
$wp_customize = $manager; // Export to global since some filters (like widget_customizer_setting_args) lack as $wp_customize context and need global.
181+
$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.
170182

171183
// Validate data.
172184
foreach ( $data as $setting_id => $setting_params ) {
@@ -203,13 +215,17 @@ public function migrate_post( $id ) {
203215
$post_data[ $prefixed_setting_id ]['type'] = $setting->type;
204216
}
205217
}
206-
$maybe_updated = wp_update_post( wp_slash( array(
207-
'ID' => $post->ID,
208-
'post_type' => 'customize_changeset',
209-
'post_content' => Customize_Snapshot_Manager::encode_json( $post_data ),
210-
) ), true );
218+
$maybe_updated = $wpdb->update( $wpdb->posts, array(
219+
'post_type' => 'customize_changeset',
220+
'post_content' => Customize_Snapshot_Manager::encode_json( $post_data ),
221+
),
222+
array(
223+
'ID' => $post->ID,
224+
)
225+
);
226+
clean_post_cache( $post );
211227

212-
$wp_customize = $original_manager; // Restore previous manager.
228+
$wp_customize = $original_manager; // Restore previous manager. WPCS: override ok.
213229

214230
return $maybe_updated;
215231
}

0 commit comments

Comments
 (0)