PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.3
Elementor Website Builder – more than just a page builder v3.32.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.32.3, at modules/floating-buttons/module.php

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