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
|
|
|
/** |
18
|
|
|
* Get the count of notices added, either for all notices (default) or for one. |
19
|
|
|
* particular notice type specified by $notice_type. |
20
|
|
|
* |
21
|
|
|
* @since 2.1 |
22
|
|
|
* @param string $notice_type The name of the notice type - either error, success or notice. [optional] |
23
|
|
|
* @return int |
24
|
|
|
*/ |
25
|
|
|
function wc_notice_count( $notice_type = '' ) { |
26
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) { |
27
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$notice_count = 0; |
32
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() ); |
33
|
|
|
|
34
|
|
|
if ( isset( $all_notices[$notice_type] ) ) { |
35
|
|
|
|
36
|
|
|
$notice_count = absint( sizeof( $all_notices[$notice_type] ) ); |
37
|
|
|
|
38
|
|
|
} elseif ( empty( $notice_type ) ) { |
39
|
|
|
|
40
|
|
|
foreach ( $all_notices as $notices ) { |
|
|
|
|
41
|
|
|
$notice_count += absint( sizeof( $all_notices ) ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $notice_count; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check if a notice has already been added. |
51
|
|
|
* |
52
|
|
|
* @since 2.1 |
53
|
|
|
* @param string $message The text to display in the notice. |
54
|
|
|
* @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
function wc_has_notice( $message, $notice_type = 'success' ) { |
58
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) { |
59
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$notices = WC()->session->get( 'wc_notices', array() ); |
64
|
|
|
$notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array(); |
65
|
|
|
return array_search( $message, $notices ) !== false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Add and store a notice. |
70
|
|
|
* |
71
|
|
|
* @since 2.1 |
72
|
|
|
* @param string $message The text to display in the notice. |
73
|
|
|
* @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] |
74
|
|
|
*/ |
75
|
|
|
function wc_add_notice( $message, $notice_type = 'success' ) { |
76
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) { |
77
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$notices = WC()->session->get( 'wc_notices', array() ); |
82
|
|
|
|
83
|
|
|
// Backward compatibility |
84
|
|
|
if ( 'success' === $notice_type ) { |
85
|
|
|
$message = apply_filters( 'woocommerce_add_message', $message ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$notices[$notice_type][] = apply_filters( 'woocommerce_add_' . $notice_type, $message ); |
89
|
|
|
|
90
|
|
|
WC()->session->set( 'wc_notices', $notices ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Unset all notices. |
95
|
|
|
* |
96
|
|
|
* @since 2.1 |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
function wc_clear_notices() { |
|
|
|
|
99
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) { |
100
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
WC()->session->set( 'wc_notices', null ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Prints messages and errors which are stored in the session, then clears them. |
108
|
|
|
* |
109
|
|
|
* @since 2.1 |
110
|
|
|
*/ |
111
|
|
|
function wc_print_notices() { |
112
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) { |
113
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() ); |
118
|
|
|
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) ); |
119
|
|
|
|
120
|
|
|
foreach ( $notice_types as $notice_type ) { |
121
|
|
|
if ( wc_notice_count( $notice_type ) > 0 ) { |
122
|
|
|
wc_get_template( "notices/{$notice_type}.php", array( |
123
|
|
|
'messages' => array_filter( $all_notices[ $notice_type ] ) |
124
|
|
|
) ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
wc_clear_notices(); |
129
|
|
|
} |
130
|
|
|
add_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 ); |
131
|
|
|
add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 ); |
132
|
|
|
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 ); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Print a single notice immediately. |
136
|
|
|
* |
137
|
|
|
* @since 2.1 |
138
|
|
|
* @param string $message The text to display in the notice. |
139
|
|
|
* @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] |
140
|
|
|
*/ |
141
|
|
|
function wc_print_notice( $message, $notice_type = 'success' ) { |
142
|
|
|
if ( 'success' === $notice_type ) { |
143
|
|
|
$message = apply_filters( 'woocommerce_add_message', $message ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
wc_get_template( "notices/{$notice_type}.php", array( |
147
|
|
|
'messages' => array( apply_filters( 'woocommerce_add_' . $notice_type, $message ) ) |
148
|
|
|
) ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns all queued notices, optionally filtered by a notice type. |
153
|
|
|
* |
154
|
|
|
* @since 2.1 |
155
|
|
|
* @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] |
156
|
|
|
* @return array|mixed |
157
|
|
|
*/ |
158
|
|
|
function wc_get_notices( $notice_type = '' ) { |
159
|
|
|
if ( ! did_action( 'woocommerce_init' ) ) { |
160
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' ); |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$all_notices = WC()->session->get( 'wc_notices', array() ); |
165
|
|
|
|
166
|
|
|
if ( empty ( $notice_type ) ) { |
167
|
|
|
$notices = $all_notices; |
168
|
|
|
} elseif ( isset( $all_notices[$notice_type] ) ) { |
169
|
|
|
$notices = $all_notices[$notice_type]; |
170
|
|
|
} else { |
171
|
|
|
$notices = array(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $notices; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add notices for WP Errors. |
179
|
|
|
* @param WP_Error $errors |
180
|
|
|
*/ |
181
|
|
|
function wc_add_wp_error_notices( $errors ) { |
182
|
|
|
if ( is_wp_error( $errors ) && $errors->get_error_messages() ) { |
183
|
|
|
foreach ( $errors->get_error_messages() as $error ) { |
184
|
|
|
wc_add_notice( $error, 'error'); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.