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' ) ) exit; // Exit if accessed directly
 14 
 15 /**
 16  * Get the count of notices added, either for all notices (default) or for one particular notice type specified
 17  * by $notice_type.
 18  *
 19  * @param  string $notice_type The name of the notice type - either error, success or notice. [optional]
 20  * @return int
 21  */
 22 function wc_notice_count( $notice_type = '' ) {
 23     $notice_count = 0;
 24     $all_notices  = WC()->session->get( 'wc_notices', array() );
 25 
 26     if ( isset( $all_notices[$notice_type] ) ) {
 27 
 28         $notice_count = absint( sizeof( $all_notices[$notice_type] ) );
 29 
 30     } elseif ( empty( $notice_type ) ) {
 31 
 32         foreach ( $all_notices as $notices ) {
 33             $notice_count += absint( sizeof( $all_notices ) );
 34         }
 35 
 36     }
 37 
 38     return $notice_count;
 39 }
 40 
 41 /**
 42  * See if a notice has already been added
 43  *
 44  * @param  string $message The text to display in the notice.
 45  * @param  string $notice_type The singular name of the notice type - either error, success or notice. [optional]
 46  * @return bool
 47  */
 48 function wc_has_notice( $message, $notice_type = 'success' ) {
 49     $notices = WC()->session->get( 'wc_notices', array() );
 50     $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array();
 51     return array_search( $message, $notices ) !== false;
 52 }
 53 
 54 /**
 55  * Add and store a notice
 56  *
 57  * @param  string $message The text to display in the notice.
 58  * @param  string $notice_type The singular name of the notice type - either error, success or notice. [optional]
 59  */
 60 function wc_add_notice( $message, $notice_type = 'success' ) {
 61 
 62     $notices = WC()->session->get( 'wc_notices', array() );
 63 
 64     // Backward compatibility
 65     if ( 'success' === $notice_type )
 66         $message = apply_filters( 'woocommerce_add_message', $message );
 67 
 68     $notices[$notice_type][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );
 69 
 70     WC()->session->set( 'wc_notices', $notices );
 71 }
 72 
 73 /**
 74  * Unset all notices
 75  *
 76  * @since 2.1
 77  */
 78 function wc_clear_notices() {
 79     WC()->session->set( 'wc_notices', null );
 80 }
 81 
 82 /**
 83  * Prints messages and errors which are stored in the session, then clears them.
 84  *
 85  * @since 2.1
 86  */
 87 function wc_print_notices() {
 88 
 89     $all_notices  = WC()->session->get( 'wc_notices', array() );
 90     $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
 91 
 92     foreach ( $notice_types as $notice_type ) {
 93         if ( wc_notice_count( $notice_type ) > 0 ) {
 94             wc_get_template( "notices/{$notice_type}.php", array(
 95                 'messages' => $all_notices[$notice_type]
 96             ) );
 97         }
 98     }
 99 
100     wc_clear_notices();
101 }
102 add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
103 add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
104 
105 /**
106  * Print a single notice immediately
107  *
108  * @param  string $message The text to display in the notice.
109  * @param  string $notice_type The singular name of the notice type - either error, success or notice. [optional]
110  */
111 function wc_print_notice( $message, $notice_type = 'success' ) {
112 
113     if ( 'success' === $notice_type )
114         $message = apply_filters( 'woocommerce_add_message', $message );
115 
116     wc_get_template( "notices/{$notice_type}.php", array(
117         'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) )
118     ) );
119 }
120 
121 /**
122  * Returns all queued notices, optionally filtered by a notice type.
123  * @param  string $notice_type The singular name of the notice type - either error, success or notice. [optional]
124  * @return array|mixed
125  */
126 function wc_get_notices( $notice_type = '' ) {
127 
128     $all_notices = WC()->session->get( 'wc_notices', array() );
129 
130     if ( empty ( $notice_type ) ) {
131         $notices = $all_notices;
132     } elseif ( isset( $all_notices[$notice_type] ) ) {
133         $notices = $all_notices[$notice_type];
134     } else {
135         $notices = array();
136     }
137 
138     return $notices;
139 }
140 
WooCommerce API documentation generated by ApiGen 2.8.0