PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
2.2.14 2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
← All changes | includes/Core/DataMigration.php +45 -37 2.2.82.2.14 View file →
@@ -1,29 +1,32 @@
1 1 <?php
2 +/**
3 + * One-off migration of legacy block markup to TableKit's current namespace
4 + *
5 + * @package TableKit
6 + */
2 7
3 8 namespace TableBuilder\Core;
4 9
5 -defined('ABSPATH') || exit;
10 +defined( 'ABSPATH' ) || exit;
6 11
7 12 /**
8 - * Enqueue registrar.
13 + * Handles one-off migration of legacy block markup after a plugin update.
9 14 *
10 15 * @since 1.0.0
11 16 * @access public
12 17 */
13 -class DataMigration
14 -{
18 +class DataMigration {
19 +
15 20 /**
16 - * Class constructor.
17 - * private for singleton
21 + * Hooks the editor-recovery script and the post-upgrade block migration into WordPress.
18 22 *
19 23 * @return void
20 24 * @since 1.0.0
21 25 */
22 - public function __construct()
23 - {
24 - add_action('enqueue_block_editor_assets', array($this, 'editor_assets'));
25 - add_action('upgrader_process_complete', array($this, 'migrate_old_blocks'), 10, 2);
26 + public function __construct() {
27 + add_action( 'enqueue_block_editor_assets', array( $this, 'editor_assets' ) );
28 + add_action( 'upgrader_process_complete', array( $this, 'migrate_old_blocks' ), 10, 2 );
26 29 }
27 30
28 31 /**
29 32 * Enqueues necessary scripts and localizes data for the admin area.
@@ -31,15 +34,16 @@
31 34 * @param string $hook The current page.
32 35 * @return void
33 36 * @since 1.0.0
34 37 */
35 - public function editor_assets($hook)
36 - {
37 - if (!in_array('auto-block-recovery/auto-block-recovery.php', apply_filters('active_plugins', get_option('active_plugins')))) {
38 + public function editor_assets( $hook ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- required by the "enqueue_block_editor_assets" action signature; not needed in the body.
39 + $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- "active_plugins" is a WordPress core filter, not defined by this plugin.
40 +
41 + if ( ! in_array( 'auto-block-recovery/auto-block-recovery.php', $active_plugins, true ) ) {
38 42 wp_enqueue_script(
39 43 'auto-block-recovery',
40 44 TABLE_BUILDER_BLOCK_PLUGIN_URL . 'assets/js/auto-block-recovery.js',
41 - array('wp-blocks', 'wp-data', 'wp-dom-ready', 'wp-i18n'),
45 + array( 'wp-blocks', 'wp-data', 'wp-dom-ready', 'wp-i18n' ),
42 46 TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
43 47 true
44 48 );
45 49 }
@@ -45,23 +49,22 @@
45 49 }
46 50 }
47 51
48 52 /**
49 - * Migrate old blocks to new format.
53 + * Migrates old blocks to new format.
50 54 *
51 55 * @param object $upgrader_object The upgrader object.
52 56 * @param array $options The options passed to the upgrader.
53 57 * @return void
54 58 */
55 - public function migrate_old_blocks($upgrader_object, $options)
56 - {
59 + public function migrate_old_blocks( $upgrader_object, $options ) {
57 60 $our_plugin = 'table-builder-block/table-builder-block.php';
58 - if (!empty($options['plugins']) && $options['action'] == 'update' && $options['type'] == 'plugin') {
59 - foreach ($options['plugins'] as $plugin) {
60 - if ($plugin == $our_plugin) {
61 - if (!get_transient('table_builder_block_migrate')) {
61 + if ( ! empty( $options['plugins'] ) && 'update' === $options['action'] && 'plugin' === $options['type'] ) {
62 + foreach ( $options['plugins'] as $plugin ) {
63 + if ( $our_plugin === $plugin ) {
64 + if ( ! get_transient( 'table_builder_block_migrate' ) ) {
62 65 $this->table_builder_block_migrate_old_blocks();
63 - set_transient('table_builder_block_migrate', TABLE_BUILDER_BLOCK_PLUGIN_VERSION);
66 + set_transient( 'table_builder_block_migrate', TABLE_BUILDER_BLOCK_PLUGIN_VERSION );
64 67 }
65 68 }
66 69 }
67 70 }
@@ -67,27 +70,30 @@
67 70 }
68 71 }
69 72
70 73 /**
71 - * Migrate old blocks to new format.
74 + * Rewrites legacy "gutenkit/table-builder*" block markup to the current
75 + * "tablebuilder/table-builder*" namespace across all matching posts.
72 76 *
73 77 * @return void
74 78 */
75 - public function table_builder_block_migrate_old_blocks()
76 - {
79 + public function table_builder_block_migrate_old_blocks() {
77 80 global $wpdb;
78 81
79 - $posts = $wpdb->get_results("
82 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-off migration run at most once per plugin upgrade (see migrate_old_blocks() above), not a per-request query; caching would add complexity for no benefit here.
83 + $posts = $wpdb->get_results(
84 + "
80 85 SELECT ID, post_content
81 86 FROM {$wpdb->posts}
82 87 WHERE post_type IN ('post', 'page', 'your_custom_post_type')
83 88 AND post_status IN ('publish', 'draft', 'pending', 'private')
84 89 AND post_content LIKE '%gutenkit/%'
85 - ");
90 + "
91 + );
86 92
87 - foreach ($posts as $post) {
93 + foreach ( $posts as $post ) {
88 94 $updated_content = str_replace(
89 - [
95 + array(
90 96 'wp:gutenkit/table-builder',
91 97 'wp:gutenkit/table-builder-row',
92 98 'wp:gutenkit/table-builder-item',
93 99 'gutenkit/table-builder',
@@ -92,10 +98,10 @@
92 98 'wp:gutenkit/table-builder-item',
93 99 'gutenkit/table-builder',
94 100 'gutenkit/table-builder-row',
95 101 'gutenkit/table-builder-item',
96 - ],
97 - [
102 + ),
103 + array(
98 104 'wp:tablebuilder/table-builder',
99 105 'wp:tablebuilder/table-builder-row',
100 106 'wp:tablebuilder/table-builder-item',
101 107 'tablebuilder/table-builder',
@@ -100,17 +106,19 @@
100 106 'wp:tablebuilder/table-builder-item',
101 107 'tablebuilder/table-builder',
102 108 'tablebuilder/table-builder-row',
103 109 'tablebuilder/table-builder-item',
104 - ],
110 + ),
105 111 $post->post_content
106 112 );
107 113
108 - if ($updated_content !== $post->post_content) {
109 - wp_update_post([
110 - 'ID' => $post->ID,
111 - 'post_content' => $updated_content,
112 - ]);
114 + if ( $updated_content !== $post->post_content ) {
115 + wp_update_post(
116 + array(
117 + 'ID' => $post->ID,
118 + 'post_content' => $updated_content,
119 + )
120 + );
113 121 }
114 122 }
115 123 }
116 124 }