PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.0
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.0
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 2.3.0, at inc/modules/scroll-to-top-button/class-scroll-to-top-button.php

300 lines 9.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 * Initialize the module's option group definitions.
33 *
34 * @return array<int, array<string, mixed>> Array of option group arrays.
35 */
36 protected function init_option_groups() {
37 return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
38 }
39
40 /**
41 * Constructor.
42 *
43 */
44 public function __construct() {
45 parent::__construct();
46
47 // Module section.
48 $this->module_section = 'improve-experience';
49
50 // Module id.
51 $this->module_id = self::MODULE_ID;
52
53 // Module default settings.
54 $this->module_default_settings = array(
55 'style' => 'merchant-style-filled',
56 'type' => 'icon',
57 'icon' => 'arrow-1',
58 'text' => esc_html__( 'Back to top', 'merchant' ),
59 'position' => 'merchant-position-right',
60 'visibility' => 'all',
61 );
62
63 // Module data.
64 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
65
66 // AI prompt examples.
67 $this->ai_examples = array(
68 'blurb' => esc_html__( 'See what AI can do for your scroll-to-top button, from repositioning it and restyling it to showing a text label or limiting it to mobile devices.', 'merchant' ),
69 'prompts' => array(
70 esc_html__( 'Explore the abilities WPVibe gives you access to, then move the scroll-to-top button to the left side of the page', 'merchant' ),
71 esc_html__( 'Check what abilities you have available through WPVibe, then switch the scroll-to-top button to the outline style with a dark gray border', 'merchant' ),
72 esc_html__( 'Look at your available WPVibe abilities, then show a "Back to top" text label next to the scroll-to-top button\'s icon', 'merchant' ),
73 esc_html__( 'See what abilities WPVibe exposes, then only show the scroll-to-top button on mobile devices', 'merchant' ),
74 ),
75 );
76
77 // Module options path.
78 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
79
80 // Is module preview page.
81 if ( is_admin() && parent::is_module_settings_page() ) {
82 self::$is_module_preview = true;
83
84 // Enqueue admin styles.
85 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
86
87 // Admin preview box.
88 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
89
90 // Custom CSS.
91 // The custom CSS should be added here as well due to ensure preview box works properly.
92 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
93
94 }
95
96 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
97 return;
98 }
99
100 // Return early if it's on admin but not in the respective module settings page.
101 if ( is_admin() && ! parent::is_module_settings_page() ) {
102 return;
103 }
104
105 // Enqueue styles.
106 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
107
108 // Enqueue scripts.
109 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
110
111 // Render the scroll to top button on footer.
112 add_action( 'wp_footer', array( $this, 'get_scroll_to_top_button' ) );
113
114 // Custom CSS.
115 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
116 }
117
118 /**
119 * Admin enqueue CSS.
120 *
121 * @return void
122 */
123 public function admin_enqueue_css() {
124 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
125 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
126
127 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
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 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
130 }
131 }
132
133 /**
134 * Enqueue CSS.
135 *
136 * @return void
137 */
138 public function enqueue_css() {
139 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/scroll-to-top-button.min.css', array(), MERCHANT_VERSION );
140 }
141
142 /**
143 * Enqueue scripts.
144 *
145 * @return void
146 */
147 public function enqueue_scripts() {
148 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 );
149 }
150
151 /**
152 * Render admin preview
153 *
154 * @param Merchant_Admin_Preview $preview
155 * @param string $module
156 *
157 * @return Merchant_Admin_Preview
158 */
159 public function render_admin_preview( $preview, $module ) {
160 if ( self::MODULE_ID === $module ) {
161 ob_start();
162 self::admin_preview_content();
163 $content = ob_get_clean();
164
165 // HTML.
166 $preview->set_html( $content );
167
168 $preview->set_class( 'position', '.merchant-scroll-to-top-button', array( 'merchant-position-right', 'merchant-position-left' ) );
169 $preview->set_class( 'style', '.merchant-scroll-to-top-button', array( 'merchant-style-filled', 'merchant-style-outline' ) );
170
171 $preview->set_css( 'side-offset', '.merchant-scroll-to-top-button', '--merchant-side-offset', 'px' );
172 $preview->set_css( 'bottom-offset', '.merchant-scroll-to-top-button', '--merchant-bottom-offset', 'px' );
173
174 $preview->set_css( 'icon-size', '.merchant-scroll-to-top-button', '--merchant-icon-size', 'px' );
175 $preview->set_css( 'padding', '.merchant-scroll-to-top-button', '--merchant-padding' , 'px');
176 $preview->set_css( 'border-radius', '.merchant-scroll-to-top-button', '--merchant-border-radius', 'px' );
177
178 $preview->set_css( 'icon-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-color' );
179 $preview->set_css( 'icon-hover-color', '.merchant-scroll-to-top-button svg', '--merchant-icon-hover-color' );
180 $preview->set_css( 'background-color', '.merchant-scroll-to-top-button', '--merchant-background-color' );
181 $preview->set_css( 'background-hover-color', '.merchant-scroll-to-top-button', '--merchant-background-hover-color' );
182
183 }
184
185 return $preview;
186 }
187
188 /**
189 * Admin preview content.
190 *
191 * @return void
192 */
193 public function admin_preview_content() {
194 ?>
195
196 <div class="mrc-preview-single-product-elements">
197 <div class="mrc-preview-left-column">
198 <div class="mrc-preview-product-image-wrapper">
199 <div class="mrc-preview-product-image"></div>
200 <div class="mrc-preview-product-image-thumbs">
201 <div class="mrc-preview-product-image-thumb"></div>
202 <div class="mrc-preview-product-image-thumb"></div>
203 <div class="mrc-preview-product-image-thumb"></div>
204 </div>
205 </div>
206 </div>
207 <div class="mrc-preview-right-column">
208 <div class="mrc-preview-text-placeholder"></div>
209 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
210 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
211 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
212 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
213 <div class="mrc-preview-addtocart-placeholder"></div>
214 </div>
215 </div>
216
217 <?php $this->get_scroll_to_top_button(); ?>
218
219 <?php
220 }
221
222 /**
223 * Get scroll to top button.
224 *
225 * @return void
226 */
227 public function get_scroll_to_top_button() {
228 $settings = $this->get_module_settings();
229
230 $html = '<div class="merchant-scroll-to-top-button ' . esc_attr( $settings[ 'position' ] ) . ' ' . esc_attr( $settings[ 'style' ] ) . ' merchant-visibility-' . esc_attr( $settings[ 'visibility' ] ) . '">';
231
232 if ( 'text-icon' === $settings[ 'type' ] ) {
233 $html .= '<span>' . esc_html( $settings[ 'text' ] ) . '</span>';
234 }
235
236 switch ( $settings[ 'icon' ] ) {
237 case 'arrow-1':
238 $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>';
239 break;
240
241 case 'arrow-2':
242 $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>';
243 break;
244
245 case 'arrow-3':
246 $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>';
247 break;
248
249 case 'arrow-4':
250 $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>';
251 break;
252 }
253
254 $html .= '</div>';
255
256 echo wp_kses( $html, merchant_kses_allowed_tags() );
257 }
258
259 /**
260 * Custom CSS.
261 *
262 * @return string
263 */
264 public function get_module_custom_css() {
265 $css = '';
266
267
268 return $css;
269 }
270
271 /**
272 * Admin custom CSS.
273 *
274 * @param string $css The custom CSS.
275 * @return string $css The custom CSS.
276 */
277 public function admin_custom_css( $css ) {
278 $css .= $this->get_module_custom_css();
279
280 return $css;
281 }
282
283 /**
284 * Frontend custom CSS.
285 *
286 * @param string $css The custom CSS.
287 * @return string $css The custom CSS.
288 */
289 public function frontend_custom_css( $css ) {
290 $css .= $this->get_module_custom_css();
291
292 return $css;
293 }
294 }
295
296 // Initialize the module.
297 add_action( 'init', function() {
298 Merchant_Modules::create_module( new Merchant_Scroll_To_Top_Button() );
299 } );
300