service_id = sanitize_key( $service_id ); $this->api_endpoint_url = esc_url_raw( $api_endpoint_url ); $this->settings_screen = sanitize_text_field( $settings_screen ); $this->plugin_file_name = $plugin_file_name; $this->plugin_name = $plugin_name; $this->option_name = $this->service_id . '_announcements'; add_action( 'admin_init', [ $this, 'get_notices' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); add_action( 'admin_notices', [ $this, 'display_notices' ] ); add_action( 'wp_ajax_a4wp_dismiss_notice', [ $this, 'dismiss_notice' ] ); } public function get_notices() { if ( ! current_user_can( 'manage_options' ) || wp_doing_ajax() || empty( $this->api_endpoint_url ) ) { return; } $notices = get_option( $this->option_name ); if ( is_array( $notices ) && isset( $notices['expiry'] ) && time() < (int) $notices['expiry'] ) { return; } $notices_expiry = 24 * 3600; $error_expiry = 3600; $data = wp_safe_remote_get( $this->api_endpoint_url, [ 'timeout' => 5 ] ); if ( is_array( $data ) && ! is_wp_error( $data ) ) { update_option( $this->option_name, [ 'data' => json_decode( wp_remote_retrieve_body( $data ), true ), 'expiry' => time() + $notices_expiry, ], false ); } else { update_option( $this->option_name, [ 'data' => 'error', 'expiry' => time() + $error_expiry, ], false ); } } public function enqueue_scripts() { wp_add_inline_script( 'jquery', 'jQuery(document).on(\'click\', \'.a4wp-notice .notice-dismiss\', function() { var $notice = jQuery(this).parent(); var notice_id = $notice.data(\'notice-id\'); var service_id = $notice.data(\'service-id\'); var data = {action: \'a4wp_dismiss_notice\', notice_id: notice_id, service_id: service_id, security: \'' . esc_js( wp_create_nonce( 'a4wp-dismiss-nonce' ) ) . '\'}; jQuery.post(ajaxurl, data); });' ); } public function dismiss_notice() { check_ajax_referer( 'a4wp-dismiss-nonce', 'security' ); if ( current_user_can( 'manage_options' ) && isset( $_POST['action'] ) && $_POST['action'] === 'a4wp_dismiss_notice' ) { $id = isset( $_POST['notice_id'] ) ? sanitize_text_field( wp_unslash( $_POST['notice_id'] ) ) : ''; $service_id = isset( $_POST['service_id'] ) ? sanitize_key( wp_unslash( $_POST['service_id'] ) ) : ''; if ( ! $service_id || $service_id !== $this->service_id || $id === '' ) { return; } $last_ids = get_user_meta( get_current_user_id(), $this->service_id . '_dismissed_notices', true ); $last_ids = is_array( $last_ids ) ? $last_ids : []; if ( ! isset( $last_ids[ $this->service_id ] ) || ! is_array( $last_ids[ $this->service_id ] ) ) { $last_ids[ $this->service_id ] = []; } $last_ids[ $this->service_id ][] = $id; $last_ids[ $this->service_id ] = array_unique( $last_ids[ $this->service_id ] ); update_user_meta( get_current_user_id(), $this->service_id . '_dismissed_notices', $last_ids ); } } private function parse_attributes( $rules ) { if ( ! is_array( $rules ) || empty( $rules ) ) { return []; } $attributes = []; foreach ( $rules as $rule ) { if ( is_string( $rule ) && strpos( $rule, ':' ) !== false ) { list( $type, $val ) = explode( ':', $rule, 2 ); $attributes[ sanitize_key( $type ) ] = sanitize_text_field( $val ); } } return $attributes; } public function display_notices() { $announcements = get_option( $this->option_name ); if ( ! is_array( $announcements ) || ! isset( $announcements['data'] ) || ! is_array( $announcements['data'] ) ) { return; } if ( isset( $announcements['data']['normal_notices'] ) && is_array( $announcements['data']['normal_notices'] ) ) { foreach ( $announcements['data']['normal_notices'] as $notice ) { if ( ! is_array( $notice ) ) { continue; } $attributes = $this->parse_attributes( $notice['rules'] ?? [] ); $this->print_notice( $attributes, $notice ); } } if ( isset( $announcements['data']['important_notices'] ) && is_array( $announcements['data']['important_notices'] ) ) { foreach ( $announcements['data']['important_notices'] as $notice ) { if ( ! is_array( $notice ) ) { continue; } $attributes = $this->parse_attributes( $notice['rules'] ?? [] ); $this->print_notice( $attributes, $notice, true ); } } } private function print_notice( $attributes, $notice, $is_important = false ) { $allowed_types = [ 'notice-info', 'notice-success', 'notice-warning', 'notice-error' ]; $raw_type = isset( $attributes['type'] ) ? $attributes['type'] : 'notice-info'; $type = in_array( $raw_type, $allowed_types, true ) ? $raw_type : 'notice-info'; $should_display = $is_important ? $this->should_display( $attributes ) : $this->should_display( $attributes, $notice ); if ( $should_display ) { $notice_id = isset( $notice['id'] ) ? (string) $notice['id'] : ''; $dismiss_cls = ! $is_important ? ' is-dismissible' : ''; echo '
'; if ( ! empty( $notice['title'] ) ) { echo '

' . esc_html( $notice['title'] ) . '

'; } if ( ! empty( $notice['content'] ) ) { echo '

' . wp_kses_post( $notice['content'] ) . '

'; } echo '
'; } } private function should_display( $attributes, $notice = null ) { $page = isset( $attributes['page'] ) ? $attributes['page'] : $this->settings_screen; $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $page !== 'all' && ( ! $screen || $page !== $screen->id ) ) { return false; } if ( null !== $notice && isset( $notice['id'] ) ) { $dismissed_ids = get_user_meta( get_current_user_id(), $this->service_id . '_dismissed_notices', true ); if ( is_array( $dismissed_ids ) && isset( $dismissed_ids[ $this->service_id ] ) && in_array( (string) $notice['id'], (array) $dismissed_ids[ $this->service_id ], true ) ) { return false; } } return true; } } }