PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
← All changes | lib/classes/class-errors.php +91 -20 3.2.2trunk View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 3 * Admin Notices Handler
4 4 *
5 - * @namespace UsabilityDynamics
5 + * @namespace wpCloud\StatelessMedia
6 6 *
7 7 * This file can be used to bootstrap any of the UD plugins, it essentially requires that you have
8 8 * a core file which will be called after 'plugins_loaded'. In addition, if the core class has
9 9 * 'activate' and 'deactivate' functions, then those will be called automatically by this class.
@@ -89,25 +89,48 @@
89 89 add_action( 'wp_ajax_stateless_enable_notice_button_action', array( $this, 'stateless_enable_notice_button_action' ) );
90 90 }
91 91
92 92 /**
93 + * Prevents adding duplicating messages
94 + * Some triggers may happen few times during site load (like 'switch_blog' hook)
95 + * adding the same message several times is not necessary
96 + *
97 + * @param string $collection
98 + * @param string $message
99 + */
100 + private function add_message( &$collection, $message ) {
101 + $exiting_keys = array_column($collection, 'key');
102 +
103 + if ( in_array( $message['key'], $exiting_keys ) ) {
104 + return;
105 + }
106 +
107 + $collection[] = $message;
108 + }
109 +
110 + /**
93 111 * Add new message for admin notices
94 112 *
95 113 * @param string $message
96 114 * @param string $type Values: 'error', 'message', 'warning'
115 + * @param string $translate Values: true, false
97 116 * @author peshkov@UD
98 117 */
99 - public function add( $message, $type = 'error' ) {
118 + public function add( $message, $type = 'error', $translate = true ) {
100 119 switch( $type ) {
101 120 case 'error':
102 - $this->errors[] = $message;
121 + $this->add_message($this->errors, $message);
103 122 break;
104 123 case 'message':
105 124 case 'warning':
106 125 case 'notice':
107 126 if(!is_array($message)){
127 + $title = $translate
128 + ? sprintf( __( '%s has the following notice:', $this->domain ), esc_html($this->name) )
129 + : sprintf( '%s has the following notice:', esc_html($this->name) );
130 +
108 131 $message = array(
109 - 'title' => sprintf( __( '<b>%s</b> has the following notice:', $this->domain ), $this->name ),
132 + 'title' => $title,
110 133 'message' => $message,
111 134 'button' => null,
112 135 );
113 136 }
@@ -114,9 +137,9 @@
114 137
115 138 if(empty($message['key'])){
116 139 $message['key'] = md5( $message['title'] );
117 140 }
118 - $this->notices[] = $message;
141 + $this->add_message($this->notices, $message);
119 142 break;
120 143 }
121 144 }
122 145
@@ -161,27 +184,32 @@
161 184 wp_enqueue_script( "sateless-error-notice-js", ud_get_stateless_media()->path( 'static/scripts/error-notice.js', 'url' ), array( 'jquery' ) );
162 185 wp_enqueue_script( "ud-dismiss", $script_path, array( 'jquery' ) );
163 186 wp_localize_script( "ud-dismiss", "_ud_vars", array(
164 187 "ajaxurl" => admin_url( 'admin-ajax.php' ),
165 - ) );
188 + ));
189 + wp_localize_script( "sateless-error-notice-js", "stateless_error_notice_vars", array(
190 + "dismiss_nonce" => wp_create_nonce( 'stateless_notice_dismiss' ),
191 + "enable_action_nonce" => wp_create_nonce( 'stateless_enable_notice_button_action' ),
192 + ));
166 193
167 -
168 194 //** Don't show the message if the user has no enough permissions. */
169 195 if ( ! function_exists( 'wp_get_current_user' ) ) {
170 196 require_once( ABSPATH . 'wp-includes/pluggable.php' );
171 197 }
172 198
173 - if(
199 + $default_show = true;
200 +
201 + if (
174 202 empty( $this->args['type'] ) ||
175 203 ( $this->args['type'] == 'plugin' && !current_user_can( 'activate_plugins' ) ) ||
176 204 ( $this->args['type'] == 'theme' && !current_user_can( 'switch_themes' ) )
177 205 ) {
178 - return;
206 + $default_show = false;
179 207 }
180 208
181 209 //** Don't show the message if on a multisite and the user isn't a super user. */
182 210 if ( is_multisite() && ! is_super_admin() ) {
183 - return;
211 + $default_show = false;
184 212 }
185 213
186 214 $errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args );
187 215 $notices = apply_filters( 'stateless:notices:admin_notices', $this->notices, $this->args );
@@ -186,12 +214,12 @@
186 214 $errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args );
187 215 $notices = apply_filters( 'stateless:notices:admin_notices', $this->notices, $this->args );
188 216
189 217 //** Errors Block */
190 - if( !empty( $errors ) && is_array( $errors ) ) {
218 + if( $default_show && !empty( $errors ) && is_array( $errors ) ) {
191 219 $message = '<ul style="none;"><li>' . implode( '</li><li>', $errors ) . '</li></ul>';
192 220 $data = array(
193 - 'title' => sprintf( __( '%s is not active due to following errors:', $this->domain ), $this->name ),
221 + 'title' => sprintf( __( '%s is not active due to following errors:', $this->domain ), esc_html($this->name) ),
194 222 'class' => 'error',
195 223 'message' => $message,
196 224 'action_links' => !empty($this->action_links[ 'errors' ])?$this->action_links[ 'errors' ]:null,
197 225 );
@@ -202,23 +230,49 @@
202 230 $has_notice = false;
203 231 //** Determine if warning has been dismissed */
204 232 if ( ! empty( $notices ) && is_array( $notices ) ) {
205 233 //** Warnings Block */
206 - foreach($notices as $notice){
207 - if(get_option( 'dismissed_notice_' . $notice['key'] )){
234 + foreach ($notices as $notice ) {
235 + if ( get_option( 'dismissed_notice_' . $notice['key'] )){
208 236 continue;
209 237 }
210 238
239 + // Check additional capabilities
240 + $capability = isset( $notice['capability'] ) && !empty( $notice['capability'] ) ? $notice['capability'] : null;
241 +
242 + if ( ( !$default_show && empty($capability) ) || ( !empty($capability) && !current_user_can($capability) ) ) {
243 + continue;
244 + }
245 +
246 + // Additional HTML classes
247 + $classes = [];
248 +
249 + if ( isset($notice['classes']) ) {
250 + $classes = $notice['classes'];
251 +
252 + if ( !is_array($classes) ) {
253 + $classes = [$classes];
254 + }
255 + }
256 +
257 + $classes[] = 'notice';
258 +
211 259 $data = wp_parse_args($notice, array(
212 260 'title' => '',
213 - 'class' => 'notice',
261 + 'class' => implode(' ', $classes),
214 262 'message' => '',
215 263 'button' => '',
216 264 'button_link' => '#',
217 265 'key' => '',
218 266 'action_links' => $this->action_links[ 'notices' ],
267 + 'dismiss' => true,
219 268 ));
220 269
270 + $button_capability = isset( $notice['button_capability'] ) && !empty( $notice['button_capability'] ) ? $notice['button_capability'] : null;
271 + if ( !empty($button_capability) && !current_user_can($button_capability) ) {
272 + $data['button'] = '';
273 + }
274 +
221 275 include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' );
222 276
223 277 $has_notice = true;
224 278 }
@@ -229,22 +283,32 @@
229 283 /**
230 284 * dismiss the notice ajax callback
231 285 * @throws \Exception
232 286 */
233 - public function dismiss_notices(){
287 + public function dismiss_notices() {
288 + check_ajax_referer('stateless_notice_dismiss');
289 +
290 + if ( !is_user_logged_in() ) {
291 + wp_send_json_error( array( 'error' => __( 'You are not allowed to do this action.', $this->domain ) ) );
292 + }
293 +
234 294 $response = array(
235 295 'success' => '0',
236 296 'error' => __( 'There was an error in request.', $this->domain ),
237 297 );
298 +
238 299 $error = false;
239 300
240 - if( empty($_POST['key']) && strpos($_POST['key'], 'dismissed_notice_') !== false ) {
301 + $option_key = isset($_POST['key']) ? sanitize_key($_POST['key']) : '';
302 +
303 + if ( strpos($option_key, 'dismissed_') !== 0 ) {
241 304 $response['error'] = __( 'Invalid key', $this->domain );
242 305 $error = true;
243 306 }
244 - else {
245 - $option_key = sanitize_key($_POST['key']);
246 - update_option( $option_key, time() );
307 +
308 + if ( !$error && update_option( $option_key, time() ) ) {
309 + do_action('wp_stateless_notice_dismissed', $option_key);
310 +
247 311 $response['success'] = '1';
248 312 $response['error'] = null;
249 313 }
250 314
@@ -255,11 +319,18 @@
255 319 * Action for the stateless_enable_notice_button_action ajax callback
256 320 * @throws \Exception
257 321 */
258 322 public function stateless_enable_notice_button_action(){
323 + check_ajax_referer('stateless_enable_notice_button_action');
324 +
325 + if ( !is_user_logged_in() ) {
326 + wp_send_json_error( array( 'error' => __( 'You are not allowed to do this action.', $this->domain ) ) );
327 + }
328 +
259 329 $response = array(
260 330 'success' => '1',
261 331 );
332 +
262 333 $error = false;
263 334
264 335 if( empty($_POST['key']) ) {
265 336 $response['success'] = '0';