PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.13
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.13
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
table-builder-block / includes / Core / DataMigration.php

DataMigration.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.13, at includes/Core/DataMigration.php

125 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * One-off migration of legacy block markup to TableKit's current namespace
4 *
5 * @package TableKit
6 */
7
8 namespace TableBuilder\Core;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Handles one-off migration of legacy block markup after a plugin update.
14 *
15 * @since 1.0.0
16 * @access public
17 */
18 class DataMigration {
19
20 /**
21 * Hooks the editor-recovery script and the post-upgrade block migration into WordPress.
22 *
23 * @return void
24 * @since 1.0.0
25 */
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 );
29 }
30
31 /**
32 * Enqueues necessary scripts and localizes data for the admin area.
33 *
34 * @param string $hook The current page.
35 * @return void
36 * @since 1.0.0
37 */
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 ) ) {
42 wp_enqueue_script(
43 'auto-block-recovery',
44 TABLE_BUILDER_BLOCK_PLUGIN_URL . 'assets/js/auto-block-recovery.js',
45 array( 'wp-blocks', 'wp-data', 'wp-dom-ready', 'wp-i18n' ),
46 TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
47 true
48 );
49 }
50 }
51
52 /**
53 * Migrates old blocks to new format.
54 *
55 * @param object $upgrader_object The upgrader object.
56 * @param array $options The options passed to the upgrader.
57 * @return void
58 */
59 public function migrate_old_blocks( $upgrader_object, $options ) {
60 $our_plugin = 'table-builder-block/table-builder-block.php';
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' ) ) {
65 $this->table_builder_block_migrate_old_blocks();
66 set_transient( 'table_builder_block_migrate', TABLE_BUILDER_BLOCK_PLUGIN_VERSION );
67 }
68 }
69 }
70 }
71 }
72
73 /**
74 * Rewrites legacy "gutenkit/table-builder*" block markup to the current
75 * "tablebuilder/table-builder*" namespace across all matching posts.
76 *
77 * @return void
78 */
79 public function table_builder_block_migrate_old_blocks() {
80 global $wpdb;
81
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 "
85 SELECT ID, post_content
86 FROM {$wpdb->posts}
87 WHERE post_type IN ('post', 'page', 'your_custom_post_type')
88 AND post_status IN ('publish', 'draft', 'pending', 'private')
89 AND post_content LIKE '%gutenkit/%'
90 "
91 );
92
93 foreach ( $posts as $post ) {
94 $updated_content = str_replace(
95 array(
96 'wp:gutenkit/table-builder',
97 'wp:gutenkit/table-builder-row',
98 'wp:gutenkit/table-builder-item',
99 'gutenkit/table-builder',
100 'gutenkit/table-builder-row',
101 'gutenkit/table-builder-item',
102 ),
103 array(
104 'wp:tablebuilder/table-builder',
105 'wp:tablebuilder/table-builder-row',
106 'wp:tablebuilder/table-builder-item',
107 'tablebuilder/table-builder',
108 'tablebuilder/table-builder-row',
109 'tablebuilder/table-builder-item',
110 ),
111 $post->post_content
112 );
113
114 if ( $updated_content !== $post->post_content ) {
115 wp_update_post(
116 array(
117 'ID' => $post->ID,
118 'post_content' => $updated_content,
119 )
120 );
121 }
122 }
123 }
124 }
125