| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Scroll To Top Button. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Scroll To Top Button Class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Scroll_To_Top_Button extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
* |
| 22 |
*/ |
| 23 |
const MODULE_ID = 'scroll-to-top-button'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Is module preview. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public static $is_module_preview = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
parent::__construct(); |
| 37 |
|
| 38 |
// Module section. |
| 39 |
$this->module_section = 'improve-experience'; |
| 40 |
|
| 41 |
// Module id. |
| 42 |
$this->module_id = self::MODULE_ID; |
| 43 |
|
| 44 |
// Module default settings. |
| 45 |
$this->module_default_settings = array( |
| 46 |
'style' => 'merchant-style-filled', |
| 47 |
'type' => 'icon', |
| 48 |
'icon' => 'arrow-1', |
| 49 |
'text' => esc_html__( 'Back to top', 'merchant' ), |
| 50 |
'position' => 'merchant-position-right', |
| 51 |
'visibility' => 'all', |
| 52 |
); |
| 53 |
|
| 54 |
// Module data. |
| 55 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 56 |
|
| 57 |
// Module options path. |
| 58 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 59 |
|
| 60 |
// Is module preview page. |
| 61 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 62 |
self::$is_module_preview = true; |
| 63 |
|
| 64 |
// Enqueue admin styles. |
| 65 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 66 |
|
| 67 |
// Admin preview box. |
| 68 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 69 |
|
| 70 |
// Custom CSS. |
| 71 |
// The custom CSS should be added here as well due to ensure preview box works properly. |
| 72 |
add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Return early if it's on admin but not in the respective module settings page. |
| 81 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Enqueue styles. |
| 86 |
add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) ); |
| 87 |
|
| 88 |
// Enqueue scripts. |
| 89 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 90 |
|
| 91 |
// Render the scroll to top button on footer. |
| 92 |
add_action( 'wp_footer', array( $this, 'get_scroll_to_top_button' ) ); |
| 93 |
|
| 94 |
// Custom CSS. |
| 95 |
add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Admin enqueue CSS. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function admin_enqueue_css() { |
| 104 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 105 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 106 |
|
| 107 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 108 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.css', array(), MERCHANT_VERSION ); |
| 109 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Enqueue CSS. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function enqueue_css() { |
| 119 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.css', array(), MERCHANT_VERSION ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Enqueue scripts. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function enqueue_scripts() { |
| 128 |
wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.js', array(), MERCHANT_VERSION, true ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Render admin preview |
| 133 |
* |
| 134 |
* @param Merchant_Admin_Preview $preview |
| 135 |
* @param string $module |
| 136 |
* |
| 137 |
* @return Merchant_Admin_Preview |
| 138 |
*/ |
| 139 |
public function render_admin_preview( $preview, $module ) { |
| 140 |
if ( self::MODULE_ID === $module ) { |
| 141 |
ob_start(); |
| 142 |
self::admin_preview_content(); |
| 143 |
$content = ob_get_clean(); |
| 144 |
|
| 145 |
// HTML. |
| 146 |
$preview->set_html( $content ); |
| 147 |
|
| 148 |
$preview->set_class( 'position', '.merchant-scroll-to-top-button', array( 'merchant-position-right', 'merchant-position-left' ) ); |
| 149 |
$preview->set_class( 'style', '.merchant-scroll-to-top-button', array( 'merchant-style-filled', 'merchant-style-outline' ) ); |
| 150 |
|
| 151 |
$preview->set_css( 'side-offset', '.merchant-scroll-to-top-button', '--merchant-side-offset', 'px' ); |
| 152 |
$preview->set_css( 'bottom-offset', '.merchant-scroll-to-top-button', '--merchant-bottom-offset', 'px' ); |
| 153 |
|
| 154 |
$preview->set_css( 'icon-size', '.merchant-scroll-to-top-button', '--merchant-icon-size', 'px' ); |
| 155 |
$preview->set_css( 'padding', '.merchant-scroll-to-top-button', '--merchant-padding' , 'px'); |
| 156 |
$preview->set_css( 'border-radius', '.merchant-scroll-to-top-button', '--merchant-border-radius', 'px' ); |
| 157 |
|
| 158 |
$preview->set_css( 'icon-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-color' ); |
| 159 |
$preview->set_css( 'icon-hover-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-hover-color' ); |
| 160 |
$preview->set_css( 'background-color', '.merchant-scroll-to-top-button', '--merchant-background-color' ); |
| 161 |
$preview->set_css( 'background-hover-color', '.merchant-scroll-to-top-button', '--merchant-background-hover-color' ); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
return $preview; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Admin preview content. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function admin_preview_content() { |
| 174 |
?> |
| 175 |
|
| 176 |
<div class="mrc-preview-single-product-elements"> |
| 177 |
<div class="mrc-preview-left-column"> |
| 178 |
<div class="mrc-preview-product-image-wrapper"> |
| 179 |
<div class="mrc-preview-product-image"></div> |
| 180 |
<div class="mrc-preview-product-image-thumbs"> |
| 181 |
<div class="mrc-preview-product-image-thumb"></div> |
| 182 |
<div class="mrc-preview-product-image-thumb"></div> |
| 183 |
<div class="mrc-preview-product-image-thumb"></div> |
| 184 |
</div> |
| 185 |
</div> |
| 186 |
</div> |
| 187 |
<div class="mrc-preview-right-column"> |
| 188 |
<div class="mrc-preview-text-placeholder"></div> |
| 189 |
<div class="mrc-preview-text-placeholder mrc-mw-70"></div> |
| 190 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 191 |
<div class="mrc-preview-text-placeholder mrc-mw-40"></div> |
| 192 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 193 |
<div class="mrc-preview-addtocart-placeholder"></div> |
| 194 |
</div> |
| 195 |
</div> |
| 196 |
|
| 197 |
<?php $this->get_scroll_to_top_button(); ?> |
| 198 |
|
| 199 |
<?php |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get scroll to top button. |
| 204 |
* |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
public function get_scroll_to_top_button() { |
| 208 |
$settings = $this->get_module_settings(); |
| 209 |
|
| 210 |
$html = '<div class="merchant-scroll-to-top-button ' . esc_attr( $settings[ 'position' ] ) . ' ' . esc_attr( $settings[ 'style' ] ) . ' merchant-visibility-' . esc_attr( $settings[ 'visibility' ] ) . '">'; |
| 211 |
|
| 212 |
if ( 'text-icon' === $settings[ 'type' ] ) { |
| 213 |
$html .= '<span>' . esc_html( $settings[ 'text' ] ) . '</span>'; |
| 214 |
} |
| 215 |
|
| 216 |
switch ( $settings[ 'icon' ] ) { |
| 217 |
case 'arrow-1': |
| 218 |
$html .= '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 15L12 8L19 15" stroke-width="1.5" stroke-linejoin="round"></path></svg>'; |
| 219 |
break; |
| 220 |
|
| 221 |
case 'arrow-2': |
| 222 |
$html .= '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 15l7-7 7 7" stroke-width="3" stroke-linejoin="round"></path></svg>'; |
| 223 |
break; |
| 224 |
|
| 225 |
case 'arrow-3': |
| 226 |
$html .= '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7 12l5.5-5.5m0 0L18 12m-5.5-5.5V19" stroke-width="1.5" stroke-linejoin="round"></path></svg>'; |
| 227 |
break; |
| 228 |
|
| 229 |
case 'arrow-4': |
| 230 |
$html .= '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7 12l5.5-5.5m0 0L18 12m-5.5-5.5V19" stroke-width="3" stroke-linejoin="round"></path></svg>'; |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
$html .= '</div>'; |
| 235 |
|
| 236 |
echo wp_kses( $html, merchant_kses_allowed_tags() ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Custom CSS. |
| 241 |
* |
| 242 |
* @return string |
| 243 |
*/ |
| 244 |
public function get_module_custom_css() { |
| 245 |
$css = ''; |
| 246 |
|
| 247 |
|
| 248 |
return $css; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Admin custom CSS. |
| 253 |
* |
| 254 |
* @param string $css The custom CSS. |
| 255 |
* @return string $css The custom CSS. |
| 256 |
*/ |
| 257 |
public function admin_custom_css( $css ) { |
| 258 |
$css .= $this->get_module_custom_css(); |
| 259 |
|
| 260 |
return $css; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Frontend custom CSS. |
| 265 |
* |
| 266 |
* @param string $css The custom CSS. |
| 267 |
* @return string $css The custom CSS. |
| 268 |
*/ |
| 269 |
public function frontend_custom_css( $css ) { |
| 270 |
$css .= $this->get_module_custom_css(); |
| 271 |
|
| 272 |
return $css; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// Initialize the module. |
| 277 |
add_action( 'init', function() { |
| 278 |
Merchant_Modules::create_module( new Merchant_Scroll_To_Top_Button() ); |
| 279 |
} ); |
| 280 |
|