PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.0
WP-Stateless – Google Cloud Storage v3.2.0
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.2.0, at lib/classes/class-errors.php

303 lines 8.7 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
186 $errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args );
187 $notices = apply_filters( 'stateless:notices:admin_notices', $this->notices, $this->args );
188
189 //** Errors Block */
190 if( !empty( $errors ) && is_array( $errors ) ) {
191 $message = '<ul style="none;"><li>' . implode( '</li><li>', $errors ) . '</li></ul>';
192 $data = array(
193 'title' => sprintf( __( '%s is not active due to following errors:', $this->domain ), $this->name ),
194 'class' => 'error',
195 'message' => $message,
196 'action_links' => !empty($this->action_links[ 'errors' ])?$this->action_links[ 'errors' ]:null,
197 );
198
199 include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' );
200 }
201
202 $has_notice = false;
203 //** Determine if warning has been dismissed */
204 if ( ! empty( $notices ) && is_array( $notices ) ) {
205 //** Warnings Block */
206 foreach($notices as $notice){
207 if(get_option( 'dismissed_notice_' . $notice['key'] )){
208 continue;
209 }
210
211 $data = wp_parse_args($notice, array(
212 'title' => '',
213 'class' => 'notice',
214 'message' => '',
215 'button' => '',
216 'button_link' => '#',
217 'key' => '',
218 'action_links' => $this->action_links[ 'notices' ],
219 ));
220
221 include ud_get_stateless_media()->path( '/static/views/error-notice.php', 'dir' );
222
223 $has_notice = true;
224 }
225 }
226
227 }
228
229 /**
230 * dismiss the notice ajax callback
231 * @throws \Exception
232 */
233 public function dismiss_notices(){
234 $response = array(
235 'success' => '0',
236 'error' => __( 'There was an error in request.', $this->domain ),
237 );
238 $error = false;
239
240 if( empty($_POST['key']) && strpos($_POST['key'], 'dismissed_notice_') !== false ) {
241 $response['error'] = __( 'Invalid key', $this->domain );
242 $error = true;
243 }
244 else {
245 $option_key = sanitize_key($_POST['key']);
246 update_option( $option_key, time() );
247 $response['success'] = '1';
248 $response['error'] = null;
249 }
250
251 wp_send_json( $response );
252 }
253
254 /**
255 * Action for the stateless_enable_notice_button_action ajax callback
256 * @throws \Exception
257 */
258 public function stateless_enable_notice_button_action(){
259 $response = array(
260 'success' => '1',
261 );
262 $error = false;
263
264 if( empty($_POST['key']) ) {
265 $response['success'] = '0';
266 $response['error'] = __( 'Invalid key', $this->domain );
267 }
268 else{
269 $option_key = sanitize_key($_POST['key']);
270 $compatibility = Module::get_module($option_key);
271 if(!empty($compatibility['self']) && is_callable(array($compatibility['self'], 'enable_compatibility'))){
272 $response['success'] = $compatibility['self']->enable_compatibility();
273 }
274 }
275
276 wp_send_json( $response );
277 }
278
279 /**
280 * Check dismiss notice timestamp if greater than 24 hrs
281 *
282 * @param string $time
283 *
284 * @return bool
285 */
286 public function check_dismiss_time( $time = '' ) {
287 if( empty( $time ) ) {
288 return true;
289 }
290 $current_time = time();
291 $diff = $current_time - 86400;
292 if ( $diff > (int)$time ) {
293 return true;
294 }
295 return false;
296 }
297
298 }
299
300 }
301
302 }
303