woocommerce-multilingual
/
addons
/
wpml-dependencies
/
lib
/
classes
/
notices
/
class-wpml-notices.php
pages
4 years ago
DismissNotices.php
4 weeks ago
class-wpml-notice-action.php
4 weeks ago
class-wpml-notice-render.php
4 weeks ago
class-wpml-notice.php
4 weeks ago
class-wpml-notices.php
4 weeks ago
class-wpml-notices.php
395 lines
| 1 | <?php |
| 2 | |
| 3 | class WPML_Notices { |
| 4 | |
| 5 | const NOTICES_OPTION_KEY = 'wpml_notices'; |
| 6 | const DISMISSED_OPTION_KEY = '_wpml_dismissed_notices'; |
| 7 | const USER_DISMISSED_KEY = '_wpml_user_dismissed_notices'; |
| 8 | const NONCE_NAME = 'wpml-notices'; |
| 9 | const DEFAULT_GROUP = 'default'; |
| 10 | |
| 11 | private $notice_render; |
| 12 | private $notices; |
| 13 | private $notices_to_remove = array(); |
| 14 | private $dismissed; |
| 15 | private $user_dismissed; |
| 16 | private $original_notices_md5; |
| 17 | |
| 18 | public function __construct( WPML_Notice_Render $notice_render ) { |
| 19 | $this->notice_render = $notice_render; |
| 20 | $this->notices = $this->filter_invalid_notices( $this->get_all_notices() ); |
| 21 | $this->dismissed = $this->get_all_dismissed(); |
| 22 | $this->original_notices_md5 = md5( maybe_serialize( $this->notices ) ); |
| 23 | } |
| 24 | |
| 25 | public function count() { |
| 26 | $all_notices = $this->get_all_notices(); |
| 27 | $count = 0; |
| 28 | foreach ( $all_notices as $group => $group_notices ) { |
| 29 | $count += count( $group_notices ); |
| 30 | } |
| 31 | |
| 32 | return $count; |
| 33 | } |
| 34 | |
| 35 | public function get_all_notices() { |
| 36 | $all_notices = get_option( self::NOTICES_OPTION_KEY ); |
| 37 | if ( ! is_array( $all_notices ) ) { |
| 38 | $all_notices = array(); |
| 39 | } |
| 40 | return $all_notices; |
| 41 | } |
| 42 | |
| 43 | private function get_all_dismissed() { |
| 44 | $dismissed = get_option( self::DISMISSED_OPTION_KEY ); |
| 45 | if ( ! is_array( $dismissed ) ) { |
| 46 | $dismissed = array(); |
| 47 | } |
| 48 | return $dismissed; |
| 49 | } |
| 50 | |
| 51 | private function init_all_user_dismissed() { |
| 52 | if ( null === $this->user_dismissed ) { |
| 53 | $this->user_dismissed = get_user_meta( get_current_user_id(), self::USER_DISMISSED_KEY, true ); |
| 54 | |
| 55 | if ( ! is_array( $this->user_dismissed ) ) { |
| 56 | $this->user_dismissed = array(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public function get_notice( $id, $group = 'default' ) { |
| 62 | $notice = null; |
| 63 | |
| 64 | if ( isset( $this->notices[ $group ][ $id ] ) ) { |
| 65 | $notice = $this->notices[ $group ][ $id ]; |
| 66 | } |
| 67 | |
| 68 | return $notice; |
| 69 | } |
| 70 | |
| 71 | public function create_notice( $id, $text, $group = 'default' ) { |
| 72 | return new WPML_Notice( $id, $text, $group ); |
| 73 | } |
| 74 | |
| 75 | public function add_notice( WPML_Notice $notice, $force_update = false ) { |
| 76 | $existing_notice = $this->notice_exists( $notice ) ? $this->notices[ $notice->get_group() ][ $notice->get_id() ] : null; |
| 77 | |
| 78 | $new_notice_is_different = null === $existing_notice || $notice->is_different( $existing_notice ); |
| 79 | |
| 80 | if ( $notice->must_reset_dismiss() && $this->is_notice_dismissed( $notice ) ) { |
| 81 | $this->undismiss_notice( $notice ); |
| 82 | } |
| 83 | |
| 84 | if ( ! $existing_notice || ( $new_notice_is_different || $force_update ) ) { |
| 85 | $this->notices[ $notice->get_group() ][ $notice->get_id() ] = $notice; |
| 86 | $this->save_notices(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public function get_new_notice( $id, $text, $group = 'default' ) { |
| 91 | return new WPML_Notice( $id, $text, $group ); |
| 92 | } |
| 93 | |
| 94 | public function get_new_notice_action( $text, $url = '#', $dismiss = false, $hide = false, $display_as_button = false ) { |
| 95 | return new WPML_Notice_Action( $text, $url, $dismiss, $hide, $display_as_button ); |
| 96 | } |
| 97 | |
| 98 | private function notice_exists( WPML_Notice $notice ) { |
| 99 | $notice_id = $notice->get_id(); |
| 100 | $notice_group = $notice->get_group(); |
| 101 | |
| 102 | return $this->group_and_id_exist( $notice_group, $notice_id ); |
| 103 | } |
| 104 | |
| 105 | private function get_notices_for_group( $group ) { |
| 106 | if ( array_key_exists( $group, $this->notices ) ) { |
| 107 | return $this->notices[ $group ]; |
| 108 | } |
| 109 | |
| 110 | return array(); |
| 111 | } |
| 112 | |
| 113 | private function save_notices() { |
| 114 | $this->remove_notices(); |
| 115 | if ( ! has_action( 'shutdown', array( $this, 'save_to_option' ) ) ) { |
| 116 | add_action( 'shutdown', array( $this, 'save_to_option' ), 1000 ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function save_to_option() { |
| 121 | if ( $this->original_notices_md5 !== md5( maybe_serialize( $this->notices ) ) ) { |
| 122 | update_option( self::NOTICES_OPTION_KEY, $this->notices, false ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private function save_dismissed() { |
| 127 | update_user_meta( get_current_user_id(), self::USER_DISMISSED_KEY, $this->user_dismissed ); |
| 128 | update_option( self::DISMISSED_OPTION_KEY, $this->dismissed, false ); |
| 129 | } |
| 130 | |
| 131 | public function remove_notices() { |
| 132 | if ( $this->notices_to_remove ) { |
| 133 | foreach ( $this->notices_to_remove as $group => &$group_notices ) { |
| 134 | foreach ( $group_notices as $id ) { |
| 135 | if ( array_key_exists( $group, $this->notices ) && array_key_exists( $id, $this->notices[ $group ] ) ) { |
| 136 | unset( $this->notices[ $group ][ $id ] ); |
| 137 | $group_notices = array_diff( $this->notices_to_remove[ $group ], array( $id ) ); |
| 138 | } |
| 139 | } |
| 140 | if ( array_key_exists( $group, $this->notices_to_remove ) && ! $this->notices_to_remove[ $group ] ) { |
| 141 | unset( $this->notices_to_remove[ $group ] ); |
| 142 | } |
| 143 | if ( array_key_exists( $group, $this->notices ) && ! $this->notices[ $group ] ) { |
| 144 | unset( $this->notices[ $group ] ); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public function admin_enqueue_scripts() { |
| 151 | if ( WPML_Block_Editor_Helper::is_edit_post() ) { |
| 152 | wp_enqueue_script( |
| 153 | 'block-editor-notices', |
| 154 | WCML_WPML_DEPENDENCY_URL . '/dist/js/blockEditorNotices/app.js', |
| 155 | array( 'wp-edit-post' ), |
| 156 | '4.5.0', |
| 157 | true |
| 158 | ); |
| 159 | } |
| 160 | if ( $this->must_display_notices() ) { |
| 161 | wp_enqueue_style( 'sitepress-style', WCML_WPML_DEPENDENCY_URL . '/res/css/style.css', array(), '4.5.0' ); |
| 162 | wp_enqueue_style( 'otgs-notices', WCML_WPML_DEPENDENCY_URL . '/res/css/otgs-notices.css', array( 'sitepress-style' ) ); |
| 163 | wp_enqueue_script( |
| 164 | 'otgs-notices', |
| 165 | WCML_WPML_DEPENDENCY_URL . '/res/js/otgs-notices.js', |
| 166 | array( 'underscore' ), |
| 167 | '4.5.0', |
| 168 | true |
| 169 | ); |
| 170 | |
| 171 | do_action( 'wpml-notices-scripts-enqueued' ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private function must_display_notices() { |
| 176 | if ( $this->notices ) { |
| 177 | foreach ( $this->notices as $group => $notices ) { |
| 178 | foreach ( $notices as $notice ) { |
| 179 | if ( $this->notice_render->must_display_notice( $notice ) && ! $this->must_hide_if_notice_exists( $notice ) ) { |
| 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | private function must_hide_if_notice_exists( WPML_Notice $notice ) { |
| 190 | $hide_if_notice_exists = $notice->get_hide_if_notice_exists(); |
| 191 | if ( $hide_if_notice_exists ) { |
| 192 | $other_notice = $this->get_notice( $hide_if_notice_exists['id'], $hide_if_notice_exists['group'] ); |
| 193 | |
| 194 | return $other_notice; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | public function admin_notices() { |
| 200 | if ( $this->notices && $this->must_display_notices() ) { |
| 201 | foreach ( $this->notices as $group => $notices ) { |
| 202 | foreach ( $notices as $notice ) { |
| 203 | if ( $notice instanceof WPML_Notice && ! $this->is_notice_dismissed( $notice ) ) { |
| 204 | $this->notice_render->render( $notice ); |
| 205 | if ( $notice->is_flash() ) { |
| 206 | $this->remove_notice( $notice->get_group(), $notice->get_id() ); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | public function wp_ajax_hide_notice() { |
| 215 | list( $notice_group, $notice_id ) = $this->parse_group_and_id(); |
| 216 | |
| 217 | if ( ! $notice_group ) { |
| 218 | $notice_group = self::DEFAULT_GROUP; |
| 219 | } |
| 220 | |
| 221 | if ( $this->has_valid_nonce() && $this->group_and_id_exist( $notice_group, $notice_id ) ) { |
| 222 | $this->remove_notice( $notice_group, $notice_id ); |
| 223 | wp_send_json_success( true ); |
| 224 | } |
| 225 | |
| 226 | wp_send_json_error( __( 'Notice does not exists.', 'sitepress' ) ); |
| 227 | } |
| 228 | |
| 229 | public function wp_ajax_dismiss_notice() { |
| 230 | list( $notice_group, $notice_id ) = $this->parse_group_and_id(); |
| 231 | |
| 232 | if ( $this->has_valid_nonce() && $this->dismiss_notice_by_id( $notice_id, $notice_group ) ) { |
| 233 | wp_send_json_success( true ); |
| 234 | } |
| 235 | |
| 236 | wp_send_json_error( __( 'Notice does not exist.', 'sitepress' ) ); |
| 237 | } |
| 238 | |
| 239 | private function dismiss_notice_by_id( $notice_id, $notice_group = null ) { |
| 240 | if ( ! $notice_group ) { |
| 241 | $notice_group = self::DEFAULT_GROUP; |
| 242 | } |
| 243 | |
| 244 | if ( $this->group_and_id_exist( $notice_group, $notice_id ) ) { |
| 245 | $notice = $this->get_notice( $notice_id, $notice_group ); |
| 246 | |
| 247 | if ( $notice ) { |
| 248 | $this->dismiss_notice( $notice ); |
| 249 | $this->remove_notice( $notice_group, $notice_id ); |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | public function wp_ajax_dismiss_group() { |
| 259 | list( $notice_group ) = $this->parse_group_and_id(); |
| 260 | |
| 261 | if ( $notice_group && $this->has_valid_nonce() && $this->dismiss_notice_group( $notice_group ) ) { |
| 262 | wp_send_json_success( true ); |
| 263 | } |
| 264 | wp_send_json_error( __( 'Group does not exist.', 'sitepress' ) ); |
| 265 | } |
| 266 | |
| 267 | private function dismiss_notice_group( $notice_group ) { |
| 268 | if ( $notice_group ) { |
| 269 | $notices = $this->get_notices_for_group( $notice_group ); |
| 270 | |
| 271 | if ( $notices ) { |
| 272 | foreach ( $notices as $notice ) { |
| 273 | $this->dismiss_notice( $notice, false ); |
| 274 | $this->remove_notice( $notice_group, $notice->get_id() ); |
| 275 | } |
| 276 | |
| 277 | $this->save_dismissed(); |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | private function parse_group_and_id() { |
| 287 | $group = isset( $_POST['group'] ) ? sanitize_text_field( $_POST['group'] ) : false; |
| 288 | $id = isset( $_POST['id'] ) ? sanitize_text_field( $_POST['id'] ) : false; |
| 289 | |
| 290 | return array( $group, $id ); |
| 291 | } |
| 292 | |
| 293 | private function has_valid_nonce() { |
| 294 | $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null; |
| 295 | return wp_verify_nonce( $nonce, self::NONCE_NAME ); |
| 296 | } |
| 297 | |
| 298 | private function group_and_id_exist( $group, $id ) { |
| 299 | return array_key_exists( $group, $this->notices ) && array_key_exists( $id, $this->notices[ $group ] ); |
| 300 | } |
| 301 | |
| 302 | public function remove_notice( $notice_group, $notice_id ) { |
| 303 | $this->notices_to_remove[ $notice_group ][] = $notice_id; |
| 304 | $this->notices_to_remove[ $notice_group ] = array_unique( $this->notices_to_remove[ $notice_group ] ); |
| 305 | $this->save_notices(); |
| 306 | |
| 307 | if ( ! is_array( $this->notices_to_remove ) ) { |
| 308 | $this->notices_to_remove = array(); |
| 309 | } |
| 310 | |
| 311 | if ( isset( $this->notices_to_remove[ $notice_group ] ) ) { |
| 312 | foreach ( $this->notices_to_remove[ $notice_group ] as $key => $notice ) { |
| 313 | if ( $notice === $notice_id ) { |
| 314 | unset( $this->notices_to_remove[ $notice_group ][ $key ] ); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public function remove_notice_group( $notice_group ) { |
| 321 | $notices = $this->get_notices_for_group( $notice_group ); |
| 322 | $notices_ids = array_keys( $notices ); |
| 323 | foreach ( $notices_ids as $notices_id ) { |
| 324 | $this->remove_notice( $notice_group, $notices_id ); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | public function dismiss_notice( WPML_Notice $notice, $persist = true ) { |
| 329 | if ( method_exists( $notice, 'is_user_restricted' ) && $notice->is_user_restricted() ) { |
| 330 | $this->init_all_user_dismissed(); |
| 331 | $this->user_dismissed[ $notice->get_group() ][ $notice->get_id() ] = md5( $notice->get_text() ); |
| 332 | } else { |
| 333 | $this->dismissed[ $notice->get_group() ][ $notice->get_id() ] = md5( $notice->get_text() ); |
| 334 | } |
| 335 | |
| 336 | if ( $persist ) { |
| 337 | $this->save_dismissed(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | public function undismiss_notice( WPML_Notice $notice, $persist = true ) { |
| 342 | if ( method_exists( $notice, 'is_user_restricted' ) && $notice->is_user_restricted() ) { |
| 343 | $this->init_all_user_dismissed(); |
| 344 | unset( $this->user_dismissed[ $notice->get_group() ][ $notice->get_id() ] ); |
| 345 | } else { |
| 346 | unset( $this->dismissed[ $notice->get_group() ][ $notice->get_id() ] ); |
| 347 | } |
| 348 | |
| 349 | if ( $persist ) { |
| 350 | $this->save_dismissed(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | public function is_notice_dismissed( WPML_Notice $notice ) { |
| 355 | $group = $notice->get_group(); |
| 356 | $id = $notice->get_id(); |
| 357 | |
| 358 | $is_dismissed = isset( $this->dismissed[ $group ][ $id ] ) && $this->dismissed[ $group ][ $id ]; |
| 359 | |
| 360 | if ( ! $is_dismissed ) { |
| 361 | $this->init_all_user_dismissed(); |
| 362 | $is_dismissed = isset( $this->user_dismissed[ $group ][ $id ] ) && $this->user_dismissed[ $group ][ $id ]; |
| 363 | } |
| 364 | |
| 365 | if ( $is_dismissed && method_exists( $notice, 'can_be_dismissed_for_different_text' ) |
| 366 | && ! $notice->can_be_dismissed_for_different_text() ) { |
| 367 | $is_dismissed = md5( $notice->get_text() ) === $this->dismissed[ $group ][ $id ]; |
| 368 | } |
| 369 | |
| 370 | return $is_dismissed; |
| 371 | } |
| 372 | |
| 373 | public function init_hooks() { |
| 374 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 375 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 11 ); |
| 376 | add_action( 'wp_ajax_otgs-hide-notice', array( $this, 'wp_ajax_hide_notice' ) ); |
| 377 | add_action( 'wp_ajax_otgs-dismiss-notice', array( $this, 'wp_ajax_dismiss_notice' ) ); |
| 378 | add_action( 'wp_ajax_otgs-dismiss-group', array( $this, 'wp_ajax_dismiss_group' ) ); |
| 379 | add_action( 'otgs_add_notice', array( $this, 'add_notice' ), 10, 2 ); |
| 380 | add_action( 'otgs_remove_notice', array( $this, 'remove_notice' ), 10, 2 ); |
| 381 | add_action( 'otgs_remove_notice_group', array( $this, 'remove_notice_group' ), 10, 1 ); |
| 382 | } |
| 383 | |
| 384 | private function filter_invalid_notices( $notices ) { |
| 385 | foreach ( $notices as $group => $notices_in_group ) { |
| 386 | foreach ( $notices_in_group as $index => $notice ) { |
| 387 | if ( ! $notice instanceof WPML_Notice ) { |
| 388 | unset( $notices[ $group ][ $index ] ); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | return $notices; |
| 393 | } |
| 394 | } |
| 395 |