compatibility.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace Elementor;
  3. use Elementor\TemplateLibrary\Source_Local;
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit; // Exit if accessed directly.
  6. }
  7. /**
  8. * Elementor compatibility.
  9. *
  10. * Elementor compatibility handler class is responsible for compatibility with
  11. * external plugins. The class resolves different issues with non-compatible
  12. * plugins.
  13. *
  14. * @since 1.0.0
  15. */
  16. class Compatibility {
  17. /**
  18. * Register actions.
  19. *
  20. * Run Elementor compatibility with external plugins using custom filters and
  21. * actions.
  22. *
  23. * @since 1.0.0
  24. * @access public
  25. * @static
  26. */
  27. public static function register_actions() {
  28. add_action( 'init', [ __CLASS__, 'init' ] );
  29. self::polylang_compatibility();
  30. if ( is_admin() || defined( 'WP_LOAD_IMPORTERS' ) ) {
  31. add_filter( 'wp_import_post_meta', [ __CLASS__, 'on_wp_import_post_meta' ] );
  32. add_filter( 'wxr_importer.pre_process.post_meta', [ __CLASS__, 'on_wxr_importer_pre_process_post_meta' ] );
  33. }
  34. }
  35. /**
  36. * Add new button to gutenberg.
  37. *
  38. * Insert new "Elementor" button to the gutenberg editor to create new post
  39. * using Elementor page builder.
  40. *
  41. * @since 1.9.0
  42. * @access public
  43. * @static
  44. */
  45. public static function add_new_button_to_gutenberg() {
  46. global $typenow;
  47. if ( ! gutenberg_can_edit_post_type( $typenow ) || ! User::is_current_user_can_edit_post_type( $typenow ) ) {
  48. return;
  49. }
  50. ?>
  51. <script type="text/javascript">
  52. document.addEventListener( 'DOMContentLoaded', function() {
  53. var dropdown = document.querySelector( '#split-page-title-action .dropdown' );
  54. if ( ! dropdown ) {
  55. return;
  56. }
  57. var url = '<?php echo esc_url( Utils::get_create_new_post_url( $typenow ) ); ?>';
  58. dropdown.insertAdjacentHTML( 'afterbegin', '<a href="' + url + '">Elementor</a>' );
  59. } );
  60. </script>
  61. <?php
  62. }
  63. /**
  64. * Init.
  65. *
  66. * Initialize Elementor compatibility with external plugins.
  67. *
  68. * Fired by `init` action.
  69. *
  70. * @since 1.0.0
  71. * @access public
  72. * @static
  73. */
  74. public static function init() {
  75. // Hotfix for NextGEN Gallery plugin.
  76. if ( defined( 'NGG_PLUGIN_VERSION' ) ) {
  77. add_filter( 'elementor/utils/get_edit_link', function( $edit_link ) {
  78. return add_query_arg( 'display_gallery_iframe', '', $edit_link );
  79. } );
  80. }
  81. // Hack for Ninja Forms.
  82. if ( class_exists( '\Ninja_Forms' ) && class_exists( '\NF_Display_Render' ) ) {
  83. add_action( 'elementor/preview/enqueue_styles', function() {
  84. ob_start();
  85. \NF_Display_Render::localize( 0 );
  86. ob_clean();
  87. wp_add_inline_script( 'nf-front-end', 'var nfForms = nfForms || [];' );
  88. } );
  89. }
  90. // Exclude our Library from sitemap.xml in Yoast SEO plugin.
  91. add_filter( 'wpseo_sitemaps_supported_post_types', function( $post_types ) {
  92. unset( $post_types[ Source_Local::CPT ] );
  93. return $post_types;
  94. } );
  95. add_filter( 'wpseo_sitemap_exclude_post_type', function( $retval, $post_type ) {
  96. if ( Source_Local::CPT === $post_type ) {
  97. $retval = true;
  98. }
  99. return $retval;
  100. }, 10, 2 );
  101. // Disable optimize files in Editor from Autoptimize plugin.
  102. add_filter( 'autoptimize_filter_noptimize', function( $retval ) {
  103. if ( Plugin::$instance->editor->is_edit_mode() ) {
  104. $retval = true;
  105. }
  106. return $retval;
  107. } );
  108. // Add the description (content) tab for a new product, so it can be edited with Elementor.
  109. add_filter( 'woocommerce_product_tabs', function( $tabs ) {
  110. if ( ! isset( $tabs['description'] ) && Plugin::$instance->preview->is_preview_mode() ) {
  111. $post = get_post();
  112. if ( empty( $post->post_content ) ) {
  113. $tabs['description'] = [
  114. 'title' => __( 'Description', 'elementor' ),
  115. 'priority' => 10,
  116. 'callback' => 'woocommerce_product_description_tab',
  117. ];
  118. }
  119. }
  120. return $tabs;
  121. } );
  122. // Fix WC session not defined in editor.
  123. if ( function_exists( 'WC' ) ) {
  124. add_action( 'elementor/editor/before_enqueue_scripts', function() {
  125. remove_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices' );
  126. remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices' );
  127. remove_action( 'woocommerce_before_single_product', 'wc_print_notices' );
  128. } );
  129. }
  130. // Fix Jetpack Contact Form in Editor Mode.
  131. if ( class_exists( 'Grunion_Editor_View' ) ) {
  132. add_action( 'elementor/editor/before_enqueue_scripts', function() {
  133. remove_action( 'media_buttons', 'grunion_media_button', 999 );
  134. remove_action( 'admin_enqueue_scripts', 'grunion_enable_spam_recheck' );
  135. remove_action( 'admin_notices', [ 'Grunion_Editor_View', 'handle_editor_view_js' ] );
  136. remove_action( 'admin_head', [ 'Grunion_Editor_View', 'admin_head' ] );
  137. } );
  138. }
  139. // Fix Popup Maker in Editor Mode.
  140. if ( class_exists( 'PUM_Admin_Shortcode_UI' ) ) {
  141. add_action( 'elementor/editor/before_enqueue_scripts', function() {
  142. $pum_admin_instance = \PUM_Admin_Shortcode_UI::instance();
  143. remove_action( 'print_media_templates', [ $pum_admin_instance, 'print_media_templates' ] );
  144. remove_action( 'admin_print_footer_scripts', [ $pum_admin_instance, 'admin_print_footer_scripts' ], 100 );
  145. remove_action( 'wp_ajax_pum_do_shortcode', [ $pum_admin_instance, 'wp_ajax_pum_do_shortcode' ] );
  146. remove_action( 'admin_enqueue_scripts', [ $pum_admin_instance, 'admin_enqueue_scripts' ] );
  147. remove_filter( 'pum_admin_var', [ $pum_admin_instance, 'pum_admin_var' ] );
  148. } );
  149. }
  150. // Fix Preview URL for https://premium.wpmudev.org/project/domain-mapping/ plugin
  151. if ( class_exists( 'domain_map' ) ) {
  152. add_filter( 'elementor/document/urls/preview', function( $preview_url ) {
  153. if ( wp_parse_url( $preview_url, PHP_URL_HOST ) !== $_SERVER['HTTP_HOST'] ) {
  154. $preview_url = \domain_map::utils()->unswap_url( $preview_url );
  155. $preview_url = add_query_arg( [
  156. 'dm' => \Domainmap_Module_Mapping::BYPASS,
  157. ], $preview_url );
  158. }
  159. return $preview_url;
  160. } );
  161. }
  162. // Gutenberg
  163. if ( function_exists( 'gutenberg_init' ) ) {
  164. add_action( 'admin_print_scripts-edit.php', [ __CLASS__, 'add_new_button_to_gutenberg' ], 11 );
  165. }
  166. }
  167. /**
  168. * Polylang compatibility.
  169. *
  170. * Fix Polylang compatibility with Elementor.
  171. *
  172. * @since 2.0.0
  173. * @access private
  174. * @static
  175. */
  176. private static function polylang_compatibility() {
  177. // Fix language if the `get_user_locale` is difference from the `get_locale
  178. if ( isset( $_REQUEST['action'] ) && 0 === strpos( $_REQUEST['action'], 'elementor' ) ) {
  179. add_action( 'set_current_user', function() {
  180. global $current_user;
  181. $current_user->locale = get_locale();
  182. } );
  183. // Fix for Polylang
  184. define( 'PLL_AJAX_ON_FRONT', true );
  185. add_action( 'pll_pre_init', function( $polylang ) {
  186. if ( isset( $_REQUEST['post'] ) ) {
  187. $post_language = $polylang->model->post->get_language( $_REQUEST['post'], 'locale' );
  188. if ( ! empty( $post_language ) ) {
  189. $_REQUEST['lang'] = $post_language->locale;
  190. }
  191. }
  192. } );
  193. }
  194. // Copy elementor data while polylang creates a translation copy
  195. add_filter( 'pll_copy_post_metas', [ __CLASS__, 'save_polylang_meta' ], 10, 4 );
  196. }
  197. /**
  198. * Save polylang meta.
  199. *
  200. * Copy elementor data while polylang creates a translation copy.
  201. *
  202. * Fired by `pll_copy_post_metas` filter.
  203. *
  204. * @since 1.6.0
  205. * @access public
  206. * @static
  207. *
  208. * @param array $keys List of custom fields names.
  209. * @param bool $sync True if it is synchronization, false if it is a copy.
  210. * @param int $from ID of the post from which we copy information.
  211. * @param int $to ID of the post to which we paste information.
  212. *
  213. * @return array List of custom fields names.
  214. */
  215. public static function save_polylang_meta( $keys, $sync, $from, $to ) {
  216. // Copy only for a new post.
  217. if ( ! $sync ) {
  218. Plugin::$instance->db->copy_elementor_meta( $from, $to );
  219. }
  220. return $keys;
  221. }
  222. /**
  223. * Process post meta before WP importer.
  224. *
  225. * Normalize Elementor post meta on import, We need the `wp_slash` in order
  226. * to avoid the unslashing during the `add_post_meta`.
  227. *
  228. * Fired by `wp_import_post_meta` filter.
  229. *
  230. * @since 1.0.0
  231. * @access public
  232. * @static
  233. *
  234. * @param array $post_meta Post meta.
  235. *
  236. * @return array Updated post meta.
  237. */
  238. public static function on_wp_import_post_meta( $post_meta ) {
  239. foreach ( $post_meta as &$meta ) {
  240. if ( '_elementor_data' === $meta['key'] ) {
  241. $meta['value'] = wp_slash( $meta['value'] );
  242. break;
  243. }
  244. }
  245. return $post_meta;
  246. }
  247. /**
  248. * Process post meta before WXR importer.
  249. *
  250. * Normalize Elementor post meta on import with the new WP_importer, We need
  251. * the `wp_slash` in order to avoid the unslashing during the `add_post_meta`.
  252. *
  253. * Fired by `wxr_importer.pre_process.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_wxr_importer_pre_process_post_meta( $post_meta ) {
  264. if ( '_elementor_data' === $post_meta['key'] ) {
  265. $post_meta['value'] = wp_slash( $post_meta['value'] );
  266. }
  267. return $post_meta;
  268. }
  269. }