| 1 |
<?php |
| 2 |
namespace Elementor; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Elementor "Tools" page in WordPress Dashboard. |
| 10 |
* |
| 11 |
* Elementor settings page handler class responsible for creating and displaying |
| 12 |
* Elementor "Tools" page in WordPress dashboard. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class Tools extends Settings_Page { |
| 17 |
|
| 18 |
/** |
| 19 |
* Settings page ID for Elementor tools. |
| 20 |
*/ |
| 21 |
const PAGE_ID = 'elementor-tools'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Register admin menu. |
| 25 |
* |
| 26 |
* Add new Elementor Tools admin menu. |
| 27 |
* |
| 28 |
* Fired by `admin_menu` action. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @access public |
| 32 |
*/ |
| 33 |
public function register_admin_menu() { |
| 34 |
add_submenu_page( |
| 35 |
Settings::PAGE_ID, |
| 36 |
__( 'Tools', 'elementor' ), |
| 37 |
__( 'Tools', 'elementor' ), |
| 38 |
'manage_options', |
| 39 |
self::PAGE_ID, |
| 40 |
[ $this, 'display_settings_page' ] |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Clear cache. |
| 46 |
* |
| 47 |
* Delete post meta containing the post CSS file data. And delete the actual |
| 48 |
* CSS files from the upload directory. |
| 49 |
* |
| 50 |
* Fired by `wp_ajax_elementor_clear_cache` action. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* @access public |
| 54 |
*/ |
| 55 |
public function ajax_elementor_clear_cache() { |
| 56 |
check_ajax_referer( 'elementor_clear_cache', '_nonce' ); |
| 57 |
|
| 58 |
Plugin::$instance->files_manager->clear_cache(); |
| 59 |
|
| 60 |
wp_send_json_success(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Replace URLs. |
| 65 |
* |
| 66 |
* Sends an ajax request to replace old URLs to new URLs. This method also |
| 67 |
* updates all the Elementor data. |
| 68 |
* |
| 69 |
* Fired by `wp_ajax_elementor_replace_url` action. |
| 70 |
* |
| 71 |
* @since 1.1.0 |
| 72 |
* @access public |
| 73 |
*/ |
| 74 |
public function ajax_elementor_replace_url() { |
| 75 |
check_ajax_referer( 'elementor_replace_url', '_nonce' ); |
| 76 |
|
| 77 |
$from = ! empty( $_POST['from'] ) ? $_POST['from'] : ''; |
| 78 |
$to = ! empty( $_POST['to'] ) ? $_POST['to'] : ''; |
| 79 |
|
| 80 |
try { |
| 81 |
$results = Utils::replace_urls( $from, $to ); |
| 82 |
wp_send_json_success( $results ); |
| 83 |
} catch ( \Exception $e ) { |
| 84 |
wp_send_json_error( $e->getMessage() ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Elementor version rollback. |
| 90 |
* |
| 91 |
* Rollback to previous Elementor version. |
| 92 |
* |
| 93 |
* Fired by `admin_post_elementor_rollback` action. |
| 94 |
* |
| 95 |
* @since 1.5.0 |
| 96 |
* @access public |
| 97 |
*/ |
| 98 |
public function post_elementor_rollback() { |
| 99 |
check_admin_referer( 'elementor_rollback' ); |
| 100 |
|
| 101 |
$rollback_versions = $this->get_rollback_versions(); |
| 102 |
if ( empty( $_GET['version'] ) || ! in_array( $_GET['version'], $rollback_versions ) ) { |
| 103 |
wp_die( __( 'Error occurred, The version selected is invalid. Try selecting different version.', 'elementor' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
$plugin_slug = basename( ELEMENTOR__FILE__, '.php' ); |
| 107 |
|
| 108 |
$rollback = new Rollback( |
| 109 |
[ |
| 110 |
'version' => $_GET['version'], |
| 111 |
'plugin_name' => ELEMENTOR_PLUGIN_BASE, |
| 112 |
'plugin_slug' => $plugin_slug, |
| 113 |
'package_url' => sprintf( 'https://downloads.wordpress.org/plugin/%s.%s.zip', $plugin_slug, $_GET['version'] ), |
| 114 |
] |
| 115 |
); |
| 116 |
|
| 117 |
$rollback->run(); |
| 118 |
|
| 119 |
wp_die( |
| 120 |
'', __( 'Rollback to Previous Version', 'elementor' ), [ |
| 121 |
'response' => 200, |
| 122 |
] |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Tools page constructor. |
| 128 |
* |
| 129 |
* Initializing Elementor "Tools" page. |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
* @access public |
| 133 |
*/ |
| 134 |
public function __construct() { |
| 135 |
parent::__construct(); |
| 136 |
|
| 137 |
add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 205 ); |
| 138 |
|
| 139 |
if ( ! empty( $_POST ) ) { |
| 140 |
add_action( 'wp_ajax_elementor_clear_cache', [ $this, 'ajax_elementor_clear_cache' ] ); |
| 141 |
add_action( 'wp_ajax_elementor_replace_url', [ $this, 'ajax_elementor_replace_url' ] ); |
| 142 |
} |
| 143 |
|
| 144 |
add_action( 'admin_post_elementor_rollback', [ $this, 'post_elementor_rollback' ] ); |
| 145 |
} |
| 146 |
|
| 147 |
private function get_rollback_versions() { |
| 148 |
$rollback_versions = get_transient( 'elementor_rollback_versions_' . ELEMENTOR_VERSION ); |
| 149 |
if ( false === $rollback_versions ) { |
| 150 |
$max_versions = 30; |
| 151 |
|
| 152 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 153 |
|
| 154 |
$plugin_information = plugins_api( |
| 155 |
'plugin_information', [ |
| 156 |
'slug' => 'elementor', |
| 157 |
] |
| 158 |
); |
| 159 |
|
| 160 |
if ( empty( $plugin_information->versions ) || ! is_array( $plugin_information->versions ) ) { |
| 161 |
return []; |
| 162 |
} |
| 163 |
|
| 164 |
krsort( $plugin_information->versions ); |
| 165 |
|
| 166 |
$rollback_versions = []; |
| 167 |
|
| 168 |
$current_index = 0; |
| 169 |
foreach ( $plugin_information->versions as $version => $download_link ) { |
| 170 |
if ( $max_versions <= $current_index ) { |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
$lowercase_version = strtolower( $version ); |
| 175 |
$is_valid_rollback_version = ! preg_match( '/(trunk|beta|rc|dev)/i', $lowercase_version ); |
| 176 |
|
| 177 |
$is_valid_rollback_version = apply_filters( |
| 178 |
'elementor/settings/tools/rollback/is_valid_rollback_version', |
| 179 |
$is_valid_rollback_version, |
| 180 |
$lowercase_version |
| 181 |
); |
| 182 |
|
| 183 |
if ( ! $is_valid_rollback_version ) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
if ( version_compare( $version, ELEMENTOR_VERSION, '>=' ) ) { |
| 188 |
continue; |
| 189 |
} |
| 190 |
|
| 191 |
$current_index++; |
| 192 |
$rollback_versions[] = $version; |
| 193 |
} |
| 194 |
|
| 195 |
set_transient( 'elementor_rollback_versions_' . ELEMENTOR_VERSION, $rollback_versions, WEEK_IN_SECONDS ); |
| 196 |
} |
| 197 |
|
| 198 |
return $rollback_versions; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Create tabs. |
| 203 |
* |
| 204 |
* Return the tools page tabs, sections and fields. |
| 205 |
* |
| 206 |
* @since 1.5.0 |
| 207 |
* @access protected |
| 208 |
* |
| 209 |
* @return array An array with the page tabs, sections and fields. |
| 210 |
*/ |
| 211 |
protected function create_tabs() { |
| 212 |
$rollback_html = '<select class="elementor-rollback-select">'; |
| 213 |
|
| 214 |
foreach ( $this->get_rollback_versions() as $version ) { |
| 215 |
$rollback_html .= "<option value='{$version}'>$version</option>"; |
| 216 |
} |
| 217 |
$rollback_html .= '</select>'; |
| 218 |
|
| 219 |
return [ |
| 220 |
'general' => [ |
| 221 |
'label' => __( 'General', 'elementor' ), |
| 222 |
'sections' => [ |
| 223 |
'tools' => [ |
| 224 |
'fields' => [ |
| 225 |
'clear_cache' => [ |
| 226 |
'label' => __( 'Regenerate CSS', 'elementor' ), |
| 227 |
'field_args' => [ |
| 228 |
'type' => 'raw_html', |
| 229 |
'html' => sprintf( '<button data-nonce="%s" class="button elementor-button-spinner" id="elementor-clear-cache-button">%s</button>', wp_create_nonce( 'elementor_clear_cache' ), __( 'Regenerate Files', 'elementor' ) ), |
| 230 |
'desc' => __( 'Styles set in Elementor are saved in CSS files in the uploads folder. Recreate those files, according to the most recent settings.', 'elementor' ), |
| 231 |
], |
| 232 |
], |
| 233 |
'reset_api_data' => [ |
| 234 |
'label' => __( 'Sync Library', 'elementor' ), |
| 235 |
'field_args' => [ |
| 236 |
'type' => 'raw_html', |
| 237 |
'html' => sprintf( '<button data-nonce="%s" class="button elementor-button-spinner" id="elementor-library-sync-button">%s</button>', wp_create_nonce( 'elementor_reset_library' ), __( 'Sync Library', 'elementor' ) ), |
| 238 |
'desc' => __( 'Elementor Library automatically updates on a daily basis. You can also manually update it by clicking on the sync button.', 'elementor' ), |
| 239 |
], |
| 240 |
], |
| 241 |
], |
| 242 |
], |
| 243 |
], |
| 244 |
], |
| 245 |
'replace_url' => [ |
| 246 |
'label' => __( 'Replace URL', 'elementor' ), |
| 247 |
'sections' => [ |
| 248 |
'replace_url' => [ |
| 249 |
'callback' => function() { |
| 250 |
$intro_text = sprintf( |
| 251 |
/* translators: %s: Codex URL */ |
| 252 |
__( '<strong>Important:</strong> It is strongly recommended that you <a target="_blank" href="%s">backup your database</a> before using Replace URL.', 'elementor' ), |
| 253 |
'http://go.elementor.com/wordpress-backups/' |
| 254 |
); |
| 255 |
$intro_text = '<div>' . $intro_text . '</div>'; |
| 256 |
|
| 257 |
echo '<h2>' . esc_html__( 'Replace URL', 'elementor' ) . '</h2>'; |
| 258 |
echo $intro_text; |
| 259 |
}, |
| 260 |
'fields' => [ |
| 261 |
'replace_url' => [ |
| 262 |
'label' => __( 'Update Site Address (URL)', 'elementor' ), |
| 263 |
'field_args' => [ |
| 264 |
'type' => 'raw_html', |
| 265 |
'html' => sprintf( '<input type="text" name="from" placeholder="http://old-url.com" class="medium-text"><input type="text" name="to" placeholder="http://new-url.com" class="medium-text"><button data-nonce="%s" class="button elementor-button-spinner" id="elementor-replace-url-button">%s</button>', wp_create_nonce( 'elementor_replace_url' ), __( 'Replace URL', 'elementor' ) ), |
| 266 |
'desc' => __( 'Enter your old and new URLs for your WordPress installation, to update all Elementor data (Relevant for domain transfers or move to \'HTTPS\').', 'elementor' ), |
| 267 |
], |
| 268 |
], |
| 269 |
], |
| 270 |
], |
| 271 |
], |
| 272 |
], |
| 273 |
'versions' => [ |
| 274 |
'label' => __( 'Version Control', 'elementor' ), |
| 275 |
'sections' => [ |
| 276 |
'rollback' => [ |
| 277 |
'label' => __( 'Rollback to Previous Version', 'elementor' ), |
| 278 |
'callback' => function() { |
| 279 |
$intro_text = sprintf( |
| 280 |
/* translators: %s: Elementor version */ |
| 281 |
__( 'Experiencing an issue with Elementor version %s? Rollback to a previous version before the issue appeared.', 'elementor' ), |
| 282 |
ELEMENTOR_VERSION |
| 283 |
); |
| 284 |
$intro_text = '<p>' . $intro_text . '</p>'; |
| 285 |
|
| 286 |
echo $intro_text; |
| 287 |
}, |
| 288 |
'fields' => [ |
| 289 |
'rollback' => [ |
| 290 |
'label' => __( 'Rollback Version', 'elementor' ), |
| 291 |
'field_args' => [ |
| 292 |
'type' => 'raw_html', |
| 293 |
'html' => sprintf( |
| 294 |
$rollback_html . '<a data-placeholder-text="' . __( 'Reinstall', 'elementor' ) . ' v{VERSION}" href="#" data-placeholder-url="%s" class="button elementor-button-spinner elementor-rollback-button">%s</a>', |
| 295 |
wp_nonce_url( admin_url( 'admin-post.php?action=elementor_rollback&version=VERSION' ), 'elementor_rollback' ), |
| 296 |
__( 'Reinstall', 'elementor' ) |
| 297 |
), |
| 298 |
'desc' => '<span style="color: red;">' . __( 'Warning: Please backup your database before making the rollback.', 'elementor' ) . '</span>', |
| 299 |
], |
| 300 |
], |
| 301 |
], |
| 302 |
], |
| 303 |
'beta' => [ |
| 304 |
'label' => __( 'Become a Beta Tester', 'elementor' ), |
| 305 |
'callback' => function() { |
| 306 |
$intro_text = __( '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' ); |
| 307 |
$intro_text = '<p>' . $intro_text . '</p>'; |
| 308 |
$newsletter_opt_in_text = sprintf( __( '<a id="beta-tester-first-to-know" href="%s">Click here</a> to join our first-to-know email updates.', 'elementor' ), '#' ); |
| 309 |
|
| 310 |
echo $intro_text; |
| 311 |
echo $newsletter_opt_in_text; |
| 312 |
}, |
| 313 |
'fields' => [ |
| 314 |
'beta' => [ |
| 315 |
'label' => __( 'Beta Tester', 'elementor' ), |
| 316 |
'field_args' => [ |
| 317 |
'type' => 'select', |
| 318 |
'default' => 'no', |
| 319 |
'options' => [ |
| 320 |
'no' => __( 'Disable', 'elementor' ), |
| 321 |
'yes' => __( 'Enable', 'elementor' ), |
| 322 |
], |
| 323 |
'desc' => '<span style="color: red;">' . __( 'Please Note: We do not recommend updating to a beta version on production sites.', 'elementor' ) . '</span>', |
| 324 |
], |
| 325 |
], |
| 326 |
], |
| 327 |
], |
| 328 |
], |
| 329 |
], |
| 330 |
]; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Get tools page title. |
| 335 |
* |
| 336 |
* Retrieve the title for the tools page. |
| 337 |
* |
| 338 |
* @since 1.5.0 |
| 339 |
* @access protected |
| 340 |
* |
| 341 |
* @return string Tools page title. |
| 342 |
*/ |
| 343 |
protected function get_page_title() { |
| 344 |
return __( 'Tools', 'elementor' ); |
| 345 |
} |
| 346 |
} |
| 347 |
|