'; } /** * Prints the beginning of wrapper element after all notices. */ public function finish_output_capturing() { echo ''; } /** * Adds menu item showing number of notifications. * * @param \WP_Admin_Bar $admin_bar WordPress admin bar. */ public function add_item_in_admin_bar( $admin_bar ) { $admin_bar->add_menu( array( 'id' => 'anm_notification_count', 'title' => esc_html__( 'No admin notices', 'admin-notices-manager' ), 'href' => '#', 'parent' => 'top-secondary', ) ); } /** * Handles AJAX requests for logging the notices. * * @return false|void */ public function log_notices() { // If we have a nonce posted, check it. if ( wp_doing_ajax() && isset( $_POST['_nonce'] ) ) { $nonce_check = wp_verify_nonce( sanitize_text_field( $_POST['_nonce'] ), 'anm-ajax-nonce' ); // phpcs:ignore if ( ! $nonce_check ) { return false; } } if ( isset( $_POST['notices'] ) && ( ! empty( $_POST['notices'] && is_array( $_POST['notices'] ) ) ) ) { $currently_held_options = get_option( 'anm-notices', array() ); $hidden_forever = get_option( 'anm-hidden-notices', array() ); $hashed_notices = array(); $details = array(); $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); foreach ( $_POST['notices'] as $index => $notice ) { // phpcs:ignore $hash = wp_hash( $notice ); $current_time = current_time( 'timestamp' ); // phpcs:ignore $hashed_notices[ $hash ] = $current_time; $details[ $index ] = array( $hash, date_i18n( $format, $current_time ) ); // phpcs:ignore // Do we already know about this notice? if ( isset( $currently_held_options[ $hash ] ) ) { $hashed_notices[ $hash ] = $currently_held_options[ $hash ]; $details[ $index ] = array( $hash, date_i18n( $format, $currently_held_options[ $hash ] ) ); } if ( in_array( $hash, $hidden_forever, true ) ) { $details[ $index ] = 'do-not-display'; } } update_option( 'anm-notices', $hashed_notices ); wp_send_json_success( $details ); } } /** * Handles AJAX requests for hiding a notice forever. * * @return false|void */ public function hide_notice_forever() { // If we have a nonce posted, check it. if ( wp_doing_ajax() && isset( $_POST['_nonce'] ) ) { $nonce_check = wp_verify_nonce( sanitize_text_field( $_POST['_nonce'] ), 'anm-ajax-nonce' ); // phpcs:ignore if ( ! $nonce_check ) { return false; } } if ( isset( $_POST['notice_hash'] ) ) { $currently_held_options = get_option( 'anm-hidden-notices', array() ); array_push( $currently_held_options, $_POST['notice_hash'] ); // phpcs:ignore update_option( 'anm-hidden-notices', $currently_held_options ); wp_send_json_success(); } } }