PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.2
WP-Stateless – Google Cloud Storage v3.0.2
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
wp-stateless / lib / classes / class-errors.php

class-errors.php in WP-Stateless – Google Cloud Storage 3.0.2, at lib/classes/class-errors.php

306 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_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 * 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']) && strpos($_POST['key'], 'dismissed_notice_') !== false ) {
245 $response['error'] = __( 'Invalid key', $this->domain );
246 $error = true;
247 }
248 else {
249 $option_key = sanitize_key($_POST['key']);
250 update_option( $option_key, time() );
251 $response['success'] = '1';
252 $response['error'] = null;
253 }
254
255 wp_send_json( $response );
256 }
257
258 /**
259 * Action for the stateless_enable_notice_button_action ajax callback
260 * @throws \Exception
261 */
262 public function stateless_enable_notice_button_action(){
263 $response = array(
264 'success' => '1',
265 );
266 $error = false;
267
268 if( empty($_POST['key']) ) {
269 $response['success'] = '0';
270 $response['error'] = __( 'Invalid key', $this->domain );
271 }
272 else{
273 $compatibility = Module::get_module($_POST['key']);
274 if(!empty($compatibility['self']) && is_callable(array($compatibility['self'], 'enable_compatibility'))){
275 $response['success'] = $compatibility['self']->enable_compatibility();
276 }
277 }
278
279 wp_send_json( $response );
280 }
281
282 /**
283 * Check dismiss notice timestamp if greater than 24 hrs
284 *
285 * @param string $time
286 *
287 * @return bool
288 */
289 public function check_dismiss_time( $time = '' ) {
290 if( empty( $time ) ) {
291 return true;
292 }
293 $current_time = time();
294 $diff = $current_time - 86400;
295 if ( $diff > (int)$time ) {
296 return true;
297 }
298 return false;
299 }
300
301 }
302
303 }
304
305 }
306