| @@ -1,116 +1,124 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace TableBuilder\Core; | |
| 4 | - | |
| 5 | -defined('ABSPATH') || exit; | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * Enqueue registrar. | |
| 9 | - * | |
| 10 | - * @since 1.0.0 | |
| 11 | - * @access public | |
| 12 | - */ | |
| 13 | -class DataMigration | |
| 14 | -{ | |
| 15 | - /** | |
| 16 | - * Class constructor. | |
| 17 | - * private for singleton | |
| 18 | - * | |
| 19 | - * @return void | |
| 20 | - * @since 1.0.0 | |
| 21 | - */ | |
| 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 | - } | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Enqueues necessary scripts and localizes data for the admin area. | |
| 30 | - * | |
| 31 | - * @param string $hook The current page. | |
| 32 | - * @return void | |
| 33 | - * @since 1.0.0 | |
| 34 | - */ | |
| 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 | - wp_enqueue_script( | |
| 39 | - 'auto-block-recovery', | |
| 40 | - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'assets/js/auto-block-recovery.js', | |
| 41 | - array('wp-blocks', 'wp-data', 'wp-dom-ready', 'wp-i18n'), | |
| 42 | - TABLE_BUILDER_BLOCK_PLUGIN_VERSION, | |
| 43 | - true | |
| 44 | - ); | |
| 45 | - } | |
| 46 | - } | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Migrate old blocks to new format. | |
| 50 | - * | |
| 51 | - * @param object $upgrader_object The upgrader object. | |
| 52 | - * @param array $options The options passed to the upgrader. | |
| 53 | - * @return void | |
| 54 | - */ | |
| 55 | - public function migrate_old_blocks($upgrader_object, $options) | |
| 56 | - { | |
| 57 | - $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')) { | |
| 62 | - $this->table_builder_block_migrate_old_blocks(); | |
| 63 | - set_transient('table_builder_block_migrate', TABLE_BUILDER_BLOCK_PLUGIN_VERSION); | |
| 64 | - } | |
| 65 | - } | |
| 66 | - } | |
| 67 | - } | |
| 68 | - } | |
| 69 | - | |
| 70 | - /** | |
| 71 | - * Migrate old blocks to new format. | |
| 72 | - * | |
| 73 | - * @return void | |
| 74 | - */ | |
| 75 | - public function table_builder_block_migrate_old_blocks() | |
| 76 | - { | |
| 77 | - global $wpdb; | |
| 78 | - | |
| 79 | - $posts = $wpdb->get_results(" | |
| 80 | - SELECT ID, post_content | |
| 81 | - FROM {$wpdb->posts} | |
| 82 | - WHERE post_type IN ('post', 'page', 'your_custom_post_type') | |
| 83 | - AND post_status IN ('publish', 'draft', 'pending', 'private') | |
| 84 | - AND post_content LIKE '%gutenkit/%' | |
| 85 | - "); | |
| 86 | - | |
| 87 | - foreach ($posts as $post) { | |
| 88 | - $updated_content = str_replace( | |
| 89 | - [ | |
| 90 | - 'wp:gutenkit/table-builder', | |
| 91 | - 'wp:gutenkit/table-builder-row', | |
| 92 | - 'wp:gutenkit/table-builder-item', | |
| 93 | - 'gutenkit/table-builder', | |
| 94 | - 'gutenkit/table-builder-row', | |
| 95 | - 'gutenkit/table-builder-item', | |
| 96 | - ], | |
| 97 | - [ | |
| 98 | - 'wp:tablebuilder/table-builder', | |
| 99 | - 'wp:tablebuilder/table-builder-row', | |
| 100 | - 'wp:tablebuilder/table-builder-item', | |
| 101 | - 'tablebuilder/table-builder', | |
| 102 | - 'tablebuilder/table-builder-row', | |
| 103 | - 'tablebuilder/table-builder-item', | |
| 104 | - ], | |
| 105 | - $post->post_content | |
| 106 | - ); | |
| 107 | - | |
| 108 | - if ($updated_content !== $post->post_content) { | |
| 109 | - wp_update_post([ | |
| 110 | - 'ID' => $post->ID, | |
| 111 | - 'post_content' => $updated_content, | |
| 112 | - ]); | |
| 113 | - } | |
| 114 | - } | |
| 115 | - } | |
| 116 | -} | |
| 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 | +} | |