wc-conditional-functions.php ➔ is_product_taxonomy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * WooCommerce Conditional Functions
4
 *
5
 * Functions for determining the current query/page.
6
 *
7
 * @author      WooThemes
8
 * @category    Core
9
 * @package     WooCommerce/Functions
10
 * @version     2.3.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * is_woocommerce - Returns true if on a page which uses WooCommerce templates (cart and checkout are standard pages with shortcodes and thus are not included).
19
 * @return bool
20
 */
21
function is_woocommerce() {
22
	return apply_filters( 'is_woocommerce', ( is_shop() || is_product_taxonomy() || is_product() ) ? true : false );
23
}
24
25
if ( ! function_exists( 'is_shop' ) ) {
26
27
	/**
28
	 * is_shop - Returns true when viewing the product type archive (shop).
29
	 * @return bool
30
	 */
31
	function is_shop() {
32
		return ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) );
33
	}
34
}
35
36
if ( ! function_exists( 'is_product_taxonomy' ) ) {
37
38
	/**
39
	 * is_product_taxonomy - Returns true when viewing a product taxonomy archive.
40
	 * @return bool
41
	 */
42
	function is_product_taxonomy() {
43
		return is_tax( get_object_taxonomies( 'product' ) );
44
	}
45
}
46
47
if ( ! function_exists( 'is_product_category' ) ) {
48
49
	/**
50
	 * is_product_category - Returns true when viewing a product category.
51
	 * @param  string $term (default: '') The term slug your checking for. Leave blank to return true on any.
52
	 * @return bool
53
	 */
54
	function is_product_category( $term = '' ) {
55
		return is_tax( 'product_cat', $term );
56
	}
57
}
58
59
if ( ! function_exists( 'is_product_tag' ) ) {
60
61
	/**
62
	 * is_product_tag - Returns true when viewing a product tag.
63
	 * @param  string $term (default: '') The term slug your checking for. Leave blank to return true on any.
64
	 * @return bool
65
	 */
66
	function is_product_tag( $term = '' ) {
67
		return is_tax( 'product_tag', $term );
68
	}
69
}
70
71
if ( ! function_exists( 'is_product' ) ) {
72
73
	/**
74
	 * is_product - Returns true when viewing a single product.
75
	 * @return bool
76
	 */
77
	function is_product() {
78
		return is_singular( array( 'product' ) );
79
	}
80
}
81
82
if ( ! function_exists( 'is_cart' ) ) {
83
84
	/**
85
	 * is_cart - Returns true when viewing the cart page.
86
	 * @return bool
87
	 */
88
	function is_cart() {
89
		return is_page( wc_get_page_id( 'cart' ) ) || defined( 'WOOCOMMERCE_CART' ) || wc_post_content_has_shortcode( 'woocommerce_cart' );
90
	}
91
}
92
93 View Code Duplication
if ( ! function_exists( 'is_checkout' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
95
	/**
96
	 * is_checkout - Returns true when viewing the checkout page.
97
	 * @return bool
98
	 */
99
	function is_checkout() {
100
		return is_page( wc_get_page_id( 'checkout' ) ) || wc_post_content_has_shortcode( 'woocommerce_checkout' ) || apply_filters( 'woocommerce_is_checkout', false );
101
	}
102
}
103
104
if ( ! function_exists( 'is_checkout_pay_page' ) ) {
105
106
	/**
107
	 * is_checkout_pay - Returns true when viewing the checkout's pay page.
108
	 * @return bool
109
	 */
110
	function is_checkout_pay_page() {
111
		global $wp;
112
113
		return is_checkout() && ! empty( $wp->query_vars['order-pay'] );
114
	}
115
}
116
117
if ( ! function_exists( 'is_wc_endpoint_url' ) ) {
118
119
	/**
120
	 * is_wc_endpoint_url - Check if an endpoint is showing.
121
	 * @param  string $endpoint
122
	 * @return bool
123
	 */
124
	function is_wc_endpoint_url( $endpoint = false ) {
125
		global $wp;
126
127
		$wc_endpoints = WC()->query->get_query_vars();
128
129
		if ( $endpoint !== false ) {
130
			if ( ! isset( $wc_endpoints[ $endpoint ] ) ) {
131
				return false;
132
			} else {
133
				$endpoint_var = $wc_endpoints[ $endpoint ];
134
			}
135
136
			return isset( $wp->query_vars[ $endpoint_var ] );
137
		} else {
138
			foreach ( $wc_endpoints as $key => $value ) {
139
				if ( isset( $wp->query_vars[ $key ] ) ) {
140
					return true;
141
				}
142
			}
143
144
			return false;
145
		}
146
	}
147
}
148
149 View Code Duplication
if ( ! function_exists( 'is_account_page' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
151
	/**
152
	 * is_account_page - Returns true when viewing an account page.
153
	 * @return bool
154
	 */
155
	function is_account_page() {
156
		return is_page( wc_get_page_id( 'myaccount' ) ) || wc_post_content_has_shortcode( 'woocommerce_my_account' ) || apply_filters( 'woocommerce_is_account_page', false );
157
	}
158
}
159
160 View Code Duplication
if ( ! function_exists( 'is_view_order_page' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
162
	/**
163
	* is_view_order_page - Returns true when on the view order page.
164
	* @return bool
165
	*/
166
	function is_view_order_page() {
167
		global $wp;
168
169
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['view-order'] ) );
170
	}
171
}
172
173 View Code Duplication
if ( ! function_exists( 'is_edit_account_page' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
175
	/**
176
	* Check for edit account page.
177
	* Returns true when viewing the edit account page.
178
	*
179
	* @since 2.5.1
180
	* @return bool
181
	*/
182
	function is_edit_account_page() {
183
		global $wp;
184
185
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['edit-account'] ) );
186
	}
187
}
188
189 View Code Duplication
if ( ! function_exists( 'is_order_received_page' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
191
	/**
192
	* is_order_received_page - Returns true when viewing the order received page.
193
	* @return bool
194
	*/
195
	function is_order_received_page() {
196
		global $wp;
197
198
		return ( is_page( wc_get_page_id( 'checkout' ) ) && isset( $wp->query_vars['order-received'] ) );
199
	}
200
}
201
202 View Code Duplication
if ( ! function_exists( 'is_add_payment_method_page' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
204
	/**
205
	* is_add_payment_method_page - Returns true when viewing the add payment method page.
206
	* @return bool
207
	*/
208
	function is_add_payment_method_page() {
209
		global $wp;
210
211
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['add-payment-method'] ) );
212
	}
213
}
214
215 View Code Duplication
if ( ! function_exists( 'is_lost_password_page' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
217
	/**
218
	* is_lost_password_page - Returns true when viewing the lost password page.
219
	* @return bool
220
	*/
221
	function is_lost_password_page() {
222
		global $wp;
223
224
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['lost-password'] ) );
225
	}
226
}
227
228
if ( ! function_exists( 'is_ajax' ) ) {
229
230
	/**
231
	 * is_ajax - Returns true when the page is loaded via ajax.
232
	 * @return bool
233
	 */
234
	function is_ajax() {
235
		return defined( 'DOING_AJAX' );
236
	}
237
}
238
239
if ( ! function_exists( 'is_store_notice_showing' ) ) {
240
241
	/**
242
	 * is_store_notice_showing - Returns true when store notice is active.
243
	 * @return bool
244
	 */
245
	function is_store_notice_showing() {
246
		return 'no' !== get_option( 'woocommerce_demo_store' );
247
	}
248
}
249
250
if ( ! function_exists( 'is_filtered' ) ) {
251
252
	/**
253
	 * is_filtered - Returns true when filtering products using layered nav or price sliders.
254
	 * @return bool
255
	 */
256
	function is_filtered() {
257
		return apply_filters( 'woocommerce_is_filtered', ( sizeof( WC_Query::get_layered_nav_chosen_attributes() ) > 0 || isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) || isset( $_GET['min_rating'] ) ) );
258
	}
259
}
260
261
if ( ! function_exists( 'taxonomy_is_product_attribute' ) ) {
262
263
	/**
264
	 * Returns true when the passed taxonomy name is a product attribute.
265
	 * @uses   $wc_product_attributes global which stores taxonomy names upon registration
266
	 * @param  string $name of the attribute
267
	 * @return bool
268
	 */
269
	function taxonomy_is_product_attribute( $name ) {
270
		global $wc_product_attributes;
271
272
		return taxonomy_exists( $name ) && array_key_exists( $name, (array) $wc_product_attributes );
273
	}
274
}
275
276
if ( ! function_exists( 'meta_is_product_attribute' ) ) {
277
278
	/**
279
	 * Returns true when the passed meta name is a product attribute.
280
	 * @param  string $name of the attribute
281
	 * @param  string $value
282
	 * @param  int $product_id
283
	 * @return bool
284
	 */
285
	function meta_is_product_attribute( $name, $value, $product_id ) {
286
		$product = wc_get_product( $product_id );
287
288
		if ( $product && method_exists( $product, 'get_variation_attributes' ) ) {
289
			$variation_attributes = $product->get_variation_attributes();
290
			$attributes           = $product->get_attributes();
291
			return ( in_array( $name, array_keys( $attributes ) ) && in_array( $value, $variation_attributes[ $attributes[ $name ]['name'] ] ) );
292
		} else {
293
			return false;
294
		}
295
	}
296
}
297
298
if ( ! function_exists( 'wc_tax_enabled' ) ) {
299
300
	/**
301
	 * Are store-wide taxes enabled?
302
	 * @return bool
303
	 */
304
	function wc_tax_enabled() {
305
		return apply_filters( 'wc_tax_enabled', get_option( 'woocommerce_calc_taxes' ) === 'yes' );
306
	}
307
}
308
309
if ( ! function_exists( 'wc_shipping_enabled' ) ) {
310
311
	/**
312
	 * Is shipping enabled?
313
	 * @return bool
314
	 */
315
	function wc_shipping_enabled() {
316
		return apply_filters( 'wc_shipping_enabled', get_option( 'woocommerce_ship_to_countries' ) !== 'disabled' );
317
	}
318
}
319
320
if ( ! function_exists( 'wc_prices_include_tax' ) ) {
321
322
	/**
323
	 * Are prices inclusive of tax?
324
	 * @return bool
325
	 */
326
	function wc_prices_include_tax() {
327
		return wc_tax_enabled() && 'yes' === get_option( 'woocommerce_prices_include_tax' );
328
	}
329
}
330
331
/**
332
 * Check if the given topic is a valid webhook topic, a topic is valid if:
333
 *
334
 * + starts with `action.woocommerce_` or `action.wc_`.
335
 * + it has a valid resource & event.
336
 *
337
 * @param  string $topic webhook topic
338
 * @return bool true if valid, false otherwise
339
 */
340
function wc_is_webhook_valid_topic( $topic ) {
341
342
	// Custom topics are prefixed with woocommerce_ or wc_ are valid
343
	if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) {
344
		return true;
345
	}
346
347
	@list( $resource, $event ) = explode( '.', $topic );
348
349
	if ( ! isset( $resource ) || ! isset( $event ) ) {
350
		return false;
351
	}
352
353
	$valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) );
354
	$valid_events    = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted' ) );
355
356
	if ( in_array( $resource, $valid_resources ) && in_array( $event, $valid_events ) ) {
357
		return true;
358
	}
359
360
	return false;
361
}
362
363
/**
364
 * Simple check for validating a URL, it must start with http:// or https://.
365
 * and pass FILTER_VALIDATE_URL validation.
366
 * @param  string $url
367
 * @return bool
368
 */
369
function wc_is_valid_url( $url ) {
370
371
	// Must start with http:// or https://
372
	if ( 0 !== strpos( $url, 'http://' ) && 0 !== strpos( $url, 'https://' ) ) {
373
		return false;
374
	}
375
376
	// Must pass validation
377
	if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
378
		return false;
379
	}
380
381
	return true;
382
}
383
384
/**
385
 * Check if the home URL is https. If it is, we don't need to do things such as 'force ssl'.
386
 *
387
 * @since  2.4.13
388
 * @return bool
389
 */
390
function wc_site_is_https() {
391
	return false !== strstr( get_option( 'home' ), 'https:' );
392
}
393
394
/**
395
 * Check if the checkout is configured for https. Look at options, WP HTTPS plugin, or the permalink itself.
396
 *
397
 * @since  2.5.0
398
 * @return bool
399
 */
400
function wc_checkout_is_https() {
401
	return wc_site_is_https() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) || class_exists( 'WordPressHTTPS' ) || strstr( wc_get_page_permalink( 'checkout' ), 'https:' );
402
}
403
404
/**
405
 * Checks whether the content passed contains a specific short code.
406
 *
407
 * @param  string $tag Shortcode tag to check.
408
 * @return bool
409
 */
410
function wc_post_content_has_shortcode( $tag = '' ) {
411
	global $post;
412
413
	return is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
414
}
415