'', 'url' => '', 'page_prefixes' => array(), 'pro_filter' => '', 'license_option' => '', 'capability' => 'manage_options', 'strings' => array(), ) ); if ( '' === $args['id'] || '' === $args['url'] || empty( $args['strings']['headline'] ) || empty( $args['strings']['cta'] ) ) { return; } self::$config = $args; add_action( 'admin_init', array( __CLASS__, 'handle_dismiss' ) ); add_action( 'admin_notices', array( __CLASS__, 'maybe_render' ) ); } /** * Whether the current admin screen is one where the notice may appear: * the Plugins screen, or one of this plugin's own pages. * * @return bool */ private static function is_allowed_screen() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && 'plugins' === $screen->id ) { return true; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen check, no state change. $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; if ( '' === $page ) { return false; } foreach ( (array) self::$config['page_prefixes'] as $prefix ) { if ( '' !== $prefix && 0 === strpos( $page, $prefix ) ) { return true; } } return false; } /** * Whether the notice should show for the current user on this screen. * * @return bool */ private static function is_eligible() { $n = self::$config; if ( empty( $n ) || ! current_user_can( $n['capability'] ) ) { return false; } if ( '' !== $n['pro_filter'] && (bool) apply_filters( $n['pro_filter'], false ) ) { return false; } if ( '' !== $n['license_option'] ) { $key = get_option( $n['license_option'], '' ); if ( ! empty( $key ) ) { return false; } } $user_id = get_current_user_id(); if ( $user_id ) { // A dismissal of this notice, or of the earlier notice it replaced, is respected. if ( get_user_meta( $user_id, self::META_PREFIX . $n['id'], true ) || get_user_meta( $user_id, self::LEGACY_META_PREFIX . $n['id'], true ) ) { return false; } } return self::is_allowed_screen(); } /** * Render the notice when eligible. * * @return void */ public static function maybe_render() { if ( ! self::is_eligible() ) { return; } $n = self::$config; $s = $n['strings']; $dismiss_url = wp_nonce_url( add_query_arg( self::DISMISS_ARG, rawurlencode( $n['id'] ) ), self::DISMISS_ARG . '_' . $n['id'] ); echo '
' . esc_html( $s['headline'] ) . '';
if ( ! empty( $s['body'] ) ) {
echo '
' . esc_html( $s['body'] );
}
echo '
'; echo '' . esc_html( $s['cta'] ) . ''; echo ' ' . esc_html( ! empty( $s['dismiss'] ) ? $s['dismiss'] : 'Dismiss' ) . ''; echo '
'; echo '