| 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 |
} |
| 117 |
|