PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/wc-conditional-functions.php

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