hasProvider(NvidiaProvider::class)) { return; } $registry->registerProvider(NvidiaProvider::class); } add_action('init', __NAMESPACE__ . '\\register_provider', 5); /** * AJAX action name used to dismiss the missing-client notice. * * @since 1.0.0 */ const DISMISS_NOTICE_ACTION = 'dbaipfn_dismiss_notice'; /** * User meta key storing the per-user dismissal of the missing-client notice. * * @since 1.0.0 */ const DISMISS_NOTICE_META = 'dbaipfn_notice_dismissed'; /** * Shows an admin notice when the PHP AI Client is not available. * * The provider can only register when the AI Client is present (bundled with * WordPress 7.0 or the WordPress AI plugin). Without it the plugin degrades * gracefully; this notice tells administrators why nothing is happening. * * The notice is scoped to the Dashboard and Plugins screens and is dismissible * per user, so it never behaves as a site-wide, persistent nag. * * @since 1.0.0 * * @return void */ function maybe_show_missing_client_notice(): void { if (class_exists(AiClient::class)) { return; } if (!current_user_can('manage_options')) { return; } // Only show on the Dashboard and Plugins screens, not on every admin page. $screen = function_exists('get_current_screen') ? get_current_screen() : null; if (!$screen || !in_array($screen->id, ['dashboard', 'plugins'], true)) { return; } // Respect a previous per-user dismissal. if (get_user_meta(get_current_user_id(), DISMISS_NOTICE_META, true)) { return; } printf( // phpcs:ignore Generic.Files.LineLength.TooLong -- Static notice markup. '

%s

', esc_attr(wp_create_nonce(DISMISS_NOTICE_ACTION)), esc_html__( // phpcs:ignore Generic.Files.LineLength.TooLong -- Translatable strings must not be split. 'AI Provider for NVIDIA requires the PHP AI Client, which is bundled with WordPress 7.0 or the WordPress AI plugin. Please install and activate it to enable the NVIDIA provider.', 'ai-provider-for-nvidia' ) ); // Persist the dismissal when the user clicks the notice's "X" (no jQuery dependency). wp_print_inline_script_tag( "document.addEventListener('click',function(e){" . "if(!e.target.classList.contains('notice-dismiss')){return;}" . "var n=e.target.closest('#ai-provider-for-nvidia-notice');" . "if(!n){return;}" . "var b=new FormData();" . "b.append('action'," . wp_json_encode(DISMISS_NOTICE_ACTION) . ");" . "b.append('nonce',n.getAttribute('data-nonce'));" . "fetch(window.ajaxurl,{method:'POST',credentials:'same-origin',body:b});" . "});" ); } add_action('admin_notices', __NAMESPACE__ . '\\maybe_show_missing_client_notice'); /** * Persists the per-user dismissal of the missing-client notice. * * @since 1.0.0 * * @return void */ function dismiss_missing_client_notice(): void { if (!current_user_can('manage_options')) { wp_send_json_error(null, 403); } check_ajax_referer(DISMISS_NOTICE_ACTION, 'nonce'); update_user_meta(get_current_user_id(), DISMISS_NOTICE_META, 1); wp_send_json_success(); } add_action('wp_ajax_' . DISMISS_NOTICE_ACTION, __NAMESPACE__ . '\\dismiss_missing_client_notice');