This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
Improve migration #119
Merged
Merged
Improve migration #119
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
'use strict'; | ||
var component = { | ||
doingAjax: false, | ||
postMigrationCount: 20 | ||
postMigrationCount: 5 | ||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
|
@@ -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' ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to:
|
||
} | ||
|
||
if ( ! empty( $query->posts ) ) { | ||
$has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) ); | ||
if ( $has_kses ) { | ||
|
@@ -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 . '.' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to:
|
||
} else { | ||
\WP_CLI::error( __( ' Failed to migrate', 'customize-snapshots' ) . ' ' . $id . '.' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to:
|
||
} | ||
} | ||
} | ||
if ( $has_kses ) { | ||
kses_init_filters(); | ||
|
@@ -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 ); | ||
|
||
|
@@ -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 ) { | ||
|
@@ -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; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: