PluginActiveEvents.php
3 weeks ago
PluginDeactivateEvents.php
3 weeks ago
PluginInitEvents.php
1 month ago
PluginLoadedEvents.php
3 weeks ago
PluginShortcode.php
2 months ago
TemplateRedirection.php
1 week ago
PluginLoadedEvents.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin Loaded events handler |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Manager; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | use Kirki\Admin; |
| 16 | use Kirki\Ajax\Page; |
| 17 | use Kirki\Frontend; |
| 18 | use Kirki\HelperFunctions; |
| 19 | |
| 20 | use function Kirki\Framework\is_rest_request; |
| 21 | |
| 22 | /** |
| 23 | * Do some task during plugin activation |
| 24 | */ |
| 25 | class PluginLoadedEvents { |
| 26 | |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Initilize the class |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | add_action( 'plugins_loaded', array( $this, 'init_plugin' ) ); |
| 36 | add_action( 'plugins_loaded', array( $this, 'do_migration_related_task' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Initialize the plugin |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function init_plugin() { |
| 45 | if ( is_admin() ) { |
| 46 | new Admin(); |
| 47 | } elseif (!is_rest_request()) { |
| 48 | new Frontend(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // This function updates the names of specific blocks to standardize them |
| 53 | public function update_block( $blocks ) { |
| 54 | // Loop through each block using its ID and content |
| 55 | foreach ( $blocks as $id => $block ) { |
| 56 | // Check if the block has a 'name' key |
| 57 | if ( isset( $block['name'] ) ) { |
| 58 | // If block name is one of the wrapper types, change it to 'items' |
| 59 | if ( |
| 60 | $block['name'] === 'collection-wrapper' || |
| 61 | $block['name'] === 'terms' || |
| 62 | $block['name'] === 'users' || |
| 63 | $block['name'] === 'comments' || |
| 64 | $block['name'] === 'menus' |
| 65 | ) { |
| 66 | $blocks[ $id ]['name'] = 'items'; |
| 67 | } |
| 68 | |
| 69 | // If block name is one of the item types, change it to 'item' |
| 70 | if ( |
| 71 | $block['name'] === 'collection-item' || |
| 72 | $block['name'] === 'term-item' || |
| 73 | $block['name'] === 'user-item' || |
| 74 | $block['name'] === 'comment-item' || |
| 75 | $block['name'] === 'menu-item' |
| 76 | ) { |
| 77 | $blocks[ $id ]['name'] = 'item'; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Return the updated blocks |
| 83 | return $blocks; |
| 84 | } |
| 85 | |
| 86 | // This function goes through all stored page meta data and updates block names |
| 87 | public function alter_collection_names() { |
| 88 | // Get all meta data where key is matched (kirki meta key) |
| 89 | $data = Page::get_all_data_by_kirki_meta_key(); |
| 90 | |
| 91 | foreach ( $data as $key => $value ) { |
| 92 | $post_id = $value['post_id']; // ID of the post |
| 93 | $meta_key = $value['meta_key']; // Meta key name |
| 94 | $meta_value = $value['meta_value']; // Serialized meta value |
| 95 | |
| 96 | $meta_value = unserialize( $meta_value, ['allowed_classes' => false] ); // Convert serialized data to array |
| 97 | |
| 98 | // If the meta value has a 'blocks' key, handle it as a full page data |
| 99 | if ( isset( $meta_value['blocks'] ) ) { |
| 100 | $blocks = $meta_value['blocks']; // Get the blocks array |
| 101 | $updated_blocks = $this->update_block( $blocks ); // Update block names |
| 102 | |
| 103 | $meta_value['blocks'] = $updated_blocks; // Set updated blocks back |
| 104 | update_post_meta( $post_id, $meta_key, $meta_value ); // Save back to DB |
| 105 | } else { |
| 106 | // If no 'blocks' key, treat entire value as blocks array |
| 107 | $blocks = $meta_value; |
| 108 | $updated_blocks = $this->update_block( $blocks ); // Update block names |
| 109 | |
| 110 | $meta_value = $updated_blocks; |
| 111 | update_post_meta( $post_id, $meta_key, $meta_value ); // Save updated value |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Check version whether db needs update or not |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function do_migration_related_task() { |
| 123 | $version_in_db = HelperFunctions::get_kirki_version_from_db(); |
| 124 | $needs_rewrite_flush = empty( $version_in_db ) || version_compare( $version_in_db, KIRKI_VERSION, '<' ); |
| 125 | |
| 126 | PluginActiveEvents::update_rbac(); |
| 127 | |
| 128 | $db_altered_in3 = '2.1.0'; |
| 129 | if ( empty( $version_in_db ) || version_compare( $version_in_db, $db_altered_in3, '<' ) ) { |
| 130 | $this->alter_collection_names(); |
| 131 | } |
| 132 | |
| 133 | $db_altered_in6 = '2.3.0'; |
| 134 | if ( empty( $version_in_db ) || version_compare( $version_in_db, $db_altered_in6, '<' ) ) { |
| 135 | PluginActiveEvents::create_custom_tables(); |
| 136 | } |
| 137 | |
| 138 | $db_altered_in6 = '2.4.0'; |
| 139 | if ( empty( $version_in_db ) || version_compare( $version_in_db, $db_altered_in6, '<' ) ) { |
| 140 | PluginActiveEvents::add_post_field_in_collaboaration_connected_table(); |
| 141 | } |
| 142 | |
| 143 | HelperFunctions::set_kirki_version_in_db(); |
| 144 | |
| 145 | if ( $needs_rewrite_flush ) { |
| 146 | flush_rewrite_rules( false ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 |