after-kubio-activation.php
1 year ago
allow-kubio-blog-override.php
2 years ago
cache-plugins.php
4 years ago
default-editor-overlay.php
1 year ago
dismissable-notice.php
1 year ago
gutenerg-plugin-check.php
1 year ago
image-size-auto-fix.php
1 year ago
kubio-fresh-site.php
2 years ago
post-insert.php
1 year ago
register-meta-fields.php
3 years ago
requirements-notices.php
1 year ago
site-urls.php
1 year ago
starter-sites-feature.php
1 year ago
svg-kses.php
1 year ago
wp-import.php
1 year ago
dismissable-notice.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | |
| 5 | /** |
| 6 | * This function adds an action which hooks into admin_notices and creates a dismissible notices via AJAX. |
| 7 | * |
| 8 | * @param string $name |
| 9 | * @param callable $callback |
| 10 | * @param integer $repeat_after - use 0 to disable the reappearance time limit |
| 11 | * @param array $params |
| 12 | * @param string $classes |
| 13 | * @return void |
| 14 | */ |
| 15 | function kubio_add_dismissable_notice( $name, $callback, $repeat_after = 0, $params = array(), $classes = '' ) { |
| 16 | |
| 17 | if ( kubio_dismissable_notice_is_dismissed( $name, $repeat_after ) ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | add_action( |
| 22 | 'admin_notices', |
| 23 | function () use ( $name, $params, $callback, $classes ) { |
| 24 | $id = 'kubio-notice-' . uniqid(); |
| 25 | $data = array( |
| 26 | 'id' => $id, |
| 27 | 'name' => $name, |
| 28 | ); |
| 29 | ?> |
| 30 | <div data-kubio-notice-id="<?php echo esc_attr( $id ); ?>" class="notice is-dismissible <?php echo esc_attr( $classes ); ?>"> |
| 31 | <?php call_user_func( $callback, $params ); ?> |
| 32 | <script> |
| 33 | jQuery(function($){ |
| 34 | var data =<?php echo wp_json_encode( $data ); ?>; |
| 35 | $(document).on('click','[data-kubio-notice-id=' + data.id + '] .notice-dismiss',function(){ |
| 36 | wp.ajax.post('kubio-dismissable-notice--dismiss',{ |
| 37 | kubio_notice_name:data.name, |
| 38 | _wpnonce: '<?php echo esc_html( wp_create_nonce( 'kubio-dismissable-notice--dismiss-nonce' ) ); ?>' |
| 39 | }); |
| 40 | }); |
| 41 | }); |
| 42 | </script> |
| 43 | </div> |
| 44 | |
| 45 | <?php |
| 46 | |
| 47 | $notices = get_option( '_kubio_dismissable_notices', array() ); |
| 48 | $notices[ $name ] = array( 'dismiss_time' => 0 ); |
| 49 | update_option( '_kubio_dismissable_notices', $notices ); |
| 50 | } |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * This is an ajax callback which marks notices as dismissed. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | function _kubio_dismiss_dismissable_notice() { |
| 60 | check_ajax_referer( 'kubio-dismissable-notice--dismiss-nonce' ); |
| 61 | $notice = Arr::get( $_REQUEST, 'kubio_notice_name', false ); |
| 62 | $notices = get_option( '_kubio_dismissable_notices', array() ); |
| 63 | |
| 64 | if ( $notice && Arr::exists( $notices, $notice ) ) { |
| 65 | $notices[ $notice ] = array( 'dismiss_time' => time() ); |
| 66 | update_option( '_kubio_dismissable_notices', $notices ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Checks if a named Kubio notice is dismissed at this moment. |
| 72 | * @param $name |
| 73 | * @param $repeat_after |
| 74 | * @return bool |
| 75 | */ |
| 76 | function kubio_dismissable_notice_is_dismissed( $name, $repeat_after = 0 ) { |
| 77 | $notices = get_option( '_kubio_dismissable_notices', array() ); |
| 78 | |
| 79 | if ( Arr::has( $notices, $name ) ) { |
| 80 | |
| 81 | $dismissed_time = Arr::get( $notices, "{$name}.dismiss_time", 0 ); |
| 82 | |
| 83 | if ( $repeat_after === 0 && $dismissed_time !== 0 ) { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | if ( $dismissed_time && time() < $dismissed_time + $repeat_after ) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | add_action( 'wp_ajax_kubio-dismissable-notice--dismiss', '_kubio_dismiss_dismissable_notice' ); |
| 95 |