wc-conditional-functions.php 10 KB

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