settings-db.php
247 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ElementsKit_Lite\Compatibility\Data_Migration; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | class Settings_Db |
| 8 | { |
| 9 | public function __construct() |
| 10 | { |
| 11 | |
| 12 | $widget_list = \ElementsKit_Lite\Config\Widget_List::instance()->get_list(); |
| 13 | $this->migrate($widget_list, 'widget'); |
| 14 | |
| 15 | $module_list = \ElementsKit_Lite\Config\Module_List::instance()->get_list(); |
| 16 | $this->migrate($module_list, 'module'); |
| 17 | |
| 18 | |
| 19 | add_action('upgrader_process_complete', [$this, 'init'], 10, 2); |
| 20 | } |
| 21 | |
| 22 | public function init($upgrader_object, $options) |
| 23 | { |
| 24 | $this->clear_cache($upgrader_object, $options); |
| 25 | $this->ekit_mail_chimp_upgrader($upgrader_object, $options); |
| 26 | |
| 27 | } |
| 28 | |
| 29 | private function migrate($list, $type) |
| 30 | { |
| 31 | $list_db = \ElementsKit_Lite\Libs\Framework\Attr::instance()->utils->get_option($type . '_list', array()); |
| 32 | $list_prepared = array(); |
| 33 | |
| 34 | if (empty($list_db[0]) || is_array($list_db[0])) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | foreach ($list as $slug => $info) { |
| 39 | if (isset($info['package']) && $info['package'] == 'pro-disabled') { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | if (isset($info['attributes']) && in_array('new', $info['attributes'])) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | $info['status'] = (in_array($slug, $list_db) ? 'active' : 'inactive'); |
| 48 | |
| 49 | $list_prepared[$slug] = $info; |
| 50 | } |
| 51 | |
| 52 | \ElementsKit_Lite\Libs\Framework\Attr::instance()->utils->save_option($type . '_list', $list_prepared); |
| 53 | } |
| 54 | |
| 55 | // TODO - remove this after 3.10.0 release |
| 56 | public function clear_cache($upgrader_object, $options) |
| 57 | { |
| 58 | $our_plugin = 'elementskit-lite/elementskit-lite.php'; |
| 59 | if (!empty($options['plugins']) && $options['action'] == 'update' && $options['type'] == 'plugin') { |
| 60 | foreach ($options['plugins'] as $plugin) { |
| 61 | if ($plugin == $our_plugin) { |
| 62 | $this->regenerate_widget_builder_widgets(); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function regenerate_widget_builder_widgets() |
| 69 | { |
| 70 | $args = array( |
| 71 | 'post_type' => 'elementskit_widget', |
| 72 | 'post_status' => 'publish', // Only get published posts |
| 73 | 'posts_per_page' => -1, |
| 74 | ); |
| 75 | |
| 76 | $posts = get_posts($args); |
| 77 | |
| 78 | if ($posts) { |
| 79 | foreach ($posts as $post) { |
| 80 | $id = $post->ID; |
| 81 | $widget_data = get_post_meta($id, 'elementskit_custom_widget_data', true); |
| 82 | if (!empty($widget_data) && is_object($widget_data)) { |
| 83 | \ElementsKit_Lite\Modules\Widget_Builder\Widget_File::instance()->create($widget_data, $id); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function ekit_mail_chimp_upgrader($upgrader_object, array $options) |
| 90 | { |
| 91 | // Stop if migration already ran |
| 92 | if (get_transient('ekit_lite_mailchimp_migrate')) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // Plugins to check for updates |
| 97 | $plugins_to_check = [ |
| 98 | 'elementskit-lite/elementskit-lite.php', |
| 99 | ]; |
| 100 | |
| 101 | // Check if any of the specified plugins are updated |
| 102 | if (!empty($options['action']) && $options['action'] === 'update') { |
| 103 | |
| 104 | $updated_plugins = []; |
| 105 | |
| 106 | if (!empty($options['plugins']) && is_array($options['plugins'])) { |
| 107 | $updated_plugins = $options['plugins']; |
| 108 | } |
| 109 | |
| 110 | if (!empty($options['plugin']) && !is_array($options['plugin'])) { |
| 111 | $updated_plugins[] = $options['plugin']; |
| 112 | } |
| 113 | |
| 114 | foreach ($plugins_to_check as $plugin_slug) { |
| 115 | |
| 116 | if (in_array($plugin_slug, $updated_plugins, true)) { |
| 117 | |
| 118 | // Store migration state |
| 119 | set_transient( |
| 120 | 'ekit_lite_mailchimp_migrate', |
| 121 | \ElementsKit_Lite::version() |
| 122 | ); |
| 123 | |
| 124 | $this->ekit_mail_chimp_migrate(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | public function ekit_mail_chimp_migrate() |
| 130 | { |
| 131 | global $wpdb; |
| 132 | $post_ids = $wpdb->get_col('SELECT `post_id` FROM `' . $wpdb->postmeta . '` WHERE `meta_key` = "_elementor_data" AND `meta_value` LIKE \'%"widgetType":"elementskit-mail-chimp"%\';'); |
| 133 | |
| 134 | foreach ($post_ids as $post_id) { |
| 135 | // if($post_id != get_the_ID()){ |
| 136 | // continue; |
| 137 | // } |
| 138 | $do_update = false; |
| 139 | $document = \Elementor\Plugin::$instance->documents->get($post_id); |
| 140 | |
| 141 | if ($document) { |
| 142 | $data = $document->get_elements_data(); |
| 143 | } |
| 144 | $data = \Elementor\Plugin::$instance->db->iterate_data($data, function ($element) use (&$do_update) { |
| 145 | |
| 146 | if (empty($element['widgetType']) || 'elementskit-mail-chimp' !== $element['widgetType'] || !empty($element['settings']['ekit_mail_chimp_fields_repeater'])) { |
| 147 | return $element; |
| 148 | } |
| 149 | |
| 150 | $is_name_field = !empty($element['settings']['ekit_mail_chimp_section_form_name_show']) && $element['settings']['ekit_mail_chimp_section_form_name_show'] == 'yes'; |
| 151 | $is_phone_field = !empty($element['settings']['ekit_mail_chimp_section_form_phone_show']) && $element['settings']['ekit_mail_chimp_section_form_phone_show'] == 'yes'; |
| 152 | |
| 153 | $repeater_fields = []; |
| 154 | |
| 155 | if ($is_name_field) { |
| 156 | $repeater_fields[] = [ |
| 157 | '_id' => wp_unique_id(), |
| 158 | 'ekit_mail_chimp_field_type' => 'text', |
| 159 | 'ekit_mail_chimp_field_label' => $element['settings']['ekit_mail_chimp_first_name_label'] ?? '', |
| 160 | 'ekit_mail_chimp_field_placeholder' => $element['settings']['ekit_mail_chimp_first_name_placeholder'] ?? '', |
| 161 | 'ekit_mail_chimp_field_name' => 'firstname', |
| 162 | 'ekit_mail_chimp_field_required' => 'yes', |
| 163 | 'ekit_mail_chimp_field_icon_show' => $element['settings']['ekit_mail_chimp_first_name_icon_show'] ?? 'yes', |
| 164 | 'ekit_mail_chimp_field_icon' => $element['settings']['ekit_mail_chimp_first_name_icons'] ?? [ |
| 165 | 'value' => 'icon icon-user', |
| 166 | 'library' => 'ekiticons', |
| 167 | ], |
| 168 | 'ekit_mail_chimp_field_icon_position' => $element['settings']['ekit_mail_chimp_first_name_icon_before_after'] ?? 'before', |
| 169 | ]; |
| 170 | } |
| 171 | if ($is_name_field) { |
| 172 | $repeater_fields[] = [ |
| 173 | '_id' => wp_unique_id(), |
| 174 | 'ekit_mail_chimp_field_type' => 'text', |
| 175 | 'ekit_mail_chimp_field_label' => $element['settings']['ekit_mail_chimp_last_name_label'] ?? '', |
| 176 | 'ekit_mail_chimp_field_placeholder' => $element['settings']['ekit_mail_chimp_last_name_placeholder'] ?? '', |
| 177 | 'ekit_mail_chimp_field_name' => 'lastname', |
| 178 | 'ekit_mail_chimp_field_required' => 'yes', |
| 179 | 'ekit_mail_chimp_field_icon_show' => $element['settings']['ekit_mail_chimp_last_name_icon_show'] ?? 'yes', |
| 180 | 'ekit_mail_chimp_field_icon' => $element['settings']['ekit_mail_chimp_last_name_icons'] ?? [ |
| 181 | 'value' => 'icon icon-user', |
| 182 | 'library' => 'ekiticons', |
| 183 | ], |
| 184 | 'ekit_mail_chimp_field_icon_position' => $element['settings']['ekit_mail_chimp_last_name_icon_before_after'] ?? 'before', |
| 185 | ]; |
| 186 | } |
| 187 | // Add Phone if enabled |
| 188 | if ($is_phone_field) { |
| 189 | $repeater_fields[] = [ |
| 190 | '_id' => wp_unique_id(), |
| 191 | 'ekit_mail_chimp_field_type' => 'tel', |
| 192 | 'ekit_mail_chimp_field_label' => $element['settings']['ekit_mail_chimp_phone_label'] ?? '', |
| 193 | 'ekit_mail_chimp_field_placeholder' => $element['settings']['ekit_mail_chimp_phone_placeholder'] ?? '', |
| 194 | 'ekit_mail_chimp_field_name' => 'phone', |
| 195 | 'ekit_mail_chimp_field_required' => 'yes', |
| 196 | 'ekit_mail_chimp_field_icon_show' => $element['settings']['ekit_mail_chimp_phone_icon_show'] ?? 'yes', |
| 197 | 'ekit_mail_chimp_field_icon' => $element['settings']['ekit_mail_chimp_phone_icons'] ?? [ |
| 198 | 'value' => 'icon icon-phone-handset', |
| 199 | 'library' => 'ekiticons', |
| 200 | ], |
| 201 | 'ekit_mail_chimp_field_icon_position' => $element['settings']['ekit_mail_chimp_phone_icon_before_after'] ?? 'before', |
| 202 | ]; |
| 203 | } |
| 204 | |
| 205 | // � |
| 206 | ALWAYS add Email field (required) |
| 207 | $repeater_fields[] = [ |
| 208 | '_id' => wp_unique_id(), |
| 209 | 'ekit_mail_chimp_field_type' => 'email', |
| 210 | 'ekit_mail_chimp_field_label' => $element['settings']['ekit_mail_chimp_email_address_label'] ?? '', |
| 211 | 'ekit_mail_chimp_field_placeholder' => $element['settings']['ekit_mail_chimp_email_address_placeholder'] ?? '', |
| 212 | 'ekit_mail_chimp_field_name' => 'email', |
| 213 | 'ekit_mail_chimp_field_required' => 'yes', |
| 214 | 'ekit_mail_chimp_field_icon_show' => $element['settings']['ekit_mail_chimp_email_icon_show'] ?? 'yes', |
| 215 | 'ekit_mail_chimp_field_icon' => $element['settings']['ekit_mail_chimp_email_icons'] ?? [ |
| 216 | 'value' => 'icon icon-envelope', |
| 217 | 'library' => 'ekiticons', |
| 218 | ], |
| 219 | 'ekit_mail_chimp_field_icon_position' => $element['settings']['ekit_mail_chimp_email_icon_before_after'] ?? 'before', |
| 220 | ]; |
| 221 | |
| 222 | |
| 223 | // Update element with migrated data |
| 224 | if (!empty($repeater_fields)) { |
| 225 | $element['settings']['ekit_mail_chimp_fields_repeater'] = $repeater_fields; |
| 226 | $do_update = true; |
| 227 | } |
| 228 | |
| 229 | return $element; |
| 230 | }); |
| 231 | if (!$do_update) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | // We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta` |
| 237 | $json_value = wp_slash(wp_json_encode($data)); |
| 238 | |
| 239 | update_metadata('post', $post_id, '_elementor_data', $json_value); |
| 240 | |
| 241 | // Clear WP cache for next step. |
| 242 | wp_cache_flush(); |
| 243 | |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 |