Açıklama Yok

wc-conditional-functions.php 12KB

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