| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Notices Handler |
| 4 |
* |
| 5 |
* @namespace wpCloud\StatelessMedia |
| 6 |
* |
| 7 |
* This file can be used to bootstrap any of the UD plugins, it essentially requires that you have |
| 8 |
* a core file which will be called after 'plugins_loaded'. In addition, if the core class has |
| 9 |
* 'activate' and 'deactivate' functions, then those will be called automatically by this class. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace wpCloud\StatelessMedia { |
| 13 |
|
| 14 |
if( !class_exists( 'wpCloud\StatelessMedia\Errors' ) ) { |
| 15 |
|
| 16 |
/** |
| 17 |
* |
| 18 |
* @author: peshkov@UD |
| 19 |
*/ |
| 20 |
class Errors extends \UsabilityDynamics\WP\Scaffold { |
| 21 |
|
| 22 |
/** |
| 23 |
* Errors |
| 24 |
* |
| 25 |
* @used admin_notices |
| 26 |
* @public |
| 27 |
* @property $errors |
| 28 |
* @type array |
| 29 |
*/ |
| 30 |
private $errors = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Messages |
| 34 |
* |
| 35 |
* @used admin_notices |
| 36 |
* @public |
| 37 |
* @property $messages |
| 38 |
* @type array |
| 39 |
*/ |
| 40 |
private $messages = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Warnings |
| 44 |
* |
| 45 |
* @used admin_notices |
| 46 |
* @public |
| 47 |
* @property $messages |
| 48 |
* @type array |
| 49 |
*/ |
| 50 |
private $warnings = array(); |
| 51 |
|
| 52 |
/** |
| 53 |
* Notices |
| 54 |
* |
| 55 |
* @used admin_notices |
| 56 |
* @public |
| 57 |
* @property $messages |
| 58 |
* @type array |
| 59 |
*/ |
| 60 |
private $notices = array(); |
| 61 |
|
| 62 |
/** |
| 63 |
* Action Links in Footer |
| 64 |
* |
| 65 |
* @used admin_notices |
| 66 |
* @public |
| 67 |
* @property $messages |
| 68 |
* @type array |
| 69 |
*/ |
| 70 |
private $action_links = array( |
| 71 |
'errors' => null, |
| 72 |
'notices' => null, |
| 73 |
); |
| 74 |
|
| 75 |
/** |
| 76 |
* Dismiss action link is available or not. |
| 77 |
* |
| 78 |
* @var bool |
| 79 |
*/ |
| 80 |
private $dismiss = true; |
| 81 |
|
| 82 |
/** |
| 83 |
* |
| 84 |
*/ |
| 85 |
public function __construct( $args ) { |
| 86 |
parent::__construct( $args ); |
| 87 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 88 |
add_action( 'wp_ajax_stateless_notice_dismiss', array( $this, 'dismiss_notices' ) ); |
| 89 |
add_action( 'wp_ajax_stateless_enable_notice_button_action', array( $this, 'stateless_enable_notice_button_action' ) ); |
| 90 |
} |
| 91 |
|
| 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 |
/** |
| 111 |
* Add new message for admin notices |
| 112 |
* |
| 113 |
* @param string $message |
| 114 |
* @param string $type Values: 'error', 'message', 'warning' |
| 115 |
* @param string $translate Values: true, false |
| 116 |
* @author peshkov@UD |
| 117 |
*/ |
| 118 |
public function add( $message, $type = 'error', $translate = true ) { |
| 119 |
switch( $type ) { |
| 120 |
case 'error': |
| 121 |
$this->add_message($this->errors, $message); |
| 122 |
break; |
| 123 |
case 'message': |
| 124 |
case 'warning': |
| 125 |
case 'notice': |
| 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 |
|
| 131 |
$message = array( |
| 132 |
'title' => $title, |
| 133 |
'message' => $message, |
| 134 |
'button' => null, |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
if(empty($message['key'])){ |
| 139 |
$message['key'] = md5( $message['title'] ); |
| 140 |
} |
| 141 |
$this->add_message($this->notices, $message); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Add footer link to specific ( errors|messages|wanrnings ) block |
| 148 |
* |
| 149 |
* @author peshkov@UD |
| 150 |
*/ |
| 151 |
public function add_action_link( $link, $type = 'error' ) { |
| 152 |
switch( $type ) { |
| 153 |
case 'error': |
| 154 |
$this->action_links[ 'errors' ][] = $link; |
| 155 |
break; |
| 156 |
case 'message': |
| 157 |
case 'warning': |
| 158 |
case 'notice': |
| 159 |
$this->action_links[ 'notices' ][] = $link; |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Determine if errors exist |
| 166 |
* |
| 167 |
* @author peshkov@UD |
| 168 |
*/ |
| 169 |
public function has_errors() { |
| 170 |
return !empty( $this->errors ) ? true : false; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Renders admin notes in case there are errors or notices on bootstrap init |
| 175 |
* |
| 176 |
* @author peshkov@UD |
| 177 |
*/ |
| 178 |
public function admin_notices() { |
| 179 |
global $wp_version; |
| 180 |
|
| 181 |
wp_enqueue_style("stateless-error-style", ud_get_stateless_media()->path('static/styles/error-notice.css')); |
| 182 |
//enqueue dismiss js for ajax requests |
| 183 |
$script_path = \UsabilityDynamics\WP\Utility::path( 'static/scripts/ud-dismiss.js', 'url' ); |
| 184 |
wp_enqueue_script( "sateless-error-notice-js", ud_get_stateless_media()->path( 'static/scripts/error-notice.js', 'url' ), array( 'jquery' ) ); |
| 185 |
wp_enqueue_script( "ud-dismiss", $script_path, array( 'jquery' ) ); |
| 186 |
wp_localize_script( "ud-dismiss", "_ud_vars", array( |
| 187 |
"ajaxurl" => admin_url( 'admin-ajax.php' ), |
| 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 |
)); |
| 193 |
|
| 194 |
//** Don't show the message if the user has no enough permissions. */ |
| 195 |
if ( ! function_exists( 'wp_get_current_user' ) ) { |
| 196 |
require_once( ABSPATH . 'wp-includes/pluggable.php' ); |
| 197 |
} |
| 198 |
|
| 199 |
$default_show = true; |
| 200 |
|
| 201 |
if ( |
| 202 |
empty( $this->args['type'] ) || |
| 203 |
( $this->args['type'] == 'plugin' && !current_user_can( 'activate_plugins' ) ) || |
| 204 |
( $this->args['type'] == 'theme' && !current_user_can( 'switch_themes' ) ) |
| 205 |
) { |
| 206 |
$default_show = false; |
| 207 |
} |
| 208 |
|
| 209 |
//** Don't show the message if on a multisite and the user isn't a super user. */ |
| 210 |
if ( is_multisite() && ! is_super_admin() ) { |
| 211 |
$default_show = false; |
| 212 |
} |
| 213 |
|
| 214 |
$errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args ); |
| 215 |
$notices = apply_filters( 'stateless:notices:admin_notices', $this->notices, $this->args ); |
| 216 |
|
| 217 |
//** Errors Block */ |
| 218 |
if( $default_show && !empty( $errors ) && is_array( $errors ) ) { |
| 219 |
$message = '<ul style="none;"><li>' . implode( '</li><li>', $errors ) . '</li></ul>'; |
| 220 |
$data = array( |
| 221 |
'title' => sprintf( __( '%s is not active due to following errors:', $this->domain ), esc_html($this->name) ), |
| 222 |
'class' => 'error', |
| 223 |
'message' => $message, |
| 224 |
'action_links' => !empty($this->action_links[ 'errors' ])?$this->action_links[ 'errors' ]:null, |
| 225 |
); |
| 226 |
|
| 227 |
include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' ); |
| 228 |
} |
| 229 |
|
| 230 |
$has_notice = false; |
| 231 |
//** Determine if warning has been dismissed */ |
| 232 |
if ( ! empty( $notices ) && is_array( $notices ) ) { |
| 233 |
//** Warnings Block */ |
| 234 |
foreach ($notices as $notice ) { |
| 235 |
if ( get_option( 'dismissed_notice_' . $notice['key'] )){ |
| 236 |
continue; |
| 237 |
} |
| 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 |
|
| 259 |
$data = wp_parse_args($notice, array( |
| 260 |
'title' => '', |
| 261 |
'class' => implode(' ', $classes), |
| 262 |
'message' => '', |
| 263 |
'button' => '', |
| 264 |
'button_link' => '#', |
| 265 |
'key' => '', |
| 266 |
'action_links' => $this->action_links[ 'notices' ], |
| 267 |
'dismiss' => true, |
| 268 |
)); |
| 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 |
|
| 275 |
include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' ); |
| 276 |
|
| 277 |
$has_notice = true; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* dismiss the notice ajax callback |
| 285 |
* @throws \Exception |
| 286 |
*/ |
| 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 |
|
| 294 |
$response = array( |
| 295 |
'success' => '0', |
| 296 |
'error' => __( 'There was an error in request.', $this->domain ), |
| 297 |
); |
| 298 |
|
| 299 |
$error = false; |
| 300 |
|
| 301 |
$option_key = isset($_POST['key']) ? sanitize_key($_POST['key']) : ''; |
| 302 |
|
| 303 |
if ( strpos($option_key, 'dismissed_') !== 0 ) { |
| 304 |
$response['error'] = __( 'Invalid key', $this->domain ); |
| 305 |
$error = true; |
| 306 |
} |
| 307 |
|
| 308 |
if ( !$error && update_option( $option_key, time() ) ) { |
| 309 |
do_action('wp_stateless_notice_dismissed', $option_key); |
| 310 |
|
| 311 |
$response['success'] = '1'; |
| 312 |
$response['error'] = null; |
| 313 |
} |
| 314 |
|
| 315 |
wp_send_json( $response ); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Action for the stateless_enable_notice_button_action ajax callback |
| 320 |
* @throws \Exception |
| 321 |
*/ |
| 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 |
|
| 329 |
$response = array( |
| 330 |
'success' => '1', |
| 331 |
); |
| 332 |
|
| 333 |
$error = false; |
| 334 |
|
| 335 |
if( empty($_POST['key']) ) { |
| 336 |
$response['success'] = '0'; |
| 337 |
$response['error'] = __( 'Invalid key', $this->domain ); |
| 338 |
} |
| 339 |
else{ |
| 340 |
$option_key = sanitize_key($_POST['key']); |
| 341 |
$compatibility = Module::get_module($option_key); |
| 342 |
if(!empty($compatibility['self']) && is_callable(array($compatibility['self'], 'enable_compatibility'))){ |
| 343 |
$response['success'] = $compatibility['self']->enable_compatibility(); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
wp_send_json( $response ); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Check dismiss notice timestamp if greater than 24 hrs |
| 352 |
* |
| 353 |
* @param string $time |
| 354 |
* |
| 355 |
* @return bool |
| 356 |
*/ |
| 357 |
public function check_dismiss_time( $time = '' ) { |
| 358 |
if( empty( $time ) ) { |
| 359 |
return true; |
| 360 |
} |
| 361 |
$current_time = time(); |
| 362 |
$diff = $current_time - 86400; |
| 363 |
if ( $diff > (int)$time ) { |
| 364 |
return true; |
| 365 |
} |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|