get_notice_by_id( $notice_id ); $capability = isset( $notice['capability'] ) ? $notice['capability'] : 'manage_options'; if ( ! apply_filters( 'astra_notices_user_cap_check', current_user_can( $capability ) ) ) { return; } if ( false === wp_verify_nonce( $nonce, 'astra-notices' ) ) { wp_send_json_error( esc_html_e( 'WordPress Nonce not validated.', 'astra' ) ); } // Valid inputs? if ( ! empty( $notice_id ) ) { if ( ! empty( $repeat_notice_after ) ) { set_transient( $notice_id, true, $repeat_notice_after ); } else { update_user_meta( get_current_user_id(), $notice_id, 'notice-dismissed' ); } wp_send_json_success(); } wp_send_json_error(); } /** * Enqueue Scripts. * * @since 1.0.0 * @return void */ public function enqueue_scripts() { wp_register_style( 'astra-notices', self::get_url() . 'notices.css', array(), self::$version ); wp_register_script( 'astra-notices', self::get_url() . 'notices.js', array( 'jquery' ), self::$version, true ); wp_localize_script( 'astra-notices', 'astraNotices', array( '_notice_nonce' => wp_create_nonce( 'astra-notices' ), ) ); } /** * Sort the notices based on the given priority of the notice. * This function is called from usort() * * @since 1.5.2 * @param array $notice_1 First notice. * @param array $notice_2 Second Notice. * @return array */ public function sort_notices( $notice_1, $notice_2 ) { if ( ! isset( $notice_1['priority'] ) ) { $notice_1['priority'] = 10; } if ( ! isset( $notice_2['priority'] ) ) { $notice_2['priority'] = 10; } return $notice_1['priority'] - $notice_2['priority']; } /** * Get all registered notices. * Since v1.1.8 it is recommended to register the notices on * * @return array|null */ private function get_notices() { usort( self::$notices, array( $this, 'sort_notices' ) ); return self::$notices; } /** * Get notice by notice_id * * @param string $notice_id Notice id. * * @return array notice based on the notice id. */ private function get_notice_by_id( $notice_id ) { if ( empty( $notice_id ) ) { return array(); } $notices = $this->get_notices(); $notice = wp_list_filter( $notices, array( 'id' => $notice_id, ) ); return ! empty( $notice ) ? $notice[0] : array(); } /** * Display the notices in the WordPress admin. * * @since 1.0.0 * @return void */ public function show_notices() { $defaults = array( 'id' => '', // Optional, Notice ID. If empty it set `astra-notices-id-<$array-index>`. 'type' => 'info', // Optional, Notice type. Default `info`. Expected [info, warning, notice, error]. 'message' => '', // Optional, Message. 'show_if' => true, // Optional, Show notice on custom condition. E.g. 'show_if' => if( is_admin() ) ? true, false, . 'repeat-notice-after' => '', // Optional, Dismiss-able notice time. It'll auto show after given time. 'display-notice-after' => false, // Optional, Dismiss-able notice time. It'll auto show after given time. 'class' => '', // Optional, Additional notice wrapper class. 'priority' => 10, // Priority of the notice. 'display-with-other-notices' => true, // Should the notice be displayed if other notices are being displayed from Astra_Notices. 'is_dismissible' => true, 'capability' => 'manage_options', // User capability - This capability is required for the current user to see this notice. ); // Count for the notices that are rendered. $notices_displayed = 0; $notices = $this->get_notices(); foreach ( $notices as $key => $notice ) { $notice = wp_parse_args( $notice, $defaults ); // Show notices only for users with `manage_options` cap. if ( ! current_user_can( $notice['capability'] ) ) { continue; } $notice['id'] = self::get_notice_id( $notice, $key ); $notice['classes'] = self::get_wrap_classes( $notice ); // Notices visible after transient expire. if ( isset( $notice['show_if'] ) && true === $notice['show_if'] ) { // don't display the notice if it is not supposed to be displayed with other notices. if ( 0 !== $notices_displayed && false === $notice['display-with-other-notices'] ) { continue; } if ( self::is_expired( $notice ) ) { self::markup( $notice ); ++$notices_displayed; } } } } /** * Render a notice. * * @since 1.0.0 * @param array $notice Notice markup. * @return void */ public static function markup( $notice = array() ) { wp_enqueue_script( 'astra-notices' ); wp_enqueue_style( 'astra-notices' ); do_action( 'astra_notice_before_markup' ); do_action( "astra_notice_before_markup_{$notice['id']}" ); ?>