| 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 |
// Mount preview url. |
| 55 |
$preview_url = site_url( '/' ); |
| 56 |
|
| 57 |
if ( function_exists( 'wc_get_page_id' ) ) { |
| 58 |
$preview_url = get_permalink( wc_get_page_id( 'shop' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
// Module data. |
| 62 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 63 |
$this->module_data[ 'preview_url' ] = $preview_url; |
| 64 |
|
| 65 |
// Module options path. |
| 66 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 67 |
|
| 68 |
// Is module preview page. |
| 69 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 70 |
self::$is_module_preview = true; |
| 71 |
|
| 72 |
// Enqueue admin styles. |
| 73 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 74 |
|
| 75 |
// Admin preview box. |
| 76 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 77 |
|
| 78 |
// Custom CSS. |
| 79 |
// The custom CSS should be added here as well due to ensure preview box works properly. |
| 80 |
add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) ); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// Return early if it's on admin but not in the respective module settings page. |
| 89 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Enqueue styles. |
| 94 |
add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) ); |
| 95 |
|
| 96 |
// Enqueue scripts. |
| 97 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 98 |
|
| 99 |
// Render the scroll to top button on footer. |
| 100 |
add_action( 'wp_footer', array( $this, 'get_scroll_to_top_button' ) ); |
| 101 |
|
| 102 |
// Custom CSS. |
| 103 |
add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) ); |
| 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 . '/scroll-to-top-button.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 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.css', array(), MERCHANT_VERSION ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Enqueue scripts. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function enqueue_scripts() { |
| 137 |
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 ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Render admin preview |
| 142 |
* |
| 143 |
* @param Merchant_Admin_Preview $preview |
| 144 |
* @param string $module |
| 145 |
* |
| 146 |
* @return Merchant_Admin_Preview |
| 147 |
*/ |
| 148 |
public function render_admin_preview( $preview, $module ) { |
| 149 |
if ( self::MODULE_ID === $module ) { |
| 150 |
ob_start(); |
| 151 |
self::admin_preview_content(); |
| 152 |
$content = ob_get_clean(); |
| 153 |
|
| 154 |
// HTML. |
| 155 |
$preview->set_html( $content ); |
| 156 |
|
| 157 |
$preview->set_class( 'position', '.merchant-scroll-to-top-button', array( 'merchant-position-right', 'merchant-position-left' ) ); |
| 158 |
$preview->set_class( 'style', '.merchant-scroll-to-top-button', array( 'merchant-style-filled', 'merchant-style-outline' ) ); |
| 159 |
|
| 160 |
$preview->set_css( 'side-offset', '.merchant-scroll-to-top-button', '--merchant-side-offset', 'px' ); |
| 161 |
$preview->set_css( 'bottom-offset', '.merchant-scroll-to-top-button', '--merchant-bottom-offset', 'px' ); |
| 162 |
|
| 163 |
$preview->set_css( 'icon-size', '.merchant-scroll-to-top-button', '--merchant-icon-size', 'px' ); |
| 164 |
$preview->set_css( 'padding', '.merchant-scroll-to-top-button', '--merchant-padding' , 'px'); |
| 165 |
$preview->set_css( 'border-radius', '.merchant-scroll-to-top-button', '--merchant-border-radius', 'px' ); |
| 166 |
|
| 167 |
$preview->set_css( 'icon-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-color' ); |
| 168 |
$preview->set_css( 'icon-hover-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-hover-color' ); |
| 169 |
$preview->set_css( 'background-color', '.merchant-scroll-to-top-button', '--merchant-background-color' ); |
| 170 |
$preview->set_css( 'background-hover-color', '.merchant-scroll-to-top-button', '--merchant-background-hover-color' ); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
return $preview; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Admin preview content. |
| 179 |
* |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public function admin_preview_content() { |
| 183 |
?> |
| 184 |
|
| 185 |
<div class="mrc-preview-single-product-elements"> |
| 186 |
<div class="mrc-preview-left-column"> |
| 187 |
<div class="mrc-preview-product-image-wrapper"> |
| 188 |
<div class="mrc-preview-product-image"></div> |
| 189 |
<div class="mrc-preview-product-image-thumbs"> |
| 190 |
<div class="mrc-preview-product-image-thumb"></div> |
| 191 |
<div class="mrc-preview-product-image-thumb"></div> |
| 192 |
<div class="mrc-preview-product-image-thumb"></div> |
| 193 |
</div> |
| 194 |
</div> |
| 195 |
</div> |
| 196 |
<div class="mrc-preview-right-column"> |
| 197 |
<div class="mrc-preview-text-placeholder"></div> |
| 198 |
<div class="mrc-preview-text-placeholder mrc-mw-70"></div> |
| 199 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 200 |
<div class="mrc-preview-text-placeholder mrc-mw-40"></div> |
| 201 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 202 |
<div class="mrc-preview-addtocart-placeholder"></div> |
| 203 |
</div> |
| 204 |
</div> |
| 205 |
|
| 206 |
<?php $this->get_scroll_to_top_button(); ?> |
| 207 |
|
| 208 |
<?php |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get scroll to top button. |
| 213 |
* |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
public function get_scroll_to_top_button() { |
| 217 |
$settings = $this->get_module_settings(); |
| 218 |
|
| 219 |
$html = '<div class="merchant-scroll-to-top-button ' . esc_attr( $settings[ 'position' ] ) . ' ' . esc_attr( $settings[ 'style' ] ) . ' merchant-visibility-' . esc_attr( $settings[ 'visibility' ] ) . '">'; |
| 220 |
|
| 221 |
if ( 'text-icon' === $settings[ 'type' ] ) { |
| 222 |
$html .= '<span>' . esc_html( $settings[ 'text' ] ) . '</span>'; |
| 223 |
} |
| 224 |
|
| 225 |
switch ( $settings[ 'icon' ] ) { |
| 226 |
case 'arrow-1': |
| 227 |
$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>'; |
| 228 |
break; |
| 229 |
|
| 230 |
case 'arrow-2': |
| 231 |
$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>'; |
| 232 |
break; |
| 233 |
|
| 234 |
case 'arrow-3': |
| 235 |
$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>'; |
| 236 |
break; |
| 237 |
|
| 238 |
case 'arrow-4': |
| 239 |
$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>'; |
| 240 |
break; |
| 241 |
} |
| 242 |
|
| 243 |
$html .= '</div>'; |
| 244 |
|
| 245 |
echo wp_kses( $html, merchant_kses_allowed_tags() ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Custom CSS. |
| 250 |
* |
| 251 |
* @return string |
| 252 |
*/ |
| 253 |
public function get_module_custom_css() { |
| 254 |
$css = ''; |
| 255 |
|
| 256 |
|
| 257 |
return $css; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Admin custom CSS. |
| 262 |
* |
| 263 |
* @param string $css The custom CSS. |
| 264 |
* @return string $css The custom CSS. |
| 265 |
*/ |
| 266 |
public function admin_custom_css( $css ) { |
| 267 |
$css .= $this->get_module_custom_css(); |
| 268 |
|
| 269 |
return $css; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Frontend custom CSS. |
| 274 |
* |
| 275 |
* @param string $css The custom CSS. |
| 276 |
* @return string $css The custom CSS. |
| 277 |
*/ |
| 278 |
public function frontend_custom_css( $css ) { |
| 279 |
$css .= $this->get_module_custom_css(); |
| 280 |
|
| 281 |
return $css; |
| 282 |
} |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
// Initialize the module. |
| 287 |
add_action( 'init', function() { |
| 288 |
new Merchant_Scroll_To_Top_Button(); |
| 289 |
} ); |
| 290 |
|