PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Framework / Admin_Message_Handler.php
woocommerce-square / includes / Framework Last commit date
Addresses 3 years ago Api 3 years ago Compatibility 3 years ago PaymentGateway 3 years ago Utilities 3 years ago Admin_Message_Handler.php 3 years ago Admin_Notice_Handler.php 3 years ago Lifecycle.php 3 years ago Plugin.php 3 years ago Plugin_Compatibility.php 3 years ago Plugin_Dependencies.php 3 years ago Square_Helper.php 4 years ago
Admin_Message_Handler.php
414 lines
1 <?php
2 /**
3 * WooCommerce Admin Message Handler
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@skyverge.com so we can send you a copy immediately.
12 *
13 * @since 3.0.0
14 * @author WooCommerce / SkyVerge
15 * @copyright Copyright (c) 2021-2022, WooCommerce.
16 * @copyright Copyright (c) 2013-2019, SkyVerge, Inc.
17 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
18 *
19 * Modified by WooCommerce on 01 December 2021.
20 */
21
22 namespace WooCommerce\Square\Framework;
23
24 defined( 'ABSPATH' ) or exit;
25
26 /**
27 * # WordPress Admin Message Handler Class
28 *
29 * This class provides a reusable wordpress admin messaging facility for setting
30 * and displaying messages and error messages across admin page requests without
31 * resorting to passing the messages as query vars.
32 *
33 * ## Usage
34 *
35 * To use simple instantiate the class then set one or more messages:
36 *
37 * `
38 * $admin_message_handler = new WP_Admin_Message_Handler( __FILE__ );
39 * $admin_message_handler->add_message( 'Hello World!' );
40 * `
41 *
42 * Then show the messages wherever you need, either with the built-in method
43 * or by writing your own:
44 *
45 * `$admin_message_handler->show_messages();`
46 */
47 class Admin_Message_Handler {
48
49
50 /** transient message prefix */
51 const MESSAGE_TRANSIENT_PREFIX = '_wp_admin_message_';
52
53 /** the message id GET name */
54 const MESSAGE_ID_GET_NAME = 'wpamhid';
55
56
57 /** @var string unique message identifier, defaults to __FILE__ unless otherwise set */
58 private $message_id;
59
60 /** @var array array of messages */
61 private $messages = array();
62
63 /** @var array array of error messages */
64 private $errors = array();
65
66 /** @var array array of warning messages */
67 private $warnings = array();
68
69 /** @var array array of info messages */
70 private $infos = array();
71
72
73 /**
74 * Construct and initialize the admin message handler class
75 *
76 * @since 3.0.0
77 * @param string $message_id optional message id. Best practice is to set
78 * this to a unique identifier based on the client plugin, such as __FILE__
79 */
80 public function __construct( $message_id = null ) {
81
82 $this->message_id = $message_id;
83
84 // load any available messages
85 $this->load_messages();
86
87 add_filter( 'wp_redirect', array( $this, 'redirect' ), 1, 2 );
88 }
89
90
91 /**
92 * Persist messages
93 *
94 * @since 3.0.0
95 * @return boolean true if any messages were set, false otherwise
96 */
97 public function set_messages() {
98
99 // any messages to persist?
100 if ( $this->message_count() > 0 || $this->info_count() > 0 || $this->warning_count() > 0 || $this->error_count() > 0 ) {
101
102 set_transient(
103 self::MESSAGE_TRANSIENT_PREFIX . $this->get_message_id(),
104 array(
105 'errors' => $this->errors,
106 'warnings' => $this->warnings,
107 'infos' => $this->infos,
108 'messages' => $this->messages,
109 ),
110 60 * 60
111 );
112
113 return true;
114 }
115
116 return false;
117 }
118
119
120 /**
121 * Loads messages
122 *
123 * @since 3.0.0
124 */
125 public function load_messages() {
126
127 // phpcs:ignore WordPress.Security.NonceVerification.Recommended - nonce is checked as part of $this->get_message_id()
128 if ( isset( $_GET[ self::MESSAGE_ID_GET_NAME ] ) && $this->get_message_id() == $_GET[ self::MESSAGE_ID_GET_NAME ] ) {
129
130 $memo = get_transient( self::MESSAGE_TRANSIENT_PREFIX . $_GET[ self::MESSAGE_ID_GET_NAME ] );
131
132 if ( isset( $memo['errors'] ) ) $this->errors = $memo['errors'];
133 if ( isset( $memo['warnings'] ) ) $this->warnings = $memo['warnings'];
134 if ( isset( $memo['infos'] ) ) $this->infos = $memo['infos'];
135 if ( isset( $memo['messages'] ) ) $this->messages = $memo['messages'];
136
137 $this->clear_messages( $_GET[ self::MESSAGE_ID_GET_NAME ] );
138 }
139 }
140
141
142 /**
143 * Clear messages and errors
144 *
145 * @since 3.0.0
146 * @param string $id the messages identifier
147 */
148 public function clear_messages( $id ) {
149 delete_transient( self::MESSAGE_TRANSIENT_PREFIX . $id );
150 }
151
152
153 /**
154 * Add an error message.
155 *
156 * @since 3.0.0
157 * @param string $error error message
158 */
159 public function add_error( $error ) {
160 $this->errors[] = $error;
161 }
162
163
164 /**
165 * Adds a warning message.
166 *
167 * @since 3.0.0
168 *
169 * @param string $message warning message to add
170 */
171 public function add_warning( $message ) {
172 $this->warnings[] = $message;
173 }
174
175 /**
176 * Add a message.
177 *
178 * @since 3.0.0
179 * @param string $message the message to add
180 */
181 public function add_message( $message ) {
182 $this->messages[] = $message;
183 }
184
185
186 /**
187 * Get error count.
188 *
189 * @since 3.0.0
190 * @return int error message count
191 */
192 public function error_count() {
193 return sizeof( $this->errors );
194 }
195
196
197 /**
198 * Gets the warning message count.
199 *
200 * @since 3.0.0
201 *
202 * @return int warning message count
203 */
204 public function warning_count() {
205 return sizeof( $this->warnings );
206 }
207
208
209 /**
210 * Gets the info message count.
211 *
212 * @since 3.0.0
213 *
214 * @return int info message count
215 */
216 public function info_count() {
217 return sizeof( $this->infos );
218 }
219
220
221 /**
222 * Get message count.
223 *
224 * @since 3.0.0
225 * @return int message count
226 */
227 public function message_count() {
228 return sizeof( $this->messages );
229 }
230
231
232 /**
233 * Get error messages
234 *
235 * @since 3.0.0
236 * @return array of error message strings
237 */
238 public function get_errors() {
239 return $this->errors;
240 }
241
242
243 /**
244 * Get an error message
245 *
246 * @since 3.0.0
247 * @param int $index the error index
248 * @return string the error message
249 */
250 public function get_error( $index ) {
251 return isset( $this->errors[ $index ] ) ? $this->errors[ $index ] : '';
252 }
253
254
255 /**
256 * Gets all warning messages.
257 *
258 * @since 3.0.0
259 *
260 * @return array
261 */
262 public function get_warnings() {
263 return $this->warnings;
264 }
265
266
267 /**
268 * Gets a specific warning message.
269 *
270 * @since 3.0.0
271 *
272 * @param int $index warning message index
273 * @return string
274 */
275 public function get_warning( $index ) {
276 return isset( $this->warnings[ $index ] ) ? $this->warnings[ $index ] : '';
277 }
278
279
280 /**
281 * Gets all info messages.
282 *
283 * @since 3.0.0
284 *
285 * @return array
286 */
287 public function get_infos() {
288 return $this->infos;
289 }
290
291
292 /**
293 * Gets a specific info message.
294 *
295 * @since 3.0.0
296 *
297 * @param int $index info message index
298 * @return string
299 */
300 public function get_info( $index ) {
301 return isset( $this->infos[ $index ] ) ? $this->infos[ $index ] : '';
302 }
303
304
305 /**
306 * Get messages
307 *
308 * @since 3.0.0
309 * @return array of message strings
310 */
311 public function get_messages() {
312 return $this->messages;
313 }
314
315
316 /**
317 * Get a message
318 *
319 * @since 3.0.0
320 * @param int $index the message index
321 * @return string the message
322 */
323 public function get_message( $index ) {
324 return isset( $this->messages[ $index ] ) ? $this->messages[ $index ] : '';
325 }
326
327
328 /**
329 * Render the errors and messages.
330 *
331 * @since 3.0.0
332 * @param array $params {
333 * Optional parameters.
334 *
335 * @type array $capabilities Any user capabilities to check if the user is allowed to view the messages,
336 * default: `manage_woocommerce`
337 * }
338 */
339 public function show_messages( $params = array() ) {
340
341 $params = wp_parse_args( $params, array(
342 'capabilities' => array(
343 'manage_woocommerce',
344 ),
345 ) );
346
347 $check_user_capabilities = array();
348
349 // check if user has at least one capability that allows to see messages
350 foreach ( $params['capabilities'] as $capability ) {
351 $check_user_capabilities[] = current_user_can( $capability );
352 }
353
354 // bail out if user has no minimum capabilities to see messages
355 if ( ! in_array( true, $check_user_capabilities, true ) ) {
356 return;
357 }
358
359 $output = '';
360
361 if ( $this->error_count() > 0 ) {
362 $output .= '<div id="wp-admin-message-handler-error" class="notice-error notice"><ul><li><strong>' . implode( '</strong></li><li><strong>', $this->get_errors() ) . '</strong></li></ul></div>';
363 }
364
365 if ( $this->warning_count() > 0 ) {
366 $output .= '<div id="wp-admin-message-handler-warning" class="notice-warning notice"><ul><li><strong>' . implode( '</strong></li><li><strong>', $this->get_warnings() ) . '</strong></li></ul></div>';
367 }
368
369 if ( $this->info_count() > 0 ) {
370 $output .= '<div id="wp-admin-message-handler-warning" class="notice-info notice"><ul><li><strong>' . implode( '</strong></li><li><strong>', $this->get_infos() ) . '</strong></li></ul></div>';
371 }
372
373 if ( $this->message_count() > 0 ) {
374 $output .= '<div id="wp-admin-message-handler-message" class="notice-success notice"><ul><li><strong>' . implode( '</strong></li><li><strong>', $this->get_messages() ) . '</strong></li></ul></div>';
375 }
376
377 echo wp_kses_post( $output );
378 }
379
380
381 /**
382 * Redirection hook which persists messages into session data.
383 *
384 * @since 3.0.0
385 * @param string $location the URL to redirect to
386 * @param int $status the http status
387 * @return string the URL to redirect to
388 */
389 public function redirect( $location, $status ) {
390
391 // add the admin message id param to the
392 if ( $this->set_messages() ) {
393 $location = add_query_arg( self::MESSAGE_ID_GET_NAME, rawurlencode( $this->get_message_id() ), $location );
394 }
395
396 return $location;
397 }
398
399
400 /**
401 * Generate a unique id to identify the messages
402 *
403 * @since 3.0.0
404 * @return string unique identifier
405 */
406 protected function get_message_id() {
407
408 if ( ! isset( $this->message_id ) ) $this->message_id = __FILE__;
409
410 return wp_create_nonce( $this->message_id );
411
412 }
413 }
414