|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
7
|
|
|
use WP_Error; |
|
8
|
|
|
|
|
9
|
|
|
class Notice |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param string $type |
|
13
|
|
|
* @param string|array|WP_Error $message |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
|
|
public function add( $type, $message, array $args = [] ) |
|
17
|
|
|
{ |
|
18
|
|
|
if( empty( array_filter( [$message, $type] )))return; |
|
19
|
|
|
$args['message'] = $message; |
|
20
|
|
|
$args['type'] = $type; |
|
21
|
|
|
add_settings_error( Application::ID, '', json_encode( $this->normalize( $args ))); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string|array|WP_Error $message |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function addError( $message, array $args = [] ) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->add( 'error', $message, $args ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string|array|WP_Error $message |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function addSuccess( $message, array $args = [] ) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->add( 'success', $message, $args ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string|array|WP_Error $message |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addWarning( $message, array $args = [] ) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->add( 'warning', $message, $args ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return static |
|
53
|
|
|
*/ |
|
54
|
|
|
public function clear() |
|
55
|
|
|
{ |
|
56
|
|
|
global $wp_settings_errors; |
|
|
|
|
|
|
57
|
|
|
$wp_settings_errors = []; |
|
58
|
|
|
delete_transient( 'settings_errors' ); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get() |
|
66
|
|
|
{ |
|
67
|
|
|
$notices = array_map( 'unserialize', |
|
68
|
|
|
array_unique( array_map( 'serialize', get_settings_errors( Application::ID ))) |
|
69
|
|
|
); |
|
70
|
|
|
if( empty( $notices ))return; |
|
71
|
|
|
return array_reduce( $notices, function( $carry, $notice ) { |
|
72
|
|
|
return $carry.$this->buildNotice( json_decode( $notice['message'], true )); |
|
73
|
|
|
}); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function buildNotice( array $args ) |
|
80
|
|
|
{ |
|
81
|
|
|
$messages = array_reduce( $args['messages'], function( $carry, $message ) { |
|
82
|
|
|
return $carry.glsr( Builder::class )->p( $message ); |
|
83
|
|
|
}); |
|
84
|
|
|
$class = 'notice notice-'.$args['type']; |
|
85
|
|
|
if( $args['inline'] ) { |
|
86
|
|
|
$class .= ' inline'; |
|
87
|
|
|
} |
|
88
|
|
|
if( $args['dismissible'] ) { |
|
89
|
|
|
$class .= ' is-dismissible'; |
|
90
|
|
|
} |
|
91
|
|
|
return glsr( Builder::class )->div( $messages, [ |
|
92
|
|
|
'class' => $class, |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function normalize( array $args ) |
|
100
|
|
|
{ |
|
101
|
|
|
$defaults = [ |
|
102
|
|
|
'dismissible' => true, |
|
103
|
|
|
'inline' => true, |
|
104
|
|
|
'message' => '', |
|
105
|
|
|
'type' => '', |
|
106
|
|
|
]; |
|
107
|
|
|
$args = shortcode_atts( $defaults, $args ); |
|
108
|
|
|
if( !in_array( $args['type'], ['error', 'warning', 'success'] )) { |
|
109
|
|
|
$args['type'] = 'success'; |
|
110
|
|
|
} |
|
111
|
|
|
$args['messages'] = is_wp_error( $args['message'] ) |
|
112
|
|
|
? (array)$args['message']->get_error_message() |
|
113
|
|
|
: (array)$args['message']; |
|
114
|
|
|
unset( $args['message'] ); |
|
115
|
|
|
return $args; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state