class-auxels-system-check.php
6 months ago
class-auxin-cli-commands.php
1 year ago
class-auxin-license-activation.php
6 months ago
class-auxin-notices.php
3 years ago
class-auxin-upgrader-ajax-handlers.php
1 year ago
class-auxin-upgrader-plugin.php
2 years ago
class-auxin-upgrader-prepare.php
3 years ago
class-auxin-upgrader-theme.php
3 years ago
class-auxin-notices.php
404 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Auxin admin notices |
| 4 | */ |
| 5 | class Auxin_Notices{ |
| 6 | |
| 7 | protected $args = array(); |
| 8 | protected $buttons = ''; |
| 9 | |
| 10 | public function __construct( $args = array() ){ |
| 11 | $defaults = array( |
| 12 | 'id' => null, |
| 13 | 'title' => '', |
| 14 | 'skin' => 'default', |
| 15 | 'image' => '', |
| 16 | 'screen_filter' => array(), |
| 17 | 'desc' => '', |
| 18 | 'initial_snooze' => '', // snooze time in milliseconds |
| 19 | 'has_close' => true, // Whether it has close button or not |
| 20 | 'buttons' => array(), |
| 21 | 'dismissible' => array( |
| 22 | 'url_key' => 'aux-hide-core-plugin-notice', |
| 23 | 'action' => 'auxin_hide_notices_nonce', |
| 24 | 'expiration' => YEAR_IN_SECONDS |
| 25 | ) |
| 26 | ); |
| 27 | $this->args = wp_parse_args( $args, $defaults ); |
| 28 | |
| 29 | if( empty( $this->args['id'] ) ){ |
| 30 | return new WP_Error( 'missing_id', __( "You need to enter a unique id for notice.", 'auxin-elements' ) ); |
| 31 | } |
| 32 | |
| 33 | if( is_array( $this->args['dismissible'] ) ){ |
| 34 | $this->flush_dismissible(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * get image |
| 40 | * |
| 41 | * @param boolean $echo |
| 42 | * @param string $before |
| 43 | * @param string $after |
| 44 | * @return void | string |
| 45 | */ |
| 46 | private function get_image( $before = '<div class="aux-notice-image">', $after = '</div>' ){ |
| 47 | |
| 48 | if ( empty( $this->args['image'] ) || ! is_array( $this->args['image'] ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $attrs = ''; |
| 53 | foreach ( $this->args['image'] as $attr_name => $attr_value ) { |
| 54 | $attrs .= sprintf( ' %s="%s"', $attr_name, $attr_value ); |
| 55 | } |
| 56 | |
| 57 | return $before . '<img '. $attrs .' />' . $after; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * get title |
| 62 | * |
| 63 | * @param boolean $echo |
| 64 | * @param string $before |
| 65 | * @param string $after |
| 66 | * @return void | string |
| 67 | */ |
| 68 | private function get_title( $before = '<h3 class="aux-notice-title">', $after = '</h3>' ){ |
| 69 | |
| 70 | if ( empty( $this->args['title'] ) ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | return $before . $this->args['title'] . $after; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * get class skin |
| 79 | * |
| 80 | * @param boolean $echo |
| 81 | * @param string $prefix |
| 82 | * @return void | string |
| 83 | */ |
| 84 | private function get_skin( $prefix = 'aux-notice-skin-' ){ |
| 85 | return $prefix . $this->args['skin']; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * get description |
| 90 | * |
| 91 | * @param boolean $echo |
| 92 | * @param string $before |
| 93 | * @param string $after |
| 94 | * @return void | string |
| 95 | */ |
| 96 | private function get_description( $before = '<p class="aux-notice-description">', $after = '</p>' ){ |
| 97 | |
| 98 | if ( strlen( $this->args['desc'] ) == 0 ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | return $before . $this->args['desc'] . $after; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * get buttons |
| 107 | * |
| 108 | * @param boolean $echo |
| 109 | * @return void |
| 110 | */ |
| 111 | private function get_buttons(){ |
| 112 | if( ! is_array( $this->args['buttons'] ) || empty( $this->args['buttons'] ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $default_args = [ |
| 117 | 'target' => '_blank', |
| 118 | 'border' => 'curve', |
| 119 | 'icon_align' => 'right', |
| 120 | 'uppercase' => 'no', |
| 121 | 'type' => 'link', |
| 122 | 'link' => '#', |
| 123 | 'expiration' => '', |
| 124 | 'extra_classes' => 'aux-notice-btn' |
| 125 | ]; |
| 126 | |
| 127 | foreach ( $this->args['buttons'] as $btn_args ) { |
| 128 | |
| 129 | $current_default_args = $default_args; |
| 130 | |
| 131 | if( !empty( $btn_args['type'] ) && 'skip' === $btn_args['type'] ){ |
| 132 | $current_default_args['style'] = 'outline'; |
| 133 | $current_default_args['color_name'] = 'ads' === $this->args['skin'] ? 'white' : 'black'; |
| 134 | $current_default_args['extra_classes'] .= ' aux-skip-notice'; |
| 135 | } else { |
| 136 | $current_default_args['extra_classes'] = 'aux-notice-cta-btn'; |
| 137 | switch ($this->args['skin']) { |
| 138 | case 'success': |
| 139 | $current_default_args['color_name'] = 'shamrock'; |
| 140 | break; |
| 141 | |
| 142 | case 'info': |
| 143 | $current_default_args['color_name'] = 'tan-hide'; |
| 144 | break; |
| 145 | |
| 146 | case 'error': |
| 147 | $current_default_args['color_name'] = 'fire-engine-red'; |
| 148 | break; |
| 149 | |
| 150 | default: |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | $btn_args = wp_parse_args( $btn_args, $current_default_args ); |
| 156 | |
| 157 | // Maye add custom expiration to the btn |
| 158 | if( $btn_args['expiration'] ){ |
| 159 | $btn_args['btn_attrs'] = 'data-expiration{'. $btn_args['expiration'] .'}'; |
| 160 | } |
| 161 | unset( $btn_args['expiration'] ); |
| 162 | |
| 163 | $this->buttons .= auxin_widget_button_callback( $btn_args ); |
| 164 | } |
| 165 | |
| 166 | return $this->buttons; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * get dismissible button |
| 171 | * |
| 172 | * @param boolean $echo |
| 173 | * @return void |
| 174 | */ |
| 175 | private function get_dismissible(){ |
| 176 | |
| 177 | if( $this->args['dismissible'] === false ){ |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | ob_start(); |
| 182 | |
| 183 | if( $this->args['has_close'] ){ |
| 184 | ?> |
| 185 | <a href="<?php echo esc_url( $this->get_nonce_url() ); ?>" class="notice-dismiss aux-skip-notice aux-close-notice"> |
| 186 | <span class="screen-reader-text"><?php echo esc_html__( 'Skip', 'auxin-elements' ); ?></span> |
| 187 | </a> |
| 188 | <?php } ?> |
| 189 | <script> |
| 190 | jQuery('.<?php echo esc_js( $this->get_unique_class() ); ?> .aux-skip-notice').on( 'click' , function(e) { |
| 191 | e.preventDefault(); |
| 192 | var expiration = this.getAttribute('data-expiration') || '<?php echo esc_js( $this->args['dismissible']['expiration'] ); ?>' |
| 193 | |
| 194 | jQuery.ajax({ |
| 195 | url : ajaxurl, |
| 196 | type: 'post', |
| 197 | data: { |
| 198 | action : 'auxin_dismissed_notice', |
| 199 | id : '<?php echo esc_js( $this->args['id'] ); ?>', |
| 200 | nonce : '<?php echo esc_js( wp_create_nonce( '_notice_nonce' ) ); ?>', |
| 201 | expiration: expiration |
| 202 | } |
| 203 | }).done(function( response ) { |
| 204 | if(response.success) { |
| 205 | jQuery(this).closest('.aux-notice-wrapper').fadeOut(); |
| 206 | } |
| 207 | }.bind(this)); |
| 208 | }); |
| 209 | </script> |
| 210 | <?php |
| 211 | return ob_get_clean(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Update dismissible transient |
| 216 | * |
| 217 | * @return void |
| 218 | */ |
| 219 | private function flush_dismissible(){ |
| 220 | if ( isset( $_GET[ $this->args['dismissible']['url_key'] ] ) && isset( $_GET[ '_notice_nonce' ] ) && $_GET[ $this->args['dismissible']['url_key'] ] === $this->args['id'] ) { |
| 221 | if ( ! wp_verify_nonce( $_GET[ '_notice_nonce' ], $this->args['dismissible']['action'] ) ) { |
| 222 | wp_die( esc_html__( 'Authorization failed. Please refresh the page and try again.', 'auxin-elements' ) ); |
| 223 | } |
| 224 | auxin_set_transient( $this->get_transient_key(), 1, $this->args['dismissible']['expiration'] ); |
| 225 | $this->args['dismissible'] = false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Undocumented function |
| 231 | * |
| 232 | * @return void |
| 233 | */ |
| 234 | private function get_nonce_url(){ |
| 235 | $actionurl = add_query_arg( $this->args['dismissible']['url_key'], $this->args['id'] ); |
| 236 | return wp_nonce_url( $actionurl, $this->args['dismissible']['action'], '_notice_nonce' ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * check dismissible |
| 241 | * |
| 242 | * @return boolean |
| 243 | */ |
| 244 | private function is_dismissible(){ |
| 245 | if( ! is_array( $this->args['dismissible'] ) || auxin_get_transient( $this->get_transient_key() ) ){ |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Check snooze time |
| 253 | * |
| 254 | * @return boolean |
| 255 | */ |
| 256 | private function is_snoozed(){ |
| 257 | if( ! empty( $this->args['initial_snooze'] ) ){ |
| 258 | $transient_key = $this->get_transient_key() . '-snooze'; |
| 259 | $snooze_time = auxin_get_transient( $transient_key ); |
| 260 | if( $snooze_time && $snooze_time > strtotime( "now" ) ){ |
| 261 | return true; |
| 262 | } elseif( $snooze_time === false ) { |
| 263 | auxin_set_transient( $transient_key, strtotime( $this->args['initial_snooze'] . " seconds" ) ); |
| 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Check screen filter |
| 272 | * |
| 273 | * @return boolean |
| 274 | */ |
| 275 | private function is_visible_screen(){ |
| 276 | $current_screen = get_current_screen(); |
| 277 | if( ! empty( $this->args['screen_filter'] ) && ! in_array( $current_screen->id, $this->args['screen_filter'] ) ) { |
| 278 | return true; |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Retrieves a transient key. |
| 285 | */ |
| 286 | private function get_transient_key(){ |
| 287 | return 'auxin-notice-' . $this->args['id']; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Retrieves a unique id for main wrapper. |
| 292 | */ |
| 293 | private function get_unique_class(){ |
| 294 | return 'auxin-notice-id-' . $this->args['id']; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Retrieves custom styles for main wrapper |
| 299 | */ |
| 300 | private function get_custom_styles(){ |
| 301 | |
| 302 | if ( ! isset( $this->args['wrapper_extra_styles'] ) || empty( $this->args['wrapper_extra_styles'] ) ) { |
| 303 | return false; |
| 304 | } else { |
| 305 | $styles = ''; |
| 306 | |
| 307 | foreach( $this->args['wrapper_extra_styles'] as $property => $value ) { |
| 308 | if ( 'custom' === $property ) { |
| 309 | $styles .= $value; |
| 310 | } else { |
| 311 | $styles .= $property . ':' . $value . ';'; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return 'style="'. $styles . '"'; |
| 316 | |
| 317 | } |
| 318 | |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * render output |
| 324 | * |
| 325 | * @param boolean $echo |
| 326 | * @return void |
| 327 | */ |
| 328 | public function render( $updated = '' ){ |
| 329 | |
| 330 | if( $this->is_dismissible() || $this->is_visible_screen() || $this->is_snoozed() ) { |
| 331 | return; |
| 332 | } |
| 333 | $updated = empty( $updated ) ? 'updated' : ''; |
| 334 | echo sprintf( |
| 335 | '<div class="%s auxin-message aux-notice-control aux-notice-wrapper %s %s" %s>%s %s %s <p class="aux-notice-submit submit">%s %s</p></div>', |
| 336 | esc_attr( $updated ), |
| 337 | $this->get_unique_class(), |
| 338 | $this->get_skin(), |
| 339 | $this->get_custom_styles(), |
| 340 | $this->get_image(), |
| 341 | $this->get_title(), |
| 342 | $this->get_description(), |
| 343 | $this->get_buttons(), |
| 344 | $this->get_dismissible() |
| 345 | ); |
| 346 | |
| 347 | } |
| 348 | |
| 349 | public static function get_ads_notices( $api_id = '26', $style = array(), $updated = '' ) { |
| 350 | $transient_id = ( $api_id == '3' || $api_id == '4' ) ? 'auxin_dashboard_ads_request' : 'auxin_ads_request'; |
| 351 | if ( false === $data = get_transient( $transient_id ) ) { |
| 352 | $body = auxin_remote_get( 'https://api.averta.net/rest/ara/v1/notifications?notification-space='. $api_id, null); |
| 353 | $data = json_decode( $body ); |
| 354 | set_transient( $transient_id, $data , 12 * HOUR_IN_SECONDS ); |
| 355 | } |
| 356 | |
| 357 | if ( empty( $data ) ) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | $ads_list = []; |
| 362 | |
| 363 | foreach( $data as $ads_data ) { |
| 364 | $args = [ |
| 365 | 'id' => 'auxin_ads_' . $ads_data->id, |
| 366 | 'title' => $ads_data->title->rendered, |
| 367 | 'desc' => $ads_data->message, |
| 368 | 'skin' => 'ads', |
| 369 | 'has_close' => false, |
| 370 | 'wrapper_extra_styles' => [ |
| 371 | 'background-color' => $ads_data->{'background-color'}, |
| 372 | 'background-image' => 'url(' . $ads_data->{'background-image'} . ')', |
| 373 | 'color' => $ads_data->color, |
| 374 | 'custom' => $ads_data->boxStyles |
| 375 | ], |
| 376 | 'buttons' => [ |
| 377 | [ |
| 378 | 'label' => $ads_data->button->label, |
| 379 | 'target' => '_blank', |
| 380 | 'link' => $ads_data->button->url, |
| 381 | 'custom_styles' => $ads_data->button->style |
| 382 | ], |
| 383 | [ |
| 384 | 'label' => __('Dismiss', 'auxin-elements'), |
| 385 | 'type' => 'skip', |
| 386 | 'expiration' => HOUR_IN_SECONDS * 2 |
| 387 | ] |
| 388 | ], |
| 389 | ]; |
| 390 | if ( ! empty( $style ) ) { |
| 391 | foreach( $style as $prop => $value ) { |
| 392 | $args['wrapper_extra_styles'][ $prop ] = $value; |
| 393 | } |
| 394 | } |
| 395 | $ads_list['auxin_dashboard_ads_' . $ads_data->id] = new self( $args ); |
| 396 | } |
| 397 | foreach ( $ads_list as $ads ) { |
| 398 | if( $ads instanceof self ){ |
| 399 | $ads->render( $updated ); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 |