| 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 ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Return early if it's on admin but not in the respective module settings page. |
| 92 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// Enqueue styles. |
| 97 |
add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) ); |
| 98 |
|
| 99 |
// Enqueue scripts. |
| 100 |
add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) ); |
| 101 |
|
| 102 |
// Render cookie banner on footer. |
| 103 |
add_action( 'wp_footer', array( $this, 'cookie_banner' ) ); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Admin enqueue CSS. |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public function admin_enqueue_css() { |
| 113 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 114 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 115 |
|
| 116 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 117 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', [], MERCHANT_VERSION ); |
| 118 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Enqueue CSS. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function enqueue_css() { |
| 128 |
|
| 129 |
// Specific module styles. |
| 130 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/cookie-banner.min.css', array(), MERCHANT_VERSION ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Enqueue scripts. |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function enqueue_scripts() { |
| 139 |
|
| 140 |
// Register and enqueue the main module script. |
| 141 |
wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/cookie-banner.min.js', array(), MERCHANT_VERSION, true ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Render admin preview |
| 146 |
* |
| 147 |
* @param Merchant_Admin_Preview $preview |
| 148 |
* @param string $module |
| 149 |
* |
| 150 |
* @return Merchant_Admin_Preview |
| 151 |
*/ |
| 152 |
public function render_admin_preview( $preview, $module ) { |
| 153 |
if ( self::MODULE_ID === $module ) { |
| 154 |
ob_start(); |
| 155 |
self::admin_preview_content(); |
| 156 |
$content = ob_get_clean(); |
| 157 |
|
| 158 |
$preview->set_html( $content ); |
| 159 |
|
| 160 |
$preview->set_class( 'theme', '.merchant-cookie-banner', array( 'merchant-cookie-banner-floating', 'merchant-cookie-banner-fixed-bottom' ) ); |
| 161 |
$preview->set_text( 'bar_text', '.merchant-cookie-banner-text' ); |
| 162 |
$preview->set_text( 'button_text', '.merchant-cookie-banner-button' ); |
| 163 |
$preview->set_css( 'background_color', '.merchant-cookie-banner-inner', '--merchant-background' ); |
| 164 |
$preview->set_css( 'text_color', '.merchant-cookie-banner-inner', '--merchant-text-color' ); |
| 165 |
$preview->set_css( 'button_background_color', '.merchant-cookie-banner-button', '--merchant-button-background' ); |
| 166 |
$preview->set_css( 'button_text_color', '.merchant-cookie-banner-button', '--merchant-button-text-color' ); |
| 167 |
$preview->set_css( 'modal_height', '.merchant-cookie-banner-inner', '--merchant-modal-height', 'px' ); |
| 168 |
} |
| 169 |
|
| 170 |
return $preview; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Admin preview content. |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function admin_preview_content() { |
| 179 |
?> |
| 180 |
|
| 181 |
<div class="mrc-preview-single-product-elements"> |
| 182 |
<div class="mrc-preview-left-column"> |
| 183 |
<div class="mrc-preview-product-image-wrapper"> |
| 184 |
<div class="mrc-preview-product-image"></div> |
| 185 |
<div class="mrc-preview-product-image-thumbs"> |
| 186 |
<div class="mrc-preview-product-image-thumb"></div> |
| 187 |
<div class="mrc-preview-product-image-thumb"></div> |
| 188 |
<div class="mrc-preview-product-image-thumb"></div> |
| 189 |
</div> |
| 190 |
</div> |
| 191 |
</div> |
| 192 |
<div class="mrc-preview-right-column"> |
| 193 |
<div class="mrc-preview-text-placeholder"></div> |
| 194 |
<div class="mrc-preview-text-placeholder mrc-mw-70"></div> |
| 195 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 196 |
<div class="mrc-preview-text-placeholder mrc-mw-40"></div> |
| 197 |
<div class="mrc-preview-addtocart-placeholder"></div> |
| 198 |
</div> |
| 199 |
</div> |
| 200 |
|
| 201 |
<?php $this->cookie_banner(); ?> |
| 202 |
|
| 203 |
<?php |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get cookie banner. |
| 208 |
* |
| 209 |
*/ |
| 210 |
public function get_cookie_banner() { |
| 211 |
$settings = $this->get_module_settings(); |
| 212 |
|
| 213 |
ob_start(); |
| 214 |
?> |
| 215 |
|
| 216 |
<div class="merchant-cookie-banner <?php echo esc_attr( $settings[ 'theme' ] ); ?>"> |
| 217 |
<div class="merchant-cookie-banner-inner"> |
| 218 |
<?php if ( ! empty( $settings[ 'close_button' ] ) ) : ?> |
| 219 |
<div class="merchant-cookie-close-button"> |
| 220 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"> |
| 221 |
<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> |
| 222 |
</svg> |
| 223 |
</div> |
| 224 |
<?php endif; ?> |
| 225 |
<div class="merchant-cookie-banner-content"> |
| 226 |
<div class="merchant-cookie-banner-text"> |
| 227 |
<?php echo esc_html( $settings[ 'bar_text' ] ); ?> |
| 228 |
<?php if ( ! empty( $settings[ 'privacy_policy_url' ] ) ) : ?> |
| 229 |
<a href="<?php echo esc_url( $settings[ 'privacy_policy_url' ] ); ?>"><?php echo esc_html( $settings[ 'privacy_policy_text' ] ); ?></a> |
| 230 |
<?php endif; ?> |
| 231 |
</div> |
| 232 |
<div class="merchant-cookie-banner-button"><?php echo esc_html( $settings[ 'button_text' ] ); ?></div> |
| 233 |
</div> |
| 234 |
</div> |
| 235 |
</div> |
| 236 |
|
| 237 |
<?php |
| 238 |
return ob_get_clean(); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Cookie banner. |
| 243 |
* |
| 244 |
*/ |
| 245 |
public function cookie_banner() { |
| 246 |
echo wp_kses( $this->get_cookie_banner(), merchant_kses_allowed_tags() ); |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
// Initialize the module. |
| 252 |
add_action( 'init', function() { |
| 253 |
new Merchant_Cookie_Banner(); |
| 254 |
} ); |
| 255 |
|