PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.3
Elementor Website Builder – more than just a page builder v3.27.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 / modules / floating-buttons / module.php

module.php in Elementor Website Builder – more than just a page builder 3.27.3, at modules/floating-buttons/module.php

607 lines 18.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\FloatingButtons;
4
5 use Elementor\Controls_Manager;
6 use Elementor\Core\Admin\Menu\Admin_Menu_Manager;
7 use Elementor\Core\Base\Document;
8 use Elementor\Core\Base\Module as BaseModule;
9 use Elementor\Core\Documents_Manager;
10 use Elementor\Core\Experiments\Manager;
11 use Elementor\Modules\FloatingButtons\Base\Widget_Floating_Bars_Base;
12 use Elementor\Modules\FloatingButtons\AdminMenuItems\Floating_Buttons_Empty_View_Menu_Item;
13 use Elementor\Modules\FloatingButtons\AdminMenuItems\Floating_Buttons_Menu_Item;
14 use Elementor\Modules\FloatingButtons\Base\Widget_Contact_Button_Base;
15 use Elementor\Modules\FloatingButtons\Control\Hover_Animation_Floating_Buttons;
16 use Elementor\Modules\FloatingButtons\Documents\Floating_Buttons;
17 use Elementor\Plugin;
18 use Elementor\TemplateLibrary\Source_Local;
19 use Elementor\Utils as ElementorUtils;
20
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit; // Exit if accessed directly
23 }
24
25 class Module extends BaseModule {
26
27 const EXPERIMENT_NAME = 'floating-buttons';
28 const FLOATING_ELEMENTS_TYPE_META_KEY = '_elementor_floating_elements_type';
29 const ROUTER_OPTION_KEY = 'elementor_floating_buttons_router_version';
30 const META_CLICK_TRACKING = '_elementor_click_tracking';
31 const CLICK_TRACKING_NONCE = 'elementor-conversion-center-click';
32 const FLOATING_BUTTONS_DOCUMENT_TYPE = 'floating-buttons';
33 const CPT_FLOATING_BUTTONS = 'e-floating-buttons';
34 const ADMIN_PAGE_SLUG_CONTACT = 'edit.php?post_type=e-floating-buttons';
35 const WIDGET_HAS_CUSTOM_BREAKPOINTS = true;
36
37 private $has_contact_pages = null;
38 private $trashed_contact_pages;
39
40 public static function is_active(): bool {
41 return Plugin::$instance->experiments->is_feature_active( 'container' );
42 }
43
44 public static function get_floating_elements_types() {
45 return [
46 'floating-buttons' => esc_html__( 'Floating Buttons', 'elementor' ),
47 'floating-bars' => esc_html__( 'Floating Bars', 'elementor' ),
48 ];
49 }
50
51 // TODO: This is a hidden experiment which needs to remain enabled like this until 3.26 for pro compatibility.
52 public static function get_experimental_data() {
53 return [
54 'name' => self::EXPERIMENT_NAME,
55 'title' => esc_html__( 'Floating Buttons', 'elementor' ),
56 'hidden' => true,
57 'default' => Manager::STATE_ACTIVE,
58 'release_status' => Manager::RELEASE_STATUS_STABLE,
59 'mutable' => false,
60 ];
61 }
62
63 public function get_name(): string {
64 return static::EXPERIMENT_NAME;
65 }
66
67 public function get_widgets(): array {
68
69 return [
70 'Contact_Buttons',
71 'Floating_Bars_Var_1',
72 ];
73 }
74
75 private function register_admin_menu_legacy( Admin_Menu_Manager $admin_menu ) {
76 $menu_args = $this->get_contact_menu_args();
77 $function = $menu_args['function'];
78 if ( is_callable( $function ) ) {
79 $admin_menu->register( $menu_args['menu_slug'], new Floating_Buttons_Empty_View_Menu_Item( $function ) );
80 } else {
81 $admin_menu->register( $menu_args['menu_slug'], new Floating_Buttons_Menu_Item() );
82 };
83
84 }
85
86 public function __construct() {
87 parent::__construct();
88
89 if ( Floating_Buttons::is_creating_floating_buttons_page() || Floating_Buttons::is_editing_existing_floating_buttons_page() ) {
90 Controls_Manager::add_tab(
91 Widget_Contact_Button_Base::TAB_ADVANCED,
92 esc_html__( 'Advanced', 'elementor' )
93 );
94
95 Controls_Manager::add_tab(
96 Widget_Floating_Bars_Base::TAB_ADVANCED,
97 esc_html__( 'Advanced', 'elementor' )
98 );
99 }
100
101 $this->register_contact_pages_cpt();
102
103 add_action( 'elementor/documents/register', function ( Documents_Manager $documents_manager ) {
104 $documents_manager->register_document_type(
105 static::FLOATING_BUTTONS_DOCUMENT_TYPE,
106 Floating_Buttons::get_class_full_name()
107 );
108 } );
109
110 add_action( 'current_screen', function() {
111 $screen = get_current_screen();
112 if ( $screen && 'edit-e-floating-buttons' === $screen->id ) {
113 $this->flush_permalinks_on_elementor_version_change();
114 }
115 });
116
117 add_action( 'wp_ajax_elementor_send_clicks', [ $this, 'handle_click_tracking' ] );
118 add_action( 'wp_ajax_nopriv_elementor_send_clicks', [ $this, 'handle_click_tracking' ] );
119
120 add_action( 'elementor/frontend/after_register_styles', [ $this, 'register_styles' ] );
121
122 add_action( 'elementor/controls/register', function ( Controls_Manager $controls_manager ) {
123 $controls_manager->register( new Hover_Animation_Floating_Buttons() );
124 });
125
126 add_filter( 'elementor/widget/common/register_css_attributes_control', function ( $common_controls ) {
127 if ( Floating_Buttons::is_creating_floating_buttons_page() || Floating_Buttons::is_editing_existing_floating_buttons_page() ) {
128 return false;
129 }
130
131 return $common_controls;
132 } );
133
134 add_filter( 'elementor/settings/controls/checkbox_list_cpt/post_type_objects', function ( $post_types ) {
135 unset( $post_types[ static::CPT_FLOATING_BUTTONS ] );
136
137 return $post_types;
138 } );
139
140 add_filter(
141 'elementor/template_library/sources/local/is_valid_template_type',
142 function ( $is_valid_template_type, $cpt ) {
143
144 if ( in_array( static::CPT_FLOATING_BUTTONS, $cpt, true ) ) {
145 return true;
146 }
147
148 return $is_valid_template_type;
149 },
150 10,
151 2
152 );
153
154 if ( ! ElementorUtils::has_pro() ) {
155 add_action( 'wp_footer', function () {
156 $this->render_floating_buttons();
157 } );
158 }
159
160 add_action( 'elementor/admin-top-bar/is-active', function ( $is_top_bar_active, $current_screen ) {
161
162 if ( strpos( $current_screen->id ?? '', static::CPT_FLOATING_BUTTONS ) !== false ) {
163 return true;
164 }
165
166 return $is_top_bar_active;
167 }, 10, 2 );
168
169 add_action( 'elementor/admin/menu/register', function( Admin_Menu_Manager $admin_menu ) {
170 $this->register_admin_menu_legacy( $admin_menu );
171 }, Source_Local::ADMIN_MENU_PRIORITY + 20 );
172
173 add_action( 'elementor/admin/localize_settings', function ( array $settings ) {
174 return $this->admin_localize_settings( $settings );
175 } );
176
177 add_action( 'elementor/editor/localize_settings', function ( $data ) {
178 return $this->editor_localize_settings( $data );
179 } );
180
181 add_filter( 'elementor/template_library/sources/local/register_taxonomy_cpts', function ( array $cpts ) {
182 $cpts[] = static::CPT_FLOATING_BUTTONS;
183
184 return $cpts;
185 } );
186
187 add_action( 'admin_init', function () {
188 $action = filter_input( INPUT_GET, 'action' );
189 $menu_args = $this->get_contact_menu_args();
190
191 switch ( $action ) {
192 case 'remove_from_entire_site':
193 $post = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT );
194 check_admin_referer( 'remove_from_entire_site_' . $post );
195 delete_post_meta( $post, '_elementor_conditions' );
196
197 wp_redirect( $menu_args['menu_slug'] );
198 exit;
199 case 'set_as_entire_site':
200 $post = filter_input( INPUT_GET, 'post', FILTER_VALIDATE_INT );
201 check_admin_referer( 'set_as_entire_site_' . $post );
202
203 $posts = get_posts( [
204 'post_type' => static::CPT_FLOATING_BUTTONS,
205 'posts_per_page' => -1,
206 'post_status' => 'publish',
207 'fields' => 'ids',
208 'no_found_rows' => true,
209 'update_post_term_cache' => false,
210 'update_post_meta_cache' => false,
211 'meta_query' => Floating_Buttons::get_meta_query_for_floating_buttons(
212 Floating_Buttons::get_floating_element_type( $post )
213 ),
214 ] );
215
216 foreach ( $posts as $post_id ) {
217 delete_post_meta( $post_id, '_elementor_conditions' );
218 }
219
220 update_post_meta( $post, '_elementor_conditions', [ 'include/general' ] );
221
222 wp_redirect( $menu_args['menu_slug'] );
223 exit;
224 default:
225 break;
226 }
227 } );
228
229 add_action( 'manage_' . static::CPT_FLOATING_BUTTONS . '_posts_columns', function( $posts_columns ) {
230 $source_local = Plugin::$instance->templates_manager->get_source( 'local' );
231 unset( $posts_columns['date'] );
232 unset( $posts_columns['comments'] );
233 $posts_columns['click_tracking'] = esc_html__( 'Click Tracking', 'elementor' );
234
235 if ( ! ElementorUtils::has_pro() ) {
236 $posts_columns['instances'] = esc_html__( 'Instances', 'elementor' );
237 }
238
239 return $source_local->admin_columns_headers( $posts_columns );
240 } );
241
242 add_action(
243 'manage_' . static::CPT_FLOATING_BUTTONS . '_posts_custom_column',
244 [ $this, 'set_admin_columns_content' ],
245 10,
246 2
247 );
248
249 add_action( 'admin_bar_menu', function ( $admin_bar ) {
250
251 $this->override_admin_bar_add_contact( $admin_bar );
252 }, 100 );
253 }
254
255 public function is_preview_for_document( $post_id ) {
256 $preview_id = ElementorUtils::get_super_global_value( $_GET, 'preview_id' );
257 $preview = ElementorUtils::get_super_global_value( $_GET, 'preview' );
258
259 return 'true' === $preview && (int) $post_id === (int) $preview_id;
260 }
261
262 public function handle_click_tracking() {
263 $data = filter_input_array( INPUT_POST, [
264 'clicks' => [
265 'filter' => FILTER_VALIDATE_INT,
266 'flags' => FILTER_REQUIRE_ARRAY,
267 ],
268 '_nonce' => FILTER_UNSAFE_RAW,
269 ] );
270
271 if ( ! wp_verify_nonce( $data['_nonce'], static::CLICK_TRACKING_NONCE ) ) {
272 wp_send_json_error( [ 'message' => 'Invalid nonce' ] );
273 }
274
275 if ( ! check_ajax_referer( static::CLICK_TRACKING_NONCE, '_nonce', false ) ) {
276 wp_send_json_error( [ 'message' => 'Invalid referrer' ] );
277 }
278
279 $posts_to_update = [];
280
281 foreach ( $data['clicks'] as $post_id ) {
282 if ( ! isset( $posts_to_update[ $post_id ] ) ) {
283 $starting_clicks = (int) get_post_meta( $post_id, static::META_CLICK_TRACKING, true );
284 $posts_to_update[ $post_id ] = $starting_clicks ? $starting_clicks : 0;
285 }
286 $posts_to_update[ $post_id ] ++;
287 }
288
289 foreach ( $posts_to_update as $post_id => $clicks ) {
290 if ( self::CPT_FLOATING_BUTTONS !== get_post_type( $post_id ) ) {
291 continue;
292 }
293
294 if ( 'publish' !== get_post_status( $post_id ) ) {
295 continue;
296 }
297
298 update_post_meta( $post_id, static::META_CLICK_TRACKING, $clicks );
299 }
300
301 wp_send_json_success();
302 }
303
304 public function set_admin_columns_content( $column_name, $post_id ) {
305 $document = Plugin::$instance->documents->get( $post_id );
306
307 if ( method_exists( $document, 'admin_columns_content' ) ) {
308 $document->admin_columns_content( $column_name );
309 }
310
311 switch ( $column_name ) {
312 case 'click_tracking':
313 $click_tracking = get_post_meta( $post_id, static::META_CLICK_TRACKING, true );
314 echo esc_html( $click_tracking );
315 break;
316 case 'instances':
317 if ( ElementorUtils::has_pro() ) {
318 break;
319 }
320 $instances = get_post_meta( $post_id, '_elementor_conditions', true );
321 if ( $instances ) {
322 echo esc_html__( 'Entire Site', 'elementor' );
323 }
324 break;
325 default:
326 break;
327 }
328 }
329
330 public function flush_permalinks_on_elementor_version_change() {
331 if ( get_option( static::ROUTER_OPTION_KEY ) !== ELEMENTOR_VERSION ) {
332 flush_rewrite_rules();
333 update_option( static::ROUTER_OPTION_KEY, ELEMENTOR_VERSION );
334 }
335 }
336
337 private function get_trashed_contact_posts(): array {
338 if ( $this->trashed_contact_pages ) {
339 return $this->trashed_contact_pages;
340 }
341
342 $this->trashed_contact_pages = $this->get_trashed_posts(
343 static::CPT_FLOATING_BUTTONS,
344 static::FLOATING_BUTTONS_DOCUMENT_TYPE
345 );
346
347 return $this->trashed_contact_pages;
348 }
349
350 private function get_trashed_posts( string $cpt, string $document_type ) {
351 $query = new \WP_Query( [
352 'no_found_rows' => true,
353 'post_type' => $cpt,
354 'post_status' => 'trash',
355 'posts_per_page' => 1,
356 'meta_key' => '_elementor_template_type',
357 'meta_value' => $document_type,
358 ] );
359
360 return $query->posts;
361 }
362
363 private function get_add_new_contact_page_url() {
364 if ( ElementorUtils::has_pro() ) {
365 return Plugin::$instance->documents->get_create_new_post_url(
366 static::CPT_FLOATING_BUTTONS,
367 static::FLOATING_BUTTONS_DOCUMENT_TYPE
368 );
369 }
370
371 return Plugin::$instance->documents->get_create_new_post_url(
372 static::CPT_FLOATING_BUTTONS,
373 static::FLOATING_BUTTONS_DOCUMENT_TYPE
374 ) . '#library';
375 }
376
377 public function print_empty_contact_pages_page() {
378 $template_sources = Plugin::$instance->templates_manager->get_registered_sources();
379 $source_local = $template_sources['local'];
380 $trashed_posts = $this->get_trashed_contact_posts();
381
382 ?>
383 <div class="e-landing-pages-empty">
384 <?php
385 /** @var Source_Local $source_local */
386 $source_local->print_blank_state_template(
387 esc_html__( 'Floating Element', 'elementor' ),
388 $this->get_add_new_contact_page_url(),
389 nl2br( esc_html__( 'Add a Floating element so your users can easily get in touch!', 'elementor' ) )
390 );
391
392 if ( ! empty( $trashed_posts ) ) : ?>
393 <div class="e-trashed-items">
394 <?php
395 printf(
396 /* translators: %1$s Link open tag, %2$s: Link close tag. */
397 esc_html__( 'Or view %1$sTrashed Items%1$s', 'elementor' ),
398 '<a href="' . esc_url( admin_url( 'edit.php?post_status=trash&post_type=' . self::CPT_FLOATING_BUTTONS ) ) . '">',
399 '</a>'
400 );
401 ?>
402 </div>
403 <?php endif; ?>
404 </div>
405 <?php
406 }
407
408 private function admin_localize_settings( $settings ) {
409 $contact_menu_slug = $this->get_contact_menu_args()['menu_slug'];
410
411 if ( static::CPT_FLOATING_BUTTONS === $contact_menu_slug ) {
412 $contact_menu_slug = 'admin.php?page=' . $contact_menu_slug;
413 }
414
415 $additional_settings = [
416 'urls' => [
417 'addNewLinkUrlContact' => $this->get_add_new_contact_page_url(),
418 'viewContactPageUrl' => $contact_menu_slug,
419 ],
420 'contactPages' => [
421 'hasPages' => $this->has_contact_pages(),
422 ],
423 ];
424
425 return array_replace_recursive( $settings, $additional_settings );
426 }
427
428 private function register_contact_pages_cpt() {
429 $this->register_post_type(
430 Floating_Buttons::get_labels(),
431 static::CPT_FLOATING_BUTTONS
432 );
433 }
434
435 private function register_post_type( array $labels, string $cpt ) {
436 $args = [
437 'labels' => $labels,
438 'public' => true,
439 'show_in_menu' => 'edit.php?post_type=elementor_library&tabs_group=library',
440 'show_in_nav_menus' => false,
441 'capability_type' => 'post',
442 'taxonomies' => [ Source_Local::TAXONOMY_TYPE_SLUG ],
443 'show_in_rest' => true,
444 'supports' => [
445 'title',
446 'editor',
447 'comments',
448 'revisions',
449 'trackbacks',
450 'author',
451 'excerpt',
452 'page-attributes',
453 'thumbnail',
454 'custom-fields',
455 'post-formats',
456 'elementor',
457 ],
458 ];
459
460 register_post_type( $cpt, $args );
461 }
462
463 private function has_contact_pages(): bool {
464 if ( null !== $this->has_contact_pages ) {
465 return $this->has_contact_pages;
466 }
467
468 $this->has_contact_pages = $this->has_pages(
469 static::CPT_FLOATING_BUTTONS,
470 static::FLOATING_BUTTONS_DOCUMENT_TYPE
471 );
472
473 return $this->has_contact_pages;
474 }
475
476 private function has_pages( string $cpt, string $document_type ): bool {
477 $posts_query = new \WP_Query( [
478 'no_found_rows' => true,
479 'post_type' => $cpt,
480 'post_status' => 'any',
481 'posts_per_page' => 1,
482 'meta_key' => '_elementor_template_type',
483 'meta_value' => $document_type,
484 ] );
485
486 return $posts_query->post_count > 0;
487
488 }
489
490 private function get_contact_menu_args(): array {
491 if ( $this->has_contact_pages() ) {
492 $menu_slug = static::ADMIN_PAGE_SLUG_CONTACT;
493 $function = null;
494 } else {
495 $menu_slug = static::CPT_FLOATING_BUTTONS;
496 $function = [ $this, 'print_empty_contact_pages_page' ];
497 }
498
499 return [
500 'menu_slug' => $menu_slug,
501 'function' => $function,
502 ];
503 }
504
505 public function override_admin_bar_add_contact( $admin_bar ): void {
506 $new_contact_page_node = $admin_bar->get_node( 'new-e-floating-buttons' );
507
508 if ( $new_contact_page_node ) {
509 $new_contact_page_node->href = $this->get_add_new_contact_page_url();
510
511 $admin_bar->add_node( $new_contact_page_node );
512 }
513 }
514
515 private function editor_localize_settings( $data ) {
516 $data['admin_floating_button_admin_url'] = admin_url( $this->get_contact_menu_args()['menu_slug'] );
517 return $data;
518 }
519
520 private function render_floating_buttons(): void {
521 if ( Plugin::$instance->preview->is_preview_mode() ) {
522 $post_id = ElementorUtils::get_super_global_value( $_GET, 'elementor-preview' );
523 $document = Plugin::$instance->documents->get( $post_id );
524
525 if (
526 $document instanceof Document &&
527 $document->get_name() === static::FLOATING_BUTTONS_DOCUMENT_TYPE
528 ) {
529 return;
530 }
531 }
532
533 $query = new \WP_Query( [
534 'post_type' => static::CPT_FLOATING_BUTTONS,
535 'posts_per_page' => - 1,
536 'post_status' => 'publish',
537 'fields' => 'ids',
538 'meta_key' => '_elementor_conditions',
539 'meta_compare' => 'EXISTS',
540 ] );
541
542 if ( ! $query->have_posts() ) {
543 return;
544 }
545
546 foreach ( $query->posts as $post_id ) {
547 $conditions = get_post_meta( $post_id, '_elementor_conditions', true );
548
549 if ( ! $conditions ) {
550 continue;
551 }
552
553 if (
554 in_array( 'include/general', $conditions ) &&
555 ! $this->is_preview_for_document( $post_id ) &&
556 get_the_ID() !== $post_id
557 ) {
558 $document = Plugin::$instance->documents->get( $post_id );
559 $document->print_content();
560 }
561 }
562 }
563
564 /**
565 * Register styles.
566 *
567 * At build time, Elementor compiles `/modules/floating-buttons/assets/scss/widgets/*.scss`
568 * to `/assets/css/widget-*.min.css`.
569 *
570 * @return void
571 */
572 public function register_styles() {
573 $direction_suffix = is_rtl() ? '-rtl' : '';
574 $widget_styles = $this->get_widgets_style_list();
575 $has_custom_breakpoints = Plugin::$instance->breakpoints->has_custom_breakpoints();
576
577 foreach ( $widget_styles as $widget_style_name => $widget_has_responsive_style ) {
578 $should_load_responsive_css = $widget_has_responsive_style ? $has_custom_breakpoints : false;
579
580 wp_register_style(
581 $widget_style_name,
582 $this->get_frontend_file_url( "{$widget_style_name}{$direction_suffix}.min.css", $should_load_responsive_css ),
583 [ 'elementor-frontend', 'elementor-icons' ],
584 $should_load_responsive_css ? null : ELEMENTOR_VERSION
585 );
586 }
587 }
588
589 private function get_widgets_style_list():array {
590 return [
591 'widget-floating-buttons' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS, // TODO: Remove in v3.27.0 [ED-15717]
592 'widget-floating-bars-base' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
593 'widget-floating-bars-var-2' => ! self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
594 'widget-floating-bars-var-3' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
595 'widget-contact-buttons-base' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
596 'widget-contact-buttons-var-1' => ! self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
597 'widget-contact-buttons-var-3' => ! self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
598 'widget-contact-buttons-var-4' => ! self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
599 'widget-contact-buttons-var-6' => ! self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
600 'widget-contact-buttons-var-7' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
601 'widget-contact-buttons-var-8' => ! self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
602 'widget-contact-buttons-var-9' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
603 'widget-contact-buttons-var-10' => self::WIDGET_HAS_CUSTOM_BREAKPOINTS,
604 ];
605 }
606 }
607