PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
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
shopbuilder / app / Modules / WishList / WishlistFns.php

WishlistFns.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at app/Modules/WishList/WishlistFns.php

466 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RadiusTheme\SB\Modules\WishList;
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 WishlistFns
16 */
17 class WishlistFns {
18 /**
19 * Singleton
20 */
21 use SingletonTrait;
22
23 /**
24 * Wishlist menu slug
25 */
26 const WC_MENU_SLUG = 'wishlist';
27
28 /**
29 * Remove product id
30 *
31 * @param int $product_id product id.
32 *
33 * @return bool|int
34 */
35 public function remove_product( $product_id ) {
36
37 if ( ! $this->is_exists_in_wishlist( $product_id ) ) {
38 return 0;
39 }
40 $pIds = $this->get_wishlist_ids();
41 $pIds = array_diff( $pIds, [ $product_id ] );
42 if ( is_user_logged_in() ) {
43 $this->update_user_wishlist_ids( $pIds );
44 } else {
45 $cookie_name = $this->get_cookie_name();
46 if ( empty( $pIds ) ) {
47 setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
48 $_COOKIE[ $cookie_name ] = false;
49 } else {
50 setcookie( $cookie_name, wp_json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
51 $_COOKIE[ $cookie_name ] = wp_json_encode( $pIds );
52 }
53 }
54
55 return true;
56 }
57
58 /**
59 * @return false|string
60 */
61 public static function get_page_url() {
62 $page_id = Fns::get_option( 'modules', 'wishlist', 'page' );
63 return $page_id && absint( $page_id ) ? get_permalink( $page_id ) : '#';
64 }
65
66 /**
67 * Product Add
68 *
69 * @param integer $product_id product id.
70 */
71 public function add_product( $product_id ) {
72
73 if ( $this->is_exists_in_wishlist( $product_id ) ) {
74 return 0;
75 }
76
77 $pIds = $this->get_wishlist_ids();
78 $pIds[] = $product_id;
79 if ( is_user_logged_in() ) {
80 $this->update_user_wishlist_ids( $pIds );
81 } else {
82 $cookie_name = $this->get_cookie_name();
83
84 setcookie( $cookie_name, wp_json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
85 $_COOKIE[ $cookie_name ] = wp_json_encode( $pIds );
86 }
87
88 return 1;
89 }
90
91 /**
92 * [get_products_data] generate wishlist products data
93 *
94 * @return array product list
95 */
96 public function get_products_data() {
97
98 $ids = $this->get_wishlist_ids();
99
100 if ( empty( $ids ) ) {
101 return [];
102 }
103
104 $args = [
105 'include' => $ids,
106 ];
107
108 $products = wc_get_products( $args );
109
110 $products_data = [];
111
112 $fields = $this->get_field_ids();
113
114 $fields = array_filter(
115 $fields,
116 function ( $field ) {
117 return 'pa_' === substr( $field, 0, 3 );
118 },
119 ARRAY_FILTER_USE_KEY
120 );
121
122 $data_none = '-';
123
124 foreach ( $products as $product ) {
125
126 $rating_count = $product->get_rating_count();
127 $average = $product->get_average_rating();
128 $quantity_args = [
129 'input_value' => 1,
130 'min_value' => $product->get_min_purchase_quantity(),
131 'max_value' => $product->get_max_purchase_quantity(),
132 ];
133
134 $products_data[ $product->get_id() ] = [
135 'id' => $product->get_id(),
136 'remove' => $product->get_id(),
137 'image' => $product->get_image() ? $product->get_image() : $data_none,
138 'title' => $product->get_title() ? $product->get_title() : $data_none,
139 'image_id' => $product->get_image_id(),
140 'permalink' => $product->get_permalink(),
141 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
142 'rating' => wc_get_rating_html( $average, $rating_count ),
143 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none,
144 'quantity' => woocommerce_quantity_input( $quantity_args, $product, false ),
145 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
146 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
147 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
148 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
149 'availability' => $this->availability_html( $product ),
150 ];
151
152 foreach ( $fields as $field_id => $field_name ) {
153 if ( taxonomy_exists( $field_id ) ) {
154 $products_data[ $product->get_id() ][ $field_id ] = [];
155 $terms = get_the_terms( $product->get_id(), $field_id );
156 if ( ! empty( $terms ) ) {
157 foreach ( $terms as $term ) {
158 $term = sanitize_term( $term, $field_id );
159 $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
160 }
161 } else {
162 $products_data[ $product->get_id() ][ $field_id ][] = '-';
163 }
164 $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
165 }
166 }
167 }
168
169 return $products_data;
170 }
171
172 /**
173 * Table field list
174 *
175 * @return array Table Field list
176 */
177 public function get_field_ids() {
178
179 $default_show = [
180 'info',
181 'price',
182 'availability',
183 'add_to_cart',
184 'remove',
185 ];
186
187 $fields_settings = Fns::get_option( 'modules', 'wishlist', 'page_show_fields', [] );
188
189 if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) {
190 $fields = $fields_settings;
191 } else {
192 $fields = $default_show;
193 }
194
195 return $fields;
196 }
197
198 /**
199 * Get default fields List
200 * return array
201 */
202 public function get_default_fields() {
203 $fields = [
204 'info' => esc_html__( 'Products', 'shopbuilder' ),
205 'image' => esc_html__( 'Image', 'shopbuilder' ),
206 'title' => esc_html__( 'Title', 'shopbuilder' ),
207 'price' => esc_html__( 'Price', 'shopbuilder' ),
208 'quantity' => esc_html__( 'Quantity', 'shopbuilder' ),
209 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ),
210 'remove' => esc_html__( 'Remove', 'shopbuilder' ),
211 'description' => esc_html__( 'Description', 'shopbuilder' ),
212 'availability' => esc_html__( 'Availability', 'shopbuilder' ),
213 'sku' => esc_html__( 'Sku', 'shopbuilder' ),
214 'weight' => esc_html__( 'Weight', 'shopbuilder' ),
215 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ),
216 ];
217 return apply_filters( 'rtsb/module/wishlist/list_default_fields', $fields );
218 }
219
220 /**
221 * [get_cookie_name] Get cookie name
222 *
223 * @return string [string]
224 */
225 public function get_cookie_name() {
226 $name = Wishlist::KEY;
227 if ( is_multisite() ) {
228 $name .= '_' . get_current_blog_id();
229 }
230
231 return $name;
232 }
233
234 /**
235 * Display_field
236 *
237 * @param string $field_id attribute slug.
238 * @param array $product product data.
239 *
240 * @return void [html]
241 */
242 public function display_field( $field_id, $product ) {
243
244 $type = $field_id;
245
246 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
247 $type = 'attribute';
248 }
249
250 switch ( $type ) {
251 case 'info':
252 ?>
253 <a href="<?php echo esc_url( get_permalink( $product['id'] ) ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a>
254 <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product['title'] ); ?></a>
255 <?php
256 break;
257
258 case 'remove':
259 ?>
260 <a href="#" class="rtsb-wishlist-remove"
261 data-product_id="<?php echo esc_attr( $product['id'] ); ?>"><i class="rtsb-icon rtsb-icon-trash-empty"></i></a>
262 <?php
263 break;
264
265 case 'image':
266 ?>
267 <a href="<?php echo esc_url( get_permalink( $product['id'] ) ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a>
268 <?php
269 break;
270
271 case 'title':
272 ?>
273 <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product[ $field_id ] ); ?></a>
274 <?php
275 break;
276 case 'quantity':
277 Fns::print_html( $product[ $field_id ] );
278 break;
279
280 case 'ratting':
281 echo '<span class="rtsb-wl-product-ratting">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
282 break;
283
284 case 'add_to_cart':
285 Fns::print_html( apply_filters( 'rtsb/modules/wishlist/add_to_cart_btn', $product[ $field_id ] ) );
286 break;
287 case 'weight':
288 if ( $product[ $field_id ] ) {
289 $unit = '-' !== $product[ $field_id ] ? get_option( 'woocommerce_weight_unit' ) : '';
290 Fns::print_html( wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ) );
291 }
292 break;
293
294 case 'description':
295 Fns::print_html( apply_filters( 'woocommerce_short_description', $product[ $field_id ] ) );
296 break;
297 case 'attribute':
298 case 'price':
299 default:
300 echo wp_kses_post( $product[ $field_id ] );
301 break;
302 }
303 }
304
305 /**
306 * Field name
307 *
308 * @param string $field field.
309 * @param bool $custom custom or not.
310 *
311 * @return string|void
312 */
313 public function field_name( $field, $custom = false ) {
314
315 if ( empty( $field ) ) {
316 return;
317 }
318
319 if ( true === $custom ) {
320 return $field;
321 }
322
323 $default = $this->get_default_fields();
324
325 $str = substr( $field, 0, 3 );
326 if ( 'pa_' === $str ) {
327 $field_name = wc_attribute_label( $field );
328 } else {
329 $field_name = $default[ $field ];
330 }
331
332 return $field_name;
333 }
334
335 /**
336 * Get wishlist product ids
337 *
338 * @return array
339 */
340 public function get_wishlist_ids() {
341 $ids = [];
342 if ( is_user_logged_in() ) {
343 $listIds = get_user_meta( get_current_user_id(), Wishlist::KEY, true );
344 if ( is_array( $listIds ) && ! empty( $listIds ) ) {
345 $ids = array_map( 'absint', $listIds );
346 }
347 } else {
348 $cookie_name = $this->get_cookie_name();
349
350 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
351 if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
352 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
353 $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
354 }
355 }
356 return Fns::get_available_products_by_ids( $ids );
357 }
358
359 /**
360 * @param array $product_ids ids.
361 * @return void
362 */
363 public function update_user_wishlist_ids( $product_ids ) {
364 $uid = get_current_user_id();
365 $product_ids = Fns::get_available_products_by_ids( $product_ids );
366 update_user_meta( $uid, Wishlist::KEY, $product_ids );
367 }
368
369 /**
370 * @param int $product_id product id.
371 *
372 * @return bool
373 */
374 public function is_exists_in_wishlist( $product_id ) {
375 $id = $product_id;
376 $list = $this->get_wishlist_ids();
377 if ( is_array( $list ) ) {
378 return in_array( absint( $id ), $list, true );
379 } else {
380 return false;
381 }
382 }
383
384 /**
385 * @param object $product product.
386 *
387 * @return string
388 */
389 public function availability_html( $product ) {
390 $html = '';
391 $availability = $product->get_availability();
392
393 if ( empty( $availability['availability'] ) ) {
394 $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' );
395 }
396
397 if ( ! empty( $availability['availability'] ) ) {
398 ob_start();
399
400 wc_get_template(
401 'single-product/stock.php',
402 [
403 'product' => $product,
404 'class' => $availability['class'],
405 'availability' => $availability['availability'],
406 ]
407 );
408
409 $html = ob_get_clean();
410 }
411
412 return apply_filters( 'woocommerce_get_stock_html', $html, $product );
413 }
414
415 /**
416 * Generate Cart button
417 *
418 * @param object $product product.
419 */
420 public function add_to_cart_html( $product ) {
421 if ( ! $product ) {
422 return;
423 }
424
425 $defaults = [
426 'quantity' => 1,
427 'class' => implode(
428 ' ',
429 array_filter(
430 [
431 'htcompare-cart-button button',
432 'product_type_' . $product->get_type(),
433 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
434 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
435 ]
436 )
437 ),
438 'attributes' => [
439 'data-product_id' => $product->get_id(),
440 'data-product_sku' => $product->get_sku(),
441 'aria-label' => $product->add_to_cart_description(),
442 ],
443 ];
444
445 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
446
447 if ( isset( $args['attributes']['aria-label'] ) ) {
448 $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
449 }
450
451 return apply_filters(
452 'woocommerce_loop_add_to_cart_link',
453 sprintf(
454 '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
455 esc_url( $product->add_to_cart_url() ),
456 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
457 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
458 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
459 esc_html( $product->add_to_cart_text() )
460 ),
461 $product,
462 $args
463 );
464 }
465 }
466