PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/elementor/includes/compatibility.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 371 lines | 192 code | 54 blank | 125 comment | 38 complexity | 5b6d26ec0f7c494a8863009d60dc16c9 MD5 | raw file
  1. <?php
  2. namespace Elementor;
  3. use Elementor\Core\DocumentTypes\PageBase;
  4. use Elementor\TemplateLibrary\Source_Local;
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit; // Exit if accessed directly.
  7. }
  8. /**
  9. * Elementor compatibility.
  10. *
  11. * Elementor compatibility handler class is responsible for compatibility with
  12. * external plugins. The class resolves different issues with non-compatible
  13. * plugins.
  14. *
  15. * @since 1.0.0
  16. */
  17. class Compatibility {
  18. /**
  19. * Register actions.
  20. *
  21. * Run Elementor compatibility with external plugins using custom filters and
  22. * actions.
  23. *
  24. * @since 1.0.0
  25. * @access public
  26. * @static
  27. */
  28. public static function register_actions() {
  29. add_action( 'init', [ __CLASS__, 'init' ] );
  30. self::polylang_compatibility();
  31. if ( is_admin() || defined( 'WP_LOAD_IMPORTERS' ) ) {
  32. add_filter( 'wp_import_post_meta', [ __CLASS__, 'on_wp_import_post_meta' ] );
  33. add_filter( 'wxr_importer.pre_process.post_meta', [ __CLASS__, 'on_wxr_importer_pre_process_post_meta' ] );
  34. }
  35. add_action( 'elementor/maintenance_mode/mode_changed', [ __CLASS__, 'clear_3rd_party_cache' ] );
  36. }
  37. public static function clear_3rd_party_cache() {
  38. // W3 Total Cache.
  39. if ( function_exists( 'w3tc_flush_all' ) ) {
  40. w3tc_flush_all();
  41. }
  42. // WP Fastest Cache.
  43. if ( ! empty( $GLOBALS['wp_fastest_cache'] ) && method_exists( $GLOBALS['wp_fastest_cache'], 'deleteCache' ) ) {
  44. $GLOBALS['wp_fastest_cache']->deleteCache();
  45. }
  46. // WP Super Cache
  47. if ( function_exists( 'wp_cache_clean_cache' ) ) {
  48. global $file_prefix;
  49. wp_cache_clean_cache( $file_prefix, true );
  50. }
  51. }
  52. /**
  53. * Add new button to gutenberg.
  54. *
  55. * Insert new "Elementor" button to the gutenberg editor to create new post
  56. * using Elementor page builder.
  57. *
  58. * @since 1.9.0
  59. * @access public
  60. * @static
  61. */
  62. public static function add_new_button_to_gutenberg() {
  63. global $typenow;
  64. if ( ! User::is_current_user_can_edit_post_type( $typenow ) ) {
  65. return;
  66. }
  67. // Introduced in WP 5.0
  68. if ( function_exists( 'use_block_editor_for_post' ) && ! use_block_editor_for_post( $typenow ) ) {
  69. return;
  70. }
  71. // Deprecated/removed in Gutenberg plugin v5.3.0
  72. if ( function_exists( 'gutenberg_can_edit_post_type' ) && ! gutenberg_can_edit_post_type( $typenow ) ) {
  73. return;
  74. }
  75. ?>
  76. <script type="text/javascript">
  77. document.addEventListener( 'DOMContentLoaded', function() {
  78. var dropdown = document.querySelector( '#split-page-title-action .dropdown' );
  79. if ( ! dropdown ) {
  80. return;
  81. }
  82. var url = '<?php echo esc_url( Plugin::$instance->documents->get_create_new_post_url( $typenow ) ); ?>';
  83. dropdown.insertAdjacentHTML( 'afterbegin', '<a href="' + url + '">Elementor</a>' );
  84. } );
  85. </script>
  86. <?php
  87. }
  88. /**
  89. * Init.
  90. *
  91. * Initialize Elementor compatibility with external plugins.
  92. *
  93. * Fired by `init` action.
  94. *
  95. * @since 1.0.0
  96. * @access public
  97. * @static
  98. */
  99. public static function init() {
  100. // Hotfix for NextGEN Gallery plugin.
  101. if ( defined( 'NGG_PLUGIN_VERSION' ) ) {
  102. add_filter( 'elementor/document/urls/edit', function( $edit_link ) {
  103. return add_query_arg( 'display_gallery_iframe', '', $edit_link );
  104. } );
  105. }
  106. // Exclude our Library from Yoast SEO plugin.
  107. add_filter( 'wpseo_sitemaps_supported_post_types', [ __CLASS__, 'filter_library_post_type' ] );
  108. add_filter( 'wpseo_accessible_post_types', [ __CLASS__, 'filter_library_post_type' ] );
  109. add_filter( 'wpseo_sitemap_exclude_post_type', function( $retval, $post_type ) {
  110. if ( Source_Local::CPT === $post_type ) {
  111. $retval = true;
  112. }
  113. return $retval;
  114. }, 10, 2 );
  115. // Disable optimize files in Editor from Autoptimize plugin.
  116. add_filter( 'autoptimize_filter_noptimize', function( $retval ) {
  117. if ( Plugin::$instance->editor->is_edit_mode() ) {
  118. $retval = true;
  119. }
  120. return $retval;
  121. } );
  122. // Add the description (content) tab for a new product, so it can be edited with Elementor.
  123. add_filter( 'woocommerce_product_tabs', function( $tabs ) {
  124. if ( ! isset( $tabs['description'] ) && Plugin::$instance->preview->is_preview_mode() ) {
  125. $post = get_post();
  126. if ( empty( $post->post_content ) ) {
  127. $tabs['description'] = [
  128. 'title' => esc_html__( 'Description', 'elementor' ),
  129. 'priority' => 10,
  130. 'callback' => 'woocommerce_product_description_tab',
  131. ];
  132. }
  133. }
  134. return $tabs;
  135. } );
  136. // Fix WC session not defined in editor.
  137. if ( class_exists( 'woocommerce' ) ) {
  138. add_action( 'elementor/editor/before_enqueue_scripts', function() {
  139. remove_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices' );
  140. remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices' );
  141. remove_action( 'woocommerce_before_single_product', 'wc_print_notices' );
  142. } );
  143. add_filter( 'elementor/maintenance_mode/is_login_page', function( $value ) {
  144. // Support Woocommerce Account Page.
  145. if ( is_account_page() && ! is_user_logged_in() ) {
  146. $value = true;
  147. }
  148. return $value;
  149. } );
  150. }
  151. // Fix Jetpack Contact Form in Editor Mode.
  152. if ( class_exists( 'Grunion_Editor_View' ) ) {
  153. add_action( 'elementor/editor/before_enqueue_scripts', function() {
  154. remove_action( 'media_buttons', 'grunion_media_button', 999 );
  155. remove_action( 'admin_enqueue_scripts', 'grunion_enable_spam_recheck' );
  156. remove_action( 'admin_notices', [ 'Grunion_Editor_View', 'handle_editor_view_js' ] );
  157. remove_action( 'admin_head', [ 'Grunion_Editor_View', 'admin_head' ] );
  158. } );
  159. }
  160. // Fix Popup Maker in Editor Mode.
  161. if ( class_exists( 'PUM_Admin_Shortcode_UI' ) ) {
  162. add_action( 'elementor/editor/before_enqueue_scripts', function() {
  163. $pum_admin_instance = \PUM_Admin_Shortcode_UI::instance();
  164. remove_action( 'print_media_templates', [ $pum_admin_instance, 'print_media_templates' ] );
  165. remove_action( 'admin_print_footer_scripts', [ $pum_admin_instance, 'admin_print_footer_scripts' ], 100 );
  166. remove_action( 'wp_ajax_pum_do_shortcode', [ $pum_admin_instance, 'wp_ajax_pum_do_shortcode' ] );
  167. remove_action( 'admin_enqueue_scripts', [ $pum_admin_instance, 'admin_enqueue_scripts' ] );
  168. remove_filter( 'pum_admin_var', [ $pum_admin_instance, 'pum_admin_var' ] );
  169. } );
  170. }
  171. // Fix Preview URL for https://github.com/wpmudev/domain-mapping plugin
  172. if ( class_exists( 'domain_map' ) ) {
  173. add_filter( 'elementor/document/urls/preview', function( $preview_url ) {
  174. if ( wp_parse_url( $preview_url, PHP_URL_HOST ) !== $_SERVER['HTTP_HOST'] ) {
  175. $preview_url = \domain_map::utils()->unswap_url( $preview_url );
  176. $preview_url = add_query_arg( [
  177. 'dm' => \Domainmap_Module_Mapping::BYPASS,
  178. ], $preview_url );
  179. }
  180. return $preview_url;
  181. } );
  182. }
  183. // Gutenberg
  184. if ( function_exists( 'gutenberg_init' ) ) {
  185. add_action( 'admin_print_scripts-edit.php', [ __CLASS__, 'add_new_button_to_gutenberg' ], 11 );
  186. }
  187. }
  188. public static function filter_library_post_type( $post_types ) {
  189. unset( $post_types[ Source_Local::CPT ] );
  190. return $post_types;
  191. }
  192. /**
  193. * Polylang compatibility.
  194. *
  195. * Fix Polylang compatibility with Elementor.
  196. *
  197. * @since 2.0.0
  198. * @access private
  199. * @static
  200. */
  201. private static function polylang_compatibility() {
  202. // Fix language if the `get_user_locale` is difference from the `get_locale
  203. if ( isset( $_REQUEST['action'] ) && 0 === strpos( $_REQUEST['action'], 'elementor' ) ) {
  204. add_action( 'set_current_user', function() {
  205. global $current_user;
  206. $current_user->locale = get_locale();
  207. } );
  208. // Fix for Polylang
  209. define( 'PLL_AJAX_ON_FRONT', true );
  210. add_action( 'pll_pre_init', function( $polylang ) {
  211. if ( isset( $_REQUEST['post'] ) ) {
  212. $post_language = $polylang->model->post->get_language( $_REQUEST['post'], 'locale' );
  213. if ( ! empty( $post_language ) ) {
  214. $_REQUEST['lang'] = $post_language->locale;
  215. }
  216. }
  217. } );
  218. }
  219. // Copy elementor data while polylang creates a translation copy
  220. add_filter( 'pll_copy_post_metas', [ __CLASS__, 'save_polylang_meta' ], 10, 4 );
  221. }
  222. /**
  223. * Save polylang meta.
  224. *
  225. * Copy elementor data while polylang creates a translation copy.
  226. *
  227. * Fired by `pll_copy_post_metas` filter.
  228. *
  229. * @since 1.6.0
  230. * @access public
  231. * @static
  232. *
  233. * @param array $keys List of custom fields names.
  234. * @param bool $sync True if it is synchronization, false if it is a copy.
  235. * @param int $from ID of the post from which we copy information.
  236. * @param int $to ID of the post to which we paste information.
  237. *
  238. * @return array List of custom fields names.
  239. */
  240. public static function save_polylang_meta( $keys, $sync, $from, $to ) {
  241. // Copy only for a new post.
  242. if ( ! $sync ) {
  243. Plugin::$instance->db->copy_elementor_meta( $from, $to );
  244. }
  245. return $keys;
  246. }
  247. /**
  248. * Process post meta before WP importer.
  249. *
  250. * Normalize Elementor post meta on import, We need the `wp_slash` in order
  251. * to avoid the unslashing during the `add_post_meta`.
  252. *
  253. * Fired by `wp_import_post_meta` filter.
  254. *
  255. * @since 1.0.0
  256. * @access public
  257. * @static
  258. *
  259. * @param array $post_meta Post meta.
  260. *
  261. * @return array Updated post meta.
  262. */
  263. public static function on_wp_import_post_meta( $post_meta ) {
  264. $is_wp_importer_before_0_7 = self::is_wp_importer_before_0_7();
  265. if ( $is_wp_importer_before_0_7 ) {
  266. foreach ( $post_meta as &$meta ) {
  267. if ( '_elementor_data' === $meta['key'] ) {
  268. $meta['value'] = wp_slash( $meta['value'] );
  269. break;
  270. }
  271. }
  272. }
  273. return $post_meta;
  274. }
  275. /**
  276. * Is WP Importer Before 0.7
  277. *
  278. * Checks if WP Importer is installed, and whether its version is older than 0.7.
  279. *
  280. * @return bool
  281. */
  282. public static function is_wp_importer_before_0_7() {
  283. $wp_importer = get_plugins( '/wordpress-importer' );
  284. if ( ! empty( $wp_importer ) ) {
  285. $wp_importer_version = $wp_importer['wordpress-importer.php']['Version'];
  286. if ( version_compare( $wp_importer_version, '0.7', '<' ) ) {
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. /**
  293. * Process post meta before WXR importer.
  294. *
  295. * Normalize Elementor post meta on import with the new WP_importer, We need
  296. * the `wp_slash` in order to avoid the unslashing during the `add_post_meta`.
  297. *
  298. * Fired by `wxr_importer.pre_process.post_meta` filter.
  299. *
  300. * @since 1.0.0
  301. * @access public
  302. * @static
  303. *
  304. * @param array $post_meta Post meta.
  305. *
  306. * @return array Updated post meta.
  307. */
  308. public static function on_wxr_importer_pre_process_post_meta( $post_meta ) {
  309. $is_wp_importer_before_0_7 = self::is_wp_importer_before_0_7();
  310. if ( $is_wp_importer_before_0_7 ) {
  311. if ( '_elementor_data' === $post_meta['key'] ) {
  312. $post_meta['value'] = wp_slash( $post_meta['value'] );
  313. }
  314. }
  315. return $post_meta;
  316. }
  317. }