PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.0
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.0
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / modules / cookie-banner / class-cookie-banner.php

class-cookie-banner.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.0, at inc/modules/cookie-banner/class-cookie-banner.php

288 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Cookie Banner.
5 *
6 * @package Merchat_Pro
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Cookie Banner class.
15 *
16 */
17 class Merchant_Cookie_Banner extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 *
22 */
23 const MODULE_ID = 'cookie-banner';
24
25 /**
26 * Is module preview.
27 *
28 */
29 public static $is_module_preview = false;
30
31 /**
32 * Initialize the module's option group definitions.
33 *
34 * @return array<int, array<string, mixed>> Array of option group arrays.
35 */
36 protected function init_option_groups() {
37 return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
38 }
39
40 /**
41 * Constructor.
42 */
43 public function __construct() {
44 parent::__construct();
45
46 // Module section.
47 $this->module_section = 'protect-your-store';
48
49 // Module id.
50 $this->module_id = self::MODULE_ID;
51
52 if ( ! is_admin() && ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
53 return;
54 }
55
56 // Module default settings.
57 $this->module_default_settings = array(
58 'theme' => 'merchant-cookie-banner-floating',
59 'bar_text' => esc_html__( '🍪 We\'re using cookies to give you the best experience on our site.', 'merchant' ),
60 'privacy_policy_text' => esc_html__( 'Learn More', 'merchant' ),
61 'privacy_policy_url' => get_privacy_policy_url(),
62 'button_text' => esc_html__( 'I understand', 'merchant' ),
63 'cookie_duration' => '365',
64 'close_button' => 1,
65 );
66
67 // Module data.
68 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
69
70 // AI prompt examples.
71 $this->ai_examples = array(
72 'blurb' => esc_html__( 'See what AI can do for your cookie banner, from rewording its message to switching its placement and linking it to your privacy policy.', 'merchant' ),
73 'prompts' => array(
74 esc_html__( 'Explore the abilities WPVibe gives you access to, then reword the cookie banner message to explain that we only use cookies to improve the shopping experience', 'merchant' ),
75 esc_html__( 'Check what abilities you have available through WPVibe, then move the cookie banner to the fixed bottom position instead of floating', 'merchant' ),
76 esc_html__( "Look at your available WPVibe abilities, then point the cookie banner's 'Learn More' link at our privacy policy page", 'merchant' ),
77 esc_html__( "See what abilities WPVibe exposes, then change the cookie banner's accept button to say 'Got it' and remember the choice for 30 days", 'merchant' ),
78 ),
79 );
80
81 // Module options path.
82 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
83
84 // Is module preview page.
85 if ( is_admin() && parent::is_module_settings_page() ) {
86 self::$is_module_preview = true;
87
88 // Enqueue admin styles.
89 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
90
91 // Admin preview box.
92 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
93
94 }
95
96 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
97 // Init translations.
98 $this->init_translations();
99 }
100
101 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
102 return;
103 }
104
105 // Return early if it's on admin but not in the respective module settings page.
106 if ( is_admin() && ! parent::is_module_settings_page() ) {
107 return;
108 }
109
110 // Enqueue styles.
111 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
112
113 // Enqueue scripts.
114 add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) );
115
116 // Render cookie banner on footer.
117 add_action( 'wp_footer', array( $this, 'cookie_banner' ) );
118 }
119
120 /**
121 * Init translations.
122 *
123 * @return void
124 */
125 public function init_translations() {
126 $settings = $this->get_module_settings();
127 if ( ! empty( $settings['bar_text'] ) ) {
128 Merchant_Translator::register_string( $settings['bar_text'], esc_html__( 'Cookie banner bar text', 'merchant' ) );
129 }
130 if ( ! empty( $settings['privacy_policy_text'] ) ) {
131 Merchant_Translator::register_string( $settings['privacy_policy_text'], esc_html__( 'Pre orders privacy policy text', 'merchant' ) );
132 }
133 if ( ! empty( $settings['privacy_policy_url'] ) ) {
134 Merchant_Translator::register_string( $settings['privacy_policy_url'], esc_html__( 'Pre orders privacy policy URL', 'merchant' ) );
135 }
136 if ( ! empty( $settings['button_text'] ) ) {
137 Merchant_Translator::register_string( $settings['button_text'], esc_html__( 'Cookie banner button text', 'merchant' ) );
138 }
139 }
140
141 /**
142 * Admin enqueue CSS.
143 *
144 * @return void
145 */
146 public function admin_enqueue_css() {
147 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
148 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
149
150 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
151 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', array(), MERCHANT_VERSION );
152 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
153 }
154 }
155
156 /**
157 * Enqueue CSS.
158 *
159 * @return void
160 */
161 public function enqueue_css() {
162
163 // Specific module styles.
164 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', array(), MERCHANT_VERSION );
165 }
166
167 /**
168 * Enqueue scripts.
169 *
170 * @return void
171 */
172 public function enqueue_scripts() {
173
174 // Register and enqueue the main module script.
175 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/cookie-banner.min.js', array(), MERCHANT_VERSION, true );
176 }
177
178 /**
179 * Render admin preview
180 *
181 * @param Merchant_Admin_Preview $preview
182 * @param string $module
183 *
184 * @return Merchant_Admin_Preview
185 */
186 public function render_admin_preview( $preview, $module ) {
187 if ( self::MODULE_ID === $module ) {
188 ob_start();
189 self::admin_preview_content();
190 $content = ob_get_clean();
191
192 $preview->set_html( $content );
193
194 $preview->set_class( 'theme', '.merchant-cookie-banner', array( 'merchant-cookie-banner-floating', 'merchant-cookie-banner-fixed-bottom' ) );
195 $preview->set_text( 'bar_text', '.merchant-cookie-banner-text' );
196 $preview->set_text( 'button_text', '.merchant-cookie-banner-button' );
197 $preview->set_css( 'background_color', '.merchant-cookie-banner-inner', '--merchant-background' );
198 $preview->set_css( 'text_color', '.merchant-cookie-banner-inner', '--merchant-text-color' );
199 $preview->set_css( 'button_background_color', '.merchant-cookie-banner-button', '--merchant-button-background' );
200 $preview->set_css( 'button_text_color', '.merchant-cookie-banner-button', '--merchant-button-text-color' );
201 $preview->set_css( 'modal_height', '.merchant-cookie-banner-inner', '--merchant-modal-height', 'px' );
202 }
203
204 return $preview;
205 }
206
207 /**
208 * Admin preview content.
209 *
210 * @return void
211 */
212 public function admin_preview_content() {
213 ?>
214
215 <div class="mrc-preview-single-product-elements">
216 <div class="mrc-preview-left-column">
217 <div class="mrc-preview-product-image-wrapper">
218 <div class="mrc-preview-product-image"></div>
219 <div class="mrc-preview-product-image-thumbs">
220 <div class="mrc-preview-product-image-thumb"></div>
221 <div class="mrc-preview-product-image-thumb"></div>
222 <div class="mrc-preview-product-image-thumb"></div>
223 </div>
224 </div>
225 </div>
226 <div class="mrc-preview-right-column">
227 <div class="mrc-preview-text-placeholder"></div>
228 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
229 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
230 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
231 <div class="mrc-preview-addtocart-placeholder"></div>
232 </div>
233 </div>
234
235 <?php $this->cookie_banner(); ?>
236
237 <?php
238 }
239
240 /**
241 * Get cookie banner.
242 *
243 */
244 public function get_cookie_banner() {
245 $settings = $this->get_module_settings();
246
247 ob_start();
248 ?>
249
250 <div class="merchant-cookie-banner <?php echo esc_attr( $settings[ 'theme' ] ); ?>">
251 <div class="merchant-cookie-banner-inner">
252 <?php if ( ! empty( $settings[ 'close_button' ] ) ) : ?>
253 <div class="merchant-cookie-close-button">
254 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
255 <path d="M193.94 256L296.5 153.44l21.15-21.15c3.12-3.12 3.12-8.19 0-11.31l-22.63-22.63c-3.12-3.12-8.19-3.12-11.31 0L160 222.06 36.29 98.34c-3.12-3.12-8.19-3.12-11.31 0L2.34 120.97c-3.12 3.12-3.12 8.19 0 11.31L126.06 256 2.34 379.71c-3.12 3.12-3.12 8.19 0 11.31l22.63 22.63c3.12 3.12 8.19 3.12 11.31 0L160 289.94 262.56 392.5l21.15 21.15c3.12 3.12 8.19 3.12 11.31 0l22.63-22.63c3.12-3.12 3.12-8.19 0-11.31L193.94 256z"></path>
256 </svg>
257 </div>
258 <?php endif; ?>
259 <div class="merchant-cookie-banner-content">
260 <div class="merchant-cookie-banner-text">
261 <?php echo esc_html( Merchant_Translator::translate( $settings[ 'bar_text' ] ) ); ?>
262 <?php if ( ! empty( $settings[ 'privacy_policy_url' ] ) ) : ?>
263 <a href="<?php echo esc_url( Merchant_Translator::translate( $settings[ 'privacy_policy_url' ] ) ); ?>"><?php echo esc_html( Merchant_Translator::translate( $settings[ 'privacy_policy_text' ] ) ); ?></a>
264 <?php endif; ?>
265 </div>
266 <div class="merchant-cookie-banner-button"><?php echo esc_html( Merchant_Translator::translate( $settings[ 'button_text' ] ) ); ?></div>
267 </div>
268 </div>
269 </div>
270
271 <?php
272 return ob_get_clean();
273 }
274
275 /**
276 * Cookie banner.
277 *
278 */
279 public function cookie_banner() {
280 echo wp_kses( $this->get_cookie_banner(), merchant_kses_allowed_tags() );
281 }
282 }
283
284 // Initialize the module.
285 add_action( 'init', function() {
286 Merchant_Modules::create_module( new Merchant_Cookie_Banner() );
287 } );
288