balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

tools.php (13007B)


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