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

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