PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.3
Elementor Website Builder – more than just a page builder v3.0.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / compatibility.php

compatibility.php in Elementor Website Builder – more than just a page builder 3.0.3, at includes/compatibility.php

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