| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get all dismissed notices. |
| 4 |
* |
| 5 |
* @since 3.1.5 |
| 6 |
* @return array Array of dismissed notices |
| 7 |
*/ |
| 8 |
function wpas_dismissed_notices() { |
| 9 |
|
| 10 |
global $current_user; |
| 11 |
|
| 12 |
$user_notices = (array) get_user_option( 'wpas_dismissed_notices', $current_user->ID ); |
| 13 |
|
| 14 |
return $user_notices; |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Check if a specific notice has been dismissed. |
| 20 |
* |
| 21 |
* @since 3.1.5 |
| 22 |
* @param string $notice Notice to check |
| 23 |
* @return boolean Whether or not the notice has been dismissed |
| 24 |
*/ |
| 25 |
function wpas_is_notice_dismissed( $notice ) { |
| 26 |
|
| 27 |
$dismissed = wpas_dismissed_notices(); |
| 28 |
|
| 29 |
if ( array_key_exists( $notice, $dismissed ) ) { |
| 30 |
return true; |
| 31 |
} else { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Dismiss a notice. |
| 39 |
* |
| 40 |
* @since 3.1.5 |
| 41 |
* @param string $notice Notice to dismiss |
| 42 |
* @return boolean|integer True on success, false on failure, meta ID if it didn't exist yet |
| 43 |
*/ |
| 44 |
function wpas_dismiss_notice( $notice ) { |
| 45 |
|
| 46 |
global $current_user; |
| 47 |
|
| 48 |
$dismissed_notices = $new = (array) wpas_dismissed_notices(); |
| 49 |
|
| 50 |
if ( ! array_key_exists( $notice, $dismissed_notices ) ) { |
| 51 |
$new[$notice] = 'true'; |
| 52 |
} |
| 53 |
|
| 54 |
$update = update_user_option( $current_user->ID, 'wpas_dismissed_notices', $new ); |
| 55 |
|
| 56 |
return $update; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Restore a dismissed notice. |
| 62 |
* |
| 63 |
* @since 3.1.5 |
| 64 |
* @param string $notice Notice to restore |
| 65 |
* @return boolean|integer True on success, false on failure, meta ID if it didn't exist yet |
| 66 |
*/ |
| 67 |
function wpas_restore_notice( $notice ) { |
| 68 |
|
| 69 |
global $current_user; |
| 70 |
|
| 71 |
$dismissed_notices = (array) wpas_dismissed_notices(); |
| 72 |
|
| 73 |
if ( array_key_exists( $notice, $dismissed_notices ) ) { |
| 74 |
unset( $dismissed_notices[$notice] ); |
| 75 |
} |
| 76 |
|
| 77 |
$update = update_user_option( $current_user->ID, 'wpas_dismissed_notices', $dismissed_notices ); |
| 78 |
|
| 79 |
return $update; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
add_action( 'wpas_do_dismiss_notice', 'wpas_grab_notice_dismiss' ); |
| 84 |
/** |
| 85 |
* Check if there is a notice to dismiss. |
| 86 |
* |
| 87 |
* @since 3.1.5 |
| 88 |
* |
| 89 |
* @param array $data Contains the notice ID |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
function wpas_grab_notice_dismiss( $data ) { |
| 94 |
|
| 95 |
$notice_id = isset( $data['notice_id'] ) ? $data['notice_id'] : false; |
| 96 |
|
| 97 |
if ( false === $notice_id ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
wpas_dismiss_notice( $notice_id ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
class AS_Admin_Notices { |
| 106 |
|
| 107 |
/** |
| 108 |
* Holds all our custom admin notices |
| 109 |
* |
| 110 |
* @since 3.2.5 |
| 111 |
* @var array |
| 112 |
*/ |
| 113 |
protected $notices; |
| 114 |
|
| 115 |
public function __construct() { |
| 116 |
$this->init(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Throw error on object clone |
| 121 |
* |
| 122 |
* The whole idea of the singleton design pattern is that there is a single |
| 123 |
* object therefore, we don't want the object to be cloned. |
| 124 |
* |
| 125 |
* @since 3.2.5 |
| 126 |
* @access protected |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function __clone() { |
| 130 |
// Cloning instances of the class is forbidden |
| 131 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'awesome-support' ), '3.2.5' ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Instantiate the object |
| 136 |
* |
| 137 |
* @since 3.1.5 |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
protected function init() { |
| 141 |
add_action( 'admin_notices', array( $this, 'display_notices' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* List the allowed notice types |
| 146 |
* |
| 147 |
* @since 3.1.5 |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
protected function notice_types() { |
| 151 |
|
| 152 |
$types = array( |
| 153 |
'updated', |
| 154 |
'error' |
| 155 |
); |
| 156 |
|
| 157 |
return $types; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add a new admin notice |
| 163 |
* |
| 164 |
* @since 3.1.5 |
| 165 |
* |
| 166 |
* @param string $type Notice type (see notice_types()) |
| 167 |
* @param string $id Notice unique ID |
| 168 |
* @param string $message Notice message |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public function add_notice( $type, $id, $message ) { |
| 173 |
|
| 174 |
/* If running in SAAS mode and the notice is something about a license then don't bother! */ |
| 175 |
if ( true === $this->is_license_notice( $type, $id, $message ) && ( true === is_saas() ) ) { |
| 176 |
return ; |
| 177 |
} |
| 178 |
|
| 179 |
if ( ! in_array( $type, $this->notice_types() ) ) { |
| 180 |
$type = 'updated'; |
| 181 |
} |
| 182 |
|
| 183 |
$id = sanitize_key( $id ); |
| 184 |
$message = wp_kses_post( $message ); |
| 185 |
|
| 186 |
$this->notices[ $id ] = array( $type, $message ); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Check to see if the notice is a license notice by inspecting the $ID. |
| 192 |
* |
| 193 |
* @since 4.4.0 |
| 194 |
* |
| 195 |
* @param string $type Notice type (see notice_types()) |
| 196 |
* @param string $id Notice unique ID |
| 197 |
* @param string $message Notice message |
| 198 |
* |
| 199 |
* @return boolean |
| 200 |
*/ |
| 201 |
public function is_license_notice( $type, $id, $message ){ |
| 202 |
if ( ( strpos( 'xxx'.$id, 'lincense_' ) >= 0 ) || ( strpos( 'xxx'.$id, 'license_' ) >= 0 ) ) { |
| 203 |
return true ; |
| 204 |
} |
| 205 |
|
| 206 |
return false ; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get all custom notices registered |
| 211 |
* |
| 212 |
* @return null|array |
| 213 |
*/ |
| 214 |
protected function get_notices() { |
| 215 |
return apply_filters( 'wpas_admin_notices', $this->notices ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Display all the custom notices |
| 220 |
* |
| 221 |
* @since 3.1.5 |
| 222 |
* @return void |
| 223 |
*/ |
| 224 |
public function display_notices() { |
| 225 |
|
| 226 |
$notices = $this->get_notices(); |
| 227 |
|
| 228 |
if ( empty( $notices ) || is_null( $notices ) ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
foreach ( $notices as $notice_id => $notice ) { |
| 233 |
|
| 234 |
if ( wpas_is_notice_dismissed( $notice_id ) ) { |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
$url = wpas_do_url( add_query_arg( $_GET, '' ), 'dismiss_notice', array( 'notice_id' => $notice_id ) ); |
| 239 |
|
| 240 |
printf( '<div class="%s"><p>%s <a href="%s"><small>(%s)</small></a></p></div>', wp_kses_post($notice[0]), wp_kses_post($notice[1]), esc_url( $url ), esc_html_x( 'Dismiss', 'Dismiss link for admin notices', 'awesome-support' ) ); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
|