| 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 |
* Render notices. |
| 153 |
* |
| 154 |
* @since 1.4.6 |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public function render() { |
| 159 |
foreach ( charitable_get_admin_notices()->get_notices() as $type => $notices ) { |
| 160 |
foreach ( $notices as $key => $notice ) { |
| 161 |
$this->render_notice( $notice['message'], $type, $notice['dismissible'], $key ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Render a notice. |
| 168 |
* |
| 169 |
* @since 1.4.6 |
| 170 |
* |
| 171 |
* @param string $notice |
| 172 |
* @param string $type |
| 173 |
* @param boolean $dismissible |
| 174 |
* @param string $notice_key |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function render_notice( $notice, $type, $dismissible = false, $notice_key = '', $paragraph_tags = true ) { |
| 178 |
if ( ! isset( $this->script_enqueued ) ) { |
| 179 |
if ( ! wp_script_is( 'charitable-admin-notice' ) ) { |
| 180 |
wp_enqueue_script( 'charitable-admin-notice' ); |
| 181 |
} |
| 182 |
|
| 183 |
$this->script_enqueued = true; |
| 184 |
} |
| 185 |
|
| 186 |
$class = 'notice charitable-notice'; |
| 187 |
|
| 188 |
switch ( $type ) { |
| 189 |
case 'error': |
| 190 |
$class .= ' notice-error'; |
| 191 |
break; |
| 192 |
|
| 193 |
case 'warning': |
| 194 |
$class .= ' notice-warning'; |
| 195 |
break; |
| 196 |
|
| 197 |
case 'success': |
| 198 |
$class .= ' updated'; |
| 199 |
break; |
| 200 |
|
| 201 |
case 'info': |
| 202 |
$class .= ' notice-info'; |
| 203 |
break; |
| 204 |
|
| 205 |
case 'five-star-review': |
| 206 |
$class .= ' notice-info notice-five-star-review'; |
| 207 |
break; |
| 208 |
|
| 209 |
case 'version': |
| 210 |
$class .= ' charitable-upgrade-notice'; |
| 211 |
break; |
| 212 |
} |
| 213 |
|
| 214 |
if ( $dismissible ) { |
| 215 |
$class .= ' is-dismissible'; |
| 216 |
} |
| 217 |
|
| 218 |
$body_text = ( $paragraph_tags ) ? '<p>%s</p>': '%s'; |
| 219 |
|
| 220 |
printf( |
| 221 |
'<div class="%s" %s>' . $body_text . '</div>', |
| 222 |
esc_attr( $class ), |
| 223 |
strlen( $notice_key ) ? 'data-notice="' . esc_attr( $notice_key ) . '"' : '', |
| 224 |
$notice |
| 225 |
); |
| 226 |
|
| 227 |
if ( strlen( $notice_key ) ) { |
| 228 |
unset( $this->notices[ $type ][ $notice_key ] ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/** |
| 234 |
* Render a lite to pro banner |
| 235 |
* |
| 236 |
* @since 1.7.0 |
| 237 |
* |
| 238 |
*/ |
| 239 |
public function render_upgrade_banner() { |
| 240 |
if ( charitable_is_pro() ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
$screen = get_current_screen(); |
| 244 |
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 ) ) ) { |
| 245 |
$banner = get_transient( 'charitable_charitablelitetopro_banner' ); |
| 246 |
if ( ! $banner ) { |
| 247 |
$utm_link = charitable_pro_upgrade_url( 'Upgrade From Lite Top Banner Link', 'To unlock more features consider upgrading to Pro.' ); |
| 248 |
$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>. |
| 249 |
','top-of-page', true); |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Render a lite to pro banner |
| 257 |
* |
| 258 |
* @since 1.7.0 |
| 259 |
* |
| 260 |
*/ |
| 261 |
public function render_five_star_rating() { |
| 262 |
|
| 263 |
$screen = get_current_screen(); |
| 264 |
|
| 265 |
// determine if we are on the current screen. |
| 266 |
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 ) ) ) { |
| 267 |
|
| 268 |
$slug = 'five-star-review'; |
| 269 |
|
| 270 |
// 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. |
| 271 |
$activated_datetime = ( false !== get_option( 'wpcharitable_activated_datetime' ) ) ? get_option( 'wpcharitable_activated_datetime' ) : false; |
| 272 |
$days = 0; |
| 273 |
if ( $activated_datetime ) { |
| 274 |
$diff = current_time( 'timestamp' ) - $activated_datetime; |
| 275 |
$days = abs( round( $diff / 86400 ) ); |
| 276 |
} |
| 277 |
|
| 278 |
$count_campaigns = wp_count_posts( 'campaign' ); |
| 279 |
$total_campaigns = isset( $count_campaigns->publish ) ? $count_campaigns->publish : 0; |
| 280 |
$count_donations = wp_count_posts( 'donation' ); |
| 281 |
$total_donations = isset( $count_donations->{'charitable-completed'} ) ? $count_donations->{'charitable-completed'} : 0; |
| 282 |
|
| 283 |
if ( $days >= apply_filters( 'charitable_days_since_activated', 14 ) && $total_campaigns >= 1 && $total_donations >= 1 ) { |
| 284 |
// check transient |
| 285 |
$star_review = get_transient( 'charitable_' . $slug . '_banner' ); |
| 286 |
|
| 287 |
// render five star rating banner/notice |
| 288 |
if ( ! $star_review ) { |
| 289 |
$message = charitable_admin_view('notices/admin-notice-five-star-review', array(), true ); |
| 290 |
$key = 'five-star-review'; |
| 291 |
$this->render_notice( $message, 'five-star-review', true, $key, false ); |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Render a banner. |
| 301 |
* |
| 302 |
* @since 1.7.0 |
| 303 |
* |
| 304 |
*/ |
| 305 |
public function render_banner( $message, $type = 'top-of-page', $dismissible = false, $data_nonce = 'charitablelitetopro', $data_id = 'charitablelitetopro', $data_lifespan = false ) { |
| 306 |
if ( ! isset( $this->script_enqueued ) ) { |
| 307 |
if ( ! wp_script_is( 'charitable-admin-notice' ) ) { |
| 308 |
wp_enqueue_script( 'charitable-admin-notice' ); |
| 309 |
} |
| 310 |
|
| 311 |
$this->script_enqueued = true; |
| 312 |
} |
| 313 |
|
| 314 |
$class = 'charitable-banner'; |
| 315 |
|
| 316 |
switch ( $type ) { |
| 317 |
case 'top-of-page': |
| 318 |
$class .= ' charitable-admin-banner-top-of-page'; |
| 319 |
break; |
| 320 |
} |
| 321 |
|
| 322 |
if ( $dismissible ) { |
| 323 |
// $class .= ' is-dismissible'; |
| 324 |
} |
| 325 |
|
| 326 |
printf( |
| 327 |
'<div class="%s" %s %s %s>%s <button type="button" class="button-link charitable-banner-dismiss">x</button></div>', |
| 328 |
esc_attr( $class ), |
| 329 |
strlen( $data_nonce ) ? 'data-notice="' . esc_attr( $data_nonce ) . '"' : '', |
| 330 |
strlen( $data_id ) ? 'data-id="' . esc_attr( $data_id ) . '"' : '', |
| 331 |
strlen( $data_lifespan ) ? 'data-lifespan="' . esc_attr( $data_lifespan ) . '"' : '', |
| 332 |
$message |
| 333 |
); |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
/** |
| 339 |
* When PHP finishes executing, stash any notices that haven't been rendered yet. |
| 340 |
* |
| 341 |
* @since 1.4.13 |
| 342 |
* |
| 343 |
* @return void |
| 344 |
*/ |
| 345 |
public function shutdown() { |
| 346 |
set_transient( 'charitable_notices', $this->notices ); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Load the notices array. |
| 351 |
* |
| 352 |
* If there are any stuffed in a transient, pull those out. Otherwise, reset a clear array. |
| 353 |
* |
| 354 |
* @since 1.4.13 |
| 355 |
* |
| 356 |
* @return void |
| 357 |
*/ |
| 358 |
public function load_notices() { |
| 359 |
$this->notices = get_transient( 'charitable_notices' ); |
| 360 |
|
| 361 |
if ( ! is_array( $this->notices ) ) { |
| 362 |
$this->clear(); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Fill admin notices from the front-end notices array. |
| 368 |
* |
| 369 |
* @since 1.6.24 |
| 370 |
* |
| 371 |
* @return void |
| 372 |
*/ |
| 373 |
public function fill_notices_from_frontend() { |
| 374 |
$notices = charitable_get_notices(); |
| 375 |
|
| 376 |
foreach ( $notices->get_notices() as $type => $type_notices ) { |
| 377 |
foreach ( $type_notices as $notice ) { |
| 378 |
$this->add_notice( $notice, $type ); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
$notices->clear(); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Clear out all existing notices. |
| 387 |
* |
| 388 |
* @since 1.4.6 |
| 389 |
* |
| 390 |
* @return void |
| 391 |
*/ |
| 392 |
public function clear() { |
| 393 |
$clear = array( |
| 394 |
'error' => array(), |
| 395 |
'warning' => array(), |
| 396 |
'success' => array(), |
| 397 |
'info' => array(), |
| 398 |
'version' => array(), |
| 399 |
); |
| 400 |
|
| 401 |
$this->notices = $clear; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Returns an array of screen IDs where the Charitable notices should be displayed. |
| 406 |
* |
| 407 |
* @uses charitable_admin_screens |
| 408 |
* |
| 409 |
* @since 1.7.0 |
| 410 |
* |
| 411 |
* @return array |
| 412 |
*/ |
| 413 |
public function get_charitable_screens() { |
| 414 |
/** |
| 415 |
* Filter admin screens where Charitable styles & scripts should be loaded. |
| 416 |
* |
| 417 |
* @since 1.7.0 |
| 418 |
* |
| 419 |
* @param string[] $screens List of screen ids. |
| 420 |
*/ |
| 421 |
return apply_filters( 'charitable_admin_notice_screens', array( |
| 422 |
'campaign', |
| 423 |
'donation', |
| 424 |
'charitable_page_charitable-settings', |
| 425 |
'edit-campaign', |
| 426 |
'edit-donation', |
| 427 |
'toplevel_page_charitable' |
| 428 |
) ); |
| 429 |
} |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
endif; |
| 434 |
|