PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.8
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.8
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 1.8, at inc/modules/cookie-banner/class-cookie-banner.php

281 lines 9.2 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 * Constructor.
33 */
34 public function __construct() {
35 parent::__construct();
36
37 // Module section.
38 $this->module_section = 'protect-your-store';
39
40 // Module id.
41 $this->module_id = self::MODULE_ID;
42
43 // Module default settings.
44 $this->module_default_settings = array(
45 'theme' => 'merchant-cookie-banner-floating',
46 'bar_text' => esc_html__( '🍪 We\'re using cookies to give you the best experience on our site.', 'merchant' ),
47 'privacy_policy_text' => esc_html__( 'Learn More', 'merchant' ),
48 'privacy_policy_url' => get_privacy_policy_url(),
49 'button_text' => esc_html__( 'I understand', 'merchant' ),
50 'cookie_duration' => '365',
51 'close_button' => 1
52 );
53
54 // Mount preview url.
55 $preview_url = site_url( '/' );
56
57 // Module data.
58 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
59 $this->module_data[ 'preview_url' ] = $preview_url;
60
61 // Mount preview url.
62 $preview_url = site_url( '/' );
63
64 if ( function_exists( 'wc_get_page_id' ) ) {
65 $preview_url = get_permalink( wc_get_page_id( 'shop' ) );
66 }
67
68 // Module data.
69 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
70 $this->module_data[ 'preview_url' ] = $preview_url;
71
72 // Module options path.
73 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
74
75 // Is module preview page.
76 if ( is_admin() && parent::is_module_settings_page() ) {
77 self::$is_module_preview = true;
78
79 // Enqueue admin styles.
80 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
81
82 // Admin preview box.
83 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
84
85 }
86
87 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
88 // Init translations.
89 $this->init_translations();
90 }
91
92 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
93 return;
94 }
95
96 // Return early if it's on admin but not in the respective module settings page.
97 if ( is_admin() && ! parent::is_module_settings_page() ) {
98 return;
99 }
100
101 // Enqueue styles.
102 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
103
104 // Enqueue scripts.
105 add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) );
106
107 // Render cookie banner on footer.
108 add_action( 'wp_footer', array( $this, 'cookie_banner' ) );
109
110 }
111
112 /**
113 * Init translations.
114 *
115 * @return void
116 */
117 public function init_translations() {
118 $settings = $this->get_module_settings();
119 if ( ! empty( $settings['bar_text'] ) ) {
120 Merchant_Translator::register_string( $settings['bar_text'], esc_html__( 'Cookie banner bar text', 'merchant' ) );
121 }
122 if ( ! empty( $settings['privacy_policy_text'] ) ) {
123 Merchant_Translator::register_string( $settings['privacy_policy_text'], esc_html__( 'Pre orders privacy policy text', 'merchant' ) );
124 }
125 if ( ! empty( $settings['privacy_policy_url'] ) ) {
126 Merchant_Translator::register_string( $settings['privacy_policy_url'], esc_html__( 'Pre orders privacy policy URL', 'merchant' ) );
127 }
128 if ( ! empty( $settings['button_text'] ) ) {
129 Merchant_Translator::register_string( $settings['button_text'], esc_html__( 'Cookie banner button text', 'merchant' ) );
130 }
131 }
132
133 /**
134 * Admin enqueue CSS.
135 *
136 * @return void
137 */
138 public function admin_enqueue_css() {
139 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
140 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
141
142 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
143 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', [], MERCHANT_VERSION );
144 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
145 }
146 }
147
148 /**
149 * Enqueue CSS.
150 *
151 * @return void
152 */
153 public function enqueue_css() {
154
155 // Specific module styles.
156 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', array(), MERCHANT_VERSION );
157 }
158
159 /**
160 * Enqueue scripts.
161 *
162 * @return void
163 */
164 public function enqueue_scripts() {
165
166 // Register and enqueue the main module script.
167 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/cookie-banner.min.js', array(), MERCHANT_VERSION, true );
168 }
169
170 /**
171 * Render admin preview
172 *
173 * @param Merchant_Admin_Preview $preview
174 * @param string $module
175 *
176 * @return Merchant_Admin_Preview
177 */
178 public function render_admin_preview( $preview, $module ) {
179 if ( self::MODULE_ID === $module ) {
180 ob_start();
181 self::admin_preview_content();
182 $content = ob_get_clean();
183
184 $preview->set_html( $content );
185
186 $preview->set_class( 'theme', '.merchant-cookie-banner', array( 'merchant-cookie-banner-floating', 'merchant-cookie-banner-fixed-bottom' ) );
187 $preview->set_text( 'bar_text', '.merchant-cookie-banner-text' );
188 $preview->set_text( 'button_text', '.merchant-cookie-banner-button' );
189 $preview->set_css( 'background_color', '.merchant-cookie-banner-inner', '--merchant-background' );
190 $preview->set_css( 'text_color', '.merchant-cookie-banner-inner', '--merchant-text-color' );
191 $preview->set_css( 'button_background_color', '.merchant-cookie-banner-button', '--merchant-button-background' );
192 $preview->set_css( 'button_text_color', '.merchant-cookie-banner-button', '--merchant-button-text-color' );
193 $preview->set_css( 'modal_height', '.merchant-cookie-banner-inner', '--merchant-modal-height', 'px' );
194 }
195
196 return $preview;
197 }
198
199 /**
200 * Admin preview content.
201 *
202 * @return void
203 */
204 public function admin_preview_content() {
205 ?>
206
207 <div class="mrc-preview-single-product-elements">
208 <div class="mrc-preview-left-column">
209 <div class="mrc-preview-product-image-wrapper">
210 <div class="mrc-preview-product-image"></div>
211 <div class="mrc-preview-product-image-thumbs">
212 <div class="mrc-preview-product-image-thumb"></div>
213 <div class="mrc-preview-product-image-thumb"></div>
214 <div class="mrc-preview-product-image-thumb"></div>
215 </div>
216 </div>
217 </div>
218 <div class="mrc-preview-right-column">
219 <div class="mrc-preview-text-placeholder"></div>
220 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
221 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
222 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
223 <div class="mrc-preview-addtocart-placeholder"></div>
224 </div>
225 </div>
226
227 <?php $this->cookie_banner(); ?>
228
229 <?php
230 }
231
232 /**
233 * Get cookie banner.
234 *
235 */
236 public function get_cookie_banner() {
237 $settings = $this->get_module_settings();
238
239 ob_start();
240 ?>
241
242 <div class="merchant-cookie-banner <?php echo esc_attr( $settings[ 'theme' ] ); ?>">
243 <div class="merchant-cookie-banner-inner">
244 <?php if ( ! empty( $settings[ 'close_button' ] ) ) : ?>
245 <div class="merchant-cookie-close-button">
246 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
247 <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>
248 </svg>
249 </div>
250 <?php endif; ?>
251 <div class="merchant-cookie-banner-content">
252 <div class="merchant-cookie-banner-text">
253 <?php echo esc_html( Merchant_Translator::translate( $settings[ 'bar_text' ] ) ); ?>
254 <?php if ( ! empty( $settings[ 'privacy_policy_url' ] ) ) : ?>
255 <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>
256 <?php endif; ?>
257 </div>
258 <div class="merchant-cookie-banner-button"><?php echo esc_html( Merchant_Translator::translate( $settings[ 'button_text' ] ) ); ?></div>
259 </div>
260 </div>
261 </div>
262
263 <?php
264 return ob_get_clean();
265 }
266
267 /**
268 * Cookie banner.
269 *
270 */
271 public function cookie_banner() {
272 echo wp_kses( $this->get_cookie_banner(), merchant_kses_allowed_tags() );
273 }
274
275 }
276
277 // Initialize the module.
278 add_action( 'init', function() {
279 new Merchant_Cookie_Banner();
280 } );
281