/includes/wc-notice-functions.php

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