/includes/wc-conditional-functions.php

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