rewrite_all = is_array($options) && isset($options['all_alt_text']) ? $options['all_alt_text'] : false; add_action('rest_api_init', array($this, 'register_rest_routes')); add_action('ai_process_media_batch', array($this, 'process_media_batch'), 10, 1); } public function register_rest_routes() { register_rest_route('ai-alt-text-generator/v1', '/start-processing', array( 'methods' => 'POST', 'callback' => array($this, 'start_processing'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/process-next', array( 'methods' => 'POST', 'callback' => array($this, 'process_next_image'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/processing-status', array( 'methods' => 'GET', 'callback' => array($this, 'get_processing_status'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/is-processing', array( 'methods' => 'GET', 'callback' => array($this, 'check_processing_status'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/stop-processing', array( 'methods' => 'POST', 'callback' => array($this, 'stop_processing'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/validate-key', array( 'methods' => 'POST', 'callback' => array($this, 'validate_api_key'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/settings', array( array( 'methods' => 'GET', 'callback' => array($this, 'get_settings'), 'permission_callback' => function() { return current_user_can('manage_options'); }, ), array( 'methods' => 'POST', 'callback' => array($this, 'update_settings'), 'permission_callback' => function() { return current_user_can('manage_options'); }, ), )); register_rest_route('ai-alt-text-generator/v1', '/models', array( 'methods' => 'GET', 'callback' => array($this, 'get_models'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/generate-test', array( 'methods' => 'POST', 'callback' => array($this, 'handle_test_generation'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); // Managed-credit mode (no API key): connect / status / disconnect. register_rest_route('ai-alt-text-generator/v1', '/managed/connect', array( 'methods' => 'POST', 'callback' => array($this, 'managed_connect'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/managed/status', array( 'methods' => 'POST', 'callback' => array($this, 'managed_status'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); register_rest_route('ai-alt-text-generator/v1', '/managed/disconnect', array( 'methods' => 'POST', 'callback' => array($this, 'managed_disconnect'), 'permission_callback' => function() { return current_user_can('manage_options'); }, )); } /** * Connect (or re-connect) a managed-credit account for this site. * * Two ways in: * - Paste an existing API token (from the store dashboard — how paid buyers * connect). We validate it against the store and store it as-is. * - Otherwise auto-register a fresh free account (instant trial credits, no * email required). Email is optional and only used to link a later purchase. */ public function managed_connect(WP_REST_Request $request) { $token = sanitize_text_field((string) $request->get_param('token')); // Path 1: connect with a pasted token. if ('' !== $token) { $res = aatg_managed_status($token); if (is_wp_error($res)) { // Business outcome (not a transport error): return 200 so the admin // JS reads the structured fields instead of a thrown apiFetch error. return new WP_REST_Response(array( 'ok' => false, 'error' => 'invalid_token', 'message' => __('That token was not recognized. Copy it again from your dashboard.', 'ai-alt-text-generator'), ), 200); } $options = aatg_text_generator_get_options(); $options['managed_token'] = $token; $options['managed_mode'] = true; $options['managed_optout'] = false; update_option('aatg_text_generator_options', $options); return new WP_REST_Response(array( 'ok' => true, 'status' => $res['status'] ?? 'active', 'plan' => $res['plan'] ?? 'free', 'credits_remaining' => $res['credits_remaining'] ?? 0, 'monthly_credits' => $res['monthly_credits'] ?? 0, ), 200); } // Path 2: auto-register a free account (email optional). $email = sanitize_email((string) $request->get_param('email')); $res = aatg_managed_register($email, home_url()); if (is_wp_error($res)) { // A paid plan already exists for this site: send the user to the // dashboard to copy their token instead of minting a new one. if ('paid_account_exists' === $res->get_error_message()) { // Business outcome: 200 so the admin JS can show the dashboard link. return new WP_REST_Response(array( 'ok' => false, 'error' => 'paid_account_exists', 'message' => __('A paid plan already exists for this site. Sign in to your dashboard, copy your API token, and paste it here.', 'ai-alt-text-generator'), 'dashboard_url' => aatg_store_url() . '/login', ), 200); } return new WP_REST_Response(array('ok' => false, 'error' => $res->get_error_message()), 502); } $options = aatg_text_generator_get_options(); if ('' !== $email) { $options['managed_email'] = $email; } $options['managed_token'] = $res['token']; $options['managed_ref'] = isset($res['account_ref']) ? $res['account_ref'] : ''; $options['managed_mode'] = true; $options['managed_optout'] = false; update_option('aatg_text_generator_options', $options); return new WP_REST_Response(array( 'ok' => true, 'status' => $res['status'] ?? 'active', 'plan' => $res['plan'] ?? 'free', 'credits_remaining' => $res['credits_remaining'] ?? 0, 'monthly_credits' => $res['monthly_credits'] ?? 0, 'email' => $email, ), 200); } /** Report the connected account's plan + balance. */ public function managed_status(WP_REST_Request $request) { $options = aatg_text_generator_get_options(); if (empty($options['managed_token'])) { return new WP_REST_Response(array('ok' => false, 'error' => 'not_connected'), 400); } $res = aatg_managed_status($options['managed_token']); // The store doesn't know this key (404 "Unknown token"). Up to 2.7.2 // this came back as an error the page couldn't read, so the user saw // "Could not refresh status." on a site that looked connected. Get a // working key instead; if this site's plan needs its token pasted, // say so and show Connect. $reconnected = false; if (is_wp_error($res) && 404 === (int) ($res->get_error_data()['status'] ?? 0)) { if (aatg_managed_recover_connection()) { $options = aatg_text_generator_get_options(); $res = aatg_managed_status($options['managed_token']); $reconnected = !is_wp_error($res); } else { $options = aatg_text_generator_get_options(); $paid = !empty($options['managed_awaiting_token']); // Business outcome: 200 so the admin JS reads the fields. return new WP_REST_Response(array( 'ok' => false, 'error' => $paid ? 'paid_account_exists' : 'not_connected', 'message' => $paid ? aatg_managed_error_message('paid_account_exists') : __('This site\'s connection key is no longer valid. Click "Connect free credits" to reconnect. Your credits are kept.', 'ai-alt-text-generator'), 'managed_connected' => !empty($options['managed_token']), 'dashboard_url' => $paid ? aatg_store_url() . '/login' : '', ), 200); } } if (is_wp_error($res)) { return new WP_REST_Response(array('ok' => false, 'error' => $res->get_error_message()), 502); } $res['reconnected'] = $reconnected; if (isset($res['credits_remaining']) && (int) $res['credits_remaining'] > 0 && function_exists('aatg_managed_clear_at_limit')) { aatg_managed_clear_at_limit(); } $res['email'] = $options['managed_email'] ?? ''; return new WP_REST_Response($res, 200); } /** Disconnect: clear the token and disable managed mode. */ public function managed_disconnect(WP_REST_Request $request) { $options = aatg_text_generator_get_options(); $options['managed_mode'] = false; $options['managed_token'] = ''; $options['managed_email'] = ''; // Remember the choice so auto-connect doesn't immediately reconnect. $options['managed_optout'] = true; update_option('aatg_text_generator_options', $options); delete_option('aatg_managed_period_end'); return new WP_REST_Response(array('ok' => true), 200); } /** * Provider attempts per image in a bulk run before it is skipped. * Covers transient failures (rate limits, timeouts); a genuinely broken * image costs two extra tries and is then skipped as before. */ const MAX_IMAGE_ATTEMPTS = 3; public function start_processing(WP_REST_Request $request) { try { // Clear any previous transient delete_transient('aatg_bulk_generation_ids'); // Get total number of images to process $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'fields' => 'ids' ); // If not processing all images, only get those without alt text. // An empty list means there is nothing to do; leaving post__in // unset here used to count the whole library instead. $unreadable = 0; if (!$this->rewrite_all) { $ids = $this->get_images_without_alt_text_ids(); $unreadable = $this->count_unreadable_without_alt_text(); if (empty($ids)) { $total_images = 0; } else { $args['post__in'] = $ids; $total_images = count(get_posts($args)); } } else { $total_images = count(get_posts($args)); } if ($total_images === 0) { return new WP_REST_Response(array( 'status' => 'error', 'message' => $unreadable > 0 ? sprintf( /* translators: %d: number of images skipped because they could not be read before. */ _n( 'Nothing left to process. %d image without alt text could not be read on an earlier run. Save it again as a JPEG or PNG and it will be included next time.', 'Nothing left to process. %d images without alt text could not be read on an earlier run. Save them again as JPEG or PNG and they will be included next time.', $unreadable, 'ai-alt-text-generator' ), $unreadable ) : 'No images found to process', 'unreadable_excluded' => $unreadable, ), 200); } // Store processing state update_option('aatg_is_processing', true); update_option('aatg_processing_total', $total_images); update_option('aatg_processing_current', 0); update_option('aatg_processing_skipped', 0); delete_option('aatg_processing_retry'); $this->touch_heartbeat(); return new WP_REST_Response(array( 'status' => 'success', 'message' => sprintf('Found %d images to process', $total_images), 'total_items' => $total_images, 'unreadable_excluded' => $unreadable, 'is_processing' => true ), 200); } catch (Exception $e) { // Clean up on error update_option('aatg_is_processing', false); update_option('aatg_processing_total', 0); update_option('aatg_processing_current', 0); return new WP_REST_Response(array( 'status' => 'error', 'message' => 'Internal server error: ' . $e->getMessage() ), 500); } } public function process_next_image() { // Set when the provider rejected the image itself (see below). $unreadable = false; try { if (!get_option('aatg_is_processing', false)) { return new WP_REST_Response(array( 'status' => 'error', 'message' => 'Processing is not active' ), 200); } $this->touch_heartbeat(); $current = get_option('aatg_processing_current', 0); $total = get_option('aatg_processing_total', 0); if ($current >= $total) { $this->finish_processing(); return new WP_REST_Response(array( 'status' => 'completed', 'message' => 'All images processed', 'current' => $current, 'total' => $total ), 200); } $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 1, 'offset' => $current ); // Check if we're processing a specific list of IDs from a bulk action $bulk_ids = get_transient('aatg_bulk_generation_ids'); if ($bulk_ids && is_array($bulk_ids)) { $args['post__in'] = $bulk_ids; $args['orderby'] = 'post__in'; } elseif (!$this->rewrite_all) { $ids = $this->get_images_without_alt_text_ids(); if (empty($ids)) { $this->finish_processing(); return new WP_REST_Response(array( 'status' => 'completed', 'message' => 'No more images to process', 'current' => $current, 'total' => $total ), 200); } $args['post__in'] = $ids; // Follow the list's order exactly. The default (date) ties for // images uploaded in the same second, and offset paging over an // undefined order skips some images and revisits others. $args['orderby'] = 'post__in'; // This candidate list SHRINKS as images gain alt text, so paging // it with a progress-based offset walks off the front of the // queue and silently skips images. Step past only the ones we // couldn't process — everything successful drops out of the // list by itself. Mirrors process_media_batch(). $args['offset'] = get_option('aatg_processing_skipped', 0); } $media_items = get_posts($args); if (empty($media_items)) { $this->finish_processing(); return new WP_REST_Response(array( 'status' => 'completed', 'message' => 'No more images to process', 'current' => $current, 'total' => $total ), 200); } $item = $media_items[0]; // The user asked for this image's alt text to be left alone (its // current text contains one of their keep-words). Check BEFORE // calling a provider: generating and then discarding the answer // burned a credit per protected image and slowed the run down. if (aatg_is_alt_text_protected($item->ID)) { $current++; update_option('aatg_processing_current', $current); // The "images without alt text" queue shrinks as images are // filled in, so it is paged by the skipped count rather than by // progress. A protected image stays in any such queue, so it has // to count as skipped or the next call would hand back the very // same image forever. if (!$this->rewrite_all && !($bulk_ids && is_array($bulk_ids))) { update_option('aatg_processing_skipped', get_option('aatg_processing_skipped', 0) + 1); } if ($current >= $total) { delete_transient('aatg_bulk_generation_ids'); } return new WP_REST_Response(array( 'status' => 'protected', 'message' => 'Existing alt text kept (matches a keep-word)', 'current' => $current, 'total' => $total, 'is_processing' => true ), 200); } // Get provider and API key from options $options = get_option('aatg_text_generator_options', array()); $provider = $options['ai_provider'] ?: 'openai'; $api_key_field = $provider . '_key'; if (!aatg_managed_mode_active() && empty($options[$api_key_field])) { throw new Exception('API key for ' . $provider . ' is not configured'); } $api_key = $options[$api_key_field]; // A sized rendition, not the original: base64-encoding a 20 MB // camera file balloons PHP memory and request time enough to take // down the whole request on some hosts, and alt text doesn't need // the extra pixels. Falls back to the original when no rendition // exists (small images). $image_base64 = aatg_get_image_base64_for_attachment($item->ID); if (empty($image_base64)) { throw new Exception('Failed to read image file'); } // Generate alt text using the selected provider $prompt = $options['prompt'] ?: 'Create a SEO optimized alt text for this image. Don\'t include quotes and keep it informative and concise.'; $language = $options['language'] ?: 'english'; $result = AATG_Provider_Factory::generate_alt_text( $provider, $image_base64, $prompt, $language, $api_key, array('attachment_id' => $item->ID, 'source' => 'bulk') ); // An account-level failure (out of credits, not activated, disabled, // bad token) will reject every remaining image in exactly the same // way. Stop the run and tell the user why, instead of grinding // through the whole library making doomed requests. if (!$result['success'] && !empty($result['code']) && function_exists('aatg_is_terminal_managed_error') && aatg_is_terminal_managed_error($result['code']) ) { $this->finish_processing(); return new WP_REST_Response(array( 'status' => 'stopped', 'code' => $result['code'], 'message' => $result['message'], 'upgrade_url' => isset($result['upgrade_url']) ? $result['upgrade_url'] : '', 'current' => $current, 'total' => $total, 'is_processing' => false, ), 200); } // The provider can't read this image (corrupt, tiny, unsupported // format). Asking again gets the same answer, so don't retry, and // leave it out of later "missing alt text" runs. One site spent // 1,385 requests in a day re-asking about ~460 such images. if (!$result['success'] && !empty($result['unreadable'])) { delete_option('aatg_processing_retry'); update_post_meta($item->ID, '_aatg_unreadable', time()); $unreadable = true; throw new Exception($result['message']); } if (!$result['success']) { // A provider failure arrives here as a well-formed error result, // so the 2.6.2 retry (which only catches thrown transport // errors) never saw it: every rate limit or provider timeout // cost the user that image on the first try. One site lost 86 // of 115 images to a single rate-limited hour that way. // // Leave current/skipped untouched so the next call picks the // SAME image, and track attempts server-side — the client // cannot retry the right image once the counters have moved. $retry = get_option('aatg_processing_retry', array()); $attempts = (isset($retry['id']) && (int) $retry['id'] === (int) $item->ID) ? (int) $retry['attempts'] + 1 : 1; if ($attempts < self::MAX_IMAGE_ATTEMPTS) { update_option('aatg_processing_retry', array('id' => (int) $item->ID, 'attempts' => $attempts), false); return new WP_REST_Response(array( 'status' => 'retry', 'message' => $result['message'], 'attempt' => $attempts, // Grows with each attempt; the client sleeps this long // so the wait never runs inside a PHP request. 'retry_in' => $attempts * 5, 'current' => $current, 'total' => $total, 'is_processing' => true, ), 200); } delete_option('aatg_processing_retry'); throw new Exception($result['message']); } delete_option('aatg_processing_retry'); $alt_text = $result['alt_text']; // Save the generated alt text (with add-on hooks). aatg_save_generated_alt_text($item->ID, $alt_text, array('source' => 'bulk')); $current++; update_option('aatg_processing_current', $current); // If processing is complete, clear the transient if ($current >= $total) { delete_transient('aatg_bulk_generation_ids'); } return new WP_REST_Response(array( 'status' => 'success', 'message' => 'Image processed successfully', 'current' => $current, 'total' => $total, 'is_processing' => true ), 200); } catch (Exception $e) { // Skip this image but continue processing $current++; update_option('aatg_processing_current', $current); // A failed image keeps its empty alt text, so the missing-alt // query would hand it back forever; the query offset steps past it. // An unreadable image has just been marked, which drops it from // that query by itself; stepping past it too would skip the next // image in line. if (!$unreadable) { update_option('aatg_processing_skipped', get_option('aatg_processing_skipped', 0) + 1); } return new WP_REST_Response(array( 'status' => 'error', 'message' => $e->getMessage(), 'unreadable' => $unreadable, 'current' => $current, 'total' => $total, 'is_processing' => true ), 200); } } public function validate_api_key(WP_REST_Request $request) { $key = $request->get_param('key'); $provider = $request->get_param('provider'); if (empty($key)) { return new WP_REST_Response(array( 'valid' => false, 'message' => 'API key is required' ), 400); } if (empty($provider)) { return new WP_REST_Response(array( 'valid' => false, 'message' => 'Provider is required' ), 400); } $result = AATG_Provider_Factory::validate_api_key($provider, $key); $status_code = $result['valid'] ? 200 : 400; return new WP_REST_Response($result, $status_code); } public function process_media_batch($batch_size) { if (!get_option('aatg_is_processing', false)) { update_option('aatg_processing_total', 0); update_option('aatg_processing_current', 0); update_option('aatg_processing_skipped', 0); return; } $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => $batch_size, 'offset' => get_option('aatg_processing_current', 0) ); // A Media Library bulk action records the exact selection. Page through // that list rather than the whole library, so we only ever touch the // images the user actually picked. Mirrors process_next_image(). $bulk_ids = get_transient('aatg_bulk_generation_ids'); if ($bulk_ids && is_array($bulk_ids)) { $args['post__in'] = $bulk_ids; $args['orderby'] = 'post__in'; } elseif (!$this->rewrite_all) { // If not rewriting all, fetch images without alt text $ids = $this->get_images_without_alt_text_ids(); if (empty($ids)) { $this->finish_processing(); return; } $args['post__in'] = $ids; // Follow the list's order exactly. The default (date) ties for // images uploaded in the same second, and offset paging over an // undefined order skips some images and revisits others. $args['orderby'] = 'post__in'; // This candidate list SHRINKS as images gain alt text, so paging it // with a progress-based offset walks off the front of the queue and // silently skips images. Step past only the ones we couldn't // process — everything successful drops out of the list by itself. $args['offset'] = get_option('aatg_processing_skipped', 0); } $media_items = get_posts($args); if (empty($media_items)) { $this->finish_processing(); return; } $admin_instance = AATG_Text_Generator_Admin::get_instance(); $current = get_option('aatg_processing_current', 0); $total = get_option('aatg_processing_total', 0); foreach ($media_items as $item) { if (!get_option('aatg_is_processing', false)) { return; } // Per image: a batch of 10 can take minutes, and the run must not // read as paused while cron is working through it. $this->touch_heartbeat(); // `current` is both the progress counter and (for fixed-list runs) // the query offset, so it advances for every image taken off the // queue — including failures. Advancing only on success re-fetched // the same failing image on every batch and the run never moved on. $current++; $saved = false; $image_url = $admin_instance->get_image_url_by_size($item->ID, 'thumbnail'); if ($image_url) { try { $alt_text = $admin_instance->generate_alt_text_with_ai( $image_url, array('attachment_id' => $item->ID, 'source' => 'bulk') ); if ($alt_text) { // Route through the shared saver so add-on hooks and the // Title/Caption/Description mirroring apply here too. $admin_instance->save_generated_alt_text( $item->ID, $alt_text, array('source' => 'bulk') ); $saved = true; } } catch (Exception $e) { // Leave $saved false; counted as skipped below. } } // generate_alt_text_with_ai() flattens every failure to an empty // string, so the reason has to come from the transient the managed // client records. Same rule as the REST path: an account-level dead // end stops the run rather than repeating for every image left. if (!$saved && function_exists('aatg_is_terminal_managed_error')) { $last = get_transient('aatg_managed_last_error'); if ($last && aatg_is_terminal_managed_error($last)) { update_option('aatg_processing_current', $current); $this->finish_processing(); return; // Deliberately not rescheduled. } } // An image we couldn't process stays in the "missing alt text" query, // so that run needs to step over it explicitly or it would be // re-fetched forever. if (!$saved) { update_option('aatg_processing_skipped', get_option('aatg_processing_skipped', 0) + 1); } update_option('aatg_processing_current', $current); // Check if we've processed all images if ($total > 0 && $current >= $total) { $this->finish_processing(); return; } } if (get_option('aatg_is_processing', false)) { wp_schedule_single_event(time() + 5, 'ai_process_media_batch', array($batch_size)); } } /** Clear all bulk-run state. Terminal step for every finish path. */ private function finish_processing() { update_option('aatg_is_processing', false); update_option('aatg_processing_total', 0); update_option('aatg_processing_current', 0); update_option('aatg_processing_skipped', 0); delete_option('aatg_processing_retry'); delete_option('aatg_processing_heartbeat'); delete_transient('aatg_bulk_generation_ids'); } private function get_images_without_alt_text_ids() { global $wpdb; $query = " SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_wp_attachment_image_alt' LEFT JOIN {$wpdb->postmeta} un ON p.ID = un.post_id AND un.meta_key = '_aatg_unreadable' WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE 'image%' AND (pm.meta_value IS NULL OR pm.meta_value = '') AND un.meta_id IS NULL ORDER BY p.ID ASC "; $results = $wpdb->get_results($query); $ids = array_map(function($result) { return $result->ID; }, $results); return $ids; } /** * Images still missing alt text that a past run found unreadable, and so * left out of get_images_without_alt_text_ids(). * * @since 2.7.3 */ private function count_unreadable_without_alt_text() { global $wpdb; return (int) $wpdb->get_var(" SELECT COUNT(DISTINCT p.ID) FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} un ON p.ID = un.post_id AND un.meta_key = '_aatg_unreadable' LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_wp_attachment_image_alt' WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE 'image%' AND (pm.meta_value IS NULL OR pm.meta_value = '') "); } /** * List the models a provider's account can actually call right now. * * GET /ai-alt-text-generator/v1/models * ?provider Defaults to the saved ai_provider. * ?key Use this key instead of the saved one (so the list can * load before the key is saved). * ?refresh Truthy bypasses the 12h cache. * * @since 2.6.5 */ public function get_models(WP_REST_Request $request) { $options = get_option('aatg_text_generator_options', array()); $provider_name = sanitize_text_field((string) $request->get_param('provider')); if ('' === $provider_name) { $provider_name = isset($options['ai_provider']) && $options['ai_provider'] ? $options['ai_provider'] : 'openai'; } $provider = AATG_Provider_Factory::get_provider($provider_name); if (!$provider) { return new WP_REST_Response(array( 'error' => 'Unknown provider: ' . $provider_name, ), 400); } $key = (string) $request->get_param('key'); $refresh_param = $request->get_param('refresh'); $refresh = !empty($refresh_param) && 'false' !== $refresh_param && '0' !== $refresh_param; $available = $provider->get_available_models('' !== $key ? $key : null, $refresh); return new WP_REST_Response(array( 'provider' => $provider_name, 'models' => $available['models'], 'default' => $provider->get_default_model(), 'current' => $provider->resolve_model(), 'live' => $available['live'], ), 200); } public function get_settings() { $defaults = aatg_text_generator_default_options(); $options = get_option('aatg_text_generator_options', $defaults); // Add provider options and their default models for the frontend $providers = AATG_Provider_Factory::get_providers(); $provider_options = array(); $default_models = array(); foreach ($providers as $name => $provider) { $provider_options[$name] = $provider->get_display_name(); $default_models[$name] = $provider->get_default_model(); } $options['available_providers'] = $provider_options; $options['default_models'] = $default_models; // Never expose the managed-credit token to the frontend; surface a // connected flag instead. $options['managed_connected'] = !empty($options['managed_token']); unset($options['managed_token']); // Let the settings UI show the Pro upsell only when Pro isn't installed. $options['pro_active'] = defined('AATG_PRO_VERSION'); // Ensure a model is set, if not, use the default for the current provider if (empty($options['model'])) { $current_provider = $options['ai_provider'] ?? 'openai'; $options['model'] = $default_models[$current_provider] ?? ''; } return new WP_REST_Response($options, 200); } public function update_settings(WP_REST_Request $request) { $settings = $request->get_params(); $defaults = aatg_text_generator_default_options(); // Remove read-only / server-managed fields the frontend echoes back. unset($settings['available_providers'], $settings['default_models'], $settings['managed_connected']); // The managed-credit connection is owned by /managed/connect and // /managed/disconnect, never by a settings save. Always keep the stored // values: a page that stayed open across a Connect still holds the old // token, and writing it back breaks every generation with "not_found". $existing = get_option('aatg_text_generator_options', array()); if (!is_array($existing)) { $existing = array(); } foreach (array('managed_token', 'managed_ref', 'managed_optout', 'managed_awaiting_token') as $field) { unset($settings[$field]); if (array_key_exists($field, $existing)) { $settings[$field] = $existing[$field]; } } // Ensure we have all required fields $settings = wp_parse_args($settings, $defaults); // A changed API key means the cached model catalog belongs to another // account — flush it so the next lookup re-fetches. foreach (AATG_Provider_Factory::get_providers() as $name => $provider) { $field = $name . '_key'; $old_key = isset($existing[$field]) ? $existing[$field] : ''; $new_key = isset($settings[$field]) ? $settings[$field] : ''; if ($old_key !== $new_key) { $provider->flush_model_cache(); } } // Delete the option first to ensure it's updated delete_option('aatg_text_generator_options'); // Save the new settings $result = update_option('aatg_text_generator_options', $settings, false); // Verify the save $saved = get_option('aatg_text_generator_options'); if (!$result || !$saved) { unset($settings['managed_token']); return new WP_REST_Response(array( 'error' => 'Failed to save settings', 'settings' => $settings ), 500); } // Re-add provider options for frontend (since it's read-only) $saved['available_providers'] = AATG_Provider_Factory::get_provider_options(); // Same shape as GET /settings: the token stays on the server, the UI // gets a connected flag. $saved['managed_connected'] = !empty($saved['managed_token']); unset($saved['managed_token']); return new WP_REST_Response($saved, 200); } public function check_processing_status() { $is_processing = get_option('aatg_is_processing', false); return new WP_REST_Response(array('is_processing' => $is_processing), 200); } public function stop_processing() { $this->finish_processing(); return new WP_REST_Response(array( 'status' => 'success', 'message' => 'Processing stopped' ), 200); } public function get_processing_status() { $is_processing = get_option('aatg_is_processing', false); $total_items = get_option('aatg_processing_total', 0); $current_item = get_option('aatg_processing_current', 0); // Validate the status - if current equals total, processing is done if ($total_items > 0 && $current_item >= $total_items) { update_option('aatg_is_processing', false); update_option('aatg_processing_total', 0); update_option('aatg_processing_current', 0); $is_processing = false; $total_items = 0; $current_item = 0; } // If not processing, report zero without writing. This endpoint is // polled every two seconds, so writing here raced the Start request: // a poll that read "not processing" a moment before the run began then // zeroed the counters it had just written, leaving is_processing = true // with 0 of 0 images — which the UI read as "completed" while the run // was still going. Reads only now; finish_processing() does the reset. if (!$is_processing) { $total_items = 0; $current_item = 0; } // Options come back from the database as strings. The page compared // "3" >= "1567" as text, which is true, and announced a stuck run as // "Bulk generation completed!". return new WP_REST_Response(array( 'is_processing' => (bool) $is_processing, 'total_items' => (int) $total_items, 'current_item' => (int) $current_item, 'paused' => self::is_paused(), ), 200); } /** * How long a run can go without a request before it counts as paused. * One image can legitimately take a minute (store timeout) plus a 30 s * retry wait, so this is well past that. */ const PAUSED_AFTER = 180; /** * A run is marked as processing but nothing is driving it. * * Bulk generation is driven by the Bulk Generation tab: each image is one * request from that page. Leave the page and the run stops, but * aatg_is_processing stays set, so the admin notice showed the same * "Processed 3 of 1567" forever with no way to tell it had stopped. * A run with no heartbeat at all started before 2.7.3 and is long dead. * * @since 2.7.3 * @return bool */ public static function is_paused() { if (!get_option('aatg_is_processing', false)) { return false; } $last = (int) get_option('aatg_processing_heartbeat', 0); return $last <= 0 || (time() - $last) > self::PAUSED_AFTER; } /** Record that the current run just did something. */ private function touch_heartbeat() { update_option('aatg_processing_heartbeat', time(), false); } public function handle_test_generation(WP_REST_Request $request) { try { $image_id = $request->get_param('image_id'); $custom_prompt = $request->get_param('prompt'); if (!$image_id) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Image ID is required' ), 400); } // Get provider and API key from options $options = get_option('aatg_text_generator_options', array()); $provider = $options['ai_provider'] ?: 'openai'; $api_key_field = $provider . '_key'; if (!aatg_managed_mode_active() && empty($options[$api_key_field])) { return new WP_REST_Response(array( 'success' => false, 'message' => 'API key for ' . $provider . ' is not configured' ), 400); } $api_key = $options[$api_key_field]; // Get image file path instead of URL $upload_dir = wp_upload_dir(); $image_meta = wp_get_attachment_metadata($image_id); if (!$image_meta || !isset($image_meta['file'])) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Failed to get image metadata' ), 400); } // Get the full server path to the image $image_path = $upload_dir['basedir'] . '/' . $image_meta['file']; // Check if file exists if (!file_exists($image_path)) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Image file not found' ), 400); } // Read image file directly $image_data = file_get_contents($image_path); if ($image_data === false) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Failed to read image file' ), 400); } // Convert image to base64 $image_base64 = base64_encode($image_data); if (empty($image_base64)) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Failed to process image' ), 400); } // Generate alt text using the selected provider $prompt = $custom_prompt ?: $options['prompt'] ?: 'Create a SEO optimized alt text for this image. Don\'t include quotes and keep it informative and concise.'; $language = $options['language'] ?: 'english'; $result = AATG_Provider_Factory::generate_alt_text( $provider, $image_base64, $prompt, $language, $api_key ); if (!$result['success']) { return new WP_REST_Response(array( 'success' => false, 'message' => $result['message'] ), 400); } $alt_text = $result['alt_text']; return new WP_REST_Response(array( 'success' => true, 'alt_text' => $alt_text ), 200); } catch (Exception $e) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Error: ' . $e->getMessage() ), 500); } } } // Initialize the class new AATG_Text_Generator_Restpoint();