1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
<?php namespace Elementor;
use Elementor\Core\Admin\Menu\Admin_Menu_Manager; use Elementor\Core\Admin\Menu\Main as MainMenu; use Elementor\Core\Kits\Manager; use Elementor\Includes\Settings\AdminMenuItems\Tools_Menu_Item;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
/** * Elementor "Tools" page in WordPress Dashboard. * * Elementor settings page handler class responsible for creating and displaying * Elementor "Tools" page in WordPress dashboard. * * @since 1.0.0 */ class Tools extends Settings_Page {
const CAPABILITY = 'manage_options';
/** * Settings page ID for Elementor tools. */ const PAGE_ID = 'elementor-tools';
private function register_admin_menu( MainMenu $menu ) { $menu->add_submenu( [ 'page_title' => esc_html__( 'Tools', 'elementor' ), 'menu_title' => esc_html__( 'Tools', 'elementor' ), 'menu_slug' => self::PAGE_ID, 'function' => [ $this, 'display_settings_page' ], 'index' => 50, ] ); }
/** * Clear cache. * * Delete post meta containing the post CSS file data. And delete the actual * CSS files from the upload directory. * * Fired by `wp_ajax_elementor_clear_cache` action. * * @since 1.0.0 * @access public */ public function ajax_elementor_clear_cache() { check_ajax_referer( 'elementor_clear_cache', '_nonce' );
if ( ! current_user_can( static::CAPABILITY ) ) { wp_send_json_error( 'Permission denied' ); }
Plugin::$instance->files_manager->clear_cache();
wp_send_json_success(); }
/** * Recreate kit. * * Recreate default kit (only when default kit does not exist). * * Fired by `wp_ajax_elementor_recreate_kit` action. * * @since 1.0.0 * @access public */ public function ajax_elementor_recreate_kit() { check_ajax_referer( 'elementor_recreate_kit', '_nonce' );
if ( ! current_user_can( static::CAPABILITY ) ) { wp_send_json_error( 'Permission denied' ); }
$kit = Plugin::$instance->kits_manager->get_active_kit();
if ( $kit->get_id() ) { wp_send_json_error( [ 'message' => esc_html__( 'There\'s already an active kit.', 'elementor' ) ], 400 ); }
$created_default_kit = Plugin::$instance->kits_manager->create_default();
if ( ! $created_default_kit ) { wp_send_json_error( [ 'message' => esc_html__( 'An error occurred while trying to create a kit.', 'elementor' ) ], 500 ); }
update_option( Manager::OPTION_ACTIVE, $created_default_kit );
wp_send_json_success( esc_html__( 'New kit have been created successfully', 'elementor' ) ); }
/** * Replace URLs. * * Sends an ajax request to replace old URLs to new URLs. This method also * updates all the Elementor data. * * Fired by `wp_ajax_elementor_replace_url` action. * * @since 1.1.0 * @access public */ public function ajax_elementor_replace_url() { check_ajax_referer( 'elementor_replace_url', '_nonce' );
if ( ! current_user_can( static::CAPABILITY ) ) { wp_send_json_error( 'Permission denied' ); }
$from = Utils::get_super_global_value( $_POST, 'from' ) ?? ''; $to = Utils::get_super_global_value( $_POST, 'to' ) ?? '';
try { $results = Utils::replace_urls( $from, $to ); wp_send_json_success( $results ); } catch ( \Exception $e ) { wp_send_json_error( $e->getMessage() ); } }
/** * Elementor version rollback. * * Rollback to previous Elementor version. * * Fired by `admin_post_elementor_rollback` action. * * @since 1.5.0 * @access public */ public function post_elementor_rollback() { check_admin_referer( 'elementor_rollback' );
if ( ! static::can_user_rollback_versions() ) { wp_die( esc_html__( 'Not allowed to rollback versions', 'elementor' ) ); }
$rollback_versions = $this->get_rollback_versions(); $version = Utils::get_super_global_value( $_GET, 'version' );
if ( empty( $version ) || ! in_array( $version, $rollback_versions, true ) ) { wp_die( esc_html__( 'An error occurred, the selected version is invalid. Try selecting different version.', 'elementor' ) ); }
/** * Filter to allow override the rollback process. * Should return an instance of `Rollback` class. * * @since 3.16.0 * * @param Rollback|null $rollback The rollback instance. * @param string $version The version to roll back to. */ $rollback = apply_filters( 'elementor/settings/rollback', null, $version );
if ( ! ( $rollback instanceof Rollback ) ) { $plugin_slug = basename( ELEMENTOR__FILE__, '.php' );
$rollback = new Rollback( [ 'version' => $version, 'plugin_name' => ELEMENTOR_PLUGIN_BASE, 'plugin_slug' => $plugin_slug, 'package_url' => sprintf( 'https://downloads.wordpress.org/plugin/%s.%s.zip', $plugin_slug, $version ), ] ); }
$rollback->run();
wp_die( '', esc_html__( 'Rollback to Previous Version', 'elementor' ), [ 'response' => 200, ] ); }
/** * Tools page constructor. * * Initializing Elementor "Tools" page. * * @since 1.0.0 * @access public */ public function __construct() { parent::__construct();
add_action( 'elementor/admin/menu/register', function( Admin_Menu_Manager $admin_menu ) { $admin_menu->register( static::PAGE_ID, new Tools_Menu_Item( $this ) ); }, Settings::ADMIN_MENU_PRIORITY + 20 );
add_action( 'wp_ajax_elementor_clear_cache', [ $this, 'ajax_elementor_clear_cache' ] ); add_action( 'wp_ajax_elementor_replace_url', [ $this, 'ajax_elementor_replace_url' ] ); add_action( 'wp_ajax_elementor_recreate_kit', [ $this, 'ajax_elementor_recreate_kit' ] );
add_action( 'admin_post_elementor_rollback', [ $this, 'post_elementor_rollback' ] ); }
private function get_rollback_versions() { $rollback_versions = get_transient( 'elementor_rollback_versions_' . ELEMENTOR_VERSION );
if ( false === $rollback_versions ) { $max_versions = 30;
$versions = apply_filters( 'elementor/settings/rollback/versions', [] );
if ( empty( $versions ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$plugin_information = plugins_api( 'plugin_information', [ 'slug' => 'elementor', ] );
if ( empty( $plugin_information->versions ) || ! is_array( $plugin_information->versions ) ) { return []; }
uksort( $plugin_information->versions, 'version_compare' ); $versions = array_keys( array_reverse( $plugin_information->versions ) ); }
$rollback_versions = [];
$current_index = 0; foreach ( $versions as $version ) { if ( $max_versions <= $current_index ) { break; }
$lowercase_version = strtolower( $version ); $is_valid_rollback_version = ! preg_match( '/(trunk|beta|rc|dev)/i', $lowercase_version );
/** * Is rollback version is valid. * * Filters the check whether the rollback version is valid. * * @param bool $is_valid_rollback_version Whether the rollback version is valid. */ $is_valid_rollback_version = apply_filters( 'elementor/settings/tools/rollback/is_valid_rollback_version', $is_valid_rollback_version, $lowercase_version );
if ( ! $is_valid_rollback_version ) { continue; }
if ( version_compare( $version, ELEMENTOR_VERSION, '>=' ) ) { continue; }
$current_index++; $rollback_versions[] = $version; }
set_transient( 'elementor_rollback_versions_' . ELEMENTOR_VERSION, $rollback_versions, WEEK_IN_SECONDS ); }
return $rollback_versions; }
/** * Create tabs. * * Return the tools page tabs, sections and fields. * * @since 1.5.0 * @access protected * * @return array An array with the page tabs, sections and fields. */ protected function create_tabs() { $rollback_html = '<select class="elementor-rollback-select">';
foreach ( $this->get_rollback_versions() as $version ) { $rollback_html .= "<option value='{$version}'>$version</option>"; } $rollback_html .= '</select>';
$tabs = [ 'general' => [ 'label' => esc_html__( 'General', 'elementor' ), 'sections' => [ 'tools' => [ 'fields' => [ 'clear_cache' => [ 'label' => esc_html__( 'Regenerate CSS & Data', 'elementor' ), 'field_args' => [ 'type' => 'raw_html', 'html' => sprintf( '<button data-nonce="%s" class="button elementor-button-spinner" id="elementor-clear-cache-button">%s</button>', wp_create_nonce( 'elementor_clear_cache' ), esc_html__( 'Regenerate Files & Data', 'elementor' ) ), 'desc' => esc_html__( 'Styles set in Elementor are saved in CSS files in the uploads folder and in the site’s database. Recreate those files and settings, according to the most recent settings.', 'elementor' ), ], ], 'reset_api_data' => [ 'label' => esc_html__( 'Sync Library', 'elementor' ), 'field_args' => [ 'type' => 'raw_html', 'html' => sprintf( '<button data-nonce="%s" class="button elementor-button-spinner" id="elementor-library-sync-button">%s</button>', wp_create_nonce( 'elementor_reset_library' ), esc_html__( 'Sync Library', 'elementor' ) ), 'desc' => esc_html__( 'Elementor Library automatically updates on a daily basis. You can also manually update it by clicking on the sync button.', 'elementor' ), ], ], ], ], ], ], 'replace_url' => [ 'label' => esc_html__( 'Replace URL', 'elementor' ), 'sections' => [ 'replace_url' => [ 'callback' => function() { echo '<h2>' . esc_html__( 'Replace URL', 'elementor' ) . '</h2>'; echo sprintf( '<p><strong>%1$s</strong> %2$s</p>', esc_html__( 'Important:', 'elementor' ), sprintf( /* translators: 1: Link open tag, 2: Link close tag. */ esc_html__( 'It is strongly recommended to %1$sbackup the database%2$s before using replacing URLs.', 'elementor' ), '<a href="https://go.elementor.com/wordpress-backups/" target="_blank">', '</a>' ) ); }, 'fields' => [ 'replace_url' => [ 'label' => esc_html__( 'Update Site Address (URL)', 'elementor' ), 'field_args' => [ 'type' => 'raw_html', 'html' => sprintf( '<input type="text" name="from" placeholder="https://old.example.com" class="large-text"><input type="text" name="to" placeholder="https://new.example.com" class="large-text"><button data-nonce="%s" class="button elementor-button-spinner" id="elementor-replace-url-button">%s</button>', wp_create_nonce( 'elementor_replace_url' ), esc_html__( 'Replace URL', 'elementor' ) ), 'desc' => esc_html__( 'Enter your old and new URLs for your WordPress installation, to update all Elementor data (Relevant for domain transfers or move to \'HTTPS\').', 'elementor' ), ], ], ], ], ], ], 'versions' => [ 'show_if' => static::can_user_rollback_versions(), 'label' => esc_html__( 'Version Control', 'elementor' ), 'sections' => [ 'rollback' => [ 'label' => esc_html__( 'Rollback to Previous Version', 'elementor' ), 'callback' => function() { $intro_text = sprintf( /* translators: %s: Elementor version. */ esc_html__( 'Experiencing an issue with Elementor version %s? Rollback to a previous version before the issue appeared.', 'elementor' ), ELEMENTOR_VERSION ); $intro_text = '<p>' . $intro_text . '</p>';
Utils::print_unescaped_internal_string( $intro_text ); }, 'fields' => [ 'rollback' => [ 'label' => esc_html__( 'Rollback Version', 'elementor' ), 'field_args' => [ 'type' => 'raw_html', 'html' => sprintf( $rollback_html . '<a data-placeholder-text="%1$s v{VERSION}" href="#" data-placeholder-url="%2$s" class="button elementor-button-spinner elementor-rollback-button">%1$s</a>', esc_html__( 'Reinstall', 'elementor' ), wp_nonce_url( admin_url( 'admin-post.php?action=elementor_rollback&version=VERSION' ), 'elementor_rollback' ) ), 'desc' => '<span style="color: red;">' . esc_html__( 'Warning: Please backup your database before making the rollback.', 'elementor' ) . '</span>', ], ], ], ], 'beta' => [ 'show_if' => $this->display_beta_tester(), 'label' => esc_html__( 'Become a Beta Tester', 'elementor' ), 'callback' => function() { echo '<p>' . esc_html__( 'Turn-on Beta Tester, to get notified when a new beta version of Elementor or Elementor Pro is available. The Beta version will not install automatically. You always have the option to ignore it.', 'elementor' ) . '</p>'; echo '<p>' . sprintf( /* translators: 1: Link open tag, 2: Link close tag. */ esc_html__( '%1$sClick here%2$s %3$sto join our first-to-know email updates.%4$s', 'elementor' ), '<a id="beta-tester-first-to-know" class="elementor-become-a-beta-tester" href="#">', '</a>', '<span class="elementor-become-a-beta-tester">', '</span>', ) . '</p>'; }, 'fields' => [ 'beta' => [ 'label' => esc_html__( 'Beta Tester', 'elementor' ), 'field_args' => [ 'type' => 'select', 'std' => 'no', 'options' => [ 'no' => esc_html__( 'Disable', 'elementor' ), 'yes' => esc_html__( 'Enable', 'elementor' ), ], 'desc' => '<span style="color: red;">' . esc_html__( 'Please Note: We do not recommend updating to a beta version on production sites.', 'elementor' ) . '</span>', ], ], ], ], ], ], ];
if ( ! Plugin::$instance->kits_manager->get_active_kit()->get_id() ) { $tabs['general']['sections']['tools']['fields']['recreate_kit'] = [ 'label' => esc_html__( 'Recreate Kit', 'elementor' ), 'field_args' => [ 'type' => 'raw_html', 'html' => sprintf( '<button data-nonce="%s" class="button elementor-button-spinner" id="elementor-recreate-kit-button">%s</button>', wp_create_nonce( 'elementor_recreate_kit' ), esc_html__( 'Recreate Kit', 'elementor' ) ), 'desc' => esc_html__( 'It seems like your site doesn\'t have any active Kit. The active Kit includes all of your Site Settings. By recreating your Kit you will able to start edit your Site Settings again.', 'elementor' ), ], ]; } return $tabs; }
/** * Get tools page title. * * Retrieve the title for the tools page. * * @since 1.5.0 * @access protected * * @return string Tools page title. */ protected function get_page_title() { return esc_html__( 'Tools', 'elementor' ); }
/** * Check if the current user can access the version control tab and rollback versions. * * @return bool */ public static function can_user_rollback_versions() { return current_user_can( 'activate_plugins' ) && current_user_can( 'update_plugins' ); }
/** * Check if the beta tester should be displayed. * * @since 3.19.0 * * @return bool */ public function display_beta_tester(): bool { $display_beta_tester = true; /** * Filter to allow override the display of the beta tester. * * @param bool $display_beta_tester Whether to display the beta tester. * * @since 3.19.0 * * return bool */ return apply_filters( 'elementor/admin/show_beta_tester', $display_beta_tester ); } }
|