init.php
223 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Compatibility\Element_Manager; |
| 3 | |
| 4 | defined('ABSPATH') || exit; |
| 5 | |
| 6 | class Init { |
| 7 | |
| 8 | public function __construct() { |
| 9 | add_action('update_option_elementor_disabled_elements', [$this, 'sync_with_elementor'], 10, 2); |
| 10 | add_action('elementskit/widgets/status/update', [$this, 'sync_with_elementskit']); |
| 11 | add_action('upgrader_process_complete', [$this, 'reset_widget_statuses'], 10, 2); |
| 12 | } |
| 13 | |
| 14 | public function sync_with_elementor($old_value, $new_value) { |
| 15 | $disabled_elements = (array) get_option( 'elementor_disabled_elements', [] ); |
| 16 | |
| 17 | if($new_value) { |
| 18 | $widgets_to_disable = array_filter( |
| 19 | $disabled_elements, |
| 20 | function( $element ) { |
| 21 | return preg_match( '/^(elementskit-|ekit-)/', $element ); |
| 22 | } |
| 23 | ); |
| 24 | |
| 25 | if($widgets_to_disable) { |
| 26 | $this->disable_widgets($widgets_to_disable); |
| 27 | } |
| 28 | } else { |
| 29 | // Re-enable all ElementsKit widgets if none are disabled |
| 30 | $this->disable_widgets(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public function disable_widgets($widgets_to_disable = []) { |
| 35 | $elementskit_options = get_option('elementskit_options'); |
| 36 | if (!$elementskit_options) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // Normalize widget keys by removing "elementskit-" or "ekit-" prefix |
| 41 | $widgets_to_disable = array_map(function ($widget) { |
| 42 | return preg_replace('/^(elementskit-|ekit-)/', '', $widget); |
| 43 | }, $widgets_to_disable); |
| 44 | |
| 45 | // Correct widget names if needed |
| 46 | $widgets_to_disable = array_map(function ($widget) { |
| 47 | return $this->correct_widget_name($widget); |
| 48 | }, $widgets_to_disable); |
| 49 | |
| 50 | $widgets_list = isset($elementskit_options['widget_list']) ? $elementskit_options['widget_list'] : []; |
| 51 | |
| 52 | if (is_array($widgets_list)) { |
| 53 | $changed = false; |
| 54 | |
| 55 | foreach ($widgets_list as $widget_key => &$widget_data) { |
| 56 | $new_status = in_array($widget_key, $widgets_to_disable, true) ? 'inactive' : 'active'; |
| 57 | |
| 58 | // Only update if the status is different |
| 59 | if (!isset($widget_data['status']) || $widget_data['status'] !== $new_status) { |
| 60 | $widget_data['status'] = $new_status; |
| 61 | $changed = true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | unset($widget_data); // break reference |
| 66 | |
| 67 | // Save updated options only if changes were made |
| 68 | if ($changed) { |
| 69 | $elementskit_options['widget_list'] = $widgets_list; |
| 70 | update_option('elementskit_options', $elementskit_options); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public function sync_with_elementskit($widgets_list) { |
| 76 | if (empty($widgets_list) || !is_array($widgets_list)) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Current disabled elements (Elementor expects prefixed keys, e.g. elementskit-foo / ekit-foo) |
| 81 | $disabled_widgets = (array) get_option('elementor_disabled_elements', []); |
| 82 | $original = $disabled_widgets; |
| 83 | |
| 84 | foreach ($widgets_list as $key => $widget) { |
| 85 | // Prefer $widget['slug'] if present; fallback to array key |
| 86 | $raw_slug = isset($widget['slug']) && $widget['slug'] ? $widget['slug'] : $key; |
| 87 | |
| 88 | // Resolve to the canonical/expected slug before prefixing |
| 89 | $slug = $this->correct_widget_name($raw_slug, true); |
| 90 | |
| 91 | $key_one = 'elementskit-' . $slug; |
| 92 | $key_two = 'ekit-' . $slug; |
| 93 | |
| 94 | $status = isset($widget['status']) ? $widget['status'] : null; |
| 95 | if ($status === null) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | if ($status === 'inactive') { |
| 100 | // Ensure both prefixed keys exist |
| 101 | if ($this->is_registered_widget($key_one) && !isset($disabled_widgets[$key_one])) { |
| 102 | $disabled_widgets[] = $key_one; |
| 103 | } |
| 104 | if ($this->is_registered_widget($key_two) && !isset($disabled_widgets[$key_two])) { |
| 105 | $disabled_widgets[] = $key_two; |
| 106 | } |
| 107 | } else { // active -> ensure both are removed |
| 108 | if (in_array($key_one, $disabled_widgets)) { |
| 109 | $disabled_widgets = array_values(array_diff($disabled_widgets, [$key_one])); |
| 110 | } |
| 111 | if (in_array($key_two, $disabled_widgets)) { |
| 112 | $disabled_widgets = array_values(array_diff($disabled_widgets, [$key_two])); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Only write if something actually changed |
| 118 | if ($disabled_widgets !== $original) { |
| 119 | update_option('elementor_disabled_elements', $disabled_widgets); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public function reset_widget_statuses($upgrader_object, $options) { |
| 124 | $should_run = true; |
| 125 | $our_plugin = 'elementskit-lite/elementskit-lite.php'; |
| 126 | if (!empty($options['plugins']) && $options['action'] == 'update' && $options['type'] == 'plugin' ) { |
| 127 | foreach($options['plugins'] as $plugin) { |
| 128 | if ($plugin != $our_plugin) { |
| 129 | $should_run = false; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Check if should run |
| 136 | if(!$should_run) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // Check if already updated |
| 141 | $is_already_update = get_transient('ekit_element_manager_compatibity'); |
| 142 | if($is_already_update) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // Run the update |
| 147 | $elementskit_options = get_option('elementskit_options'); |
| 148 | if (!$elementskit_options) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | // Get all widgets |
| 153 | $widgets_list = $elementskit_options['widget_list'] ?? []; |
| 154 | $widgets_to_disable = array_filter($widgets_list, function ($widget) { |
| 155 | return isset($widget['status']) && $widget['status'] === 'inactive'; |
| 156 | }); |
| 157 | |
| 158 | if($widgets_to_disable) { |
| 159 | $this->sync_with_elementskit($widgets_to_disable); |
| 160 | } |
| 161 | |
| 162 | // Set transient to avoid running again |
| 163 | set_transient('ekit_element_manager_compatibity', true); |
| 164 | } |
| 165 | |
| 166 | public function correct_widget_name($slug = '', $reverse = false) { |
| 167 | if($reverse) { |
| 168 | if($slug === 'advanced-accordion') { |
| 169 | return 'advance-accordion'; |
| 170 | } |
| 171 | |
| 172 | if($slug === 'advanced-tab') { |
| 173 | return 'tab'; |
| 174 | } |
| 175 | |
| 176 | if($slug === 'social') { |
| 177 | return 'social-media'; |
| 178 | } |
| 179 | |
| 180 | if($slug === 'tab') { |
| 181 | return 'simple-tab'; |
| 182 | } |
| 183 | } else { |
| 184 | if($slug === 'advance-accordion') { |
| 185 | return 'advanced-accordion'; |
| 186 | } |
| 187 | |
| 188 | if($slug === 'tab') { |
| 189 | return 'advanced-tab'; |
| 190 | } |
| 191 | |
| 192 | if($slug === 'social-media') { |
| 193 | return 'social'; |
| 194 | } |
| 195 | |
| 196 | if($slug === 'simple-tab') { |
| 197 | return 'tab'; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return $slug; |
| 202 | } |
| 203 | |
| 204 | public function is_registered_widget($id = '') { |
| 205 | $widgets_manager = \Elementor\Plugin::instance()->widgets_manager; |
| 206 | $widgets = $widgets_manager->get_widget_types(); |
| 207 | |
| 208 | $widget_list = []; |
| 209 | foreach ( $widgets as $widget ) { |
| 210 | $widget_list[$widget->get_name()] = [ |
| 211 | 'slug' => $widget->get_name(), // unique ID (e.g., 'heading', 'button', 'image-box') |
| 212 | 'title'=> $widget->get_title(), // display title |
| 213 | ]; |
| 214 | } |
| 215 | |
| 216 | if($id) { |
| 217 | return isset($widget_list[$id]) ? $widget_list[$id] : null; |
| 218 | } |
| 219 | |
| 220 | return $widget_list; |
| 221 | } |
| 222 | } |
| 223 |