Completed
Pull Request — master (#11372)
by Matty
08:38
created

wc-conditional-functions.php ➔ wc_is_webhook_valid_topic()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 11
nc 4
nop 1
dl 0
loc 21
rs 7.551
c 1
b 0
f 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
37
if ( ! function_exists( 'is_product_taxonomy' ) ) {
38
39
	/**
40
	 * is_product_taxonomy - Returns true when viewing a product taxonomy archive.
41
	 * @return bool
42
	 */
43
	function is_product_taxonomy() {
44
		return is_tax( get_object_taxonomies( 'product' ) );
45
	}
46
47
}
48
49
if ( ! function_exists( 'is_product_category' ) ) {
50
51
	/**
52
	 * is_product_category - Returns true when viewing a product category.
53
	 * @param  string $term (default: '') The term slug your checking for. Leave blank to return true on any.
54
	 * @return bool
55
	 */
56
	function is_product_category( $term = '' ) {
57
		return is_tax( 'product_cat', $term );
58
	}
59
60
}
61
62
if ( ! function_exists( 'is_product_tag' ) ) {
63
64
	/**
65
	 * is_product_tag - Returns true when viewing a product tag.
66
	 * @param  string $term (default: '') The term slug your checking for. Leave blank to return true on any.
67
	 * @return bool
68
	 */
69
	function is_product_tag( $term = '' ) {
70
		return is_tax( 'product_tag', $term );
71
	}
72
73
}
74
75
if ( ! function_exists( 'is_product' ) ) {
76
77
	/**
78
	 * is_product - Returns true when viewing a single product.
79
	 * @return bool
80
	 */
81
	function is_product() {
82
		return is_singular( array( 'product' ) );
83
	}
84
85
}
86
87
if ( ! function_exists( 'is_cart' ) ) {
88
89
	/**
90
	 * is_cart - Returns true when viewing the cart page.
91
	 * @return bool
92
	 */
93
	function is_cart() {
94
		return is_page( wc_get_page_id( 'cart' ) ) || defined( 'WOOCOMMERCE_CART' ) || wc_post_content_has_shortcode( 'woocommerce_cart' );
95
	}
96
97
}
98
99 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...
100
101
	/**
102
	 * is_checkout - Returns true when viewing the checkout page.
103
	 * @return bool
104
	 */
105
	function is_checkout() {
106
		return is_page( wc_get_page_id( 'checkout' ) ) || wc_post_content_has_shortcode( 'woocommerce_checkout' ) || apply_filters( 'woocommerce_is_checkout', false );
107
	}
108
109
}
110
111
if ( ! function_exists( 'is_checkout_pay_page' ) ) {
112
113
	/**
114
	 * is_checkout_pay - Returns true when viewing the checkout's pay page.
115
	 * @return bool
116
	 */
117
	function is_checkout_pay_page() {
118
		global $wp;
119
120
		return is_checkout() && ! empty( $wp->query_vars['order-pay'] );
121
	}
122
123
}
124
125
if ( ! function_exists( 'is_wc_endpoint_url' ) ) {
126
127
	/**
128
	 * is_wc_endpoint_url - Check if an endpoint is showing.
129
	 * @param  string $endpoint
130
	 * @return bool
131
	 */
132
	function is_wc_endpoint_url( $endpoint = false ) {
133
		global $wp;
134
135
		$wc_endpoints = WC()->query->get_query_vars();
136
137
		if ( $endpoint !== false ) {
138
			if ( ! isset( $wc_endpoints[ $endpoint ] ) ) {
139
				return false;
140
			} else {
141
				$endpoint_var = $wc_endpoints[ $endpoint ];
142
			}
143
144
			return isset( $wp->query_vars[ $endpoint_var ] );
145
		} else {
146
			foreach ( $wc_endpoints as $key => $value ) {
147
				if ( isset( $wp->query_vars[ $key ] ) ) {
148
					return true;
149
				}
150
			}
151
152
			return false;
153
		}
154
	}
155
156
}
157
158 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...
159
160
	/**
161
	 * is_account_page - Returns true when viewing an account page.
162
	 * @return bool
163
	 */
164
	function is_account_page() {
165
		return is_page( wc_get_page_id( 'myaccount' ) ) || wc_post_content_has_shortcode( 'woocommerce_my_account' ) || apply_filters( 'woocommerce_is_account_page', false );
166
	}
167
168
}
169
170 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...
171
172
	/**
173
	 * is_view_order_page - Returns true when on the view order page.
174
	 * @return bool
175
	 */
176
	function is_view_order_page() {
177
		global $wp;
178
179
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['view-order'] ) );
180
	}
181
182
}
183
184 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...
185
186
	/**
187
	 * Check for edit account page.
188
	 * Returns true when viewing the edit account page.
189
	 *
190
	 * @since 2.5.1
191
	 * @return bool
192
	 */
193
	function is_edit_account_page() {
194
		global $wp;
195
196
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['edit-account'] ) );
197
	}
198
199
}
200
201 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...
202
203
	/**
204
	 * is_order_received_page - Returns true when viewing the order received page.
205
	 * @return bool
206
	 */
207
	function is_order_received_page() {
208
		global $wp;
209
210
		return ( is_page( wc_get_page_id( 'checkout' ) ) && isset( $wp->query_vars['order-received'] ) );
211
	}
212
213
}
214
215 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...
216
217
	/**
218
	 * is_add_payment_method_page - Returns true when viewing the add payment method page.
219
	 * @return bool
220
	 */
221
	function is_add_payment_method_page() {
222
		global $wp;
223
224
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['add-payment-method'] ) );
225
	}
226
227
}
228
229 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...
230
231
	/**
232
	 * is_lost_password_page - Returns true when viewing the lost password page.
233
	 * @return bool
234
	 */
235
	function is_lost_password_page() {
236
		global $wp;
237
238
		return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['lost-password'] ) );
239
	}
240
241
}
242
243
if ( ! function_exists( 'is_ajax' ) ) {
244
245
	/**
246
	 * is_ajax - Returns true when the page is loaded via ajax.
247
	 * @return bool
248
	 */
249
	function is_ajax() {
250
		return defined( 'DOING_AJAX' );
251
	}
252
253
}
254
255
if ( ! function_exists( 'is_store_notice_showing' ) ) {
256
257
	/**
258
	 * is_store_notice_showing - Returns true when store notice is active.
259
	 * @return bool
260
	 */
261
	function is_store_notice_showing() {
262
		return 'no' !== get_option( 'woocommerce_demo_store' );
263
	}
264
265
}
266
267
if ( ! function_exists( 'is_filtered' ) ) {
268
269
	/**
270
	 * is_filtered - Returns true when filtering products using layered nav or price sliders.
271
	 * @return bool
272
	 */
273
	function is_filtered() {
274
		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'] ) ) );
275
	}
276
277
}
278
279
if ( ! function_exists( 'taxonomy_is_product_attribute' ) ) {
280
281
	/**
282
	 * Returns true when the passed taxonomy name is a product attribute.
283
	 * @uses   $wc_product_attributes global which stores taxonomy names upon registration
284
	 * @param  string $name of the attribute
285
	 * @return bool
286
	 */
287
	function taxonomy_is_product_attribute( $name ) {
288
		global $wc_product_attributes;
289
290
		return taxonomy_exists( $name ) && array_key_exists( $name, (array) $wc_product_attributes );
291
	}
292
293
}
294
295
if ( ! function_exists( 'meta_is_product_attribute' ) ) {
296
297
	/**
298
	 * Returns true when the passed meta name is a product attribute.
299
	 * @param  string $name       of the attribute
300
	 * @param  string $value
301
	 * @param  int    $product_id
302
	 * @return bool
303
	 */
304
	function meta_is_product_attribute( $name, $value, $product_id ) {
305
		$product = wc_get_product( $product_id );
306
307
		if ( $product && method_exists( $product, 'get_variation_attributes' ) ) {
308
			$variation_attributes = $product->get_variation_attributes();
309
			$attributes           = $product->get_attributes();
310
			return ( in_array( $name, array_keys( $attributes ) ) && in_array( $value, $variation_attributes[ $attributes[ $name ]['name'] ] ) );
311
		} else {
312
			return false;
313
		}
314
	}
315
316
}
317
318
if ( ! function_exists( 'wc_tax_enabled' ) ) {
319
320
	/**
321
	 * Are store-wide taxes enabled?
322
	 * @return bool
323
	 */
324
	function wc_tax_enabled() {
325
		return apply_filters( 'wc_tax_enabled', get_option( 'woocommerce_calc_taxes' ) === 'yes' );
326
	}
327
328
}
329
330
if ( ! function_exists( 'wc_shipping_enabled' ) ) {
331
332
	/**
333
	 * Is shipping enabled?
334
	 * @return bool
335
	 */
336
	function wc_shipping_enabled() {
337
		return apply_filters( 'wc_shipping_enabled', get_option( 'woocommerce_ship_to_countries' ) !== 'disabled' );
338
	}
339
340
}
341
342
if ( ! function_exists( 'wc_prices_include_tax' ) ) {
343
344
	/**
345
	 * Are prices inclusive of tax?
346
	 * @return bool
347
	 */
348
	function wc_prices_include_tax() {
349
		return wc_tax_enabled() && 'yes' === get_option( 'woocommerce_prices_include_tax' );
350
	}
351
352
}
353
354
/**
355
 * Check if the given topic is a valid webhook topic, a topic is valid if:
356
 *
357
 * + starts with `action.woocommerce_` or `action.wc_`.
358
 * + it has a valid resource & event.
359
 *
360
 * @param  string $topic webhook topic
361
 * @return bool          true if valid, false otherwise
362
 */
363
function wc_is_webhook_valid_topic( $topic ) {
364
	// Custom topics are prefixed with woocommerce_ or wc_ are valid
365
	if ( 0 === strpos( $topic, 'action.woocommerce_' ) || 0 === strpos( $topic, 'action.wc_' ) ) {
366
		return true;
367
	}
368
369
	@list( $resource, $event ) = explode( '.', $topic );
370
371
	if ( ! isset( $resource ) || ! isset( $event ) ) {
372
		return false;
373
	}
374
375
	$valid_resources = apply_filters( 'woocommerce_valid_webhook_resources', array( 'coupon', 'customer', 'order', 'product' ) );
376
	$valid_events    = apply_filters( 'woocommerce_valid_webhook_events', array( 'created', 'updated', 'deleted' ) );
377
378
	if ( in_array( $resource, $valid_resources ) && in_array( $event, $valid_events ) ) {
379
		return true;
380
	}
381
382
	return false;
383
}
384
385
/**
386
 * Simple check for validating a URL, it must start with http:// or https://.
387
 * and pass FILTER_VALIDATE_URL validation.
388
 * @param  string $url
389
 * @return bool
390
 */
391
function wc_is_valid_url( $url ) {
392
	// Must start with http:// or https://
393
	if ( 0 !== strpos( $url, 'http://' ) && 0 !== strpos( $url, 'https://' ) ) {
394
		return false;
395
	}
396
397
	// Must pass validation
398
	if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
399
		return false;
400
	}
401
402
	return true;
403
}
404
405
/**
406
 * Check if the home URL is https. If it is, we don't need to do things such as 'force ssl'.
407
 *
408
 * @since  2.4.13
409
 * @return bool
410
 */
411
function wc_site_is_https() {
412
	return false !== strstr( get_option( 'home' ), 'https:' );
413
}
414
415
/**
416
 * Check if the checkout is configured for https. Look at options, WP HTTPS plugin, or the permalink itself.
417
 *
418
 * @since  2.5.0
419
 * @return bool
420
 */
421
function wc_checkout_is_https() {
422
	return wc_site_is_https() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) || class_exists( 'WordPressHTTPS' ) || strstr( wc_get_page_permalink( 'checkout' ), 'https:' );
423
}
424
425
/**
426
 * Checks whether the content passed contains a specific short code.
427
 *
428
 * @param  string $tag Shortcode tag to check.
429
 * @return bool
430
 */
431
function wc_post_content_has_shortcode( $tag = '' ) {
432
	global $post;
433
434
	return is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, $tag );
435
}
436