settings = $settings; $this->generator = $generator; $post_types = $this->settings->get_enabled_post_types(); foreach ( $post_types as $post_type ) { add_filter( "manage_{$post_type}_posts_columns", array( $this, 'add_column' ) ); add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'render_column' ), 10, 2 ); } add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Append "AI Featured Image" as the last column. */ public function add_column( $columns ) { $columns['aifi_image'] = __( 'AI Featured Image', 'ai-featured-image-generator' ); return $columns; } /** * Render the AI Image column cell. * * @param string $column Column slug. * @param int $post_id Post ID. */ public function render_column( $column, $post_id ) { if ( $column !== 'aifi_image' ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } $has_thumb = has_post_thumbnail( $post_id ); $thumb_url = $has_thumb ? get_the_post_thumbnail_url( $post_id, array( 60, 60 ) ) : ''; $link_label = $has_thumb ? __( 'Regenerate Image', 'ai-featured-image-generator' ) : __( 'Generate Image', 'ai-featured-image-generator' ); echo '
'; if ( $has_thumb ) { printf( '', esc_url( $thumb_url ) ); } else { echo '' . esc_html__( 'No image', 'ai-featured-image-generator' ) . ''; } printf( '', absint( $post_id ), $has_thumb ? 1 : 0, esc_attr( $this->cost_line() ), esc_html( $link_label ) ); // Results stay here until the next action, and are read out to screen readers. // An automatic image from publishing shows its state here until then. $auto = Auto_Generate::state( $post_id ); printf( '%s', $auto && 'failed' === $auto['status'] ? ' is-error' : '', esc_html( Auto_Generate::state_text( $auto ) ) ); echo '
'; } /** * The cost sentence, built once per page. * * @return string */ private function cost_line() { static $line = null; if ( null === $line ) { $line = Settings::get_cost_context( $this->settings->get_settings() )['line']; } return $line; } /** * Enqueue scripts and styles on the post list screen for enabled post types. * * @param string $hook Current admin page hook. */ public function enqueue_scripts( $hook ) { if ( $hook !== 'edit.php' ) { return; } $screen = get_current_screen(); if ( ! $screen || empty( $screen->post_type ) ) { return; } $enabled_post_types = $this->settings->get_enabled_post_types(); if ( ! in_array( $screen->post_type, $enabled_post_types, true ) ) { return; } wp_enqueue_style( 'aifi-admin', AIFI_PLUGIN_URL . 'assets/css/admin.css', array(), AIFI_VERSION ); wp_enqueue_script( 'aifi-list', AIFI_PLUGIN_URL . 'assets/js/admin-list.js', array( 'jquery' ), AIFI_VERSION, true ); wp_localize_script( 'aifi-list', 'aifiListData', array( 'nonce' => wp_create_nonce( 'wp_rest' ), 'restUrl' => rest_url( 'aifi/v1/list-generate' ), 'i18n' => array( 'generating' => __( 'Generating, up to a minute…', 'ai-featured-image-generator' ), 'success' => __( 'Image generated.', 'ai-featured-image-generator' ), 'promptWritten' => __( 'Prompt written from your post content.', 'ai-featured-image-generator' ), 'promptSaved' => __( 'Used your saved prompt.', 'ai-featured-image-generator' ), 'dismiss' => __( 'Dismiss', 'ai-featured-image-generator' ), 'error' => __( 'Error generating image. Please try again.', 'ai-featured-image-generator' ), 'confirmOverwrite' => __( 'This will replace your current featured image. Continue?', 'ai-featured-image-generator' ), 'regenerate' => __( 'Regenerate Image', 'ai-featured-image-generator' ), 'generate' => __( 'Generate Image', 'ai-featured-image-generator' ), ), ) ); } /** * Register the list-generate REST endpoint. */ public function register_routes() { register_rest_route( 'aifi/v1', '/list-generate', array( 'methods' => 'POST', 'callback' => array( $this, 'list_generate_image' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'post_id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), ), ) ); } /** * Permission callback for list-generate endpoint. * * @param \WP_REST_Request $request Request object. * @return bool|\WP_Error */ public function check_permission( $request ) { if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) { return new \WP_Error( 'rest_forbidden', __( 'Invalid nonce.', 'ai-featured-image-generator' ), array( 'status' => rest_authorization_required_code() ) ); } $post_id = absint( $request['post_id'] ); if ( ! current_user_can( 'edit_post', $post_id ) ) { return new \WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to edit this post.', 'ai-featured-image-generator' ), array( 'status' => rest_authorization_required_code() ) ); } // Making an image adds it to the Media Library and spends the site's credits, so the // user must be allowed to upload files (a Contributor is not). if ( ! current_user_can( 'upload_files' ) ) { return new \WP_Error( 'rest_forbidden', __( 'Your account cannot add images to the Media Library, so it cannot generate them.', 'ai-featured-image-generator' ), array( 'status' => rest_authorization_required_code() ) ); } $post = get_post( $post_id ); if ( ! $post ) { return new \WP_Error( 'rest_invalid_post', __( 'Invalid post.', 'ai-featured-image-generator' ), array( 'status' => 404 ) ); } $enabled_post_types = $this->settings->get_enabled_post_types(); if ( ! in_array( $post->post_type, $enabled_post_types, true ) ) { return new \WP_Error( 'rest_post_type_not_enabled', __( 'AI image generation is not enabled for this post type.', 'ai-featured-image-generator' ), array( 'status' => 403 ) ); } return true; } /** * Generate AI featured image for a single post from the list screen. * * @param \WP_REST_Request $request Request object. * @return \WP_REST_Response|\WP_Error */ public function list_generate_image( $request ) { $post_id = $request['post_id']; // One generation per post at a time (shared with /generate and auto-generate). The lock // covers the whole request and is released at the end. $lock_key = 'aifi_lock_' . $post_id; if ( ! REST::take_lock( $lock_key, REST::LOCK_TTL ) ) { return new \WP_Error( 'rate_limited', __( 'Please wait a moment before generating another image.', 'ai-featured-image-generator' ), array( 'status' => 429 ) ); } try { return $this->generate_locked( $post_id ); } finally { set_transient( $lock_key, true, REST::COOLDOWN ); } } /** * The /list-generate work, run while the per-post lock is held. * * @param int $post_id Post ID. * @return \WP_REST_Response|\WP_Error */ private function generate_locked( $post_id ) { $post = get_post( $post_id ); $settings_data = $this->settings->get_settings(); // A Pro-only provider without a usable licence is refused before any paid request. $provider = Bulk::usable_provider( $settings_data ); if ( is_wp_error( $provider ) ) { return $provider; } if ( ! Providers::has_api_key( $settings_data, $provider ) ) { return new \WP_Error( 'no_api_key', sprintf( /* translators: %s: provider name */ __( 'API key not configured for %s. Please configure it in the plugin settings.', 'ai-featured-image-generator' ), Providers::label( $provider ) ), array( 'status' => 400 ) ); } // A prompt the user saved in the editor wins and is never overwritten. Only when // there is none is one written from the post content (an extra paid text call), // and that one is saved so the editor shows what was used. $prompt = trim( (string) get_post_meta( $post_id, '_aifi_custom_prompt', true ) ); $prompt_source = $prompt !== '' ? 'saved' : 'title'; if ( $prompt === '' ) { $prompt_result = $this->generator->generate_prompt_from_content( $post_id ); if ( is_wp_error( $prompt_result ) ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { error_log( 'AIFI list-generate: prompt generation failed for post ' . $post_id . ': ' . $prompt_result->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } } elseif ( trim( (string) $prompt_result ) !== '' ) { $prompt = $prompt_result; $prompt_source = 'written'; // update_post_meta() unslashes its value, so slash it to keep backslashes in the prompt. update_post_meta( $post_id, '_aifi_custom_prompt', wp_slash( $prompt ) ); } } $result = $this->generator->generate_image( $post_id, $prompt, '', $post->post_title, '', null ); if ( is_wp_error( $result ) ) { return $result; } $response = array( 'success' => true, 'attachment_id' => $result, 'thumbnail_url' => wp_get_attachment_image_url( $result, array( 60, 60 ) ), 'url' => wp_get_attachment_url( $result ), 'prompt_source' => $prompt_source, ); // Surface a non-fatal WebP-conversion fallback notice, if any. $webp_notice = $this->generator->get_last_webp_notice(); if ( $webp_notice ) { $response['webp_notice'] = $webp_notice['message']; $response['webp_notice_reason'] = $webp_notice['reason']; } return rest_ensure_response( $response ); } }