@@ -29,131 +29,131 @@ |
||
| 29 | 29 | * Exit if called directly. |
| 30 | 30 | */ |
| 31 | 31 | if ( ! defined( 'ABSPATH' ) ) { |
| 32 | - die; |
|
| 32 | + die; |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | if ( ! class_exists( 'PAnD' ) ) { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Class PAnD |
|
| 39 | - */ |
|
| 40 | - class PAnD { |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Init hooks. |
|
| 44 | - */ |
|
| 45 | - public static function init() { |
|
| 46 | - add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) ); |
|
| 47 | - add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Enqueue javascript and variables. |
|
| 52 | - */ |
|
| 53 | - public static function load_script() { |
|
| 54 | - |
|
| 55 | - if ( is_customize_preview() ) { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - wp_enqueue_script( |
|
| 60 | - 'dismissible-notices', |
|
| 61 | - plugins_url( 'dismiss-notice.js', __FILE__ ), |
|
| 62 | - array( 'jquery', 'common' ), |
|
| 63 | - false, |
|
| 64 | - true |
|
| 65 | - ); |
|
| 66 | - |
|
| 67 | - wp_localize_script( |
|
| 68 | - 'dismissible-notices', |
|
| 69 | - 'dismissible_notice', |
|
| 70 | - array( |
|
| 71 | - 'nonce' => wp_create_nonce( 'dismissible-notice' ), |
|
| 72 | - ) |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Handles Ajax request to persist notices dismissal. |
|
| 78 | - * Uses check_ajax_referer to verify nonce. |
|
| 79 | - */ |
|
| 80 | - public static function dismiss_admin_notice() { |
|
| 81 | - $option_name = sanitize_text_field( $_POST['option_name'] ); |
|
| 82 | - $dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
|
| 83 | - |
|
| 84 | - if ( 'forever' != $dismissible_length ) { |
|
| 85 | - // If $dismissible_length is not an integer default to 1 |
|
| 86 | - $dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
|
| 87 | - $dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - check_ajax_referer( 'dismissible-notice', 'nonce' ); |
|
| 91 | - self::set_admin_notice_cache( $option_name, $dismissible_length ); |
|
| 92 | - wp_die(); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Is admin notice active? |
|
| 97 | - * |
|
| 98 | - * @param string $arg data-dismissible content of notice. |
|
| 99 | - * |
|
| 100 | - * @return bool |
|
| 101 | - */ |
|
| 102 | - public static function is_admin_notice_active( $arg ) { |
|
| 103 | - $array = explode( '-', $arg ); |
|
| 104 | - $length = array_pop( $array ); |
|
| 105 | - $option_name = implode( '-', $array ); |
|
| 106 | - $db_record = self::get_admin_notice_cache( $option_name ); |
|
| 107 | - if ( 'forever' == $db_record ) { |
|
| 108 | - return false; |
|
| 109 | - } elseif ( absint( $db_record ) >= time() ) { |
|
| 110 | - return false; |
|
| 111 | - } else { |
|
| 112 | - return true; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Returns admin notice cached timeout. |
|
| 118 | - * |
|
| 119 | - * @access public |
|
| 120 | - * |
|
| 121 | - * @param string|bool $id admin notice name or false. |
|
| 122 | - * |
|
| 123 | - * @return array|bool The timeout. False if expired. |
|
| 124 | - */ |
|
| 125 | - public static function get_admin_notice_cache( $id = false ) { |
|
| 126 | - if ( ! $id ) { |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - $cache_key = 'pand-' . md5( $id ); |
|
| 130 | - $timeout = get_site_option( $cache_key ); |
|
| 131 | - $timeout = 'forever' === $timeout ? time() + 60 : $timeout; |
|
| 132 | - |
|
| 133 | - if ( empty( $timeout ) || time() > $timeout ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $timeout; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Sets admin notice timeout in site option. |
|
| 142 | - * |
|
| 143 | - * @access public |
|
| 144 | - * |
|
| 145 | - * @param string $id Data Identifier. |
|
| 146 | - * @param string|bool $timeout Timeout for admin notice. |
|
| 147 | - * |
|
| 148 | - * @return bool |
|
| 149 | - */ |
|
| 150 | - public static function set_admin_notice_cache( $id, $timeout ) { |
|
| 151 | - $cache_key = 'pand-' . md5( $id ); |
|
| 152 | - update_site_option( $cache_key, $timeout ); |
|
| 153 | - |
|
| 154 | - return true; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - } |
|
| 37 | + /** |
|
| 38 | + * Class PAnD |
|
| 39 | + */ |
|
| 40 | + class PAnD { |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Init hooks. |
|
| 44 | + */ |
|
| 45 | + public static function init() { |
|
| 46 | + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) ); |
|
| 47 | + add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Enqueue javascript and variables. |
|
| 52 | + */ |
|
| 53 | + public static function load_script() { |
|
| 54 | + |
|
| 55 | + if ( is_customize_preview() ) { |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + wp_enqueue_script( |
|
| 60 | + 'dismissible-notices', |
|
| 61 | + plugins_url( 'dismiss-notice.js', __FILE__ ), |
|
| 62 | + array( 'jquery', 'common' ), |
|
| 63 | + false, |
|
| 64 | + true |
|
| 65 | + ); |
|
| 66 | + |
|
| 67 | + wp_localize_script( |
|
| 68 | + 'dismissible-notices', |
|
| 69 | + 'dismissible_notice', |
|
| 70 | + array( |
|
| 71 | + 'nonce' => wp_create_nonce( 'dismissible-notice' ), |
|
| 72 | + ) |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Handles Ajax request to persist notices dismissal. |
|
| 78 | + * Uses check_ajax_referer to verify nonce. |
|
| 79 | + */ |
|
| 80 | + public static function dismiss_admin_notice() { |
|
| 81 | + $option_name = sanitize_text_field( $_POST['option_name'] ); |
|
| 82 | + $dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
|
| 83 | + |
|
| 84 | + if ( 'forever' != $dismissible_length ) { |
|
| 85 | + // If $dismissible_length is not an integer default to 1 |
|
| 86 | + $dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
|
| 87 | + $dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + check_ajax_referer( 'dismissible-notice', 'nonce' ); |
|
| 91 | + self::set_admin_notice_cache( $option_name, $dismissible_length ); |
|
| 92 | + wp_die(); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Is admin notice active? |
|
| 97 | + * |
|
| 98 | + * @param string $arg data-dismissible content of notice. |
|
| 99 | + * |
|
| 100 | + * @return bool |
|
| 101 | + */ |
|
| 102 | + public static function is_admin_notice_active( $arg ) { |
|
| 103 | + $array = explode( '-', $arg ); |
|
| 104 | + $length = array_pop( $array ); |
|
| 105 | + $option_name = implode( '-', $array ); |
|
| 106 | + $db_record = self::get_admin_notice_cache( $option_name ); |
|
| 107 | + if ( 'forever' == $db_record ) { |
|
| 108 | + return false; |
|
| 109 | + } elseif ( absint( $db_record ) >= time() ) { |
|
| 110 | + return false; |
|
| 111 | + } else { |
|
| 112 | + return true; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Returns admin notice cached timeout. |
|
| 118 | + * |
|
| 119 | + * @access public |
|
| 120 | + * |
|
| 121 | + * @param string|bool $id admin notice name or false. |
|
| 122 | + * |
|
| 123 | + * @return array|bool The timeout. False if expired. |
|
| 124 | + */ |
|
| 125 | + public static function get_admin_notice_cache( $id = false ) { |
|
| 126 | + if ( ! $id ) { |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + $cache_key = 'pand-' . md5( $id ); |
|
| 130 | + $timeout = get_site_option( $cache_key ); |
|
| 131 | + $timeout = 'forever' === $timeout ? time() + 60 : $timeout; |
|
| 132 | + |
|
| 133 | + if ( empty( $timeout ) || time() > $timeout ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $timeout; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Sets admin notice timeout in site option. |
|
| 142 | + * |
|
| 143 | + * @access public |
|
| 144 | + * |
|
| 145 | + * @param string $id Data Identifier. |
|
| 146 | + * @param string|bool $timeout Timeout for admin notice. |
|
| 147 | + * |
|
| 148 | + * @return bool |
|
| 149 | + */ |
|
| 150 | + public static function set_admin_notice_cache( $id, $timeout ) { |
|
| 151 | + $cache_key = 'pand-' . md5( $id ); |
|
| 152 | + update_site_option( $cache_key, $timeout ); |
|
| 153 | + |
|
| 154 | + return true; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | 159 | } |