admin
7 years ago
api
7 years ago
deprecated
7 years ago
donors
7 years ago
emails
7 years ago
forms
7 years ago
gateways
7 years ago
libraries
7 years ago
payments
7 years ago
actions.php
7 years ago
ajax-functions.php
7 years ago
class-give-async-process.php
8 years ago
class-give-background-updater.php
7 years ago
class-give-cache.php
7 years ago
class-give-cli-commands.php
8 years ago
class-give-comment.php
7 years ago
class-give-cron.php
8 years ago
class-give-db-donor-meta.php
8 years ago
class-give-db-donors.php
7 years ago
class-give-db-form-meta.php
8 years ago
class-give-db-logs-meta.php
8 years ago
class-give-db-logs.php
7 years ago
class-give-db-meta.php
7 years ago
class-give-db-payment-meta.php
7 years ago
class-give-db-sequential-ordering.php
7 years ago
class-give-db-sessions.php
7 years ago
class-give-db.php
8 years ago
class-give-donate-form.php
8 years ago
class-give-donor-wall-widget.php
7 years ago
class-give-donor.php
7 years ago
class-give-email-access.php
7 years ago
class-give-html-elements.php
7 years ago
class-give-license-handler.php
7 years ago
class-give-logging.php
8 years ago
class-give-readme-parser.php
8 years ago
class-give-roles.php
8 years ago
class-give-scripts.php
7 years ago
class-give-session.php
7 years ago
class-give-stats.php
8 years ago
class-give-template-loader.php
8 years ago
class-give-tooltips.php
8 years ago
class-give-translation.php
8 years ago
class-notices.php
7 years ago
country-functions.php
8 years ago
currency-functions.php
7 years ago
error-tracking.php
8 years ago
filters.php
7 years ago
formatting.php
7 years ago
import-functions.php
7 years ago
install.php
7 years ago
login-register.php
8 years ago
misc-functions.php
7 years ago
plugin-compatibility.php
8 years ago
post-types.php
8 years ago
price-functions.php
7 years ago
process-donation.php
7 years ago
shortcodes.php
7 years ago
template-functions.php
8 years ago
user-functions.php
7 years ago
class-notices.php
724 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Notices Class. |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Notices |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.8.9 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Give_Notices Class |
| 19 | * |
| 20 | * @since 1.8.9 |
| 21 | */ |
| 22 | class Give_Notices { |
| 23 | /** |
| 24 | * List of notices |
| 25 | * @var array |
| 26 | * @since 1.8.9 |
| 27 | * @access private |
| 28 | */ |
| 29 | private static $notices = array(); |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Flag to check if any notice auto dismissible among all notices |
| 34 | * |
| 35 | * @since 1.8.9 |
| 36 | * @access private |
| 37 | * @var bool |
| 38 | */ |
| 39 | private static $has_auto_dismissible_notice = false; |
| 40 | |
| 41 | /** |
| 42 | * Flag to check if any notice has dismiss interval among all notices |
| 43 | * |
| 44 | * @since 1.8.9 |
| 45 | * @access private |
| 46 | * @var bool |
| 47 | */ |
| 48 | private static $has_dismiss_interval_notice = false; |
| 49 | |
| 50 | /** |
| 51 | * Get things started. |
| 52 | * |
| 53 | * @since 1.8.9 |
| 54 | */ |
| 55 | public function __construct() { |
| 56 | add_action( 'admin_notices', array( $this, 'render_admin_notices' ), 999 ); |
| 57 | add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) ); |
| 58 | |
| 59 | add_action( 'give_frontend_notices', array( $this, 'render_frontend_notices' ), 999 ); |
| 60 | add_action( 'give_pre_form_output', array( $this, 'render_frontend_form_notices' ), 10, 1 ); |
| 61 | add_action( 'give_ajax_donation_errors', array( $this, 'render_frontend_notices' ) ); |
| 62 | |
| 63 | /** |
| 64 | * Backward compatibility for deprecated params. |
| 65 | * |
| 66 | * @since 1.8.14 |
| 67 | */ |
| 68 | add_filter( 'give_register_notice_args', array( $this, 'bc_deprecated_params' ) ); |
| 69 | add_filter( 'give_frontend_errors_args', array( $this, 'bc_deprecated_params' ) ); |
| 70 | add_filter( 'give_frontend_notice_args', array( $this, 'bc_deprecated_params' ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Add backward compatibility to deprecated params. |
| 75 | * |
| 76 | * @since 1.8.14 |
| 77 | * @access public |
| 78 | * |
| 79 | * @param array $args Array of notice params |
| 80 | * |
| 81 | * @return array |
| 82 | */ |
| 83 | public function bc_deprecated_params( $args ) { |
| 84 | /** |
| 85 | * Param: auto_dismissible |
| 86 | * deprecated in 1.8.14 |
| 87 | * |
| 88 | * Check if auto_dismissible is set and it true then unset and change dismissible parameter value to auto |
| 89 | */ |
| 90 | if ( isset( $args['auto_dismissible'] ) ) { |
| 91 | if ( ! empty( $args['auto_dismissible'] ) ) { |
| 92 | $args['dismissible'] = 'auto'; |
| 93 | } |
| 94 | // unset auto_dismissible as it has been deprecated. |
| 95 | unset( $args['auto_dismissible'] ); |
| 96 | } |
| 97 | |
| 98 | return $args; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Register notice. |
| 103 | * |
| 104 | * @since 1.8.9 |
| 105 | * @access public |
| 106 | * |
| 107 | * @param $notice_args |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | public function register_notice( $notice_args ) { |
| 112 | // Bailout. |
| 113 | if ( empty( $notice_args['id'] ) || array_key_exists( $notice_args['id'], self::$notices ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | $notice_args = wp_parse_args( |
| 118 | $notice_args, |
| 119 | array( |
| 120 | 'id' => '', |
| 121 | 'description' => '', |
| 122 | |
| 123 | /* |
| 124 | * Add custom notice html |
| 125 | * Note: This param has more priority then description, so if you have both param then this one will be use |
| 126 | * for generating notice html. Most of feature of notice attach to core generated html, so if you set |
| 127 | * custom html then please add required classes and data attribute which help to apply feature on notice. |
| 128 | * |
| 129 | * @since 1.8.16 |
| 130 | */ |
| 131 | 'description_html' => '', |
| 132 | |
| 133 | /* |
| 134 | * Add New Parameter and remove the auto_dismissible parameter. |
| 135 | * Value: auto/true/false |
| 136 | * |
| 137 | * @since 1.8.14 |
| 138 | */ |
| 139 | 'dismissible' => true, |
| 140 | |
| 141 | // Value: error/warning/success/info/updated |
| 142 | 'type' => 'error', |
| 143 | |
| 144 | // Value: null/user/all |
| 145 | 'dismissible_type' => null, |
| 146 | |
| 147 | // Value: shortly/permanent/null/custom |
| 148 | 'dismiss_interval' => null, |
| 149 | |
| 150 | // Only set it when custom is defined. |
| 151 | 'dismiss_interval_time' => null, |
| 152 | |
| 153 | |
| 154 | ) |
| 155 | ); |
| 156 | |
| 157 | /** |
| 158 | * Filter to modify Notice args before it get add |
| 159 | * |
| 160 | * @since 1.8.14 |
| 161 | */ |
| 162 | $notice_args = apply_filters( 'give_register_notice_args', $notice_args ); |
| 163 | |
| 164 | // Set extra dismiss links if any. |
| 165 | if ( false !== strpos( $notice_args['description'], 'data-dismiss-interval' ) ) { |
| 166 | |
| 167 | preg_match_all( "/data-([^\"]*)=\"([^\"]*)\"/", $notice_args['description'], $extra_notice_dismiss_link ); |
| 168 | |
| 169 | if ( ! empty( $extra_notice_dismiss_link ) ) { |
| 170 | $extra_notice_dismiss_links = array_chunk( current( $extra_notice_dismiss_link ), 3 ); |
| 171 | foreach ( $extra_notice_dismiss_links as $extra_notice_dismiss_link ) { |
| 172 | // Create array og key ==> value by parsing query string created after renaming data attributes. |
| 173 | $data_attribute_query_str = str_replace( array( 'data-', '-', '"' ), array( |
| 174 | '', |
| 175 | '_', |
| 176 | '', |
| 177 | ), implode( '&', $extra_notice_dismiss_link ) ); |
| 178 | |
| 179 | $notice_args['extra_links'][] = wp_parse_args( $data_attribute_query_str ); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | self::$notices[ $notice_args['id'] ] = $notice_args; |
| 186 | |
| 187 | // Auto set show param if not already set. |
| 188 | if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) { |
| 189 | self::$notices[ $notice_args['id'] ]['show'] = $this->is_notice_dismissed( $notice_args ) ? false : true; |
| 190 | } |
| 191 | |
| 192 | // Auto set time interval for shortly. |
| 193 | if ( 'shortly' === self::$notices[ $notice_args['id'] ]['dismiss_interval'] ) { |
| 194 | self::$notices[ $notice_args['id'] ]['dismiss_interval_time'] = DAY_IN_SECONDS; |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Display notice. |
| 202 | * |
| 203 | * @since 1.8.9 |
| 204 | * |
| 205 | */ |
| 206 | public function render_admin_notices() { |
| 207 | // Bailout. |
| 208 | if ( empty( self::$notices ) ) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $output = ''; |
| 213 | |
| 214 | foreach ( self::$notices as $notice_id => $notice ) { |
| 215 | // Check flag set to true to show notice. |
| 216 | if ( ! $notice['show'] ) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | // Render custom html. |
| 222 | if( ! empty( $notice['description_html'] ) ) { |
| 223 | $output .= "{$notice['description_html']} \n"; |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | // Check if notice dismissible or not. |
| 228 | if ( ! self::$has_auto_dismissible_notice ) { |
| 229 | self::$has_auto_dismissible_notice = ( 'auto' === $notice['dismissible'] ); |
| 230 | } |
| 231 | |
| 232 | // Check if notice dismissible or not. |
| 233 | if ( ! self::$has_dismiss_interval_notice ) { |
| 234 | self::$has_dismiss_interval_notice = $notice['dismiss_interval']; |
| 235 | } |
| 236 | |
| 237 | $css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] ); |
| 238 | |
| 239 | $css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}"; |
| 240 | $output .= sprintf( |
| 241 | '<div id="%1$s" class="%2$s" data-dismissible="%3$s" data-dismissible-type="%4$s" data-dismiss-interval="%5$s" data-notice-id="%6$s" data-security="%7$s" data-dismiss-interval-time="%8$s" style="display: none">' . " \n", |
| 242 | $css_id, |
| 243 | $css_class, |
| 244 | give_clean( $notice['dismissible'] ), |
| 245 | $notice['dismissible_type'], |
| 246 | $notice['dismiss_interval'], |
| 247 | $notice['id'], |
| 248 | empty( $notice['dismissible_type'] ) ? '' : wp_create_nonce( "give_edit_{$notice_id}_notice" ), |
| 249 | $notice['dismiss_interval_time'] |
| 250 | ); |
| 251 | |
| 252 | $output .= ( 0 === strpos( $notice['description'], '<div' ) || 0 === strpos( $notice['description'], '<p' ) ? $notice['description'] : "<p>{$notice['description']}</p>" ); |
| 253 | $output .= "</div> \n"; |
| 254 | } |
| 255 | |
| 256 | echo $output; |
| 257 | |
| 258 | $this->print_js(); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /** |
| 263 | * Render give frontend notices. |
| 264 | * |
| 265 | * @since 1.8.9 |
| 266 | * @access public |
| 267 | * |
| 268 | * @param int $form_id |
| 269 | */ |
| 270 | public function render_frontend_notices( $form_id = 0 ) { |
| 271 | $errors = give_get_errors(); |
| 272 | |
| 273 | $request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0; |
| 274 | |
| 275 | // Sanity checks first: Ensure that gateway returned errors display on the appropriate form. |
| 276 | if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | if ( $errors ) { |
| 281 | self::print_frontend_errors( $errors ); |
| 282 | |
| 283 | give_clear_errors(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Renders notices for different actions depending on |
| 289 | * the type of form display option. |
| 290 | * |
| 291 | * @since 2.2 |
| 292 | * @access public |
| 293 | * |
| 294 | * @param int $form_id Form ID. |
| 295 | * |
| 296 | * @return void |
| 297 | */ |
| 298 | public function render_frontend_form_notices( $form_id ) { |
| 299 | $display_option = give_get_meta( $form_id, '_give_payment_display', true ); |
| 300 | |
| 301 | if ( 'modal' === $display_option ) { |
| 302 | add_action( 'give_payment_mode_top', array( $this, 'render_frontend_notices' ) ); |
| 303 | } else { |
| 304 | add_action( 'give_pre_form', array( $this, 'render_frontend_notices' ), 11 ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Print notice js. |
| 310 | * |
| 311 | * @since 1.8.9 |
| 312 | * @access private |
| 313 | */ |
| 314 | private function print_js() { |
| 315 | if ( self::$has_auto_dismissible_notice ) : |
| 316 | ?> |
| 317 | <script> |
| 318 | jQuery(document).ready(function () { |
| 319 | // auto hide setting message in 5 seconds. |
| 320 | window.setTimeout( |
| 321 | function () { |
| 322 | jQuery('.give-notice[data-dismissible="auto"]').slideUp(); |
| 323 | }, |
| 324 | 5000 |
| 325 | ); |
| 326 | }) |
| 327 | </script> |
| 328 | <?php |
| 329 | endif; |
| 330 | |
| 331 | if ( self::$has_dismiss_interval_notice ) : |
| 332 | ?> |
| 333 | <script> |
| 334 | jQuery(document).ready(function () { |
| 335 | var $body = jQuery('body'); |
| 336 | |
| 337 | $body.on('click', '.give_dismiss_notice', function (e) { |
| 338 | var $parent = jQuery(this).parents('.give-notice'), |
| 339 | custom_notice_data = { |
| 340 | 'dismissible_type': jQuery(this).data('dismissible-type'), |
| 341 | 'dismiss_interval': jQuery(this).data('dismiss-interval'), |
| 342 | 'dismiss_interval_time': jQuery(this).data('dismiss-interval-time') |
| 343 | }; |
| 344 | |
| 345 | $parent.find('button.notice-dismiss').trigger('click', [custom_notice_data]); |
| 346 | return false; |
| 347 | }); |
| 348 | |
| 349 | $body.on('click', 'button.notice-dismiss', function (e, custom_notice_data) { |
| 350 | var $parent = jQuery(this).parents('.give-notice'), |
| 351 | custom_notice_data = custom_notice_data || {}; |
| 352 | |
| 353 | e.preventDefault(); |
| 354 | |
| 355 | var data = { |
| 356 | 'give-action': 'dismiss_notices', |
| 357 | 'notice_id': $parent.data('notice-id'), |
| 358 | 'dismissible_type': $parent.data('dismissible-type'), |
| 359 | 'dismiss_interval': $parent.data('dismiss-interval'), |
| 360 | 'dismiss_interval_time': $parent.data('dismiss-interval-time'), |
| 361 | '_wpnonce': $parent.data('security') |
| 362 | }; |
| 363 | |
| 364 | if (Object.keys(custom_notice_data).length) { |
| 365 | jQuery.extend(data, custom_notice_data); |
| 366 | } |
| 367 | |
| 368 | // Bailout. |
| 369 | if ( |
| 370 | !data.dismiss_interval || |
| 371 | !data.dismissible_type |
| 372 | ) { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | jQuery.post( |
| 377 | '<?php echo admin_url(); ?>admin-ajax.php', |
| 378 | data, |
| 379 | function (response) { |
| 380 | |
| 381 | }) |
| 382 | }) |
| 383 | }); |
| 384 | </script> |
| 385 | <?php |
| 386 | endif; |
| 387 | ?> |
| 388 | <script> |
| 389 | jQuery(document).ready(function($){ |
| 390 | // Fix notice appearance issue. |
| 391 | window.setTimeout( |
| 392 | function(){ |
| 393 | $('.give-notice').slideDown(); |
| 394 | }, |
| 395 | 1000 |
| 396 | ); |
| 397 | }); |
| 398 | </script> |
| 399 | <?php |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /** |
| 404 | * Hide notice. |
| 405 | * |
| 406 | * @since 1.8.9 |
| 407 | * @access public |
| 408 | */ |
| 409 | public function dismiss_notices() { |
| 410 | $_post = give_clean( $_POST ); |
| 411 | $notice_id = esc_attr( $_post['notice_id'] ); |
| 412 | |
| 413 | // Bailout. |
| 414 | if ( |
| 415 | empty( $notice_id ) || |
| 416 | empty( $_post['dismissible_type'] ) || |
| 417 | empty( $_post['dismiss_interval'] ) || |
| 418 | ! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' ) |
| 419 | ) { |
| 420 | wp_send_json_error(); |
| 421 | } |
| 422 | |
| 423 | $notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] ); |
| 424 | if ( 'user' === $_post['dismissible_type'] ) { |
| 425 | $current_user = wp_get_current_user(); |
| 426 | $notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID ); |
| 427 | } |
| 428 | |
| 429 | $notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null; |
| 430 | |
| 431 | // Save option to hide notice. |
| 432 | Give_Cache::set( $notice_key, true, $notice_dismiss_time, true ); |
| 433 | |
| 434 | /** |
| 435 | * Fire the action when notice dismissed |
| 436 | * |
| 437 | * @since 2.2.0 |
| 438 | */ |
| 439 | do_action( 'give_notice_dismissed', $_post ); |
| 440 | |
| 441 | wp_send_json_success(); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * Get notice key. |
| 447 | * |
| 448 | * @since 1.8.9 |
| 449 | * @access public |
| 450 | * |
| 451 | * @param string $notice_id |
| 452 | * @param string $dismiss_interval |
| 453 | * @param int $user_id |
| 454 | * |
| 455 | * @return string |
| 456 | */ |
| 457 | public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) { |
| 458 | $notice_key = "_give_notice_{$notice_id}"; |
| 459 | |
| 460 | if ( ! empty( $dismiss_interval ) ) { |
| 461 | $notice_key .= "_{$dismiss_interval}"; |
| 462 | } |
| 463 | |
| 464 | if ( $user_id ) { |
| 465 | $notice_key .= "_{$user_id}"; |
| 466 | } |
| 467 | |
| 468 | $notice_key = sanitize_key( $notice_key ); |
| 469 | |
| 470 | return $notice_key; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /** |
| 475 | * Get notice dismiss link. |
| 476 | * |
| 477 | * @param $notice_args |
| 478 | * |
| 479 | * @return string |
| 480 | */ |
| 481 | public function get_dismiss_link( $notice_args ) { |
| 482 | $notice_args = wp_parse_args( |
| 483 | $notice_args, |
| 484 | array( |
| 485 | 'title' => __( 'Click here', 'give' ), |
| 486 | 'dismissible_type' => '', |
| 487 | 'dismiss_interval' => '', |
| 488 | 'dismiss_interval_time' => null, |
| 489 | ) |
| 490 | ); |
| 491 | |
| 492 | return sprintf( |
| 493 | '<a href="#" class="give_dismiss_notice" data-dismissible-type="%1$s" data-dismiss-interval="%2$s" data-dismiss-interval-time="%3$s">%4$s</a>', |
| 494 | $notice_args['dismissible_type'], |
| 495 | $notice_args['dismiss_interval'], |
| 496 | $notice_args['dismiss_interval_time'], |
| 497 | $notice_args['title'] |
| 498 | ); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | /** |
| 503 | * Check if notice dismissed or not |
| 504 | * |
| 505 | * @since 1.8.9 |
| 506 | * @access public |
| 507 | * |
| 508 | * @param array $notice |
| 509 | * |
| 510 | * @return bool|null |
| 511 | */ |
| 512 | public function is_notice_dismissed( $notice ) { |
| 513 | $notice_key = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] ); |
| 514 | $is_notice_dismissed = false; |
| 515 | |
| 516 | if ( 'user' === $notice['dismissible_type'] ) { |
| 517 | $current_user = wp_get_current_user(); |
| 518 | $notice_key = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID ); |
| 519 | } |
| 520 | |
| 521 | $notice_data = Give_Cache::get( $notice_key, true ); |
| 522 | |
| 523 | // Find notice dismiss link status if notice has extra dismissible links. |
| 524 | if ( ( empty( $notice_data ) || is_wp_error( $notice_data ) ) && ! empty( $notice['extra_links'] ) ) { |
| 525 | |
| 526 | foreach ( $notice['extra_links'] as $extra_link ) { |
| 527 | $new_notice_data = wp_parse_args( $extra_link, $notice ); |
| 528 | unset( $new_notice_data['extra_links'] ); |
| 529 | |
| 530 | if ( $is_notice_dismissed = $this->is_notice_dismissed( $new_notice_data ) ) { |
| 531 | return $is_notice_dismissed; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | $is_notice_dismissed = ! empty( $notice_data ) && ! is_wp_error( $notice_data ); |
| 537 | |
| 538 | return $is_notice_dismissed; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /** |
| 543 | * Print frontend errors. |
| 544 | * |
| 545 | * @since 1.8.9 |
| 546 | * @access public |
| 547 | * |
| 548 | * @param array $errors |
| 549 | */ |
| 550 | public static function print_frontend_errors( $errors ) { |
| 551 | // Bailout. |
| 552 | if ( ! $errors ) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Change auto_dismissible to dismissible and set the value to true |
| 558 | * |
| 559 | * @since 1.8.14 |
| 560 | */ |
| 561 | $default_notice_args = array( |
| 562 | 'dismissible' => true, |
| 563 | 'dismiss_interval' => 5000, |
| 564 | ); |
| 565 | |
| 566 | // Note: we will remove give_errors class in future. |
| 567 | $classes = apply_filters( 'give_error_class', array( 'give_notices', 'give_errors' ) ); |
| 568 | |
| 569 | echo sprintf( '<div class="%s">', implode( ' ', $classes ) ); |
| 570 | |
| 571 | // Loop error codes and display errors. |
| 572 | foreach ( $errors as $error_id => $error ) { |
| 573 | // Backward compatibility v<1.8.11 |
| 574 | if ( is_string( $error ) ) { |
| 575 | $error = array( |
| 576 | 'message' => $error, |
| 577 | 'notice_args' => array(), |
| 578 | ); |
| 579 | } |
| 580 | |
| 581 | $notice_args = wp_parse_args( $error['notice_args'], $default_notice_args ); |
| 582 | |
| 583 | /** |
| 584 | * Filter to modify Frontend Errors args before errors is display. |
| 585 | * |
| 586 | * @since 1.8.14 |
| 587 | */ |
| 588 | $notice_args = apply_filters( 'give_frontend_errors_args', $notice_args ); |
| 589 | |
| 590 | echo sprintf( |
| 591 | '<div class="give_error give_notice" id="give_error_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d"> |
| 592 | <p><strong>%4$s</strong>: %5$s</p> |
| 593 | </div>', |
| 594 | $error_id, |
| 595 | give_clean( $notice_args['dismissible'] ), |
| 596 | absint( $notice_args['dismiss_interval'] ), |
| 597 | esc_html__( 'Error', 'give' ), |
| 598 | $error['message'] |
| 599 | ); |
| 600 | } |
| 601 | |
| 602 | echo '</div>'; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Print frontend notice. |
| 607 | * Notice: notice type can be success/error/warning |
| 608 | * |
| 609 | * @since 1.8.9 |
| 610 | * @access public |
| 611 | * |
| 612 | * @param string $message |
| 613 | * @param bool $echo |
| 614 | * @param string $notice_type |
| 615 | * @param array $notice_args |
| 616 | * |
| 617 | * @return string |
| 618 | */ |
| 619 | public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = array() ) { |
| 620 | if ( empty( $message ) ) { |
| 621 | return ''; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Change auto_dismissible to dismissible and set the value to true |
| 626 | * |
| 627 | * @since 1.8.14 |
| 628 | */ |
| 629 | $default_notice_args = array( |
| 630 | 'dismissible' => false, |
| 631 | 'dismiss_type' => 'auto', |
| 632 | 'dismiss_interval' => 5000, |
| 633 | ); |
| 634 | |
| 635 | $notice_args = wp_parse_args( $notice_args, $default_notice_args ); |
| 636 | |
| 637 | // Notice dismissible must be true for dismiss type. |
| 638 | $notice_args['dismiss_type'] = ! $notice_args['dismissible'] ? '' : $notice_args['dismiss_type']; |
| 639 | |
| 640 | /** |
| 641 | * Filter to modify Frontend notice args before notices is display. |
| 642 | * |
| 643 | * @since 1.8.14 |
| 644 | */ |
| 645 | $notice_args = apply_filters( 'give_frontend_notice_args', $notice_args ); |
| 646 | |
| 647 | $close_icon = 'manual' === $notice_args['dismiss_type'] ? |
| 648 | sprintf( |
| 649 | '<img class="notice-dismiss give-notice-close" src="%s" />', |
| 650 | esc_url( GIVE_PLUGIN_URL . 'assets/dist/images/close.svg' ) |
| 651 | |
| 652 | ) : |
| 653 | ''; |
| 654 | |
| 655 | // Note: we will remove give_errors class in future. |
| 656 | $error = sprintf( |
| 657 | '<div class="give_notices give_errors" id="give_error_%1$s"> |
| 658 | <p class="give_notice give_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d" data-dismiss-type="%4$s"> |
| 659 | %5$s |
| 660 | </p> |
| 661 | %6$s |
| 662 | </div>', |
| 663 | $notice_type, |
| 664 | give_clean( $notice_args['dismissible'] ), |
| 665 | absint( $notice_args['dismiss_interval'] ), |
| 666 | give_clean( $notice_args['dismiss_type'] ), |
| 667 | $message, |
| 668 | $close_icon |
| 669 | |
| 670 | ); |
| 671 | |
| 672 | if ( ! $echo ) { |
| 673 | return $error; |
| 674 | } |
| 675 | |
| 676 | echo $error; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Print Inline Notice. |
| 681 | * Note: dismissible feature will note work if notice will add to dom by javascript after document load. |
| 682 | * |
| 683 | * @param array $notice_args An array of notice arguments. |
| 684 | * |
| 685 | * @todo Implement render_admin_notices function within this function in future. |
| 686 | * |
| 687 | * @access public |
| 688 | * @since 1.8.17 |
| 689 | * |
| 690 | * @return string |
| 691 | */ |
| 692 | public function print_admin_notices( $notice_args = array() ) { |
| 693 | // Bailout. |
| 694 | if ( empty( $notice_args['description'] ) ) { |
| 695 | return ''; |
| 696 | } |
| 697 | |
| 698 | $defaults = array( |
| 699 | 'id' => '', |
| 700 | 'echo' => true, |
| 701 | 'notice_type' => 'warning', |
| 702 | 'dismissible' => true, |
| 703 | ); |
| 704 | $notice_args = wp_parse_args( $notice_args, $defaults ); |
| 705 | |
| 706 | $output = ''; |
| 707 | $css_id = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' ); |
| 708 | $css_class = "notice-{$notice_args['notice_type']} give-notice notice inline"; |
| 709 | $css_class .= ( $notice_args['dismissible'] ) ? ' is-dismissible' : ''; |
| 710 | $output .= sprintf( |
| 711 | '<div id="%1$s" class="%2$s"><p>%3$s</p></div>', |
| 712 | $css_id, |
| 713 | $css_class, |
| 714 | $notice_args['description'] |
| 715 | ); |
| 716 | |
| 717 | if ( ! $notice_args['echo'] ) { |
| 718 | return $output; |
| 719 | } |
| 720 | |
| 721 | echo $output; |
| 722 | } |
| 723 | } |
| 724 |