PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.10.5
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.10.5
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 / scroll-to-top-button / class-scroll-to-top-button.php

class-scroll-to-top-button.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.10.5, at inc/modules/scroll-to-top-button/class-scroll-to-top-button.php

288 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Admin enqueue CSS.
108 *
109 * @return void
110 */
111 public function admin_enqueue_css() {
112 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
113 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
114
115 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
116 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.css', array(), MERCHANT_VERSION );
117 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
118 }
119 }
120
121 /**
122 * Enqueue CSS.
123 *
124 * @return void
125 */
126 public function enqueue_css() {
127 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.css', array(), MERCHANT_VERSION );
128 }
129
130 /**
131 * Enqueue scripts.
132 *
133 * @return void
134 */
135 public function enqueue_scripts() {
136 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 );
137 }
138
139 /**
140 * Render admin preview
141 *
142 * @param Merchant_Admin_Preview $preview
143 * @param string $module
144 *
145 * @return Merchant_Admin_Preview
146 */
147 public function render_admin_preview( $preview, $module ) {
148 if ( self::MODULE_ID === $module ) {
149 ob_start();
150 self::admin_preview_content();
151 $content = ob_get_clean();
152
153 // HTML.
154 $preview->set_html( $content );
155
156 $preview->set_class( 'position', '.merchant-scroll-to-top-button', array( 'merchant-position-right', 'merchant-position-left' ) );
157 $preview->set_class( 'style', '.merchant-scroll-to-top-button', array( 'merchant-style-filled', 'merchant-style-outline' ) );
158
159 $preview->set_css( 'side-offset', '.merchant-scroll-to-top-button', '--merchant-side-offset', 'px' );
160 $preview->set_css( 'bottom-offset', '.merchant-scroll-to-top-button', '--merchant-bottom-offset', 'px' );
161
162 $preview->set_css( 'icon-size', '.merchant-scroll-to-top-button', '--merchant-icon-size', 'px' );
163 $preview->set_css( 'padding', '.merchant-scroll-to-top-button', '--merchant-padding' , 'px');
164 $preview->set_css( 'border-radius', '.merchant-scroll-to-top-button', '--merchant-border-radius', 'px' );
165
166 $preview->set_css( 'icon-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-color' );
167 $preview->set_css( 'icon-hover-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-hover-color' );
168 $preview->set_css( 'background-color', '.merchant-scroll-to-top-button', '--merchant-background-color' );
169 $preview->set_css( 'background-hover-color', '.merchant-scroll-to-top-button', '--merchant-background-hover-color' );
170
171 }
172
173 return $preview;
174 }
175
176 /**
177 * Admin preview content.
178 *
179 * @return void
180 */
181 public function admin_preview_content() {
182 ?>
183
184 <div class="mrc-preview-single-product-elements">
185 <div class="mrc-preview-left-column">
186 <div class="mrc-preview-product-image-wrapper">
187 <div class="mrc-preview-product-image"></div>
188 <div class="mrc-preview-product-image-thumbs">
189 <div class="mrc-preview-product-image-thumb"></div>
190 <div class="mrc-preview-product-image-thumb"></div>
191 <div class="mrc-preview-product-image-thumb"></div>
192 </div>
193 </div>
194 </div>
195 <div class="mrc-preview-right-column">
196 <div class="mrc-preview-text-placeholder"></div>
197 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
198 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
199 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
200 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
201 <div class="mrc-preview-addtocart-placeholder"></div>
202 </div>
203 </div>
204
205 <?php $this->get_scroll_to_top_button(); ?>
206
207 <?php
208 }
209
210 /**
211 * Get scroll to top button.
212 *
213 * @return void
214 */
215 public function get_scroll_to_top_button() {
216 $settings = $this->get_module_settings();
217
218 $html = '<div class="merchant-scroll-to-top-button ' . esc_attr( $settings[ 'position' ] ) . ' ' . esc_attr( $settings[ 'style' ] ) . ' merchant-visibility-' . esc_attr( $settings[ 'visibility' ] ) . '">';
219
220 if ( 'text-icon' === $settings[ 'type' ] ) {
221 $html .= '<span>' . esc_html( $settings[ 'text' ] ) . '</span>';
222 }
223
224 switch ( $settings[ 'icon' ] ) {
225 case 'arrow-1':
226 $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>';
227 break;
228
229 case 'arrow-2':
230 $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>';
231 break;
232
233 case 'arrow-3':
234 $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>';
235 break;
236
237 case 'arrow-4':
238 $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>';
239 break;
240 }
241
242 $html .= '</div>';
243
244 echo wp_kses( $html, merchant_kses_allowed_tags() );
245 }
246
247 /**
248 * Custom CSS.
249 *
250 * @return string
251 */
252 public function get_module_custom_css() {
253 $css = '';
254
255
256 return $css;
257 }
258
259 /**
260 * Admin custom CSS.
261 *
262 * @param string $css The custom CSS.
263 * @return string $css The custom CSS.
264 */
265 public function admin_custom_css( $css ) {
266 $css .= $this->get_module_custom_css();
267
268 return $css;
269 }
270
271 /**
272 * Frontend custom CSS.
273 *
274 * @param string $css The custom CSS.
275 * @return string $css The custom CSS.
276 */
277 public function frontend_custom_css( $css ) {
278 $css .= $this->get_module_custom_css();
279
280 return $css;
281 }
282 }
283
284 // Initialize the module.
285 add_action( 'init', function() {
286 new Merchant_Scroll_To_Top_Button();
287 } );
288