target_post_types() as $type) { add_filter("bulk_actions-edit-{$type}", [$this, 'add_bulk_action']); add_filter("handle_bulk_actions-edit-{$type}", [$this, 'handle_bulk_action'], 10, 3); } add_action('admin_notices', [$this, 'render_notice']); } /** * Public post types that hold editable content. * * Attachments are excluded: their alt text is the source these actions read from. */ private function target_post_types(): array { $types = get_post_types(['public' => true], 'names'); unset($types['attachment']); return $types; } /** * @param array $actions Bulk actions registered for the current list screen. * @return array */ public function add_bulk_action($actions) { // Two entries rather than a checkbox plus a dialog, because a bulk action // cannot confirm. The label carries the warning, and the safe one is first. $actions[self::ACTION] = __('Apply AiGude alt text to images', 'aigude-tools'); $actions[self::ACTION_OVERWRITE] = __('Apply AiGude alt text (overwrite existing)', 'aigude-tools'); return $actions; } /** * @param string $redirect Redirect URL the list screen will send the user to. * @param string $action Selected bulk action. * @param array $post_ids Selected post IDs. * @return string */ public function handle_bulk_action($redirect, $action, $post_ids) { if ($action !== self::ACTION && $action !== self::ACTION_OVERWRITE) { return $redirect; } $overwrite = ($action === self::ACTION_OVERWRITE); $posts = 0; $images = 0; foreach ((array) $post_ids as $post_id) { $post_id = (int) $post_id; if (!current_user_can('edit_post', $post_id)) { continue; } $filled = $this->apply_to_post($post_id, $overwrite); if ($filled > 0) { $posts++; $images += $filled; } } return add_query_arg([ 'aigude_alt_posts' => $posts, 'aigude_alt_images' => $images, ], $redirect); } /** * Write alt text into one post's stored content. * * Without $overwrite the operation only fills blanks, so it is non-destructive. * With $overwrite it replaces existing alt text, including text an editor wrote, * matching the TYPO3 extension's unticked "Skip existing" behaviour. Both modes * skip images whose alt already equals the stored value, so both are idempotent. * * @return int Number of images updated. */ private function apply_to_post(int $post_id, bool $overwrite = false): int { $post = get_post($post_id); if (!$post || $post->post_content === '' || stripos($post->post_content, 'post_content); // Pass 1: bookmark images that need an alt and collect their attachment IDs, // so the meta for all of them can be primed in a single query. $pending = []; while ($tags->next_tag('img')) { if (!$overwrite && !empty($tags->get_attribute('alt'))) { continue; } if (!preg_match('/wp-image-(\d+)/i', (string) $tags->get_attribute('class'), $m)) { continue; } $bookmark = 'aigude_' . count($pending); if ($tags->set_bookmark($bookmark)) { $pending[$bookmark] = (int) $m[1]; } } if (!$pending) { return 0; } update_postmeta_cache(array_values(array_unique($pending))); // Pass 2: seek back to each bookmark and set the attribute. $filled = 0; foreach ($pending as $bookmark => $attachment_id) { $alt = trim(wp_strip_all_tags((string) get_post_meta($attachment_id, '_wp_attachment_image_alt', true))); if ($alt === '') { $tags->release_bookmark($bookmark); continue; } $tags->seek($bookmark); // Skip when the value is already correct, so a second run of either mode // reports zero changes and writes nothing. if ((string) $tags->get_attribute('alt') === $alt) { $tags->release_bookmark($bookmark); continue; } $tags->set_attribute('alt', $alt); $tags->release_bookmark($bookmark); $filled++; } if ($filled === 0) { return 0; } // wp_update_post() unslashes what it receives, so the content must be slashed // on the way in or literal backslashes are stripped from the markup. wp_update_post([ 'ID' => $post_id, 'post_content' => wp_slash($tags->get_updated_html()), ]); return $filled; } /** * Report the result of a bulk run on the list screen the user lands back on. */ public function render_notice(): void { if (!isset($_GET['aigude_alt_posts'], $_GET['aigude_alt_images'])) { return; } $posts = (int) $_GET['aigude_alt_posts']; $images = (int) $_GET['aigude_alt_images']; if ($images === 0) { $message = __('No images needed alt text. Images already carrying alt text are never changed.', 'aigude-tools'); } else { $message = sprintf( /* translators: 1: number of images updated, 2: number of posts updated. */ _n( 'Applied alt text to %1$d image across %2$d entry.', 'Applied alt text to %1$d images across %2$d entries.', $images, 'aigude-tools' ), $images, $posts ); } printf('

%s

', esc_html($message)); } }