tools.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor "Tools" page in WordPress Dashboard.
  8. *
  9. * Elementor settings page handler class responsible for creating and displaying
  10. * Elementor "Tools" page in WordPress dashboard.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Tools extends Settings_Page {
  15. /**
  16. * Settings page ID for Elementor tools.
  17. */
  18. const PAGE_ID = 'elementor-tools';
  19. /**
  20. * Register admin menu.
  21. *
  22. * Add new Elementor Tools admin menu.
  23. *
  24. * Fired by `admin_menu` action.
  25. *
  26. * @since 1.0.0
  27. * @access public
  28. */
  29. public function register_admin_menu() {
  30. add_submenu_page(
  31. Settings::PAGE_ID,
  32. __( 'Tools', 'elementor' ),
  33. __( 'Tools', 'elementor' ),
  34. 'manage_options',
  35. self::PAGE_ID,
  36. [ $this, 'display_settings_page' ]
  37. );
  38. }
  39. /**
  40. * Clear cache.
  41. *
  42. * Delete post meta containing the post CSS file data. And delete the actual
  43. * CSS files from the upload directory.
  44. *
  45. * Fired by `wp_ajax_elementor_clear_cache` action.
  46. *
  47. * @since 1.0.0
  48. * @access public
  49. */
  50. public function ajax_elementor_clear_cache() {
  51. check_ajax_referer( 'elementor_clear_cache', '_nonce' );
  52. Plugin::$instance->files_manager->clear_cache();
  53. wp_send_json_success();
  54. }
  55. /**
  56. * Replace URLs.
  57. *
  58. * Sends an ajax request to replace old URLs to new URLs. This method also
  59. * updates all the Elementor data.
  60. *
  61. * Fired by `wp_ajax_elementor_replace_url` action.
  62. *
  63. * @since 1.1.0
  64. * @access public
  65. */
  66. public function ajax_elementor_replace_url() {
  67. check_ajax_referer( 'elementor_replace_url', '_nonce' );
  68. $from = ! empty( $_POST['from'] ) ? $_POST['from'] : '';
  69. $to = ! empty( $_POST['to'] ) ? $_POST['to'] : '';
  70. try {
  71. $results = Utils::replace_urls( $from, $to );
  72. wp_send_json_success( $results );
  73. } catch ( \Exception $e ) {
  74. wp_send_json_error( $e->getMessage() );
  75. }
  76. }
  77. /**
  78. * Elementor version rollback.
  79. *
  80. * Rollback to previous Elementor version.
  81. *
  82. * Fired by `admin_post_elementor_rollback` action.
  83. *
  84. * @since 1.5.0
  85. * @access public
  86. */
  87. public function post_elementor_rollback() {
  88. check_admin_referer( 'elementor_rollback' );
  89. $plugin_slug = basename( ELEMENTOR__FILE__, '.php' );
  90. $rollback = new Rollback(
  91. [
  92. 'version' => ELEMENTOR_PREVIOUS_STABLE_VERSION,
  93. 'plugin_name' => ELEMENTOR_PLUGIN_BASE,
  94. 'plugin_slug' => $plugin_slug,
  95. 'package_url' => sprintf( 'https://downloads.wordpress.org/plugin/%s.%s.zip', $plugin_slug, ELEMENTOR_PREVIOUS_STABLE_VERSION ),
  96. ]
  97. );
  98. $rollback->run();
  99. wp_die(
  100. '', __( 'Rollback to Previous Version', 'elementor' ), [
  101. 'response' => 200,
  102. ]
  103. );
  104. }
  105. /**
  106. * Tools page constructor.
  107. *
  108. * Initializing Elementor "Tools" page.
  109. *
  110. * @since 1.0.0
  111. * @access public
  112. */
  113. public function __construct() {
  114. parent::__construct();
  115. add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 205 );
  116. if ( ! empty( $_POST ) ) {
  117. add_action( 'wp_ajax_elementor_clear_cache', [ $this, 'ajax_elementor_clear_cache' ] );
  118. add_action( 'wp_ajax_elementor_replace_url', [ $this, 'ajax_elementor_replace_url' ] );
  119. }
  120. add_action( 'admin_post_elementor_rollback', [ $this, 'post_elementor_rollback' ] );
  121. }
  122. /**
  123. * Create tabs.
  124. *
  125. * Return the tools page tabs, sections and fields.
  126. *
  127. * @since 1.5.0
  128. * @access protected
  129. *
  130. * @return array An array with the page tabs, sections and fields.
  131. */
  132. protected function create_tabs() {
  133. return [
  134. 'general' => [
  135. 'label' => __( 'General', 'elementor' ),
  136. 'sections' => [
  137. 'tools' => [
  138. 'fields' => [
  139. 'clear_cache' => [
  140. 'label' => __( 'Regenerate CSS', 'elementor' ),
  141. 'field_args' => [
  142. 'type' => 'raw_html',
  143. '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' ) ),
  144. 'desc' => __( 'Styles set in Elementor are saved in CSS files in the uploads folder. Recreate those files, according to the most recent settings.', 'elementor' ),
  145. ],
  146. ],
  147. 'reset_api_data' => [
  148. 'label' => __( 'Sync Library', 'elementor' ),
  149. 'field_args' => [
  150. 'type' => 'raw_html',
  151. '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' ) ),
  152. 'desc' => __( 'Elementor Library automatically updates on a daily basis. You can also manually update it by clicking on the sync button.', 'elementor' ),
  153. ],
  154. ],
  155. ],
  156. ],
  157. ],
  158. ],
  159. 'replace_url' => [
  160. 'label' => __( 'Replace URL', 'elementor' ),
  161. 'sections' => [
  162. 'replace_url' => [
  163. 'callback' => function() {
  164. $intro_text = sprintf(
  165. /* translators: %s: Codex URL */
  166. __( '<strong>Important:</strong> It is strongly recommended that you <a target="_blank" href="%s">backup your database</a> before using Replace URL.', 'elementor' ),
  167. 'https://codex.wordpress.org/WordPress_Backups'
  168. );
  169. $intro_text = '<div>' . $intro_text . '</div>';
  170. echo $intro_text;
  171. },
  172. 'fields' => [
  173. 'replace_url' => [
  174. 'label' => __( 'Update Site Address (URL)', 'elementor' ),
  175. 'field_args' => [
  176. 'type' => 'raw_html',
  177. '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' ) ),
  178. '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' ),
  179. ],
  180. ],
  181. ],
  182. ],
  183. ],
  184. ],
  185. 'versions' => [
  186. 'label' => __( 'Version Control', 'elementor' ),
  187. 'sections' => [
  188. 'rollback' => [
  189. 'label' => __( 'Rollback to Previous Version', 'elementor' ),
  190. 'callback' => function() {
  191. $intro_text = sprintf(
  192. /* translators: %s: Elementor version */
  193. __( 'Experiencing an issue with Elementor version %s? Rollback to a previous version before the issue appeared.', 'elementor' ),
  194. ELEMENTOR_VERSION
  195. );
  196. $intro_text = '<p>' . $intro_text . '</p>';
  197. echo $intro_text;
  198. },
  199. 'fields' => [
  200. 'rollback' => [
  201. 'label' => __( 'Rollback Version', 'elementor' ),
  202. 'field_args' => [
  203. 'type' => 'raw_html',
  204. 'html' => sprintf(
  205. '<a href="%s" class="button elementor-button-spinner elementor-rollback-button">%s</a>',
  206. wp_nonce_url( admin_url( 'admin-post.php?action=elementor_rollback' ), 'elementor_rollback' ),
  207. sprintf(
  208. /* translators: %s: Elementor previous stable version */
  209. __( 'Reinstall v%s', 'elementor' ),
  210. ELEMENTOR_PREVIOUS_STABLE_VERSION
  211. )
  212. ),
  213. 'desc' => '<span style="color: red;">' . __( 'Warning: Please backup your database before making the rollback.', 'elementor' ) . '</span>',
  214. ],
  215. ],
  216. ],
  217. ],
  218. 'beta' => [
  219. 'label' => __( 'Become a Beta Tester', 'elementor' ),
  220. 'callback' => function() {
  221. $intro_text = __( 'Turn-on Beta Tester, to get notified when a new beta version of Elementor or E-Pro is available. The Beta version will not install automatically. You always have the option to ignore it.', 'elementor' );
  222. $intro_text = '<p>' . $intro_text . '</p>';
  223. echo $intro_text;
  224. },
  225. 'fields' => [
  226. 'beta' => [
  227. 'label' => __( 'Beta Tester', 'elementor' ),
  228. 'field_args' => [
  229. 'type' => 'select',
  230. 'default' => 'no',
  231. 'options' => [
  232. 'no' => __( 'Disable', 'elementor' ),
  233. 'yes' => __( 'Enable', 'elementor' ),
  234. ],
  235. 'desc' => '<span style="color: red;">' . __( 'Please Note: We do not recommend updating to a beta version on production sites.', 'elementor' ) . '</span>',
  236. ],
  237. ],
  238. ],
  239. ],
  240. ],
  241. ],
  242. ];
  243. }
  244. /**
  245. * Display settings page.
  246. *
  247. * Output the content for the settings page.
  248. *
  249. * @since 1.5.2
  250. * @access public
  251. */
  252. public function display_settings_page() {
  253. wp_enqueue_script( 'elementor-dialog' );
  254. parent::display_settings_page();
  255. }
  256. /**
  257. * Get tools page title.
  258. *
  259. * Retrieve the title for the tools page.
  260. *
  261. * @since 1.5.0
  262. * @access protected
  263. *
  264. * @return string Tools page title.
  265. */
  266. protected function get_page_title() {
  267. return __( 'Tools', 'elementor' );
  268. }
  269. }