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

279 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 * Init translations.
113 *
114 * @return void
115 */
116 public function init_translations() {
117 $settings = $this->get_module_settings();
118 if ( ! empty( $settings['bar_text'] ) ) {
119 Merchant_Translator::register_string( $settings['bar_text'], esc_html__( 'Cookie banner bar text', 'merchant' ) );
120 }
121 if ( ! empty( $settings['privacy_policy_text'] ) ) {
122 Merchant_Translator::register_string( $settings['privacy_policy_text'], esc_html__( 'Pre orders privacy policy text', 'merchant' ) );
123 }
124 if ( ! empty( $settings['privacy_policy_url'] ) ) {
125 Merchant_Translator::register_string( $settings['privacy_policy_url'], esc_html__( 'Pre orders privacy policy URL', 'merchant' ) );
126 }
127 if ( ! empty( $settings['button_text'] ) ) {
128 Merchant_Translator::register_string( $settings['button_text'], esc_html__( 'Cookie banner button text', 'merchant' ) );
129 }
130 }
131
132 /**
133 * Admin enqueue CSS.
134 *
135 * @return void
136 */
137 public function admin_enqueue_css() {
138 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
139 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
140
141 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
142 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', array(), MERCHANT_VERSION );
143 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
144 }
145 }
146
147 /**
148 * Enqueue CSS.
149 *
150 * @return void
151 */
152 public function enqueue_css() {
153
154 // Specific module styles.
155 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', array(), MERCHANT_VERSION );
156 }
157
158 /**
159 * Enqueue scripts.
160 *
161 * @return void
162 */
163 public function enqueue_scripts() {
164
165 // Register and enqueue the main module script.
166 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/cookie-banner.min.js', array(), MERCHANT_VERSION, true );
167 }
168
169 /**
170 * Render admin preview
171 *
172 * @param Merchant_Admin_Preview $preview
173 * @param string $module
174 *
175 * @return Merchant_Admin_Preview
176 */
177 public function render_admin_preview( $preview, $module ) {
178 if ( self::MODULE_ID === $module ) {
179 ob_start();
180 self::admin_preview_content();
181 $content = ob_get_clean();
182
183 $preview->set_html( $content );
184
185 $preview->set_class( 'theme', '.merchant-cookie-banner', array( 'merchant-cookie-banner-floating', 'merchant-cookie-banner-fixed-bottom' ) );
186 $preview->set_text( 'bar_text', '.merchant-cookie-banner-text' );
187 $preview->set_text( 'button_text', '.merchant-cookie-banner-button' );
188 $preview->set_css( 'background_color', '.merchant-cookie-banner-inner', '--merchant-background' );
189 $preview->set_css( 'text_color', '.merchant-cookie-banner-inner', '--merchant-text-color' );
190 $preview->set_css( 'button_background_color', '.merchant-cookie-banner-button', '--merchant-button-background' );
191 $preview->set_css( 'button_text_color', '.merchant-cookie-banner-button', '--merchant-button-text-color' );
192 $preview->set_css( 'modal_height', '.merchant-cookie-banner-inner', '--merchant-modal-height', 'px' );
193 }
194
195 return $preview;
196 }
197
198 /**
199 * Admin preview content.
200 *
201 * @return void
202 */
203 public function admin_preview_content() {
204 ?>
205
206 <div class="mrc-preview-single-product-elements">
207 <div class="mrc-preview-left-column">
208 <div class="mrc-preview-product-image-wrapper">
209 <div class="mrc-preview-product-image"></div>
210 <div class="mrc-preview-product-image-thumbs">
211 <div class="mrc-preview-product-image-thumb"></div>
212 <div class="mrc-preview-product-image-thumb"></div>
213 <div class="mrc-preview-product-image-thumb"></div>
214 </div>
215 </div>
216 </div>
217 <div class="mrc-preview-right-column">
218 <div class="mrc-preview-text-placeholder"></div>
219 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
220 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
221 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
222 <div class="mrc-preview-addtocart-placeholder"></div>
223 </div>
224 </div>
225
226 <?php $this->cookie_banner(); ?>
227
228 <?php
229 }
230
231 /**
232 * Get cookie banner.
233 *
234 */
235 public function get_cookie_banner() {
236 $settings = $this->get_module_settings();
237
238 ob_start();
239 ?>
240
241 <div class="merchant-cookie-banner <?php echo esc_attr( $settings[ 'theme' ] ); ?>">
242 <div class="merchant-cookie-banner-inner">
243 <?php if ( ! empty( $settings[ 'close_button' ] ) ) : ?>
244 <div class="merchant-cookie-close-button">
245 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
246 <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>
247 </svg>
248 </div>
249 <?php endif; ?>
250 <div class="merchant-cookie-banner-content">
251 <div class="merchant-cookie-banner-text">
252 <?php echo esc_html( Merchant_Translator::translate( $settings[ 'bar_text' ] ) ); ?>
253 <?php if ( ! empty( $settings[ 'privacy_policy_url' ] ) ) : ?>
254 <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>
255 <?php endif; ?>
256 </div>
257 <div class="merchant-cookie-banner-button"><?php echo esc_html( Merchant_Translator::translate( $settings[ 'button_text' ] ) ); ?></div>
258 </div>
259 </div>
260 </div>
261
262 <?php
263 return ob_get_clean();
264 }
265
266 /**
267 * Cookie banner.
268 *
269 */
270 public function cookie_banner() {
271 echo wp_kses( $this->get_cookie_banner(), merchant_kses_allowed_tags() );
272 }
273 }
274
275 // Initialize the module.
276 add_action( 'init', function() {
277 new Merchant_Cookie_Banner();
278 } );
279