1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* WooCommerce Product Embed. |
4
|
|
|
* |
5
|
|
|
* @version 2.4.11 |
6
|
|
|
* @package WooCommerce/Classes/Embed |
7
|
|
|
* @category Class |
8
|
|
|
* @author WooThemes |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
12
|
|
|
exit; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Embed Class which handles any WooCommerce Products that are embedded on this site or another site. |
17
|
|
|
* |
18
|
|
|
* @class WC_Embed |
19
|
|
|
* @version 2.4.11 |
20
|
|
|
* @author WooThemes |
21
|
|
|
*/ |
22
|
|
|
class WC_Embed { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Init embed class. |
26
|
|
|
* |
27
|
|
|
* @since 2.4.11 |
28
|
|
|
*/ |
29
|
|
|
public static function init() { |
30
|
|
|
|
31
|
|
|
// Filter all of the content that's going to be embedded. |
32
|
|
|
add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 ); |
33
|
|
|
|
34
|
|
|
// Make sure no comments display. Doesn't make sense for products. |
35
|
|
|
add_action( 'embed_content_meta', array( __CLASS__, 'remove_comments_button' ), 5 ); |
36
|
|
|
|
37
|
|
|
// In the comments place let's display the product rating. |
38
|
|
|
add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 ); |
39
|
|
|
|
40
|
|
|
// Add some basic styles. |
41
|
|
|
add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Remove comments button on product embeds. |
46
|
|
|
* |
47
|
|
|
* @since 2.5.6 |
48
|
|
|
*/ |
49
|
|
|
public static function remove_comments_button() { |
50
|
|
|
if ( self::is_embedded_product() ) { |
51
|
|
|
remove_action( 'embed_content_meta', 'print_embed_comments_button' ); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check if this is an embedded product - to make sure we don't mess up regular posts. |
57
|
|
|
* |
58
|
|
|
* @since 2.4.11 |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public static function is_embedded_product() { |
62
|
|
|
if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create the excerpt for embedded products - we want to add the buy button to it. |
70
|
|
|
* |
71
|
|
|
* @since 2.4.11 |
72
|
|
|
* @param string $excerpt Embed short description. |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public static function the_excerpt( $excerpt ) { |
76
|
|
|
global $post; |
77
|
|
|
|
78
|
|
|
// Get product. |
79
|
|
|
$_product = wc_get_product( get_the_ID() ); |
80
|
|
|
|
81
|
|
|
// Make sure we're only affecting embedded products. |
82
|
|
|
if ( self::is_embedded_product() ) { |
83
|
|
|
echo '<p><span class="wc-embed-price">' . $_product->get_price_html() . '</span></p>'; |
84
|
|
|
|
85
|
|
|
if ( ! empty( $post->post_excerpt ) ) { |
86
|
|
|
ob_start(); |
87
|
|
|
woocommerce_template_single_excerpt(); |
88
|
|
|
$excerpt = ob_get_clean(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Add the button. |
92
|
|
|
$excerpt .= self::product_buttons(); |
93
|
|
|
} |
94
|
|
|
return $excerpt; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create the button to go to the product page for embedded products. |
99
|
|
|
* |
100
|
|
|
* @since 2.4.11 |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public static function product_buttons() { |
104
|
|
|
$_product = wc_get_product( get_the_ID() ); |
105
|
|
|
$buttons = array(); |
106
|
|
|
$button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>'; |
107
|
|
|
|
108
|
|
|
if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) { |
109
|
|
|
$buttons[] = sprintf( $button, esc_url( add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ) ), esc_html__( 'Buy Now', 'woocommerce' ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$buttons[] = sprintf( $button, get_the_permalink(), esc_html__( 'Read More', 'woocommerce' ) ); |
113
|
|
|
|
114
|
|
|
return '<p>' . implode( ' ', $buttons ) . '</p>'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Prints the markup for the rating stars. |
119
|
|
|
* |
120
|
|
|
* @since 2.4.11 |
121
|
|
|
*/ |
122
|
|
|
public static function get_ratings() { |
123
|
|
|
// Make sure we're only affecting embedded products. |
124
|
|
|
if ( self::is_embedded_product() && ( $_product = wc_get_product( get_the_ID() ) ) && $_product->get_average_rating() > 0 ) { |
125
|
|
|
?> |
126
|
|
|
<div class="wc-embed-rating"> |
127
|
|
|
<?php echo esc_html( sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $_product->get_average_rating() ) ); ?> |
128
|
|
|
</div> |
129
|
|
|
<?php |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Basic styling. |
135
|
|
|
*/ |
136
|
|
|
public static function print_embed_styles() { |
137
|
|
|
if ( ! self::is_embedded_product() ) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
?> |
141
|
|
|
<style type="text/css"> |
142
|
|
|
a.wc-embed-button { |
143
|
|
|
border-radius: 4px; |
144
|
|
|
border: 1px solid #ddd; |
145
|
|
|
box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.05); |
146
|
|
|
display:inline-block; |
147
|
|
|
padding: .5em; |
148
|
|
|
} |
149
|
|
|
a.wc-embed-button:hover, a.wc-embed-button:focus { |
150
|
|
|
border: 1px solid #ccc; |
151
|
|
|
box-shadow: 0px 1px 0 0px rgba(0, 0, 0, 0.1); |
152
|
|
|
color: #999; |
153
|
|
|
text-decoration: none; |
154
|
|
|
} |
155
|
|
|
.wp-embed-excerpt p { |
156
|
|
|
margin: 0 0 1em; |
157
|
|
|
} |
158
|
|
|
.wc-embed-price { |
159
|
|
|
display: block; |
160
|
|
|
opacity: .75; |
161
|
|
|
font-weight: 700; |
162
|
|
|
margin-top: -.75em; |
163
|
|
|
} |
164
|
|
|
.wc-embed-rating { |
165
|
|
|
display: inline-block; |
166
|
|
|
} |
167
|
|
|
</style> |
168
|
|
|
<?php |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
WC_Embed::init(); |
173
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.