PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 1.0.0
ShopBuilder – WooCommerce Builder For Elementor v1.0.0
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
← All changes | app/Modules/Compare/CompareFrontEnd.php +420 -419 2.0.21.0.0 View file →
@@ -1,419 +1,420 @@
1 -<?php
2 -
3 -namespace RadiusTheme\SB\Modules\Compare;
4 -
5 -use RadiusTheme\SB\Helpers\Fns;
6 -use RadiusTheme\SB\Traits\SingletonTrait;
7 -use WC_Product;
8 -
9 -// Do not allow directly accessing this file.
10 -if ( ! defined( 'ABSPATH' ) ) {
11 - exit( 'This script cannot be accessed directly.' );
12 -}
13 -
14 -
15 -class CompareFrontEnd {
16 - use SingletonTrait;
17 -
18 - public function __construct() {
19 - // Template
20 -
21 - add_filter( 'body_class', [ $this, 'add_body_class' ] );
22 -
23 - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );
24 -
25 - add_action( 'rtsb/modules/compare/frontend/display', [ $this, 'button_hook' ] );
26 - // Add do_action('rtsb/modules/compare/frontend/display' ); for display anywhere.
27 -
28 - add_action( 'rtsb/modules/compare/print_button', [ $this, 'print_button' ] );
29 -
30 - // ShortCode
31 - add_shortcode( 'rtsb_compare_list', [ $this, 'list_shortcode' ] );
32 - add_shortcode( 'rtsb_compare_button', [ $this, 'button_shortcode' ] );
33 - add_shortcode( 'rtsb_compare_counter', [ $this, 'counter_shortcode' ] );
34 -
35 - $this->attach_button();
36 -
37 - }
38 -
39 -
40 - public function enqueue() {
41 - wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION );
42 - wp_register_script(
43 - 'rtsb-compare',
44 - rtsb()->get_assets_uri( 'modules/compare.js' ),
45 - [
46 - 'jquery',
47 - 'rtsb-public',
48 - ],
49 - RTSB_VERSION,
50 - true
51 - );
52 - wp_enqueue_style( 'rtsb-modules' );
53 - wp_enqueue_script( 'rtsb-compare' );
54 -
55 - $params = apply_filters(
56 - 'rtsb/module/compare/js_params',
57 - [
58 - 'product_id' => get_the_ID(),
59 - 'resturl' => get_rest_url(),
60 - 'isLoggedIn' => is_user_logged_in(),
61 - 'pageUrl' => CompareFns::instance()->get_page_url(),
62 - 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
63 - 'notice' => [
64 - 'added' => Fns::get_option( 'modules', 'compare', 'notice_added_text', esc_html__( 'Product added!', 'shopbuilder' ) ),
65 - 'removed' => Fns::get_option( 'modules', 'compare', 'notice_removed_text', esc_html__( 'Product removed!', 'shopbuilder' ) ),
66 - 'browse' => Fns::get_option( 'modules', 'compare', 'browse_list_text', esc_html__( 'Browse compare!', 'shopbuilder' ) ),
67 - 'error' => esc_html__( 'Some thing went wrong!!', 'shopbuilder' ),
68 - ],
69 - ]
70 - );
71 -
72 - wp_localize_script( 'rtsb-compare', 'rtsbCompareParams', $params );
73 - }
74 -
75 - /**
76 - * Add the "Add to Wishlist" button. Needed to use in wp_head hook.
77 - *
78 - * @return void
79 - * @since 1.0.0
80 - */
81 - public function attach_button() {
82 - $positions = apply_filters(
83 - 'rtsb/module/compare/product_btn_positions',
84 - [
85 - 'before_cart_btn' => [
86 - 'hook' => 'woocommerce_before_add_to_cart_button',
87 - 'priority' => 20,
88 - ],
89 - 'after_add_to_cart' => [
90 - 'hook' => 'woocommerce_single_product_summary',
91 - 'priority' => 31,
92 - ],
93 - 'after_thumbnail' => [
94 - 'hook' => 'woocommerce_product_thumbnails',
95 - 'priority' => 21,
96 - ],
97 - 'after_summary' => [
98 - 'hook' => 'woocommerce_after_single_product_summary',
99 - 'priority' => 11,
100 - ],
101 -
102 - 'custom' => [
103 - 'hook' => Fns::get_option( 'modules', 'compare', 'product_custom_hook_name', '' ),
104 - 'priority' => Fns::get_option( 'modules', 'compare', 'product_custom_hook_priority', 10 ),
105 - ],
106 - ]
107 - );
108 -
109 - // Add the link "Add to wishlist".
110 -
111 - $product_btn_enable = Fns::get_option( 'modules', 'compare', 'show_btn_product_page', true, 'checkbox' );
112 - $product_btn_position = Fns::get_option( 'modules', 'compare', 'product_btn_position', 'after_add_to_cart' );
113 -
114 - if ( $product_btn_enable && 'shortcode' !== $product_btn_position && isset( $positions[ $product_btn_position ]['hook'] ) ) {
115 - add_action(
116 - $positions[ $product_btn_position ]['hook'],
117 - [
118 - $this,
119 - 'button_hook',
120 - ],
121 - isset( $positions[ $product_btn_position ]['priority'] ) ? absint( $positions[ $product_btn_position ]['priority'] ) : ''
122 - );
123 - }
124 -
125 - // check if Add to wishlist button is enabled for loop.
126 - $loop_btn_enable = Fns::get_option( 'modules', 'compare', 'show_btn_on_loop', true, 'checkbox' );
127 -
128 - if ( ! $loop_btn_enable ) {
129 - return;
130 - }
131 -
132 - $positions = apply_filters(
133 - 'rtsb/module/compare/loop_btn_position',
134 - [
135 -
136 - 'before_add_to_cart' => [
137 - 'hook' => 'woocommerce_after_shop_loop_item',
138 - 'priority' => 7,
139 - ],
140 - 'after_add_to_cart' => [
141 - 'hook' => 'woocommerce_after_shop_loop_item',
142 - 'priority' => 15,
143 - ],
144 - 'custom' => [
145 - 'hook' => Fns::get_option( 'modules', 'compare', 'loop_custom_hook_name', '' ),
146 - 'priority' => Fns::get_option( 'modules', 'compare', 'loop_custom_hook_priority', 10 ),
147 - ],
148 - ]
149 - );
150 -
151 - // Add the link "Add to wishlist" in the loop.
152 -
153 - $loop_btn_position = Fns::get_option( 'modules', 'compare', 'loop_btn_position', 'after_add_to_cart' );
154 -
155 - if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) {
156 - add_action(
157 - $positions[ $loop_btn_position ]['hook'],
158 - [
159 - $this,
160 - 'button_hook',
161 - ],
162 - isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : ''
163 - );
164 - }
165 -
166 - // TODO:: Maybe this hooks is not working. Need to Check gutenberg compatibility.
167 - // Add the link "Add to wishlist" for Gutenberg blocks.
168 - add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 );
169 - }
170 -
171 -
172 - /**
173 - * Add ATW button to Products block item
174 - *
175 - * @param string $item_html HTML of the single block item.
176 - * @param array $data Data used to render the item.
177 - * @param WC_Product $product Current product.
178 - *
179 - * @return string Filtered HTML.
180 - */
181 - public function add_button_for_block( $item_html, $data, $product ) {
182 - // Add the link "Add to wishlist" in the loop.
183 - $shop_btn_position = Fns::get_option( 'modules', 'compare', 'loop_btn_position', 'after_add_to_cart' );
184 -
185 - ob_start();
186 - $this->print_button( $product->get_id() );
187 - $button = ob_get_clean();
188 - $parts = [];
189 -
190 - preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts );
191 -
192 - if ( ! $parts || count( $parts ) < 5 ) {
193 - return $item_html;
194 - }
195 -
196 - // removes first match (entire match).
197 - array_shift( $parts );
198 -
199 - // removes empty parts.
200 - $parts = array_filter( $parts );
201 -
202 - // searches for index to cut parts array.
203 - switch ( $shop_btn_position ) {
204 - case 'before_add_to_cart':
205 - $index = 2;
206 - break;
207 - case 'after_add_to_cart':
208 - $index = 3;
209 - break;
210 - default:
211 - $index = 0;
212 - break;
213 - }
214 -
215 - // if index is found, stitch button in correct position.
216 - if ( $index ) {
217 - $first_set = array_slice( $parts, 0, $index );
218 - $second_set = array_slice( $parts, $index );
219 -
220 - $parts = array_merge(
221 - $first_set,
222 - (array) $button,
223 - $second_set
224 - );
225 -
226 - // replace li classes.
227 - $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 add-to-wishlist-' . $shop_btn_position . '"', $parts[0] );
228 - }
229 -
230 - // join all parts together and return item.
231 - return implode( '', $parts );
232 - }
233 -
234 -
235 - /**
236 - * @return string|void
237 - */
238 - public function print_button( $product_id = 0 ) {
239 - global $product;
240 -
241 - // product object.
242 - $current_product = $product_id ? wc_get_product( $product_id ) : false;
243 - $current_product = $current_product ?: $product;
244 -
245 - if ( ! $current_product instanceof WC_Product ) {
246 - return '';
247 - }
248 -
249 - // product parent.
250 - $current_product_parent = $current_product->get_parent_id();
251 -
252 - // button class.
253 - $classes = [];
254 - // check if product is already in compare list.
255 - $icon_html = '<i class="rtsb-icon rtsb-icon-arrows-cw"></i>';
256 - $exists = CompareFns::instance()->is_exists_in_list( $current_product->get_id() );
257 - if ( $exists ) {
258 - $classes[] = 'rtsb-compare-remove';
259 - $button_text = Fns::get_option( 'modules', 'compare', 'button_text', esc_html__( 'Remove from list', 'shopbuilder' ) );
260 -
261 - $button_text = apply_filters( 'rtsb/modules/compare/remove_from_list_button_text', $button_text );
262 - } else {
263 - $classes[] = 'rtsb-compare-add';
264 - // labels & icons settings.
265 -
266 - $button_text = Fns::get_option( 'modules', 'compare', 'button_text', esc_html__( 'Add to Compare', 'shopbuilder' ) );
267 -
268 - // button text.
269 - $button_text = apply_filters( 'rtsb/module/compare/add_to_list_button_txt', $button_text );
270 - }
271 - // get product type.
272 - $product_type = $current_product->get_type();
273 - $params = [
274 - 'exists' => $exists,
275 - 'classes' => $classes,
276 - 'product_id' => $current_product->get_id(),
277 - 'parent_product_id' => $current_product_parent ?: $current_product->get_id(),
278 - 'product_type' => $product_type,
279 - 'button_text' => $button_text,
280 - 'show_button_text' => false,
281 - 'left_text' => apply_filters( 'rtsb/module/compare/button_left_text', '' ),
282 - 'right_text' => apply_filters( 'rtsb/module/compare/button_right_text', '' ),
283 - ];
284 - $atts = apply_filters( 'rtsb/module/compare/button_params', $params );
285 -
286 - $atts['icon_html'] = apply_filters( 'rtsb/module/compare/icon_html', $icon_html, $atts );
287 -
288 - Fns::load_template( 'compare/button', $atts );
289 -
290 - }
291 -
292 -
293 - /**
294 - * Print "Add to compare" button
295 - *
296 - * @return void
297 - */
298 - public function button_hook() {
299 -
300 - if ( ! apply_filters( 'rtsb/module/compare/show_button', true ) ) {
301 - return;
302 - }
303 - global $product;
304 - if ( $product instanceof WC_Product ) {
305 - do_action( 'rtsb/modules/compare/print_button', $product->get_id() );
306 - }
307 - }
308 -
309 -
310 - /**
311 - * List Shortcode callable function
312 - *
313 - * @param array $atts
314 - * @param string $content
315 - *
316 - * @return string [HTML]
317 - */
318 - public function list_shortcode( $atts, $content = '' ) {
319 - wp_enqueue_style( 'rtsb-modules' );
320 - wp_enqueue_script( 'rtsb-compare' );
321 -
322 - /* Fetch From option data */
323 -
324 - $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
325 -
326 - /* Product and Field */
327 - $products = CompareFns::instance()->get_products_data();
328 - $field_ids = [ 'primary' ] + CompareFns::instance()->get_list_field_ids();
329 -
330 - $return_to_shop_text = Fns::get_option( 'modules', 'compare', 'return_to_shop_text', esc_html__( 'Return to shop', 'shopbuilder' ) );
331 -
332 - $default_atts = [
333 - 'compare' => self::instance(),
334 - 'products' => $products,
335 - 'field_ids' => $field_ids,
336 - 'return_to_shop_text' => $return_to_shop_text,
337 - 'empty_text' => ! empty( $empty_text ) ? $empty_text : '',
338 - ];
339 -
340 - $atts = shortcode_atts( $default_atts, $atts, $content );
341 - $args = apply_filters( 'rtsb/module/compare/list_args', $atts );
342 -
343 - return Fns::load_template( 'compare/list', $args, true );
344 - }
345 -
346 -
347 - /**
348 - * Compare button Shortcode callable function
349 - *
350 - * @param array $atts
351 - * @param string $content
352 - *
353 - * @return string [HTML]
354 - */
355 - public function button_shortcode( $atts, $content = '' ) {
356 - wp_enqueue_style( 'rtsb-modules' );
357 - wp_enqueue_script( 'rtsb-compare' );
358 - global $product;
359 - if ( ! $product instanceof WC_Product ) {
360 - return '';
361 - }
362 - ob_start();
363 -
364 - do_action( 'rtsb/modules/compare/print_button', $product->get_id() );
365 -
366 - return ob_get_clean();
367 - }
368 -
369 - /**
370 - * Compare counter Shortcode callable function
371 - *
372 - * @param array $atts
373 - * @param string $content
374 - *
375 - * @return string [HTML]
376 - */
377 - public function counter_shortcode( $atts, $content = '' ) {
378 - wp_enqueue_style( 'rtsb-modules' );
379 -
380 - $product_ids = CompareFns::instance()->get_compared_product_ids();
381 -
382 - $button_text = esc_html__( 'Compare', 'shopbuilder' );
383 - $page_url = CompareFns::instance()->get_page_url();
384 -
385 - $default_atts = [
386 - 'product_ids' => $product_ids,
387 - 'item_count' => count( $product_ids ),
388 - 'page_url' => $page_url,
389 - 'button_text' => $button_text,
390 - 'text' => '',
391 - ];
392 -
393 - $atts = shortcode_atts( $default_atts, $atts, $content );
394 - $count_attr = apply_filters( 'rtsb/module/compare/counter_args', $atts );
395 -
396 - return Fns::load_template( 'compare/counter', $count_attr, true );
397 - }
398 -
399 -
400 - /**
401 - * Add specific body class when the Wishlist page is opened
402 - *
403 - * @param array $classes Existing boy classes.
404 - *
405 - * @return array
406 - */
407 - public function add_body_class( $classes ) {
408 -
409 - $compare_page_id = Fns::get_option( 'modules', 'compare', 'page', 0, 'number' );
410 -
411 - if ( ! empty( $compare_page_id ) && is_page( $compare_page_id ) ) {
412 - $classes[] = 'rtsb-compare-page';
413 - $classes[] = 'woocommerce';
414 - $classes[] = 'woocommerce-page';
415 - }
416 -
417 - return $classes;
418 - }
419 -}
1 +<?php
2 +
3 +namespace RadiusTheme\SB\Modules\Compare;
4 +
5 +use RadiusTheme\SB\Helpers\Fns;
6 +use RadiusTheme\SB\Traits\SingletonTrait;
7 +use WC_Product;
8 +
9 +// Do not allow directly accessing this file.
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit( 'This script cannot be accessed directly.' );
12 +}
13 +
14 +
15 +class CompareFrontEnd {
16 + use SingletonTrait;
17 +
18 + public function __construct() {
19 + // Template
20 + add_action( 'init', [ $this, 'attach_button' ] );
21 + add_filter( 'body_class', [ $this, 'add_body_class' ] );
22 +
23 + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );
24 +
25 + add_action( 'rtsb/modules/compare/frontend/display', [ $this, 'button_hook' ] );
26 + // Add do_action('rtsb/modules/compare/frontend/display' ); for display anywhere.
27 +
28 +
29 + add_action( 'rtsb/modules/compare/print_button', [ $this, 'print_button' ] );
30 +
31 + // ShortCode
32 + add_shortcode( 'rtsb_compare_list', [ $this, 'list_shortcode' ] );
33 + add_shortcode( 'rtsb_compare_button', [ $this, 'button_shortcode' ] );
34 + add_shortcode( 'rtsb_compare_counter', [ $this, 'counter_shortcode' ] );
35 +
36 + }
37 +
38 +
39 + public function enqueue() {
40 + wp_register_style( 'rtsb-modules', rtsb()->get_assets_uri( 'modules/modules.css' ), [], RTSB_VERSION );
41 + wp_register_script(
42 + 'rtsb-compare',
43 + rtsb()->get_assets_uri( 'modules/compare.js' ),
44 + [
45 + 'jquery',
46 + 'rtsb-public',
47 + ],
48 + RTSB_VERSION,
49 + true
50 + );
51 + wp_enqueue_style( 'rtsb-modules' );
52 + wp_enqueue_script( 'rtsb-compare' );
53 +
54 + $params = apply_filters(
55 + 'rtsb/module/compare/js_params',
56 + [
57 + 'product_id' => get_the_ID(),
58 + 'resturl' => get_rest_url(),
59 + 'isLoggedIn' => is_user_logged_in(),
60 + 'pageUrl' => CompareFns::instance()->get_page_url(),
61 + 'rest_nonce' => wp_create_nonce( 'wp_rest' ),
62 + 'notice' => [
63 + 'added' => Fns::get_option( 'modules', 'compare', 'notice_added_text', esc_html__( 'Product added!', 'shopbuilder' ) ),
64 + 'removed' => Fns::get_option( 'modules', 'compare', 'notice_removed_text', esc_html__( 'Product removed!', 'shopbuilder' ) ),
65 + 'browse' => Fns::get_option( 'modules', 'compare', 'browse_list_text', esc_html__( 'Browse compare!', 'shopbuilder' ) ),
66 + 'error' => esc_html__( 'Some thing went wrong!!', 'shopbuilder' ),
67 + 'position' => Fns::get_option( 'general', 'notification', 'notification_position', 'center-center' ),
68 + ],
69 + ]
70 + );
71 +
72 + wp_localize_script( 'rtsb-compare', 'rtsbCompareParams', $params );
73 + }
74 +
75 + /**
76 + * Add the "Add to Wishlist" button. Needed to use in wp_head hook.
77 + *
78 + * @return void
79 + * @since 1.0.0
80 + */
81 + public function attach_button() {
82 + $positions = apply_filters(
83 + 'rtsb/module/compare/product_btn_positions',
84 + [
85 + 'before_cart_btn' => [
86 + 'hook' => 'woocommerce_before_add_to_cart_button',
87 + 'priority' => 20,
88 + ],
89 + 'after_add_to_cart' => [
90 + 'hook' => 'woocommerce_single_product_summary',
91 + 'priority' => 31,
92 + ],
93 + 'after_thumbnail' => [
94 + 'hook' => 'woocommerce_product_thumbnails',
95 + 'priority' => 21,
96 + ],
97 + 'after_summary' => [
98 + 'hook' => 'woocommerce_after_single_product_summary',
99 + 'priority' => 11,
100 + ],
101 +
102 + 'custom' => [
103 + 'hook' => Fns::get_option( 'modules', 'compare', 'product_custom_hook_name', ''),
104 + 'priority' => Fns::get_option( 'modules', 'compare', 'product_custom_hook_priority', 10),
105 + ],
106 + ]
107 + );
108 +
109 + // Add the link "Add to wishlist".
110 +
111 + $product_btn_enable = Fns::get_option( 'modules', 'compare', 'show_btn_product_page', true, 'checkbox');
112 + $product_btn_position = Fns::get_option( 'modules', 'compare', 'product_btn_position', 'after_add_to_cart');
113 +
114 + if ( $product_btn_enable && 'shortcode' !== $product_btn_position && isset( $positions[ $product_btn_position ]['hook'] ) ) {
115 + add_action(
116 + $positions[ $product_btn_position ]['hook'],
117 + [
118 + $this,
119 + 'button_hook',
120 + ],
121 + isset( $positions[ $product_btn_position ]['priority'] ) ? absint( $positions[ $product_btn_position ]['priority'] ) : ''
122 + );
123 + }
124 +
125 + // check if Add to wishlist button is enabled for loop.
126 + $loop_btn_enable = Fns::get_option( 'modules', 'compare', 'show_btn_on_loop', true, 'checkbox' );
127 +
128 + if ( ! $loop_btn_enable ) {
129 + return;
130 + }
131 +
132 + $positions = apply_filters(
133 + 'rtsb/module/compare/loop_btn_position',
134 + [
135 + 'top_thumbnail' => [
136 + 'hook' => 'woocommerce_before_shop_loop_item',
137 + 'priority' => 5,
138 + ],
139 + 'before_add_to_cart' => [
140 + 'hook' => 'woocommerce_after_shop_loop_item',
141 + 'priority' => 7,
142 + ],
143 + 'after_add_to_cart' => [
144 + 'hook' => 'woocommerce_after_shop_loop_item',
145 + 'priority' => 15,
146 + ],
147 + 'custom' => [
148 + 'hook' => Fns::get_option( 'modules', 'compare', 'loop_custom_hook_name', '' ),
149 + 'priority' => Fns::get_option( 'modules', 'compare', 'loop_custom_hook_priority', 10 ),
150 + ],
151 + ]
152 + );
153 +
154 + // Add the link "Add to wishlist" in the loop.
155 +
156 + $loop_btn_position = Fns::get_option( 'modules', 'compare', 'loop_btn_position', 'after_add_to_cart' );
157 +
158 + if ( 'shortcode' !== $loop_btn_position && isset( $positions[ $loop_btn_position ]['hook'] ) ) {
159 + add_action(
160 + $positions[ $loop_btn_position ]['hook'],
161 + [
162 + $this,
163 + 'button_hook',
164 + ],
165 + isset( $positions[ $loop_btn_position ]['priority'] ) ? $positions[ $loop_btn_position ]['priority'] : ''
166 + );
167 + }
168 +
169 + // Add the link "Add to wishlist" for Gutenberg blocks.
170 + add_filter( 'woocommerce_blocks_product_grid_item_html', [ $this, 'add_button_for_block' ], 10, 3 );
171 + }
172 +
173 +
174 + /**
175 + * Add ATW button to Products block item
176 + *
177 + * @param string $item_html HTML of the single block item.
178 + * @param array $data Data used to render the item.
179 + * @param WC_Product $product Current product.
180 + *
181 + * @return string Filtered HTML.
182 + */
183 + public function add_button_for_block( $item_html, $data, $product ) {
184 + // Add the link "Add to wishlist" in the loop.
185 + $shop_btn_position = Fns::get_option( 'modules', 'compare', 'loop_btn_position', 'after_add_to_cart' );
186 +
187 + ob_start();
188 + $this->print_button( $product->get_id() );
189 + $button = ob_get_clean();
190 + $parts = [];
191 +
192 + preg_match( '/(<li class=".*?">)[\S|\s]*?(<a .*?>[\S|\s]*?<\/a>)([\S|\s]*?)(<\/li>)/', $item_html, $parts );
193 +
194 + if ( ! $parts || count( $parts ) < 5 ) {
195 + return $item_html;
196 + }
197 +
198 + // removes first match (entire match).
199 + array_shift( $parts );
200 +
201 + // removes empty parts.
202 + $parts = array_filter( $parts );
203 +
204 + // searches for index to cut parts array.
205 + switch ( $shop_btn_position ) {
206 + case 'top_thumbnail':
207 + $index = 1;
208 + break;
209 + case 'before_add_to_cart':
210 + $index = 2;
211 + break;
212 + case 'after_add_to_cart':
213 + $index = 3;
214 + break;
215 + default:
216 + $index = 0;
217 + break;
218 + }
219 +
220 + // if index is found, stitch button in correct position.
221 + if ( $index ) {
222 + $first_set = array_slice( $parts, 0, $index );
223 + $second_set = array_slice( $parts, $index );
224 +
225 + $parts = array_merge(
226 + $first_set,
227 + (array) $button,
228 + $second_set
229 + );
230 +
231 + // replace li classes.
232 + $parts[0] = preg_replace( '/class="(.*)"/', 'class="$1 add-to-wishlist-' . $shop_btn_position . '"', $parts[0] );
233 + }
234 +
235 + // join all parts together and return item.
236 + return implode( '', $parts );
237 + }
238 +
239 +
240 + /**
241 + * @return string|void
242 + */
243 + public function print_button( $product_id = 0 ) {
244 + global $product;
245 +
246 + // product object.
247 + $current_product = $product_id ? wc_get_product( $product_id ) : false;
248 + $current_product = $current_product ?: $product;
249 +
250 + if ( ! $current_product instanceof WC_Product ) {
251 + return '';
252 + }
253 +
254 + // product parent.
255 + $current_product_parent = $current_product->get_parent_id();
256 +
257 + // button class.
258 + $classes = [];
259 + // check if product is already in compare list.
260 + $icon_html = '<i class="rtsb-icon rtsb-icon-arrows-cw"></i>';
261 + $exists = CompareFns::instance()->is_exists_in_list( $current_product->get_id() );
262 + if ( $exists ) {
263 + $classes[] = 'rtsb-compare-remove';
264 + $button_text = Fns::get_option( 'modules', 'compare', 'button_text', esc_html__( 'Remove from list', 'shopbuilder' ) );
265 +
266 + $button_text = apply_filters( 'rtsb/modules/compare/remove_from_list_button_text', $button_text );
267 + } else {
268 + $classes[] = 'rtsb-compare-add';
269 + // labels & icons settings.
270 +
271 + $button_text = Fns::get_option( 'modules', 'compare', 'button_text', esc_html__( 'Add to Compare', 'shopbuilder' ) );
272 +
273 + // button text.
274 + $button_text = apply_filters( 'rtsb/module/compare/add_to_list_button_txt', $button_text );
275 + }
276 + // get product type.
277 + $product_type = $current_product->get_type();
278 + $params = [
279 + 'exists' => $exists,
280 + 'classes' => $classes,
281 + 'product_id' => $current_product->get_id(),
282 + 'parent_product_id' => $current_product_parent ?: $current_product->get_id(),
283 + 'product_type' => $product_type,
284 + 'button_text' => $button_text,
285 + 'left_text' => apply_filters( 'rtsb/module/compare/button_left_text', '' ),
286 + 'right_text' => apply_filters( 'rtsb/module/compare/button_right_text', '' ),
287 + ];
288 + $atts = apply_filters( 'rtsb/module/compare/button_params', $params );
289 +
290 + $atts['icon_html'] = apply_filters( 'rtsb/module/compare/icon_html', $icon_html, $atts );
291 +
292 + Fns::load_template( 'compare/button', $atts );
293 +
294 + }
295 +
296 +
297 + /**
298 + * Print "Add to compare" button
299 + *
300 + * @return void
301 + */
302 + public function button_hook() {
303 +
304 + if ( ! apply_filters( 'rtsb/module/compare/show_button', true ) ) {
305 + return;
306 + }
307 + global $product;
308 + if( $product instanceof WC_Product ) {
309 + do_action('rtsb/modules/compare/print_button', $product->get_id());
310 + }
311 + }
312 +
313 +
314 + /**
315 + * List Shortcode callable function
316 + *
317 + * @param array $atts
318 + * @param string $content
319 + *
320 + * @return string [HTML]
321 + */
322 + public function list_shortcode( $atts, $content = '' ) {
323 + wp_enqueue_style( 'rtsb-modules' );
324 + wp_enqueue_script( 'rtsb-compare' );
325 +
326 + /* Fetch From option data */
327 +
328 + $empty_text = Fns::get_option( 'modules', 'compare', 'empty_table_text', esc_html__( 'No product found at your list.', 'shopbuilder' ) );
329 +
330 + /* Product and Field */
331 + $products = CompareFns::instance()->get_products_data();
332 + $field_ids = [ 'primary' ] + CompareFns::instance()->get_list_field_ids();
333 +
334 + $return_to_shop_text = Fns::get_option( 'modules', 'compare', 'return_to_shop_text', esc_html__( 'Return to shop', 'shopbuilder' ) );
335 +
336 + $default_atts = [
337 + 'compare' => self::instance(),
338 + 'products' => $products,
339 + 'field_ids' => $field_ids,
340 + 'return_to_shop_text' => $return_to_shop_text,
341 + 'empty_text' => ! empty( $empty_text ) ? $empty_text : '',
342 + ];
343 +
344 + $atts = shortcode_atts( $default_atts, $atts, $content );
345 + $args = apply_filters( 'rtsb/module/compare/list_args', $atts );
346 + return Fns::load_template( 'compare/list', $args, true );
347 + }
348 +
349 +
350 + /**
351 + * Compare button Shortcode callable function
352 + *
353 + * @param array $atts
354 + * @param string $content
355 + *
356 + * @return string [HTML]
357 + */
358 + public function button_shortcode( $atts, $content = '' ) {
359 + wp_enqueue_style( 'rtsb-modules' );
360 + wp_enqueue_script( 'rtsb-compare' );
361 + global $product;
362 +
363 + ob_start();
364 +
365 + do_action( 'rtsb/modules/compare/print_button', $product->get_id() );
366 +
367 + return ob_get_clean();
368 + }
369 +
370 + /**
371 + * Compare counter Shortcode callable function
372 + *
373 + * @param array $atts
374 + * @param string $content
375 + *
376 + * @return string [HTML]
377 + */
378 + public function counter_shortcode( $atts, $content = '' ) {
379 + wp_enqueue_style( 'rtsb-modules' );
380 +
381 + $product_ids = CompareFns::instance()->get_compared_product_ids();
382 +
383 + $button_text = esc_html__( 'Compare', 'shopbuilder' );
384 + $page_url = CompareFns::instance()->get_page_url();
385 +
386 + $default_atts = [
387 + 'product_ids' => $product_ids,
388 + 'item_count' => count( $product_ids ),
389 + 'page_url' => $page_url,
390 + 'button_text' => $button_text,
391 + 'text' => '',
392 + ];
393 +
394 + $atts = shortcode_atts( $default_atts, $atts, $content );
395 + $count_attr = apply_filters( 'rtsb/module/compare/counter_args', $atts );
396 +
397 + return Fns::load_template( 'compare/counter', $count_attr, true );
398 + }
399 +
400 +
401 + /**
402 + * Add specific body class when the Wishlist page is opened
403 + *
404 + * @param array $classes Existing boy classes.
405 + *
406 + * @return array
407 + */
408 + public function add_body_class( $classes ) {
409 +
410 + $compare_page_id = Fns::get_option( 'modules', 'compare', 'page', 0, 'number' );
411 +
412 + if ( ! empty( $compare_page_id ) && is_page( $compare_page_id ) ) {
413 + $classes[] = 'rtsb-compare-page';
414 + $classes[] = 'woocommerce';
415 + $classes[] = 'woocommerce-page';
416 + }
417 +
418 + return $classes;
419 + }
420 +}