1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Exception; |
7
|
|
|
use GeminiLabs\Pollux\Application; |
8
|
|
|
|
9
|
|
|
class Notice |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Application |
13
|
|
|
*/ |
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
public function __construct( Application $app ) |
17
|
|
|
{ |
18
|
|
|
$this->app = $app; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @method void addError( mixed $messages, array $args ) |
23
|
|
|
* @method void addInfo( mixed $messages, array $args ) |
24
|
|
|
* @method void addSuccess( mixed $messages, array $args ) |
25
|
|
|
* @method void addWarning( mixed $messages, array $args ) |
26
|
|
|
*/ |
27
|
|
|
public function __call( $name, $args ) |
28
|
|
|
{ |
29
|
|
|
$method = strtolower( $name ); |
30
|
|
|
$status = substr( $method, 3 ); |
31
|
|
|
if( 'add' == substr( $method, 0, 3 ) && in_array( $status, ['error', 'info', 'success', 'warning'] )) { |
32
|
|
|
return call_user_func_array( [$this, 'addNotice'], array_merge( [$status], $args )); |
33
|
|
|
} |
34
|
|
|
throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name )); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @property array $all |
39
|
|
|
*/ |
40
|
|
|
public function __get( $property ) |
41
|
|
|
{ |
42
|
|
|
if( $property == 'all' ) { |
43
|
|
|
return $this->app->notices; |
44
|
|
|
} |
45
|
|
|
throw new Exception( sprintf( 'Not a valid property: %s', $property )); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function activateButton( array $plugin ) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$actionUrl = self_admin_url( sprintf( 'plugins.php?action=activate&plugin=%s', $plugin['plugin'] )); |
54
|
|
|
return $this->button( sprintf( '%s %s', __( 'Activate', 'pollux' ), $plugin['name'] ), [ |
55
|
|
|
'data-name' => $plugin['name'], |
56
|
|
|
'data-plugin' => $plugin['plugin'], |
57
|
|
|
'data-slug' => $plugin['slug'], |
58
|
|
|
'href' => wp_nonce_url( $actionUrl, sprintf( 'activate-plugin_%s', $plugin['plugin'] )), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $title |
64
|
|
|
* @param string $url |
|
|
|
|
65
|
|
|
* @param string $attributes |
|
|
|
|
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function button( $title, array $atts = [] ) |
69
|
|
|
{ |
70
|
|
|
$atts = wp_parse_args( $atts, [ |
71
|
|
|
'class' => '', |
72
|
|
|
'href' => '', |
73
|
|
|
]); |
74
|
|
|
$atts['class'] = trim( $atts['class'] . ' button button-small' ); |
75
|
|
|
$attributes = array_reduce( array_keys( $atts ), function( $carry, $key ) use( $atts ) { |
76
|
|
|
return $carry . sprintf( ' %s="%s"', $key, $atts[$key] ); |
77
|
|
|
}); |
78
|
|
|
return sprintf( '<a%s>%s</a>', $attributes, $title ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return void |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
public function generate( array $notice, $unset = true ) |
85
|
|
|
{ |
86
|
|
|
if( $unset ) { |
87
|
|
|
$index = array_search( $notice, $this->app->notices ); |
88
|
|
|
if( $index !== false ) { |
89
|
|
|
unset( $this->app->notices[$index] ); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $this->buildNotice( $notice ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function installButton( array $plugin ) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$actionUrl = self_admin_url( sprintf( 'update.php?action=install-plugin&plugin=%s', $plugin['slug'] )); |
101
|
|
|
return $this->button( sprintf( '%s %s', __( 'Install', 'pollux' ), $plugin['name'] ), [ |
102
|
|
|
'data-name' => $plugin['name'], |
103
|
|
|
'data-plugin' => $plugin['plugin'], |
104
|
|
|
'data-slug' => $plugin['slug'], |
105
|
|
|
'href' => wp_nonce_url( $actionUrl, sprintf( 'install-plugin_%s', $plugin['slug'] )), |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function updateButton( array $plugin ) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$actionUrl = self_admin_url( sprintf( 'update.php?action=upgrade-plugin&plugin=%s', $plugin['plugin'] )); |
115
|
|
|
return $this->button( sprintf( '%s %s', __( 'Update', 'pollux' ), $plugin['name'] ), [ |
116
|
|
|
'data-name' => $plugin['name'], |
117
|
|
|
'data-plugin' => $plugin['plugin'], |
118
|
|
|
'data-slug' => $plugin['slug'], |
119
|
|
|
'href' => wp_nonce_url( $actionUrl, sprintf( 'upgrade-plugin_%s', $plugin['plugin'] )), |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $string |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function title( $string ) |
128
|
|
|
{ |
129
|
|
|
return sprintf( '<strong>%s</strong>', $string ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $type |
134
|
|
|
* @param string|array $messages |
135
|
|
|
* @param bool $dismissible |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
protected function addNotice( $type, $messages, $dismissible = true ) |
139
|
|
|
{ |
140
|
|
|
$this->app->notices[] = [ |
141
|
|
|
'dismissible' => $dismissible, |
142
|
|
|
'message' => $this->buildMessage( array_filter( (array) $messages )), |
143
|
|
|
'type' => $type, |
144
|
|
|
]; |
145
|
|
|
$this->app->notices = array_unique( $this->app->notices, SORT_REGULAR ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected function buildMessage( array $messages ) |
152
|
|
|
{ |
153
|
|
|
foreach( $messages as $key => &$message ) { |
154
|
|
|
if( !is_wp_error( $message ))continue; |
155
|
|
|
$message = $message->get_error_message(); |
156
|
|
|
} |
157
|
|
|
return wpautop( implode( PHP_EOL . PHP_EOL, $messages )); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function buildNotice( array $notice ) |
164
|
|
|
{ |
165
|
|
|
$class = sprintf( 'notice notice-%s', $notice['type'] ); |
166
|
|
|
if( $notice['dismissible'] ) { |
167
|
|
|
$class .= ' is-dismissible'; |
168
|
|
|
} |
169
|
|
|
return sprintf( '<div class="pollux-notice %s">%s</div>', $class, $notice['message'] ); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.