PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
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 / vendor / udx / lib-wp-bootstrap / lib / classes / class-errors.php

class-errors.php in WP-Stateless – Google Cloud Storage 3.0.3, at vendor/udx/lib-wp-bootstrap/lib/classes/class-errors.php

267 lines 9.1 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 namespace UsabilityDynamics\WP {
12
13 if( !class_exists( 'UsabilityDynamics\WP\Errors' ) ) {
14
15 /**
16 *
17 * @author: peshkov@UD
18 */
19 class Errors extends Scaffold {
20
21 /**
22 * Errors
23 *
24 * @used admin_notices
25 * @public
26 * @property $errors
27 * @type array
28 */
29 protected $errors = array();
30
31 /**
32 * Messages
33 *
34 * @used admin_notices
35 * @public
36 * @property $messages
37 * @type array
38 */
39 protected $messages = array();
40
41 /**
42 * Warnings
43 *
44 * @used admin_notices
45 * @public
46 * @property $messages
47 * @type array
48 */
49 protected $warnings = array();
50
51 /**
52 * Action Links in Footer
53 *
54 * @used admin_notices
55 * @public
56 * @property $messages
57 * @type array
58 */
59 protected $action_links = array();
60
61 /**
62 * Dismiss action link is available or not.
63 *
64 * @var bool
65 */
66 protected $dismiss = true;
67
68 /**
69 *
70 */
71 public function __construct( $args ) {
72 parent::__construct( $args );
73 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
74 add_action( 'wp_ajax_ud_dismiss', array( $this, 'dismiss_notices' ) );
75 }
76
77 /**
78 * Add new message for admin notices
79 *
80 * @param string $message
81 * @param string $type Values: 'error', 'message', 'warning'
82 * @author peshkov@UD
83 */
84 public function add( $message, $type = 'error' ) {
85 switch( $type ) {
86 case 'error':
87 $this->errors[] = $message;
88 break;
89 case 'message':
90 $this->messages[] = $message;
91 break;
92 case 'warning':
93 $this->warnings[] = $message;
94 break;
95 }
96 }
97
98 /**
99 * Add footer link to specific ( errors|messages|wanrnings ) block
100 *
101 * @author peshkov@UD
102 */
103 public function add_action_link( $link, $type = 'error' ) {
104 switch( $type ) {
105 case 'error':
106 $this->action_links[ 'errors' ][] = $link;
107 break;
108 case 'message':
109 $this->action_links[ 'messages' ][] = $link;
110 break;
111 case 'message':
112 $this->action_links[ 'warnings' ][] = $link;
113 break;
114 }
115 }
116
117 /**
118 * Determine if errors exist
119 *
120 * @author peshkov@UD
121 */
122 public function has_errors() {
123 return !empty( $this->errors ) ? true : false;
124 }
125
126 /**
127 * Renders admin notes in case there are errors or notices on bootstrap init
128 *
129 * @author peshkov@UD
130 */
131 public function admin_notices() {
132 global $wp_version;
133
134 //** Don't show the message if the user has no enough permissions. */
135 if ( ! function_exists( 'wp_get_current_user' ) ) {
136 require_once( ABSPATH . 'wp-includes/pluggable.php' );
137 }
138
139 if(
140 empty( $this->args['type'] ) ||
141 ( $this->args['type'] == 'plugin' && !current_user_can( 'activate_plugins' ) ) ||
142 ( $this->args['type'] == 'theme' && !current_user_can( 'switch_themes' ) )
143 ) {
144 return;
145 }
146
147 //** Don't show the message if on a multisite and the user isn't a super user. */
148 if ( is_multisite() && ! is_super_admin() ) {
149 return;
150 }
151 //** Ignore messages on TGM Plugin Activation page */
152 if( TGM_Plugin_Activation::get_instance()->is_tgmpa_page() ) {
153 return;
154 }
155
156 $errors = apply_filters( 'ud:errors:admin_notices', $this->errors, $this->args );
157 $messages = apply_filters( 'ud:messages:admin_notices', $this->messages, $this->args );
158 $warnings = apply_filters( 'ud:warnings:admin_notices', $this->warnings, $this->args );
159
160 if( !empty( $errors ) || !empty( $messages ) || !empty( $warnings ) ) {
161 echo "<style>.ud-admin-notice a { text-decoration: underline !important; } .ud-admin-notice { display: block !important; } .ud-admin-notice.update-nag { border-color: #ffba00 !important; }</style>";
162 }
163
164 //** Errors Block */
165 if( !empty( $errors ) && is_array( $errors ) ) {
166 $message = '<ul style="list-style:disc inside;"><li>' . implode( '</li><li>', $errors ) . '</li></ul>';
167 $message = sprintf( __( '<p><b>%s</b> is not active due to following errors:</p> %s', $this->domain ), $this->name, $message );
168 if( !empty( $this->action_links[ 'errors' ] ) && is_array( $this->action_links[ 'errors' ] ) ) {
169 $message .= '<p>' . implode( ' | ', $this->action_links[ 'errors' ] ) . '</p>';
170 }
171 echo '<div class="ud-admin-notice error fade" style="padding:11px;">' . $message . '</div>';
172 }
173
174 //** Determine if warning has been dismissed */
175 $warning_dismissed = get_option( ( 'dismissed_warning_' . sanitize_key( $this->name ) ) );
176 $show_warnings = $this->check_dismiss_time( $warning_dismissed );
177 if ( $show_warnings && ! empty( $warnings ) && is_array( $warnings ) ) {
178 //** Warnings Block */
179 $message = '<ul style="list-style:disc inside;"><li>' . implode( '</li><li>', $warnings ) . '</li></ul>';
180 $message = sprintf( __( '<p><b>%s</b> has the following warnings:</p> %s', $this->domain ), $this->name, $message );
181 if( $this->dismiss ) {
182 $this->action_links[ 'warnings' ][] = '<a class="dismiss-warning dismiss" data-key="dismissed_warning_' . sanitize_key( $this->name ).'" href="#">' . __( 'Dismiss this warning', $this->domain ) . '</a>';
183 }
184 if( !empty( $this->action_links[ 'warnings' ] ) && is_array( $this->action_links[ 'warnings' ] ) ) {
185 $message .= '<p>' . implode( ' | ', $this->action_links[ 'warnings' ] ) . '</p>';
186 }
187 echo '<div class="ud-admin-notice updated update-nag fade" style="padding:11px;">' . $message . '</div>';
188 }
189
190 //** Determine if message has been dismissed */
191 $message_dismissed = get_option( ( 'dismissed_notice_' . sanitize_key( $this->name ) ) );
192 if ( empty( $message_dismissed ) ) {
193 //** Notices Block */
194 if( !empty( $messages ) && is_array( $messages ) ) {
195 $message = '<ul style="list-style:disc inside;"><li>' . implode( '</li><li>', $messages ) . '</li></ul>';
196 if( !empty( $errors ) ) {
197 $message = sprintf( __( '<p><b>%s</b> has the following additional notices:</p> %s', $this->domain ), $this->name, $message );
198 } else {
199 $message = sprintf( __( '<p><b>%s</b> is active, but has the following notices:</p> %s', $this->domain ), $this->name, $message );
200 }
201 if( $this->dismiss ) {
202 $this->action_links[ 'messages' ][] = '<a class="dismiss-notice dismiss" data-key="dismissed_notice_' . sanitize_key( $this->name ).'" href="#">' . __( 'Dismiss this notice', $this->domain ) . '</a>';
203 }
204 $message .= '<p>' . implode( ' | ', $this->action_links[ 'messages' ] ) . '</p>';
205 echo '<div class="ud-admin-notice updated fade" style="padding:11px;">' . $message . '</div>';
206 }
207 }
208
209 if ( $show_warnings || empty( $message_dismissed ) ) {
210 //enqueue dismiss js for ajax requests
211 $script_path = Utility::path( 'static/scripts/ud-dismiss.js', 'url' );
212 wp_enqueue_script( "ud-dismiss", $script_path, array( 'jquery' ) );
213 wp_localize_script( "ud-dismiss", "_ud_vars", array(
214 "ajaxurl" => admin_url( 'admin-ajax.php' ),
215 ) );
216 }
217
218 }
219
220 /**
221 * dismiss the notice ajax callback
222 * @throws \Exception
223 */
224 public function dismiss_notices(){
225 $response = array(
226 'success' => '0',
227 'error' => __( 'There was an error in request.', $this->domain ),
228 );
229 $error = false;
230
231 if( empty($_POST['key']) ) {
232 $response['error'] = __( 'Invalid key', $this->domain );
233 $error = true;
234 }
235
236 if ( ! $error && update_option( ( $_POST['key'] ), time() ) ) {
237 $response['success'] = '1';
238 }
239
240 wp_send_json( $response );
241 }
242
243 /**
244 * Check dismiss notice timestamp if greater than 24 hrs
245 *
246 * @param string $time
247 *
248 * @return bool
249 */
250 public function check_dismiss_time( $time = '' ) {
251 if( empty( $time ) ) {
252 return true;
253 }
254 $current_time = time();
255 $diff = $current_time - 86400;
256 if ( $diff > (int)$time ) {
257 return true;
258 }
259 return false;
260 }
261
262 }
263
264 }
265
266 }
267