wc-notice-functions.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * WooCommerce Message Functions
  4. *
  5. * Functions for error/message handling and display.
  6. *
  7. * @package WooCommerce/Functions
  8. * @version 2.1.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * Get the count of notices added, either for all notices (default) or for one.
  15. * particular notice type specified by $notice_type.
  16. *
  17. * @since 2.1
  18. * @param string $notice_type Optional. The name of the notice type - either error, success or notice.
  19. * @return int
  20. */
  21. function wc_notice_count( $notice_type = '' ) {
  22. if ( ! did_action( 'woocommerce_init' ) ) {
  23. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  24. return;
  25. }
  26. $notice_count = 0;
  27. $all_notices = WC()->session->get( 'wc_notices', array() );
  28. if ( isset( $all_notices[ $notice_type ] ) ) {
  29. $notice_count = count( $all_notices[ $notice_type ] );
  30. } elseif ( empty( $notice_type ) ) {
  31. foreach ( $all_notices as $notices ) {
  32. $notice_count += count( $notices );
  33. }
  34. }
  35. return $notice_count;
  36. }
  37. /**
  38. * Check if a notice has already been added.
  39. *
  40. * @since 2.1
  41. * @param string $message The text to display in the notice.
  42. * @param string $notice_type Optional. The name of the notice type - either error, success or notice.
  43. * @return bool
  44. */
  45. function wc_has_notice( $message, $notice_type = 'success' ) {
  46. if ( ! did_action( 'woocommerce_init' ) ) {
  47. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  48. return false;
  49. }
  50. $notices = WC()->session->get( 'wc_notices', array() );
  51. $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array();
  52. return array_search( $message, $notices, true ) !== false;
  53. }
  54. /**
  55. * Add and store a notice.
  56. *
  57. * @since 2.1
  58. * @param string $message The text to display in the notice.
  59. * @param string $notice_type Optional. The name of the notice type - either error, success or notice.
  60. */
  61. function wc_add_notice( $message, $notice_type = 'success' ) {
  62. if ( ! did_action( 'woocommerce_init' ) ) {
  63. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  64. return;
  65. }
  66. $notices = WC()->session->get( 'wc_notices', array() );
  67. // Backward compatibility.
  68. if ( 'success' === $notice_type ) {
  69. $message = apply_filters( 'woocommerce_add_message', $message );
  70. }
  71. $notices[ $notice_type ][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );
  72. WC()->session->set( 'wc_notices', $notices );
  73. }
  74. /**
  75. * Set all notices at once.
  76. *
  77. * @since 2.6.0
  78. * @param mixed $notices Array of notices.
  79. */
  80. function wc_set_notices( $notices ) {
  81. if ( ! did_action( 'woocommerce_init' ) ) {
  82. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.6' );
  83. return;
  84. }
  85. WC()->session->set( 'wc_notices', $notices );
  86. }
  87. /**
  88. * Unset all notices.
  89. *
  90. * @since 2.1
  91. */
  92. function wc_clear_notices() {
  93. if ( ! did_action( 'woocommerce_init' ) ) {
  94. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  95. return;
  96. }
  97. WC()->session->set( 'wc_notices', null );
  98. }
  99. /**
  100. * Prints messages and errors which are stored in the session, then clears them.
  101. *
  102. * @since 2.1
  103. */
  104. function wc_print_notices() {
  105. if ( ! did_action( 'woocommerce_init' ) ) {
  106. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  107. return;
  108. }
  109. $all_notices = WC()->session->get( 'wc_notices', array() );
  110. $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
  111. foreach ( $notice_types as $notice_type ) {
  112. if ( wc_notice_count( $notice_type ) > 0 ) {
  113. wc_get_template( "notices/{$notice_type}.php", array(
  114. 'messages' => array_filter( $all_notices[ $notice_type ] ),
  115. ) );
  116. }
  117. }
  118. wc_clear_notices();
  119. }
  120. add_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 );
  121. add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
  122. add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
  123. /**
  124. * Print a single notice immediately.
  125. *
  126. * @since 2.1
  127. * @param string $message The text to display in the notice.
  128. * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice.
  129. */
  130. function wc_print_notice( $message, $notice_type = 'success' ) {
  131. if ( 'success' === $notice_type ) {
  132. $message = apply_filters( 'woocommerce_add_message', $message );
  133. }
  134. wc_get_template( "notices/{$notice_type}.php", array(
  135. 'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) ),
  136. ) );
  137. }
  138. /**
  139. * Returns all queued notices, optionally filtered by a notice type.
  140. *
  141. * @since 2.1
  142. * @param string $notice_type Optional. The singular name of the notice type - either error, success or notice.
  143. * @return array|mixed
  144. */
  145. function wc_get_notices( $notice_type = '' ) {
  146. if ( ! did_action( 'woocommerce_init' ) ) {
  147. wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
  148. return;
  149. }
  150. $all_notices = WC()->session->get( 'wc_notices', array() );
  151. if ( empty( $notice_type ) ) {
  152. $notices = $all_notices;
  153. } elseif ( isset( $all_notices[ $notice_type ] ) ) {
  154. $notices = $all_notices[ $notice_type ];
  155. } else {
  156. $notices = array();
  157. }
  158. return $notices;
  159. }
  160. /**
  161. * Add notices for WP Errors.
  162. *
  163. * @param WP_Error $errors Errors.
  164. */
  165. function wc_add_wp_error_notices( $errors ) {
  166. if ( is_wp_error( $errors ) && $errors->get_error_messages() ) {
  167. foreach ( $errors->get_error_messages() as $error ) {
  168. wc_add_notice( $error, 'error' );
  169. }
  170. }
  171. }