|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Context Cards Service |
|
4
|
|
|
* |
|
5
|
|
|
* @since 3.22.0 |
|
6
|
|
|
* @package Wordlift |
|
7
|
|
|
* @subpackage Wordlift/public |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
class Wordlift_Context_Cards_Service { |
|
11
|
|
|
|
|
12
|
|
|
public function enqueue_scripts() { |
|
13
|
|
|
|
|
14
|
|
|
$show_context_cards = apply_filters( 'wl_context_cards_show', true ); |
|
15
|
|
|
$context_cards_base_url = apply_filters( 'wl_context_cards_base_url', get_rest_url( null, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/jsonld' ) ); |
|
16
|
|
|
|
|
17
|
|
|
/* |
|
18
|
|
|
* Do not load wordlift-cloud on a non-static home page |
|
19
|
|
|
* |
|
20
|
|
|
* @since 3.27.4 |
|
21
|
|
|
*/ |
|
22
|
|
|
if ( is_front_page() && is_home() ) { |
|
23
|
|
|
// Default homepage - force hide |
|
24
|
|
|
$show_context_cards = false; |
|
25
|
|
|
} elseif ( is_front_page() ) { |
|
26
|
|
|
// Static homepage - do nothing |
|
27
|
|
|
} elseif ( is_home() ) { |
|
28
|
|
|
// Blog page - force hide |
|
29
|
|
|
$show_context_cards = false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ( $show_context_cards ) { |
|
33
|
|
|
wp_enqueue_script( 'wordlift-cloud' ); |
|
34
|
|
|
wp_localize_script( 'wordlift-cloud', '_wlCloudSettings', array( |
|
35
|
|
|
'selector' => 'a.wl-entity-page-link', |
|
36
|
|
|
'url' => $context_cards_base_url |
|
37
|
|
|
) ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
add_filter( 'wl_anchor_data_attributes', array( $this, 'anchor_data_attributes' ), 10, 2 ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function anchor_data_attributes( $attributes, $post_id ) { |
|
44
|
|
|
|
|
45
|
|
|
$supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
46
|
|
|
$post_type = get_post_type( $post_id ); |
|
47
|
|
|
$enabled_templates = apply_filters( 'wl_context_cards_enabled_templates', array( 'product' ) ); |
|
48
|
|
|
|
|
49
|
|
|
if ( in_array( $post_type, $supported_types ) && in_array( $post_type, $enabled_templates ) ) { |
|
50
|
|
|
|
|
51
|
|
|
$additional_attributes = array( 'post-type-template' => $post_type ); |
|
52
|
|
|
|
|
53
|
|
|
switch($post_type){ |
|
54
|
|
|
case 'product': |
|
55
|
|
|
$product = wc_get_product( $post_id ); |
|
56
|
|
|
$additional_attributes['template-payload'] = json_encode( |
|
57
|
|
|
array( |
|
58
|
|
|
'regular_price' => $product->get_regular_price(), |
|
59
|
|
|
'currency_symbol' => get_woocommerce_currency_symbol(), |
|
60
|
|
|
'discount_pc' => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0, |
|
61
|
|
|
'average_rating' => $product->get_average_rating(), |
|
62
|
|
|
'rating_count' => $product->get_rating_count(), |
|
63
|
|
|
'rating_html' => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ) |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $attributes + $additional_attributes; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $attributes; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|