'forbidden'], 403); $id = isset($_POST['attachment_id']) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0; $res = Acpimg_Managed::run('alt_text', $id, [], 'default'); if (is_wp_error($res)) wp_send_json_error(['error' => $res->get_error_code(), 'message' => $res->get_error_message()]); wp_send_json_success($res); } public static function ajax_bulk() { check_ajax_referer('acpimg_action', '_wpnonce'); if (!current_user_can('manage_options')) wp_send_json_error(['error' => 'forbidden'], 403); $ids = self::find_missing_alt(200); foreach ($ids as $id) { Acpimg_Job_Queue::enqueue($id, 'alt_text', ['tier' => 'default']); } wp_send_json_success(['enqueued' => count($ids)]); } public static function find_missing_alt($limit = 200) { global $wpdb; // %i identifier placeholders for both table names; prepare() inlined into // get_col() so PluginCheck's NotPrepared sniff can see it. // phpcs:ignore WordPress.DB.DirectDatabaseQuery $ids = $wpdb->get_col($wpdb->prepare( "SELECT p.ID FROM %i p LEFT JOIN %i pm ON pm.post_id = p.ID AND pm.meta_key = %s WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE %s AND (pm.meta_value IS NULL OR pm.meta_value = '') LIMIT %d", $wpdb->posts, $wpdb->postmeta, '_wp_attachment_image_alt', 'image/%', (int) $limit )); return array_map('intval', $ids); } public static function finish($attachment_id, $output, $tier) { $text = is_array($output) ? implode(' ', $output) : (string) $output; $text = trim(wp_strip_all_tags($text)); if ($text === '') return new WP_Error('acpimg_empty_alt', 'empty alt text'); update_post_meta($attachment_id, '_wp_attachment_image_alt', sanitize_text_field($text)); return ['alt' => $text, 'attachment_id' => $attachment_id]; } public static function finish_job($null, $feature, $image_id, $output, $tier, $extra) { if ($feature !== 'alt_text') return $null; return self::finish($image_id, $output, $tier); } }