PluginProbe
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager / 0.0.3
Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager v0.0.3
0.0.11 0.0.10 0.0.9 0.0.8 0.0.7 0.0.6 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5
cookiez / modules / elementor / module.php

module.php in Cookie Consent – GDPR & CCPA Cookie Banner & Consent Manager 0.0.3, at modules/elementor/module.php

163 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Cookiez\Modules\Elementor;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use Cookiez\Classes\Module_Base;
10 use Cookiez\Modules\Elementor\Classes\{
11 Banner_Settings_Integration,
12 Document_Utils,
13 Popup_Admin_Filter,
14 };
15 use Cookiez\Modules\Elementor\Documents\Types\{
16 Cookie_Consent_Document,
17 Preferences_Banner_Document,
18 };
19 use Cookiez\Modules\Elementor\Widgets\{
20 Cookie_Consent_Content,
21 Cookie_Consent_Footer,
22 Cookie_Consent_Heading,
23 Preferences_Content,
24 Preferences_Footer,
25 Preferences_Heading,
26 };
27 use Elementor\Core\Base\Document as Elementor_Document;
28 use Elementor\Core\Documents_Manager;
29 use Elementor\Elements_Manager;
30 use Elementor\Widgets_Manager;
31
32 class Module extends Module_Base {
33
34 private Popup_Admin_Filter $popup_admin_filter;
35 private Banner_Settings_Integration $banner_settings_integration;
36
37 public function get_name(): string {
38 return 'Elementor';
39 }
40
41 public static function is_active(): bool {
42 return defined( 'ELEMENTOR_VERSION' ) && defined( 'ELEMENTOR_PRO_VERSION' );
43 }
44
45 public static function routes_list(): array {
46 return [
47 'Create_Template',
48 ];
49 }
50
51 public function register_documents( Documents_Manager $documents_manager ): void {
52 $documents_manager->register_document_type(
53 Cookie_Consent_Document::DOCUMENT_TYPE,
54 Cookie_Consent_Document::class
55 );
56
57 $documents_manager->register_document_type(
58 Preferences_Banner_Document::DOCUMENT_TYPE,
59 Preferences_Banner_Document::class
60 );
61 }
62
63 public function register_widget_categories( Elements_Manager $elements_manager ): void {
64 $elements_manager->add_category(
65 'cookiez',
66 [
67 'title' => esc_html__( 'Cookie Consent', 'cookiez' ),
68 'icon' => 'fa fa-cookie',
69 ]
70 );
71 }
72
73 public function register_widgets( Widgets_Manager $widgets_manager ): void {
74 $widgets_manager->register( new Cookie_Consent_Heading() );
75 $widgets_manager->register( new Cookie_Consent_Content() );
76 $widgets_manager->register( new Cookie_Consent_Footer() );
77 $widgets_manager->register( new Preferences_Heading() );
78 $widgets_manager->register( new Preferences_Content() );
79 $widgets_manager->register( new Preferences_Footer() );
80 }
81
82 public function register_widget_styles(): void {
83 wp_register_style(
84 'cookiez-widget-cookie-consent',
85 COOKIEZ_ASSETS_URL . 'build/elementor-widgets/cookie-consent.css',
86 [],
87 COOKIEZ_VERSION
88 );
89 wp_register_style(
90 'cookiez-widget-preferences-banner',
91 COOKIEZ_ASSETS_URL . 'build/elementor-widgets/preferences-banner.css',
92 [],
93 COOKIEZ_VERSION
94 );
95 }
96
97 public function ensure_cookie_consent_defaults_on_document_save( $document ): void {
98 $document_name = Document_Utils::get_document_name( $document );
99 if ( null === $document_name || ! method_exists( $document, 'get_main_id' ) ) {
100 return;
101 }
102
103 $post_id = (int) $document->get_main_id();
104 if ( 0 >= $post_id ) {
105 return;
106 }
107
108 $defaults_map = Document_Utils::get_document_defaults_map();
109 if ( isset( $defaults_map[ $document_name ] ) ) {
110 $defaults_map[ $document_name ]::set_default_template_data( $post_id );
111 }
112 }
113
114 public function force_popup_editor_config_for_cookiez_documents( array $additional_config, int $post_id ): array {
115 $document_type = get_post_meta( $post_id, Elementor_Document::TYPE_META_KEY, true );
116
117 if ( ! is_string( $document_type ) || ! Document_Utils::is_cookiez_document_type( $document_type ) ) {
118 return $additional_config;
119 }
120
121 $additional_config['container'] = '.elementor-popup-modal .dialog-widget-content';
122
123 return $additional_config;
124 }
125
126 public function force_popup_wrapper_type_for_cookiez_documents( array $attributes, $document ): array {
127 $document_name = Document_Utils::get_document_name( $document );
128 if ( null === $document_name || ! Document_Utils::is_cookiez_document_type( $document_name ) ) {
129 return $attributes;
130 }
131
132 $attributes['data-elementor-type'] = 'popup';
133
134 return $attributes;
135 }
136
137 public function __construct() {
138 $this->register_routes();
139
140 $this->popup_admin_filter = new Popup_Admin_Filter();
141 $this->banner_settings_integration = new Banner_Settings_Integration();
142
143 add_action( 'elementor/documents/register', [ $this, 'register_documents' ], 100 );
144 add_action( 'elementor/document/after_save', [ $this, 'ensure_cookie_consent_defaults_on_document_save' ], 20, 1 );
145 add_filter( 'elementor/document/config', [ $this, 'force_popup_editor_config_for_cookiez_documents' ], 10, 2 );
146 add_filter( 'elementor/document/wrapper_attributes', [ $this, 'force_popup_wrapper_type_for_cookiez_documents' ], 10, 2 );
147
148 add_action( 'elementor/elements/categories_registered', [ $this, 'register_widget_categories' ] );
149 add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] );
150 add_action( 'wp_enqueue_scripts', [ $this, 'register_widget_styles' ] );
151 add_action( 'elementor/editor/after_enqueue_styles', [ $this, 'register_widget_styles' ] );
152
153 add_action( 'parse_query', [ $this->popup_admin_filter, 'restrict_popup_admin_list_to_standard_popups' ], 20 );
154 add_action( 'admin_head', [ $this->popup_admin_filter, 'hide_standard_popup_subtype_tab' ] );
155
156 add_filter( 'cookiez/banner/settings', [ $this->banner_settings_integration, 'inject_elementor_cookie_consent_id' ] );
157 add_filter( 'cookiez/banner/settings', [ $this->banner_settings_integration, 'inject_elementor_preferences_banner_id' ] );
158
159 add_filter( 'cookiez/settings/data', [ $this->banner_settings_integration, 'inject_elementor_cookie_consent_id_for_admin' ] );
160 add_filter( 'cookiez/settings/data', [ $this->banner_settings_integration, 'inject_elementor_preferences_banner_id_for_admin' ] );
161 }
162 }
163