| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Notices Handler |
| 4 |
* |
| 5 |
* @namespace UsabilityDynamics |
| 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_ud_dismiss', array( $this, 'dismiss_notices' ) ); |
| 89 |
add_action( 'wp_ajax_button_action', array( $this, 'button_action' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add new message for admin notices |
| 94 |
* |
| 95 |
* @param string $message |
| 96 |
* @param string $type Values: 'error', 'message', 'warning' |
| 97 |
* @author peshkov@UD |
| 98 |
*/ |
| 99 |
public function add( $message, $type = 'error' ) { |
| 100 |
switch( $type ) { |
| 101 |
case 'error': |
| 102 |
$this->errors[] = $message; |
| 103 |
break; |
| 104 |
case 'message': |
| 105 |
case 'warning': |
| 106 |
case 'notice': |
| 107 |
if(!is_array($message)){ |
| 108 |
$message = array( |
| 109 |
'title' => sprintf( __( '<b>%s</b> has the following notice:', $this->domain ), $this->name ), |
| 110 |
'message' => $message, |
| 111 |
'button' => null, |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
if(empty($message['key'])){ |
| 116 |
$message['key'] = md5( $message['title'] ); |
| 117 |
} |
| 118 |
$this->notices[] = $message; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Add footer link to specific ( errors|messages|wanrnings ) block |
| 125 |
* |
| 126 |
* @author peshkov@UD |
| 127 |
*/ |
| 128 |
public function add_action_link( $link, $type = 'error' ) { |
| 129 |
switch( $type ) { |
| 130 |
case 'error': |
| 131 |
$this->action_links[ 'errors' ][] = $link; |
| 132 |
break; |
| 133 |
case 'message': |
| 134 |
case 'warning': |
| 135 |
case 'notice': |
| 136 |
$this->action_links[ 'notices' ][] = $link; |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Determine if errors exist |
| 143 |
* |
| 144 |
* @author peshkov@UD |
| 145 |
*/ |
| 146 |
public function has_errors() { |
| 147 |
return !empty( $this->errors ) ? true : false; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Renders admin notes in case there are errors or notices on bootstrap init |
| 152 |
* |
| 153 |
* @author peshkov@UD |
| 154 |
*/ |
| 155 |
public function admin_notices() { |
| 156 |
global $wp_version; |
| 157 |
|
| 158 |
wp_enqueue_style("stateless-error-style", ud_get_stateless_media()->path('static/styles/error-notice.css')); |
| 159 |
//enqueue dismiss js for ajax requests |
| 160 |
$script_path = \UsabilityDynamics\WP\Utility::path( 'static/scripts/ud-dismiss.js', 'url' ); |
| 161 |
wp_enqueue_script( "sateless-error-notice-js", ud_get_stateless_media()->path( 'static/scripts/error-notice.js', 'url' ), array( 'jquery' ) ); |
| 162 |
wp_enqueue_script( "ud-dismiss", $script_path, array( 'jquery' ) ); |
| 163 |
wp_localize_script( "ud-dismiss", "_ud_vars", array( |
| 164 |
"ajaxurl" => admin_url( 'admin-ajax.php' ), |
| 165 |
) ); |
| 166 |
|
| 167 |
|
| 168 |
//** Don't show the message if the user has no enough permissions. */ |
| 169 |
if ( ! function_exists( 'wp_get_current_user' ) ) { |
| 170 |
require_once( ABSPATH . 'wp-includes/pluggable.php' ); |
| 171 |
} |
| 172 |
|
| 173 |
if( |
| 174 |
empty( $this->args['type'] ) || |
| 175 |
( $this->args['type'] == 'plugin' && !current_user_can( 'activate_plugins' ) ) || |
| 176 |
( $this->args['type'] == 'theme' && !current_user_can( 'switch_themes' ) ) |
| 177 |
) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
//** Don't show the message if on a multisite and the user isn't a super user. */ |
| 182 |
if ( is_multisite() && ! is_super_admin() ) { |
| 183 |
return; |
| 184 |
} |
| 185 |
//** Ignore messages on TGM Plugin Activation page */ |
| 186 |
if( \UsabilityDynamics\WP\TGM_Plugin_Activation::get_instance()->is_tgmpa_page() ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
$errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args ); |
| 191 |
$notices = apply_filters( 'stateless:notices:admin_notices', $this->notices, $this->args ); |
| 192 |
|
| 193 |
//** Errors Block */ |
| 194 |
if( !empty( $errors ) && is_array( $errors ) ) { |
| 195 |
$message = '<ul style="none;"><li>' . implode( '</li><li>', $errors ) . '</li></ul>'; |
| 196 |
$data = array( |
| 197 |
'title' => sprintf( __( '%s is not active due to following errors:', $this->domain ), $this->name ), |
| 198 |
'class' => 'error', |
| 199 |
'message' => $message, |
| 200 |
'action_links' => !empty($this->action_links[ 'errors' ])?$this->action_links[ 'errors' ]:null, |
| 201 |
); |
| 202 |
|
| 203 |
include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' ); |
| 204 |
} |
| 205 |
|
| 206 |
$has_notice = false; |
| 207 |
//** Determine if warning has been dismissed */ |
| 208 |
if ( ! empty( $notices ) && is_array( $notices ) ) { |
| 209 |
//** Warnings Block */ |
| 210 |
foreach($notices as $notice){ |
| 211 |
if(get_option( 'dismissed_notice_' . $notice['key'] )){ |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
$data = wp_parse_args($notice, array( |
| 216 |
'title' => '', |
| 217 |
'class' => 'notice', |
| 218 |
'message' => '', |
| 219 |
'button' => '', |
| 220 |
'button_link' => '#', |
| 221 |
'key' => '', |
| 222 |
'action_links' => $this->action_links[ 'notices' ], |
| 223 |
)); |
| 224 |
|
| 225 |
include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' ); |
| 226 |
|
| 227 |
$has_notice = true; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* dismiss the notice ajax callback |
| 235 |
* @throws \Exception |
| 236 |
*/ |
| 237 |
public function dismiss_notices(){ |
| 238 |
$response = array( |
| 239 |
'success' => '0', |
| 240 |
'error' => __( 'There was an error in request.', $this->domain ), |
| 241 |
); |
| 242 |
$error = false; |
| 243 |
|
| 244 |
if( empty($_POST['key']) ) { |
| 245 |
$response['error'] = __( 'Invalid key', $this->domain ); |
| 246 |
$error = true; |
| 247 |
} |
| 248 |
|
| 249 |
if ( ! $error && update_option( ( $_POST['key'] ), time() ) ) { |
| 250 |
$response['success'] = '1'; |
| 251 |
$response['error'] = null; |
| 252 |
} |
| 253 |
|
| 254 |
wp_send_json( $response ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Action for the button_action ajax callback |
| 259 |
* @throws \Exception |
| 260 |
*/ |
| 261 |
public function button_action(){ |
| 262 |
$response = array( |
| 263 |
'success' => '1', |
| 264 |
); |
| 265 |
$error = false; |
| 266 |
|
| 267 |
if( empty($_POST['key']) ) { |
| 268 |
$response['success'] = '0'; |
| 269 |
$response['error'] = __( 'Invalid key', $this->domain ); |
| 270 |
} |
| 271 |
|
| 272 |
do_action($_POST['key']); |
| 273 |
|
| 274 |
wp_send_json( $response ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Check dismiss notice timestamp if greater than 24 hrs |
| 279 |
* |
| 280 |
* @param string $time |
| 281 |
* |
| 282 |
* @return bool |
| 283 |
*/ |
| 284 |
public function check_dismiss_time( $time = '' ) { |
| 285 |
if( empty( $time ) ) { |
| 286 |
return true; |
| 287 |
} |
| 288 |
$current_time = time(); |
| 289 |
$diff = $current_time - 86400; |
| 290 |
if ( $diff > (int)$time ) { |
| 291 |
return true; |
| 292 |
} |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|