Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 17 additions & 43 deletions includes/Admin/Namer_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function add_hooks() {
add_action( 'admin_menu', array( $this, 'add_page' ) );
add_action( 'admin_post_' . self::ACTION_ANALYZE, array( $this, 'handle_analyze' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_notices', array( $this, 'render_ai_notice' ) );
add_action( 'wp_ajax_plugin_check_namer_analyze', array( $this, 'ajax_analyze' ) );
}

Expand Down Expand Up @@ -214,40 +213,6 @@ protected function get_author_name_from_request() {
return trim( $author );
}

/**
* Renders admin notice when AI connectors are not configured.
*
* @since 1.9.0
*/
public function render_ai_notice() {
$screen = get_current_screen();
if ( ! $screen || $screen->id !== $this->hook_suffix ) {
return;
}

if ( ! current_user_can( 'manage_options' ) ) {
return;
}

$ai_config = $this->get_ai_config();
if ( ! is_wp_error( $ai_config ) ) {
return;
}
?>
<div class="notice notice-warning is-dismissible">
<p>
<?php
printf(
/* translators: %s: Error message. */
__( 'To use the Namer tool, configure AI connectors in WordPress 7.0+ settings. Details: %s', 'plugin-check' ),
esc_html( $ai_config->get_error_message() )
);
?>
</p>
</div>
<?php
}

/**
* Renders the page.
*
Expand All @@ -268,15 +233,24 @@ public function render_page() {
<?php
if ( is_wp_error( $ai_config ) ) {
?>
<p class="description">
<?php esc_html_e( 'The Plugin Namer requires WordPress 7.0+ with configured AI connectors. Please enable AI connectors in core to use this tool.', 'plugin-check' ); ?>
</p>
<p>
<a class="button button-primary" href="<?php echo esc_url( admin_url( 'options-connectors.php' ) ); ?>">
<?php esc_html_e( 'Configure AI Connectors', 'plugin-check' ); ?>
</a>
</p>
<div class="notice notice-error">
<p>
<?php
echo esc_html( $ai_config->get_error_message() );
?>
</p>
</div>

<?php
if ( true === $this->check_ai_prerequisites() && is_wp_error( $this->check_ai_connectors() ) ) {
?>
<p>
<a class="button button-primary" href="<?php echo esc_url( admin_url( 'options-connectors.php' ) ); ?>">
<?php esc_html_e( 'Configure AI Connectors', 'plugin-check' ); ?>
</a>
</p>
<?php
}
return;
}
?>
Expand Down
67 changes: 58 additions & 9 deletions includes/Traits/AI_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,84 @@
trait AI_Utils {

/**
* Gets AI configuration from core AI connectors.
* Checks AI prerequisites: feature flag, function availability, and version.
*
* @since 1.9.0
* @since 2.0.0
*
* @param string $model_preference Selected model preference (optional).
* @return array|WP_Error AI config array or error.
* @return true|WP_Error True if all prerequisites are met, WP_Error otherwise.
*/
protected function get_ai_config( $model_preference = '' ) {
if ( ! function_exists( 'wp_ai_client_prompt' ) ) {
protected function check_ai_prerequisites() {

if ( function_exists( 'wp_supports_ai' ) && ! wp_supports_ai() ) {
return new WP_Error(
'ai_client_not_available',
__( 'The AI client is not available. This feature requires WordPress 7.0 or newer.', 'plugin-check' )
'ai_disabled',
__( 'The Plugin Check Namer feature requires AI support to be enabled on this site. Please enable AI functionality to use this feature.', 'plugin-check' )
);
}

if ( ! is_wp_version_compatible( '7.0' ) ) {
return new WP_Error(
'ai_client_not_available',
__( 'The AI client is only available in WordPress 7.0 or newer.', 'plugin-check' )
sprintf(
/* translators: %s: WordPress version. */
__( 'The Plugin Check Namer Tool requires WordPress version 7.0 or higher. You are running WordPress version %s.', 'plugin-check' ),
get_bloginfo( 'version' )
)
);
}

return true;
}

/**
* Checks that at least one AI connector is configured and active.
*
* @since 2.0.0
*
* @return true|WP_Error True if a connector is available, WP_Error otherwise.
*/
protected function check_ai_connectors() {
if ( $this->has_no_active_ai_connectors() ) {
return new WP_Error(
'ai_not_configured',
__( 'AI connectors are not configured. Please connect and enable an AI provider in WordPress 7.0+ settings.', 'plugin-check' )
);
}

return true;
}

/**
* Gets AI configuration from core AI connectors.
*
* @since 1.9.0
*
* @param string $model_preference Selected model preference (optional).
* @return array|WP_Error AI config array or error.
*/
protected function get_ai_config( $model_preference = '' ) {

$prerequisites = $this->check_ai_prerequisites();
if ( is_wp_error( $prerequisites ) ) {
return $prerequisites;
}

$connectors = $this->check_ai_connectors();
if ( is_wp_error( $connectors ) ) {
return $connectors;
}

if ( ! function_exists( 'wp_ai_client_prompt' ) ) {
return new WP_Error(
'ai_client_not_available',
sprintf(
/* translators: %s: WordPress version. */
__( 'The Plugin Check Namer Tool requires WordPress version 7.0 or newer. You are running WordPress version %s.', 'plugin-check' ),
get_bloginfo( 'version' )
)
);
}

$builder = wp_ai_client_prompt( 'Plugin Check AI availability test.' );
if ( is_wp_error( $builder ) ) {
return $builder;
Expand Down
Loading