AdminNoticesService.php
169 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Admin\Notices; |
| 4 | |
| 5 | /** |
| 6 | * Admin notices service |
| 7 | */ |
| 8 | class AdminNoticesService { |
| 9 | /** |
| 10 | * Notice key. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $notice_key = 'surecart_dismissed_notice'; |
| 15 | |
| 16 | /** |
| 17 | * Showing the response notice? |
| 18 | * |
| 19 | * @var boolean |
| 20 | */ |
| 21 | protected $showing_response_notice = false; |
| 22 | |
| 23 | /** |
| 24 | * Bootstrap notice dismissal. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function bootstrap() { |
| 29 | add_action( 'admin_init', [ $this, 'dismiss' ] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Is the notice dismissed. |
| 34 | * |
| 35 | * @param string $name Notice name. |
| 36 | * |
| 37 | * @return boolean |
| 38 | */ |
| 39 | public function isDismissed( $name ) { |
| 40 | return (bool) get_option( $this->notice_key . '_' . sanitize_text_field( $name ), false ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Dismiss the notice. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function dismiss() { |
| 49 | // permissions check. |
| 50 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // not our notices, bail. |
| 55 | if ( ! isset( $_GET['surecart_action'] ) || 'dismiss_notices' !== sanitize_text_field( wp_unslash( $_GET['surecart_action'] ) ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | // get notice. |
| 60 | $notice = ! empty( $_GET['surecart_notice'] ) ? sanitize_text_field( wp_unslash( $_GET['surecart_notice'] ) ) : ''; |
| 61 | if ( ! $notice ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // verify nonce. |
| 66 | if ( ! wp_verify_nonce( sanitize_text_field( $_GET['surecart_nonce'] ), 'surecart_notice_nonce' ) ) { |
| 67 | wp_die( esc_html__( 'Your session expired - please try again.', 'surecart' ) ); |
| 68 | exit; |
| 69 | } |
| 70 | |
| 71 | // notice is dismissed. |
| 72 | update_option( $this->notice_key . '_' . sanitize_text_field( $notice ), 1 ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Show the response notice. |
| 77 | * |
| 78 | * @param array $notice Notice data to show. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function showResponseNotice( $notice = [] ) { |
| 83 | // already showing it. |
| 84 | if ( $this->showing_response_notice ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 89 | $notice_data = json_decode( base64_decode( implode( '', (array) $notice ) ) ); |
| 90 | if ( empty( $notice_data->message ) ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $this->showing_response_notice = true; |
| 95 | |
| 96 | add_action( |
| 97 | 'admin_notices', |
| 98 | function() use ( $notice_data ) { |
| 99 | echo wp_kses_post( |
| 100 | $this->render( |
| 101 | [ |
| 102 | 'name' => 'update_notice_' . \SureCart::plugin()->version(), |
| 103 | 'type' => sanitize_text_field( $notice_data->type ), |
| 104 | 'title' => esc_html__( 'SureCart', 'surecart' ), |
| 105 | 'text' => esc_html( $notice_data->message ), |
| 106 | ] |
| 107 | ) |
| 108 | ); |
| 109 | } |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Render the notice |
| 115 | * |
| 116 | * @return string |
| 117 | */ |
| 118 | public function render( $args = [] ) { |
| 119 | $args = wp_parse_args( |
| 120 | $args, |
| 121 | [ |
| 122 | 'title' => '', |
| 123 | 'type' => 'info', |
| 124 | 'text' => '', |
| 125 | 'name' => '', |
| 126 | ] |
| 127 | ); |
| 128 | |
| 129 | if ( $this->isDismissed( $args['name'] ) ) { |
| 130 | return ''; |
| 131 | } |
| 132 | |
| 133 | ob_start(); ?> |
| 134 | |
| 135 | <div class="notice notice-<?php echo sanitize_html_class( $args['type'] ); ?>"> |
| 136 | <p> |
| 137 | <strong> |
| 138 | <?php echo esc_html( $args['title'] ); ?> |
| 139 | </strong> |
| 140 | </p> |
| 141 | |
| 142 | <?php echo wp_kses_post( $args['text'] ); ?> |
| 143 | |
| 144 | <?php if ( ! empty( $args['name'] ) ) : ?> |
| 145 | <p> |
| 146 | <a href=" |
| 147 | <?php |
| 148 | echo esc_url( |
| 149 | add_query_arg( |
| 150 | [ |
| 151 | 'surecart_action' => 'dismiss_notices', |
| 152 | 'surecart_notice' => sanitize_text_field( $args['name'] ), |
| 153 | 'surecart_nonce' => wp_create_nonce( 'surecart_notice_nonce' ), |
| 154 | ] |
| 155 | ) |
| 156 | ); |
| 157 | ?> |
| 158 | "> |
| 159 | <?php esc_html_e( 'Dismiss Notice', 'surecart' ); ?> |
| 160 | </a> |
| 161 | </p> |
| 162 | <?php endif; ?> |
| 163 | </div> |
| 164 | |
| 165 | <?php |
| 166 | return ob_get_clean(); |
| 167 | } |
| 168 | } |
| 169 |