PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / admin / class-charitable-admin-notices.php

class-charitable-admin-notices.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.11, at includes/admin/class-charitable-admin-notices.php

449 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Contains the class that is used to register and retrieve notices in the admin like errors, warnings, success messages, etc.
4 *
5 * @package Charitable/Classes/Charitable_Admin_Notices
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.4.6
10 * @version 1.6.24
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Admin_Notices' ) ) :
19
20 /**
21 * Charitable_Admin_Notices
22 *
23 * @since 1.4.6
24 */
25 class Charitable_Admin_Notices extends Charitable_Notices {
26
27 /**
28 * Whether the script has been enqueued.
29 *
30 * @since 1.4.6
31 *
32 * @var boolean
33 */
34 private $script_enqueued;
35
36 /**
37 * Returns and/or create the single instance of this class.
38 *
39 * @since 1.4.6
40 *
41 * @return Charitable_Admin_Notices
42 */
43 public static function get_instance() {
44 return charitable()->registry()->get( 'admin_notices' );
45 }
46
47 /**
48 * Create class object. A private constructor, so this is used in a singleton context.
49 *
50 * @since 1.4.6
51 * @since 1.5.4 Access changed to public.
52 */
53 public function __construct() {
54 $this->load_notices();
55
56 add_action( 'admin_notices', array( $this, 'render_upgrade_banner' ) );
57 add_action( 'admin_notices', array( $this, 'render_five_star_rating' ) );
58
59 }
60
61 /**
62 * Adds a notice message.
63 *
64 * @since 1.4.6
65 *
66 * @param string $message
67 * @param string $type
68 * @param string $key Optional. If not set, next numeric key is used.
69 * @return void
70 */
71 public function add_notice( $message, $type, $key = false, $dismissible = false ) {
72 if ( false === $key ) {
73 $this->notices[ $type ][] = array(
74 'message' => $message,
75 'dismissible' => $dismissible,
76 );
77 } else {
78 $this->notices[ $type ][ $key ] = array(
79 'message' => $message,
80 'dismissible' => $dismissible,
81 );
82 }
83 }
84
85 /**
86 * Adds an error message.
87 *
88 * @since 1.4.6
89 *
90 * @param string $message
91 * @param string $key Optional. If not set, next numeric key is used.
92 * @return void
93 */
94 public function add_error( $message, $key = false, $dismissible = false ) {
95 $this->add_notice( $message, 'error', $key, $dismissible );
96 }
97
98 /**
99 * Adds a warning message.
100 *
101 * @since 1.4.6
102 *
103 * @param string $message
104 * @param string $key Optional. If not set, next numeric key is used.
105 * @return void
106 */
107 public function add_warning( $message, $key = false, $dismissible = false ) {
108 $this->add_notice( $message, 'warning', $key, $dismissible );
109 }
110
111 /**
112 * Adds a success message.
113 *
114 * @since 1.4.6
115 *
116 * @param string $message
117 * @param string $key Optional. If not set, next numeric key is used.
118 * @return void
119 */
120 public function add_success( $message, $key = false, $dismissible = false ) {
121 $this->add_notice( $message, 'success', $key, $dismissible );
122 }
123
124 /**
125 * Adds an info message.
126 *
127 * @since 1.4.6
128 *
129 * @param string $message
130 * @param string $key Optional. If not set, next numeric key is used.
131 * @return void
132 */
133 public function add_info( $message, $key = false, $dismissible = false ) {
134 $this->add_notice( $message, 'info', $key, $dismissible );
135 }
136
137 /**
138 * Adds a version update message.
139 *
140 * @since 1.4.6
141 *
142 * @param string $message
143 * @param string $key Optional. If not set, next numeric key is used.
144 * @param boolean $dismissible Optional. Set to true by default.
145 * @return void
146 */
147 public function add_version_update( $message, $key = false, $dismissible = true ) {
148 $this->add_notice( $message, 'version', $key, $dismissible );
149 }
150
151 /**
152 * Adds a third party warning message.
153 *
154 * @since 1.7.0.8
155 *
156 * @param string $message
157 * @param string $key Optional. If not set, next numeric key is used.
158 * @param boolean $dismissible Optional. Set to true by default.
159 * @return void
160 */
161 public function add_third_party_warning( $message, $key = false, $dismissible = true ) {
162 $this->add_notice( $message, 'warning', $key, $dismissible );
163 }
164
165 /**
166 * Render notices.
167 *
168 * @since 1.4.6
169 *
170 * @return void
171 */
172 public function render() {
173 foreach ( charitable_get_admin_notices()->get_notices() as $type => $notices ) {
174 foreach ( $notices as $key => $notice ) {
175 $this->render_notice( $notice['message'], $type, $notice['dismissible'], $key );
176 }
177 }
178 }
179
180 /**
181 * Render a notice.
182 *
183 * @since 1.4.6
184 *
185 * @param string $notice
186 * @param string $type
187 * @param boolean $dismissible
188 * @param string $notice_key
189 * @return void
190 */
191 public function render_notice( $notice, $type, $dismissible = false, $notice_key = '', $paragraph_tags = true ) {
192 if ( ! isset( $this->script_enqueued ) ) {
193 if ( ! wp_script_is( 'charitable-admin-notice' ) ) {
194 wp_enqueue_script( 'charitable-admin-notice' );
195 }
196
197 $this->script_enqueued = true;
198 }
199
200 $class = 'notice charitable-notice';
201
202 switch ( $type ) {
203 case 'error':
204 $class .= ' notice-error';
205 break;
206
207 case 'warning':
208 $class .= ' notice-warning';
209 break;
210
211 case 'success':
212 $class .= ' updated';
213 break;
214
215 case 'info':
216 $class .= ' notice-info';
217 break;
218
219 case 'five-star-review':
220 $class .= ' notice-info notice-five-star-review';
221 break;
222
223 case 'version':
224 $class .= ' charitable-upgrade-notice';
225 break;
226 }
227
228 if ( $dismissible ) {
229 $class .= ' is-dismissible';
230 }
231
232 $body_text = ( $paragraph_tags ) ? '<p>%s</p>': '%s';
233
234 printf(
235 '<div class="%s" %s>' . $body_text . '</div>',
236 esc_attr( $class ),
237 strlen( $notice_key ) ? 'data-notice="' . esc_attr( $notice_key ) . '"' : '',
238 $notice
239 );
240
241 if ( strlen( $notice_key ) ) {
242 unset( $this->notices[ $type ][ $notice_key ] );
243 }
244 }
245
246
247 /**
248 * Render a lite to pro banner
249 *
250 * @since 1.7.0
251 *
252 */
253 public function render_upgrade_banner() {
254 if ( charitable_is_pro() ) {
255 return;
256 }
257 $screen = get_current_screen();
258 if ( ! is_null( $screen ) && ( in_array( $screen->id, $this->get_charitable_screens() ) || ( isset( $screen->taxonomy ) && 'campaign_category' === $screen->taxonomy ) || ( isset( $screen->taxonomy ) && 'campaign_tag' === $screen->taxonomy ) ) ) {
259 $banner = get_transient( 'charitable_charitablelitetopro_banner' );
260 if ( ! $banner ) {
261 $utm_link = charitable_pro_upgrade_url( 'Upgrade From Lite Top Banner Link', 'To unlock more features consider upgrading to Pro.' );
262 $this->render_banner('You\'re using WP Charitable Lite. To unlock more features consider <a href="' . $utm_link . '" target="_blank" rel="noopener noreferrer">upgrading to Pro</a>.
263 ','top-of-page', true);
264 }
265
266 }
267 }
268
269 /**
270 * Render a lite to pro banner
271 *
272 * @since 1.7.0
273 *
274 */
275 public function render_five_star_rating() {
276
277 $screen = get_current_screen();
278
279 // determine if we are on the current screen.
280 if ( ! is_null( $screen ) && ( in_array( $screen->id, $this->get_charitable_screens() ) || ( isset( $screen->taxonomy ) && 'campaign_category' === $screen->taxonomy ) || ( isset( $screen->taxonomy ) && 'campaign_tag' === $screen->taxonomy ) ) ) {
281
282 $slug = 'five-star-review';
283
284 // determine when to display this message. for now, there should be some sensible boundaries before showing the notification: a minimum of 14 days of use, created one donation form and received at least one donation.
285 $activated_datetime = ( false !== get_option( 'wpcharitable_activated_datetime' ) ) ? get_option( 'wpcharitable_activated_datetime' ) : false;
286 $days = 0;
287 if ( $activated_datetime ) {
288 $diff = current_time( 'timestamp' ) - $activated_datetime;
289 $days = abs( round( $diff / 86400 ) );
290 }
291
292 $count_campaigns = wp_count_posts( 'campaign' );
293 $total_campaigns = isset( $count_campaigns->publish ) ? $count_campaigns->publish : 0;
294 $count_donations = wp_count_posts( 'donation' );
295 $total_donations = isset( $count_donations->{'charitable-completed'} ) ? $count_donations->{'charitable-completed'} : 0;
296
297 if ( $days >= apply_filters( 'charitable_days_since_activated', 14 ) && $total_campaigns >= 1 && $total_donations >= 1 ) {
298 // check transient
299 $star_review = get_transient( 'charitable_' . $slug . '_banner' );
300
301 // render five star rating banner/notice
302 if ( $star_review ) {
303 $message = charitable_admin_view('notices/admin-notice-five-star-review', array(), true );
304 $key = 'five-star-review';
305 $this->render_notice( $message, 'five-star-review', true, $key, false );
306 }
307
308 }
309
310 }
311 }
312
313 /**
314 * Render a banner.
315 *
316 * @since 1.7.0
317 *
318 */
319 public function render_banner( $message, $type = 'top-of-page', $dismissible = false, $data_nonce = 'charitablelitetopro', $data_id = 'charitablelitetopro', $data_lifespan = false ) {
320 if ( ! isset( $this->script_enqueued ) ) {
321 if ( ! wp_script_is( 'charitable-admin-notice' ) ) {
322 wp_enqueue_script( 'charitable-admin-notice' );
323 }
324
325 $this->script_enqueued = true;
326 }
327
328 $class = 'charitable-banner';
329
330 switch ( $type ) {
331 case 'top-of-page':
332 $class .= ' charitable-admin-banner-top-of-page';
333 break;
334 }
335
336 if ( $dismissible ) {
337 // $class .= ' is-dismissible';
338 }
339
340 printf(
341 '<div class="%s" %s %s %s>%s <button type="button" class="button-link charitable-banner-dismiss">x</button></div>',
342 esc_attr( $class ),
343 strlen( $data_nonce ) ? 'data-notice="' . esc_attr( $data_nonce ) . '"' : '',
344 strlen( $data_id ) ? 'data-id="' . esc_attr( $data_id ) . '"' : '',
345 strlen( $data_lifespan ) ? 'data-lifespan="' . esc_attr( $data_lifespan ) . '"' : '',
346 $message
347 );
348
349 }
350
351
352 /**
353 * When PHP finishes executing, stash any notices that haven't been rendered yet.
354 *
355 * @since 1.4.13
356 *
357 * @return void
358 */
359 public function shutdown() {
360 set_transient( 'charitable_notices', $this->notices );
361 }
362
363 /**
364 * Load the notices array.
365 *
366 * If there are any stuffed in a transient, pull those out. Otherwise, reset a clear array.
367 *
368 * @since 1.4.13
369 *
370 * @return void
371 */
372 public function load_notices() {
373 $this->notices = get_transient( 'charitable_notices' );
374
375 if ( ! is_array( $this->notices ) ) {
376 $this->clear();
377 }
378 }
379
380 /**
381 * Fill admin notices from the front-end notices array.
382 *
383 * @since 1.6.24
384 *
385 * @return void
386 */
387 public function fill_notices_from_frontend() {
388 $notices = charitable_get_notices();
389
390 foreach ( $notices->get_notices() as $type => $type_notices ) {
391 foreach ( $type_notices as $notice ) {
392 $this->add_notice( $notice, $type );
393 }
394 }
395
396 $notices->clear();
397 }
398
399 /**
400 * Clear out all existing notices.
401 *
402 * @since 1.4.6
403 *
404 * @return void
405 */
406 public function clear() {
407 $clear = array(
408 'error' => array(),
409 'warning' => array(),
410 'success' => array(),
411 'info' => array(),
412 'version' => array(),
413 );
414
415 $this->notices = $clear;
416 }
417
418 /**
419 * Returns an array of screen IDs where the Charitable notices should be displayed.
420 *
421 * @uses charitable_admin_screens
422 *
423 * @since 1.7.0
424 *
425 * @return array
426 */
427 public function get_charitable_screens() {
428 /**
429 * Filter admin screens where Charitable styles & scripts should be loaded.
430 *
431 * @since 1.7.0
432 *
433 * @param string[] $screens List of screen ids.
434 */
435 return apply_filters( 'charitable_admin_notice_screens', array(
436 'campaign',
437 'donation',
438 'charitable_page_charitable-settings',
439 'edit-campaign',
440 'edit-donation',
441 'toplevel_page_charitable',
442 'charitable_page_charitable-addons'
443 ) );
444 }
445
446 }
447
448 endif;
449