' . esc_html__( 'No active attachment attributes found.', 'ai-for-seo' ) . '

'; return; } $ai4seo_can_administer_plugin = ai4seo_can_administer_plugin(); require_once __DIR__ . '/list-filters.php'; $ai4seo_list_status_snapshot = ai4seo_read_content_type_status_snapshot( AI4SEO_BULK_GENERATION_QUEUE_CONTEXT_ATTACHMENT_ATTRIBUTES, true ); $ai4seo_list_read_failures = ai4seo_content_type_list_read_failures(); $ai4seo_list_coverage_issues = array(); // ___________________________________________________________________________________________ \\ // === PREPARE =============================================================================== \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ global $wpdb; $ai4seo_allowed_attachment_mime_types = ai4seo_get_allowed_attachment_mime_types(); $ai4seo_waiting_attachment_mime_types = ai4seo_get_enabled_attachment_mime_types(); // Treat an explicitly provided ID list as an additional media-table filter for modal reuse. if ( isset( $ai4seo_attachment_post_ids_filter ) ) { $ai4seo_attachment_post_ids_filter_is_active = true; $ai4seo_attachment_post_ids_filter = array_values( array_filter( ai4seo_normalize_related_attachment_post_ids( (array) $ai4seo_attachment_post_ids_filter ) ) ); } else { $ai4seo_attachment_post_ids_filter_is_active = false; $ai4seo_attachment_post_ids_filter = array(); } $ai4seo_is_related_attachments_modal = ( ! empty( $ai4seo_is_related_attachments_modal ) && $ai4seo_attachment_post_ids_filter_is_active ); $ai4seo_main_attachment_post_type = 'attachment'; $ai4seo_nice_post_type = 'media'; $ai4seo_post_types = ai4seo_get_supported_attachment_post_types( ! $ai4seo_attachment_post_ids_filter_is_active ); $ai4seo_additional_post_types = array(); $ai4seo_account_readiness = ai4seo_get_account_readiness(); $ai4seo_current_credits_balance = $ai4seo_account_readiness['credits']; $ai4seo_media_label_singular = _n( 'media', 'media', 1, 'ai-for-seo' ); $ai4seo_media_label_plural = _n( 'media', 'media', 2, 'ai-for-seo' ); $ai4seo_total_pages = 1; // Keep modal pagination isolated from the full media page by reading the shared filter page parameter. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Capability-gated read-only pagination cursor; no state is changed. $ai4seo_current_page = absint( wp_unslash( $_REQUEST['ai4seo_page'] ?? 1 ) ); if ( $ai4seo_current_page < 1 ) { $ai4seo_current_page = 1; } // Start with empty row state so optimized media lists do not parse global status options before page IDs are known. $ai4seo_pending_attributes_attachment_post_ids = array(); $ai4seo_processing_attributes_attachment_post_ids = array(); $ai4seo_missing_attachment_attributes_post_ids = array(); $ai4seo_failed_attributes_attachment_post_ids = array(); $ai4seo_hidden_attachment_attributes_post_ids = array(); $ai4seo_auto_queue_disallowed_attachment_attributes_post_ids = array(); $ai4seo_fully_covered_attachment_post_ids = array(); $ai4seo_generated_attachment_post_ids = array(); $ai4seo_waiting_to_get_queued_attachment_post_ids = array(); $ai4seo_status_filter_counts = array(); $ai4seo_total_items = 0; $ai4seo_current_page_attachment_post_ids = array(); $ai4seo_sort_value_map = array(); $ai4seo_attachment_attributes_coverage_for_sorting = array(); $ai4seo_attachment_coverage_read_succeeded = false; $ai4seo_attachment_attributes_coverage_summary_for_sorting = array(); $ai4seo_num_total_attachment_attributes_for_sorting = 0; $ai4seo_should_derive_current_page_attachment_status_ids = false; $ai4seo_should_defer_status_filters = false; $ai4seo_disabled_attachment_post_author_ids = ai4seo_get_disabled_attachment_post_author_ids(); // Read media WPML exclusions once so filters, status counts, and queue previews use the same scope. $ai4seo_disabled_attachment_attributes_wpml_language_codes = ai4seo_get_disabled_attachment_attributes_wpml_language_codes(); $ai4seo_do_generate_attachment_attributes_for_fully_covered_entries = ai4seo_do_generate_attachment_attributes_for_fully_covered_entries(); // ___________________________________________________________________________________________ \\ // === READ ================================================================================== \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ // handle the import of NextGen Gallery images. if ( $ai4seo_can_administer_plugin && ! $ai4seo_is_related_attachments_modal && ai4seo_is_plugin_or_theme_active( AI4SEO_THIRD_PARTY_PLUGIN_NEXTGEN_GALLERY ) ) { $ai4seo_num_not_imported_nextgen_gallery_images = 0; $ai4seo_nextgen_gallery_image_import_counts = ai4seo_read_nextgen_gallery_image_import_counts(); if ( false === $ai4seo_nextgen_gallery_image_import_counts ) { ai4seo_debug_message( 984321699, 'Could not read the coherent NextGEN provider/imported image counts. Database error: ' . $wpdb->last_error ); } else { $ai4seo_num_not_imported_nextgen_gallery_images = max( 0, $ai4seo_nextgen_gallery_image_import_counts['provider_count'] - $ai4seo_nextgen_gallery_image_import_counts['imported_count'] ); } $ai4seo_import_nextgen_gallery_button = ai4seo_get_icon_button_tag( 'file-export', sprintf( /* translators: 1: Number of images that can be imported from NextGen Gallery */ esc_html__( 'Import NextGen Gallery images (%s)', 'ai-for-seo' ), $ai4seo_num_not_imported_nextgen_gallery_images ), ( $ai4seo_num_not_imported_nextgen_gallery_images ? 'ai4seo-primary-button' : 'ai4seo-inactive-button' ), ( $ai4seo_num_not_imported_nextgen_gallery_images ? 'ai4seo_import_nextgen_gallery_images(this);' : '' ) ); } // Prepare arguments for the wp-query. $ai4seo_posts_query_arguments = array( 'post_status' => array( 'publish', 'future', 'inherit' ), 'post_type' => count( $ai4seo_post_types ) > 1 ? $ai4seo_post_types : reset( $ai4seo_post_types ), 'post_mime_type' => $ai4seo_allowed_attachment_mime_types, 'posts_per_page' => 20, 'orderby' => 'ID', 'order' => 'DESC', 'perm' => 'editable', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters -- The editable media inventory deliberately includes every language. 'suppress_filters' => true, 'lang' => 'all', ); if ( $ai4seo_disabled_attachment_post_author_ids ) { $ai4seo_posts_query_arguments['author__not_in'] = $ai4seo_disabled_attachment_post_author_ids; } // Carry the source post ID through filter/search/pagination links when attachment.php is rendered in the modal. $ai4seo_attachment_filter_hidden_fields = array(); $ai4seo_related_attachments_modal_post_id = isset( $ai4seo_related_attachments_modal_post_id ) ? absint( $ai4seo_related_attachments_modal_post_id ) : 0; if ( $ai4seo_is_related_attachments_modal && $ai4seo_related_attachments_modal_post_id > 0 ) { $ai4seo_attachment_filter_hidden_fields['post_id'] = $ai4seo_related_attachments_modal_post_id; } // Related-media modal filters must stay constrained to the scanner result before search SQL is built. $ai4seo_scoped_attachment_post_ids = $ai4seo_attachment_post_ids_filter_is_active ? $ai4seo_attachment_post_ids_filter : null; // Build shared controls after resolving modal scope so search and exact fallback paths use the same candidate boundary. $ai4seo_filter_context = ai4seo_setup_content_type_filters( array( 'form_action' => ai4seo_get_subpage_url( $ai4seo_nice_post_type, array( 'ai4seo_page' => 1 ) ), 'nonce_action' => 'ai4seo_content_type_filter_form', 'nonce_name' => 'ai4seo_content_type_filter_nonce', 'post_types' => (array) $ai4seo_post_types, 'post_status' => array( 'publish', 'future', 'inherit' ), 'post_mime_types' => (array) $ai4seo_allowed_attachment_mime_types, 'author_not_in' => $ai4seo_disabled_attachment_post_author_ids, 'disabled_wpml_language_codes' => $ai4seo_disabled_attachment_attributes_wpml_language_codes, 'search_file_meta' => true, 'scoped_post_ids' => $ai4seo_scoped_attachment_post_ids, 'per_page' => 20, 'hidden_fields' => $ai4seo_attachment_filter_hidden_fields, ) ); $ai4seo_filter_status = $ai4seo_filter_context['filter_status']; $ai4seo_filter_language = $ai4seo_filter_context['filter_language']; // Reuse the shared sort normalizer so media rows, links, and AJAX-hydrated filters stay in sync. $ai4seo_sort_args = ai4seo_normalize_content_type_sort_args( $ai4seo_filter_context['orderby'] ?? 'id', $ai4seo_filter_context['order'] ?? 'desc' ); $ai4seo_orderby = $ai4seo_sort_args['orderby']; $ai4seo_order = $ai4seo_sort_args['order']; $ai4seo_search_ids = $ai4seo_filter_context['search_ids']; $ai4seo_items_per_page = (int) $ai4seo_filter_context['per_page']; if ( $ai4seo_items_per_page < 1 ) { $ai4seo_items_per_page = 20; } // Bulk-generation settings are needed by both the optimized resolver and the exact legacy branch. $ai4seo_is_bulk_generation_activated = ai4seo_is_bulk_generation_enabled( $ai4seo_main_attachment_post_type ); $ai4seo_is_bulk_generation_checked_phrase = ( $ai4seo_is_bulk_generation_activated ? 'checked' : '' ); $ai4seo_should_auto_queue_bulk_generation_entries = ai4seo_should_auto_queue_bulk_generation_entries(); $ai4seo_bulk_generation_new_or_existing_filter = ai4seo_get_setting( AI4SEO_SETTING_BULK_GENERATION_NEW_OR_EXISTING_FILTER ); $ai4seo_bulk_generation_new_or_existing_filter_reference_timestamp = ai4seo_read_environmental_variable( AI4SEO_ENVIRONMENTAL_VARIABLE_BULK_GENERATION_NEW_OR_EXISTING_FILTER_REFERENCE_TIME ); $ai4seo_bulk_generation_date_filter_state = ai4seo_get_bulk_generation_date_filter_state( $ai4seo_bulk_generation_new_or_existing_filter, $ai4seo_bulk_generation_new_or_existing_filter_reference_timestamp ); $ai4seo_approximate_cost_per_attachment_post = ai4seo_calculate_attachment_attributes_credits_cost_per_attachment_post(); // Pagination reuses the shared filter helper so media and post-type pages sanitize query args consistently. $ai4seo_filter_query_args = ai4seo_get_content_type_filter_query_args( $ai4seo_filter_context ); // Try the bounded resolver for the full media page; related-media scopes stay exact and self-contained. $ai4seo_content_type_list_result = array( 'is_optimized' => false ); if ( ai4seo_uses_post_state_tables() || ( ! $ai4seo_attachment_post_ids_filter_is_active && ai4seo_can_edit_others_posts_for_post_types( (array) $ai4seo_post_types ) ) ) { $ai4seo_content_type_list_result = ai4seo_resolve_optimized_content_type_list( array( 'content_context' => AI4SEO_BULK_GENERATION_QUEUE_CONTEXT_ATTACHMENT_ATTRIBUTES, 'post_types' => (array) $ai4seo_post_types, 'scoped_post_ids' => $ai4seo_attachment_post_ids_filter_is_active ? $ai4seo_attachment_post_ids_filter : null, 'post_status' => array( 'publish', 'future', 'inherit' ), 'post_mime_types' => (array) $ai4seo_allowed_attachment_mime_types, 'waiting_mime_types' => $ai4seo_waiting_attachment_mime_types, 'author_not_in' => $ai4seo_disabled_attachment_post_author_ids, 'disabled_wpml_language_codes' => $ai4seo_disabled_attachment_attributes_wpml_language_codes, 'filter_status' => $ai4seo_filter_status, 'filter_text' => (string) ( $ai4seo_filter_context['filter_text'] ?? '' ), 'filter_language' => $ai4seo_filter_language, 'orderby' => $ai4seo_orderby, 'order' => $ai4seo_order, 'current_page' => $ai4seo_current_page, 'per_page' => $ai4seo_items_per_page, 'status_options' => $ai4seo_filter_context['status_options'], 'is_bulk_generation_activated' => $ai4seo_is_bulk_generation_activated, 'should_auto_queue_bulk_generation_entries' => $ai4seo_should_auto_queue_bulk_generation_entries, 'has_enough_credits' => ( $ai4seo_account_readiness['credits_ready'] && $ai4seo_current_credits_balance >= $ai4seo_approximate_cost_per_attachment_post ), 'new_or_existing_filter' => $ai4seo_bulk_generation_new_or_existing_filter, 'new_or_existing_filter_reference_timestamp' => $ai4seo_bulk_generation_new_or_existing_filter_reference_timestamp, ) ); } if ( ! empty( $ai4seo_content_type_list_result['is_optimized'] ) ) { // The optimized media path returns only current-page row state plus cheap counts for the initial render. $ai4seo_current_page = (int) ( $ai4seo_content_type_list_result['current_page'] ?? $ai4seo_current_page ); $ai4seo_current_page_attachment_post_ids = ai4seo_filter_editable_post_ids( (array) ( $ai4seo_content_type_list_result['post_ids'] ?? array() ) ); $ai4seo_total_items = (int) ( $ai4seo_content_type_list_result['total_items'] ?? 0 ); $ai4seo_total_pages = (int) ( $ai4seo_content_type_list_result['total_pages'] ?? 1 ); $ai4seo_status_filter_counts = (array) ( $ai4seo_content_type_list_result['status_counts'] ?? array() ); $ai4seo_should_derive_current_page_attachment_status_ids = (bool) ( $ai4seo_content_type_list_result['should_derive_current_page_status_ids'] ?? false ); $ai4seo_should_defer_status_filters = (bool) ( $ai4seo_content_type_list_result['should_defer_status_counts'] ?? false ); $ai4seo_current_page_status_ids = (array) ( $ai4seo_content_type_list_result['current_page_status_ids'] ?? array() ); $ai4seo_pending_attributes_attachment_post_ids = (array) ( $ai4seo_current_page_status_ids['queued'] ?? array() ); $ai4seo_processing_attributes_attachment_post_ids = (array) ( $ai4seo_current_page_status_ids['processing'] ?? array() ); $ai4seo_failed_attributes_attachment_post_ids = (array) ( $ai4seo_current_page_status_ids['failed'] ?? array() ); $ai4seo_hidden_attachment_attributes_post_ids = (array) ( $ai4seo_current_page_status_ids['hidden'] ?? array() ); $ai4seo_auto_queue_disallowed_attachment_attributes_post_ids = (array) ( $ai4seo_current_page_status_ids['auto_queue_disallowed'] ?? array() ); $ai4seo_missing_attachment_attributes_post_ids = (array) ( $ai4seo_current_page_status_ids['missing'] ?? array() ); $ai4seo_fully_covered_attachment_post_ids = (array) ( $ai4seo_current_page_status_ids['complete'] ?? array() ); $ai4seo_generated_attachment_post_ids = (array) ( $ai4seo_current_page_status_ids['generated'] ?? array() ); } else { if ( $ai4seo_attachment_post_ids_filter_is_active && ! $ai4seo_attachment_post_ids_filter ) { // Empty related-media scopes must stay empty and avoid global media status option reads. $ai4seo_candidate_attachment_post_ids = array(); } else { // The legacy path reads global status options only after scoped modal emptiness is ruled out. $ai4seo_pending_attributes_attachment_post_ids = $ai4seo_list_status_snapshot['ids']['queued']; $ai4seo_processing_attributes_attachment_post_ids = $ai4seo_list_status_snapshot['ids']['processing']; $ai4seo_missing_attachment_attributes_post_ids = $ai4seo_list_status_snapshot['ids']['missing']; $ai4seo_failed_attributes_attachment_post_ids = $ai4seo_list_status_snapshot['ids']['failed']; $ai4seo_hidden_attachment_attributes_post_ids = $ai4seo_list_status_snapshot['ids']['hidden']; $ai4seo_auto_queue_disallowed_attachment_attributes_post_ids = $ai4seo_list_status_snapshot['ids']['auto_queue_disallowed']; $ai4seo_fully_covered_attachment_post_ids = $ai4seo_list_status_snapshot['ids']['complete']; $ai4seo_generated_attachment_post_ids = $ai4seo_list_status_snapshot['ids']['generated']; } // Apply the related-ID filter before the normal status/language/sort flow so modal filters behave like the full page. if ( is_array( $ai4seo_search_ids ) ) { $ai4seo_candidate_attachment_post_ids = $ai4seo_attachment_post_ids_filter_is_active ? array_values( array_intersect( $ai4seo_search_ids, $ai4seo_attachment_post_ids_filter ) ) : $ai4seo_search_ids; } elseif ( ! $ai4seo_attachment_post_ids_filter_is_active || $ai4seo_attachment_post_ids_filter ) { $ai4seo_candidate_arguments = $ai4seo_posts_query_arguments; $ai4seo_candidate_arguments['fields'] = 'ids'; $ai4seo_candidate_arguments['posts_per_page'] = -1; $ai4seo_candidate_arguments['no_found_rows'] = true; $ai4seo_candidate_arguments['lang'] = 'all'; if ( $ai4seo_attachment_post_ids_filter_is_active ) { $ai4seo_candidate_arguments['post__in'] = $ai4seo_attachment_post_ids_filter; } $ai4seo_candidate_attachment_post_ids = ai4seo_with_wpml_all_languages( function () use ( $ai4seo_candidate_arguments ) { $posts = get_posts( $ai4seo_candidate_arguments ); if ( $GLOBALS['wpdb']->last_error ) { ai4seo_content_type_list_read_failures( true ); } return $posts; } ); } $ai4seo_candidate_attachment_post_ids = ai4seo_filter_editable_post_ids( (array) $ai4seo_candidate_attachment_post_ids ); // Intersect again after WP_Query/search handling so all candidate paths honor the modal's related-ID scope. if ( $ai4seo_attachment_post_ids_filter_is_active ) { $ai4seo_candidate_attachment_post_ids = array_values( array_intersect( $ai4seo_candidate_attachment_post_ids, $ai4seo_attachment_post_ids_filter ) ); } rsort( $ai4seo_candidate_attachment_post_ids, SORT_NUMERIC ); $ai4seo_candidate_attachment_post_ids = ai4seo_filter_post_ids_by_language( $ai4seo_candidate_attachment_post_ids, $ai4seo_filter_language ); // Apply WPML active-language scope after the selected-language filter because both rely on per-entry WPML details. $ai4seo_candidate_attachment_post_ids = ai4seo_filter_post_ids_by_disabled_wpml_languages( $ai4seo_candidate_attachment_post_ids, $ai4seo_disabled_attachment_attributes_wpml_language_codes ); $ai4seo_visible_candidate_attachment_post_ids = array_values( array_diff( $ai4seo_candidate_attachment_post_ids, $ai4seo_hidden_attachment_attributes_post_ids ) ); $ai4seo_waiting_to_get_queued_attachment_post_ids = ai4seo_get_content_type_waiting_to_get_queued_post_ids( $ai4seo_visible_candidate_attachment_post_ids, $ai4seo_missing_attachment_attributes_post_ids, $ai4seo_pending_attributes_attachment_post_ids, $ai4seo_processing_attributes_attachment_post_ids, $ai4seo_failed_attributes_attachment_post_ids, array( 'is_bulk_generation_activated' => $ai4seo_is_bulk_generation_activated, 'should_auto_queue_bulk_generation_entries' => $ai4seo_should_auto_queue_bulk_generation_entries, 'has_enough_credits' => ( $ai4seo_account_readiness['credits_ready'] && $ai4seo_current_credits_balance >= $ai4seo_approximate_cost_per_attachment_post ), 'new_or_existing_filter' => $ai4seo_bulk_generation_new_or_existing_filter, 'new_or_existing_filter_reference_timestamp' => $ai4seo_bulk_generation_new_or_existing_filter_reference_timestamp, 'hidden_ids' => $ai4seo_hidden_attachment_attributes_post_ids, 'waiting_mime_types' => $ai4seo_waiting_attachment_mime_types, 'post_types' => $ai4seo_post_types, 'post_status' => $ai4seo_posts_query_arguments['post_status'], 'auto_queue_disallowed_ids' => $ai4seo_auto_queue_disallowed_attachment_attributes_post_ids, ) ); $ai4seo_status_map = array( 'complete' => $ai4seo_fully_covered_attachment_post_ids, 'missing' => $ai4seo_missing_attachment_attributes_post_ids, 'waiting_to_queue' => $ai4seo_waiting_to_get_queued_attachment_post_ids, 'queued' => $ai4seo_pending_attributes_attachment_post_ids, 'processing' => $ai4seo_processing_attributes_attachment_post_ids, 'failed' => $ai4seo_failed_attributes_attachment_post_ids, 'hidden' => $ai4seo_hidden_attachment_attributes_post_ids, ); $ai4seo_status_filter_counts = ai4seo_get_content_type_filter_status_counts( $ai4seo_candidate_attachment_post_ids, $ai4seo_status_map, $ai4seo_filter_context['status_options'], $ai4seo_hidden_attachment_attributes_post_ids ); $ai4seo_filtered_candidate_attachment_post_ids = ai4seo_filter_post_ids_by_status( $ai4seo_candidate_attachment_post_ids, 'unverified' === $ai4seo_filter_status ? 'all' : $ai4seo_filter_status, $ai4seo_status_map, $ai4seo_hidden_attachment_attributes_post_ids ); $ai4seo_filtered_candidate_attachment_post_ids = array_values( array_unique( array_map( 'intval', $ai4seo_filtered_candidate_attachment_post_ids ) ) ); if ( 'seo_progress' === $ai4seo_orderby ) { $ai4seo_attachment_attributes_coverage_for_sorting = ai4seo_read_and_analyse_attachment_attributes_coverage( $ai4seo_filtered_candidate_attachment_post_ids, $ai4seo_attachment_coverage_read_succeeded, $ai4seo_list_coverage_issues ); $ai4seo_attachment_attributes_coverage_summary_for_sorting = ai4seo_get_attachment_attributes_coverage_summary( $ai4seo_attachment_attributes_coverage_for_sorting ); $ai4seo_num_total_attachment_attributes_for_sorting = ai4seo_get_active_num_attachment_attributes(); foreach ( $ai4seo_filtered_candidate_attachment_post_ids as $ai4seo_this_attachment_post_id_for_sorting ) { if ( ! $ai4seo_attachment_coverage_read_succeeded || ! isset( $ai4seo_attachment_attributes_coverage_summary_for_sorting[ $ai4seo_this_attachment_post_id_for_sorting ] ) ) { $ai4seo_sort_value_map[ $ai4seo_this_attachment_post_id_for_sorting ] = null; continue; } $ai4seo_sort_value_map[ $ai4seo_this_attachment_post_id_for_sorting ] = $ai4seo_num_total_attachment_attributes_for_sorting ? min( 100, round( ( ( $ai4seo_attachment_attributes_coverage_summary_for_sorting[ $ai4seo_this_attachment_post_id_for_sorting ] ?? 0 ) / $ai4seo_num_total_attachment_attributes_for_sorting ) * 100, 2 ) ) : 100; } } $ai4seo_filtered_candidate_attachment_post_ids = ai4seo_sort_content_type_ids( $ai4seo_filtered_candidate_attachment_post_ids, $ai4seo_orderby, $ai4seo_order, $ai4seo_sort_value_map ); $ai4seo_total_items = count( $ai4seo_filtered_candidate_attachment_post_ids ); $ai4seo_total_pages = $ai4seo_total_items ? (int) ceil( $ai4seo_total_items / $ai4seo_items_per_page ) : 1; if ( $ai4seo_total_pages < 1 ) { $ai4seo_total_pages = 1; } if ( $ai4seo_current_page > $ai4seo_total_pages ) { $ai4seo_current_page = $ai4seo_total_pages; } if ( $ai4seo_current_page < 1 ) { $ai4seo_current_page = 1; } $ai4seo_offset = ( $ai4seo_current_page - 1 ) * $ai4seo_items_per_page; $ai4seo_current_page_attachment_post_ids = array_slice( $ai4seo_filtered_candidate_attachment_post_ids, $ai4seo_offset, $ai4seo_items_per_page ); } ai4seo_prepare_content_type_list_view( $ai4seo_filter_context, $ai4seo_status_filter_counts, $ai4seo_current_page_attachment_post_ids, $ai4seo_list_status_snapshot, $ai4seo_list_read_failures ); if ( 'unverified' === $ai4seo_filter_status ) { $ai4seo_list_status_snapshot['actions_available'] = false; } $ai4seo_all_attachment_posts = array(); if ( $ai4seo_current_page_attachment_post_ids ) { $ai4seo_posts_query_arguments['post__in'] = $ai4seo_current_page_attachment_post_ids; $ai4seo_posts_query_arguments['orderby'] = 'post__in'; $ai4seo_posts_query_arguments['order'] = 'DESC'; $ai4seo_posts_query_arguments['posts_per_page'] = count( $ai4seo_current_page_attachment_post_ids ); $ai4seo_post_data = ai4seo_with_wpml_all_languages( function () use ( $ai4seo_posts_query_arguments ) { $query = new WP_Query( $ai4seo_posts_query_arguments ); if ( $GLOBALS['wpdb']->last_error ) { ai4seo_content_type_list_read_failures( true ); } return $query; } ); $ai4seo_all_attachment_posts = ( $ai4seo_post_data instanceof WP_Query ) ? ( $ai4seo_post_data->posts ?? array() ) : array(); unset( $ai4seo_post_data ); } // fetch all post ids from this page. ai4seo_prepare_content_type_list_view( $ai4seo_filter_context, $ai4seo_status_filter_counts, $ai4seo_current_page_attachment_post_ids, $ai4seo_list_status_snapshot, $ai4seo_list_read_failures ); $ai4seo_current_attachment_post_ids = array_map( function ( $ai4seo_attachment_post ) { return (int) $ai4seo_attachment_post->ID; }, $ai4seo_all_attachment_posts ); $ai4seo_recent_attachment_activity_entries_by_post_id = ai4seo_get_latest_activity_entries_by_post_id( array( 'attachment-attributes-bulk-generated', ) ); // read attributes coverage. if ( 'seo_progress' === $ai4seo_orderby ) { $ai4seo_current_attachment_post_ids_map = array_flip( $ai4seo_current_attachment_post_ids ); $ai4seo_attachment_attributes_coverage = array_intersect_key( $ai4seo_attachment_attributes_coverage_for_sorting, $ai4seo_current_attachment_post_ids_map ); $ai4seo_attachment_attributes_coverage_summary = array_intersect_key( $ai4seo_attachment_attributes_coverage_summary_for_sorting, $ai4seo_current_attachment_post_ids_map ); $ai4seo_num_total_attachment_attributes = $ai4seo_num_total_attachment_attributes_for_sorting; } else { $ai4seo_attachment_attributes_coverage = ai4seo_read_and_analyse_attachment_attributes_coverage( $ai4seo_current_attachment_post_ids, $ai4seo_attachment_coverage_read_succeeded, $ai4seo_list_coverage_issues ); $ai4seo_attachment_attributes_coverage_summary = ai4seo_get_attachment_attributes_coverage_summary( $ai4seo_attachment_attributes_coverage ); $ai4seo_num_total_attachment_attributes = ai4seo_get_active_num_attachment_attributes(); } // Large all-lists derive complete/missing media state from the rendered page instead of global coverage options. if ( $ai4seo_should_derive_current_page_attachment_status_ids ) { $ai4seo_generated_attachment_post_ids = ai4seo_read_post_ids_with_postmeta_key( $ai4seo_current_attachment_post_ids, AI4SEO_POST_META_GENERATED_DATA_META_KEY ); $ai4seo_generated_attachment_post_id_lookup = array_flip( array_map( 'intval', $ai4seo_generated_attachment_post_ids ) ); $ai4seo_fully_covered_attachment_post_ids = array(); $ai4seo_missing_attachment_attributes_post_ids = array(); foreach ( $ai4seo_current_attachment_post_ids as $ai4seo_this_current_attachment_post_id ) { if ( ! $ai4seo_attachment_coverage_read_succeeded || ! isset( $ai4seo_attachment_attributes_coverage_summary[ $ai4seo_this_current_attachment_post_id ] ) ) { continue; } $ai4seo_this_current_attachment_post_id = (int) $ai4seo_this_current_attachment_post_id; $ai4seo_this_current_attachment_coverage_summary = (int) ( $ai4seo_attachment_attributes_coverage_summary[ $ai4seo_this_current_attachment_post_id ] ?? 0 ); $ai4seo_this_current_attachment_coverage_percentage = $ai4seo_num_total_attachment_attributes ? min( 100, round( ( $ai4seo_this_current_attachment_coverage_summary / $ai4seo_num_total_attachment_attributes ) * 100, 2 ) ) : 100; $ai4seo_this_current_attachment_is_generated = isset( $ai4seo_generated_attachment_post_id_lookup[ $ai4seo_this_current_attachment_post_id ] ); if ( $ai4seo_this_current_attachment_coverage_percentage >= 100 && ( ! $ai4seo_do_generate_attachment_attributes_for_fully_covered_entries || $ai4seo_this_current_attachment_is_generated ) ) { $ai4seo_fully_covered_attachment_post_ids[] = $ai4seo_this_current_attachment_post_id; continue; } $ai4seo_missing_attachment_attributes_post_ids[] = $ai4seo_this_current_attachment_post_id; } } // remove entries from $ai4seo_all_failed_to_fill_attributes_attachment_post_ids that are not on this page. // Canonicalize status sources once before every strict row-level membership check below. $ai4seo_failed_attributes_attachment_post_ids = ai4seo_normalize_post_ids_from_option_value( $ai4seo_failed_attributes_attachment_post_ids ); $ai4seo_fully_covered_attachment_post_ids = ai4seo_normalize_post_ids_from_option_value( $ai4seo_fully_covered_attachment_post_ids ); $ai4seo_generated_attachment_post_ids = ai4seo_normalize_post_ids_from_option_value( $ai4seo_generated_attachment_post_ids ); $ai4seo_current_page_failed_to_fill_attachment_post_ids = array(); if ( $ai4seo_all_attachment_posts ) { foreach ( $ai4seo_all_attachment_posts as $ai4seo_this_attachment_post ) { $ai4seo_this_attachment_post_id = (int) $ai4seo_this_attachment_post->ID; if ( in_array( $ai4seo_this_attachment_post_id, $ai4seo_failed_attributes_attachment_post_ids, true ) ) { $ai4seo_current_page_failed_to_fill_attachment_post_ids[] = $ai4seo_this_attachment_post_id; } } } // Define variable for the label of the failed-attributes-generations-link. $ai4seo_retry_all_failed_attachment_attributes_generations_link_label = __( 'Retry all failed', 'ai-for-seo' ); // retry all failed attachment attributes generations link. $ai4seo_retry_all_failed_attachment_attributes_generations_link_tag = $ai4seo_can_administer_plugin ? ai4seo_get_small_icon_button_tag( 'rotate', $ai4seo_retry_all_failed_attachment_attributes_generations_link_label, '', 'ai4seo_retry_all_failed_attachment_attributes(this); return false;' ) : ''; // Give AJAX hydration a stable target for the full media table retry action. $ai4seo_retry_all_failed_attachment_attributes_container_id = 'ai4seo-retry-all-failed-attachment-attributes'; // The related-media modal is scoped, while retry-all-failed is still a global media action. $ai4seo_should_show_retry_all_failed_attachment_attributes_generations_link = $ai4seo_can_administer_plugin && ! $ai4seo_is_related_attachments_modal && ai4seo_should_show_content_type_retry_all_failed_button( $ai4seo_filter_context['status_options'], $ai4seo_status_filter_counts ); $ai4seo_retry_all_failed_attachment_attributes_container_class = $ai4seo_should_show_retry_all_failed_attachment_attributes_generations_link ? '' : ' ai4seo-display-none'; $ai4seo_consider_purchasing_more_credits_link_tag = $ai4seo_can_administer_plugin ? ai4seo_get_small_icon_button_tag( 'circle-plus', __( 'Get more Credits', 'ai-for-seo' ), 'ai4seo-primary-button', 'ai4seo_close_all_modals();ai4seo_open_get_more_credits_modal();' ) : ''; $ai4seo_active_attachment_attributes = ai4seo_get_active_attachment_attributes(); $ai4seo_active_attachment_attribute_names = array(); foreach ( $ai4seo_attachment_field_descriptors as $ai4seo_field_identifier => $ai4seo_field_details ) { if ( in_array( $ai4seo_field_identifier, $ai4seo_active_attachment_attributes, true ) && isset( $ai4seo_field_details['name'] ) ) { $ai4seo_active_attachment_attribute_names[ $ai4seo_field_identifier ] = $ai4seo_field_details['name']; } } // Use a modal-specific checkbox name so bulk actions only read selections inside the related-media modal. $ai4seo_bulk_generation_queue_checkbox_name = $ai4seo_is_related_attachments_modal ? 'ai4seo_bulk_generation_queue_related_attachment_post_ids' : 'ai4seo_bulk_generation_queue_attachment_post_ids'; $ai4seo_bulk_generation_queue_action_controls = ai4seo_get_bulk_generation_queue_action_controls( AI4SEO_BULK_GENERATION_QUEUE_CONTEXT_ATTACHMENT_ATTRIBUTES, $ai4seo_bulk_generation_queue_checkbox_name, $ai4seo_filter_status, array( 'list_location' => $ai4seo_is_related_attachments_modal ? 'related_attachments_modal' : 'main', ) ); $ai4seo_content_type_filter_controls_html = ai4seo_get_content_type_filter_controls_html( $ai4seo_filter_context, $ai4seo_status_filter_counts, $ai4seo_total_items, $ai4seo_all_attachment_posts ? $ai4seo_bulk_generation_queue_action_controls : '', array( 'defer_status_filters' => $ai4seo_should_defer_status_filters, 'list_location' => $ai4seo_is_related_attachments_modal ? 'related-media' : 'main', 'content_context' => AI4SEO_BULK_GENERATION_QUEUE_CONTEXT_ATTACHMENT_ATTRIBUTES, 'post_type' => $ai4seo_main_attachment_post_type, 'retry_all_failed_button_target' => ( $ai4seo_can_administer_plugin && ! $ai4seo_is_related_attachments_modal ) ? $ai4seo_retry_all_failed_attachment_attributes_container_id : '', ) ); // ___________________________________________________________________________________________ \\ // === OUTPUT ================================================================================ \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ // Hide media-page-only import actions when this table is embedded in the related-media modal. if ( ! $ai4seo_is_related_attachments_modal && isset( $ai4seo_import_nextgen_gallery_button ) && $ai4seo_import_nextgen_gallery_button ) { echo "
"; ai4seo_echo_wp_kses( $ai4seo_import_nextgen_gallery_button ); echo '
'; } ai4seo_echo_wp_kses( $ai4seo_content_type_filter_controls_html ); ai4seo_echo_wp_kses( ai4seo_get_account_quality_notice() ); if ( ! $ai4seo_filter_context['view_available'] ) { echo '

' . esc_html( ai4seo_get_content_type_list_unavailable_message( $ai4seo_filter_context ) ) . '

'; return; } ai4seo_echo_wp_kses( ai4seo_get_content_type_selection_notice( $ai4seo_current_attachment_post_ids, array_keys( $ai4seo_attachment_attributes_coverage_summary ), $ai4seo_list_status_snapshot ) ); // Explain why automatic queuing is paused without leaking the notice into the related-media modal. if ( ! $ai4seo_is_related_attachments_modal && empty( $ai4seo_bulk_generation_date_filter_state['is_valid'] ) ) { echo "

" . esc_html__( 'SEO Autopilot paused:', 'ai-for-seo' ) . ' '; echo esc_html( ai4seo_get_invalid_bulk_generation_date_filter_message() ); echo '

'; } // Stop script if no posts have been found -> show message and stop page rendering. if ( ! $ai4seo_all_attachment_posts ) { $ai4seo_remove_filters_button_html = ai4seo_get_content_type_remove_filters_button_html( $ai4seo_filter_context ); echo '

'; echo $ai4seo_is_related_attachments_modal ? esc_html__( 'No related media found.', 'ai-for-seo' ) : esc_html__( 'No relevant media found.', 'ai-for-seo' ); if ( $ai4seo_remove_filters_button_html ) { echo ' '; ai4seo_echo_wp_kses( $ai4seo_remove_filters_button_html ); } echo '

'; return; } // Keep the full-page WPML explanation out of the modal because related media already inherits normal filters. if ( ! $ai4seo_is_related_attachments_modal && ai4seo_is_plugin_or_theme_active( AI4SEO_THIRD_PARTY_PLUGIN_WPML ) ) { echo '

'; echo '' . esc_html__( 'Heads up:', 'ai-for-seo' ) . ' '; echo esc_html__( 'Your images appear on different language versions of your website. Therefore, each image needs to be analyzed for each language separately to ensure optimal SEO performance across all languages.', 'ai-for-seo' ); echo '

'; } // Display table with entries. // Resolve active ARIA states once so table headers and their direction-toggle links stay synchronized. $ai4seo_id_column_aria_sort = ai4seo_get_content_type_sortable_column_aria_sort_value( 'id', $ai4seo_filter_context ); $ai4seo_title_column_aria_sort = ai4seo_get_content_type_sortable_column_aria_sort_value( 'title', $ai4seo_filter_context ); $ai4seo_seo_progress_column_aria_sort = ai4seo_get_content_type_sortable_column_aria_sort_value( 'seo_progress', $ai4seo_filter_context ); echo "
"; echo ""; echo ''; echo "'; echo "'; // Label the otherwise visual-only preview column for assistive technology. echo ''; echo "'; echo "'; // Label the otherwise visual-only per-row action column for assistive technology. echo ''; echo ''; // Resolve shared row fields once for status, preview, and action output. $ai4seo_attachment_timestamp_timezone = ai4seo_get_timezone()->getName(); foreach ( $ai4seo_all_attachment_posts as $ai4seo_this_attachment ) { // Keep edit destinations separate from filtered media sources used by previews and integration fallbacks. $ai4seo_this_post_attachment_id = (int) $ai4seo_this_attachment->ID; $ai4seo_this_attachment_title = $ai4seo_this_attachment->post_title ?? ''; $ai4seo_this_attachment_post_type = $ai4seo_this_attachment->post_type ?? ''; $ai4seo_this_mime_type = $ai4seo_this_attachment->post_mime_type ?? ''; $ai4seo_this_attachment_source_url = wp_get_attachment_url( $ai4seo_this_post_attachment_id ); // Imported NextGen entries are plugin-owned pseudo attachments, so WordPress has no native attachment URL for them. if ( ! $ai4seo_this_attachment_source_url && AI4SEO_NEXTGEN_GALLERY_POST_TYPE === $ai4seo_this_attachment_post_type ) { $ai4seo_this_attachment_source_url = $ai4seo_this_attachment->guid ?? ''; } $ai4seo_this_post_link = get_edit_post_link( $ai4seo_this_attachment ); $ai4seo_this_post_link_description = sprintf( /* translators: %d: Media ID. */ __( 'Edit media ID %d in WordPress (opens in a new tab)', 'ai-for-seo' ), $ai4seo_this_post_attachment_id ); if ( ! $ai4seo_this_post_link ) { $ai4seo_this_post_link = $ai4seo_this_attachment_source_url; $ai4seo_this_post_link_description = sprintf( /* translators: %d: Media ID. */ __( 'View media file ID %d (opens in a new tab)', 'ai-for-seo' ), $ai4seo_this_post_attachment_id ); } $ai4seo_this_attachment_language = ai4seo_try_get_post_language_by_checking_multilanguage_plugins( $ai4seo_this_post_attachment_id ); $ai4seo_this_attachment_title_with_language = $ai4seo_this_attachment_title; if ( '' !== $ai4seo_this_attachment_language ) { $ai4seo_this_attachment_title_with_language .= ' (' . $ai4seo_this_attachment_language . ')'; } $ai4seo_this_attachment_post_date_gmt = (string) ( $ai4seo_this_attachment->post_date_gmt ?? '' ); $ai4seo_this_attachment_post_date = (string) ( $ai4seo_this_attachment->post_date ?? '' ); $ai4seo_this_attachment_post_modified_gmt = (string) ( $ai4seo_this_attachment->post_modified_gmt ?? '' ); $ai4seo_this_attachment_post_modified = (string) ( $ai4seo_this_attachment->post_modified ?? '' ); // Preserve the absolute timestamp consumed by row filtering independently from presentation formatting. $ai4seo_this_attachment_date_timestamp = false; if ( ai4seo_is_valid_mysql_datetime( $ai4seo_this_attachment_post_date_gmt ) ) { $ai4seo_this_attachment_date_timestamp = strtotime( $ai4seo_this_attachment_post_date_gmt . ' UTC' ); } // Prefer the shared GMT path while allowing display-only recovery from a validated legacy local date. $ai4seo_this_attachment_post_date_display = ai4seo_get_attachment_upload_time_display( $ai4seo_this_attachment_post_date_gmt, $ai4seo_attachment_timestamp_timezone, $ai4seo_this_attachment_post_date ); // Preserve the table's established marker when WordPress has no valid upload date. if ( '' === $ai4seo_this_attachment_post_date_display ) { $ai4seo_this_attachment_post_date_display = '-'; } // Keep row-level exclusion aligned with queue SQL and the waiting-to-queue status resolver. $ai4seo_is_excluded_by_new_or_existing_filter = ! empty( $ai4seo_bulk_generation_date_filter_state['is_valid'] ) && ! ai4seo_does_bulk_generation_date_filter_include_timestamp( $ai4seo_bulk_generation_date_filter_state, (int) $ai4seo_this_attachment_date_timestamp ); // this attachment attributes coverage. $ai4seo_this_attachment_attribute_coverage_summary = $ai4seo_attachment_attributes_coverage_summary[ $ai4seo_this_post_attachment_id ] ?? 0; if ( $ai4seo_active_attachment_attributes ) { $ai4seo_this_attachment_attribute_coverage_percentage = $ai4seo_num_total_attachment_attributes ? round( ( $ai4seo_this_attachment_attribute_coverage_summary / $ai4seo_num_total_attachment_attributes ) * 100, 2 ) : 100; $ai4seo_this_attachment_attribute_coverage_percentage = min( 100, $ai4seo_this_attachment_attribute_coverage_percentage ); if ( ! $ai4seo_attachment_coverage_read_succeeded || ! isset( $ai4seo_attachment_attributes_coverage_summary[ $ai4seo_this_post_attachment_id ] ) ) { $ai4seo_this_attachment_attribute_coverage_percentage = null; } $ai4seo_this_attachment_attributes_is_not_covered = ( $ai4seo_this_attachment_attribute_coverage_percentage < 100 ); } else { $ai4seo_this_attachment_attribute_coverage_percentage = 100; $ai4seo_this_attachment_attributes_is_not_covered = false; } $ai4seo_this_attachment_post_is_fully_covered = null !== $ai4seo_this_attachment_attribute_coverage_percentage && $ai4seo_this_attachment_attribute_coverage_percentage >= 100 && in_array( $ai4seo_this_post_attachment_id, $ai4seo_fully_covered_attachment_post_ids, true ); $ai4seo_this_attachment_attributes_is_not_finished = ( $ai4seo_this_attachment_attribute_coverage_percentage < 100 ); $ai4seo_this_attachment_post_is_generated = $ai4seo_list_status_snapshot['quality']['generated'] ? in_array( $ai4seo_this_post_attachment_id, $ai4seo_generated_attachment_post_ids, true ) : null; $ai4seo_this_attachment_sub_info_rows = array(); $ai4seo_this_attachment_has_recent_activity = isset( $ai4seo_recent_attachment_activity_entries_by_post_id[ $ai4seo_this_post_attachment_id ] ); $ai4seo_this_recent_activity_details_subtext_tag = ''; // Recent activity entries should link directly to the editor where details can be inspected. if ( $ai4seo_this_attachment_has_recent_activity ) { $ai4seo_this_recent_activity_details_onclick = 'ai4seo_open_attachment_attributes_editor_modal(' . absint( $ai4seo_this_post_attachment_id ); if ( $ai4seo_current_attachment_post_ids ) { $ai4seo_this_recent_activity_details_onclick .= ', ' . wp_json_encode( array_values( array_map( 'intval', $ai4seo_current_attachment_post_ids ) ) ); } $ai4seo_this_recent_activity_details_onclick .= ');'; $ai4seo_this_recent_activity_details_subtext_tag = ai4seo_get_recent_activity_details_subtext_tag( $ai4seo_this_recent_activity_details_onclick, $ai4seo_recent_attachment_activity_entries_by_post_id[ $ai4seo_this_post_attachment_id ] ); } // Retain raw UTC/local creation and modification values for diagnostics, with markers for missing data. $ai4seo_this_attachment_upload_timestamp_tooltip = sprintf( /* translators: 1: post_date_gmt, 2: post_date, 3: post_modified_gmt, 4: post_modified */ __( "post_date_gmt: %1\$s\npost_date: %2\$s\npost_modified_gmt: %3\$s\npost_modified: %4\$s", 'ai-for-seo' ), $ai4seo_this_attachment_post_date_gmt ? $ai4seo_this_attachment_post_date_gmt : '-', $ai4seo_this_attachment_post_date ? $ai4seo_this_attachment_post_date : '-', $ai4seo_this_attachment_post_modified_gmt ? $ai4seo_this_attachment_post_modified_gmt : '-', $ai4seo_this_attachment_post_modified ? $ai4seo_this_attachment_post_modified : '-' ); $ai4seo_this_attachment_sub_info_rows[] = "" . sprintf( /* translators: %s: upload timestamp */ esc_html__( 'Upload time: %s', 'ai-for-seo' ), esc_html( $ai4seo_this_attachment_post_date_display ) ) . ''; $ai4seo_this_attachment_sub_info_rows[] = sprintf( /* translators: %s: MIME type */ esc_html__( 'MIME type: %s', 'ai-for-seo' ), esc_html( $ai4seo_this_mime_type ? $ai4seo_this_mime_type : '-' ) ); $ai4seo_this_attachment_sub_info_rows[] = null === $ai4seo_this_attachment_post_is_generated ? esc_html__( 'Generation status unavailable.', 'ai-for-seo' ) : ( $ai4seo_this_attachment_post_is_generated ? sprintf( /* translators: %s: plugin name */ esc_html__( '%s has generated data for this media file.', 'ai-for-seo' ), esc_html( AI4SEO_PLUGIN_NAME ) ) : sprintf( /* translators: %s: plugin name */ __( "%s has not generated data for this media file yet.", 'ai-for-seo' ), esc_html( AI4SEO_PLUGIN_NAME ) ) ); $ai4seo_this_attachment_sub_info_html = "
" . implode( ' • ', $ai4seo_this_attachment_sub_info_rows ) . '
'; $ai4seo_is_attachment_post_failed = in_array( $ai4seo_this_post_attachment_id, $ai4seo_current_page_failed_to_fill_attachment_post_ids, true ); // Check queue state separately, so Pending and Processing can have different visuals. $ai4seo_is_attachment_post_missing = in_array( $ai4seo_this_post_attachment_id, $ai4seo_missing_attachment_attributes_post_ids, true ); $ai4seo_is_attachment_post_pending = in_array( $ai4seo_this_post_attachment_id, $ai4seo_pending_attributes_attachment_post_ids, true ); $ai4seo_is_attachment_post_processing = in_array( $ai4seo_this_post_attachment_id, $ai4seo_processing_attributes_attachment_post_ids, true ); $ai4seo_is_attachment_post_auto_queue_disallowed = in_array( $ai4seo_this_post_attachment_id, $ai4seo_auto_queue_disallowed_attachment_attributes_post_ids, true ); $ai4seo_is_attachment_post_waiting_to_get_queued = false; $ai4seo_is_insufficient_credits = false; $ai4seo_should_show_auto_queue_disallowed_note = false; // Failed entries should keep their failed state even if stale queue data exists. if ( $ai4seo_is_attachment_post_failed ) { $ai4seo_is_attachment_post_pending = false; $ai4seo_is_attachment_post_processing = false; } // Missing entries are waiting for SEO Autopilot excavation only when auto queueing can actually pick them up. $ai4seo_row_readiness = ai4seo_get_content_type_row_readiness( $ai4seo_this_post_attachment_id, null !== $ai4seo_this_attachment_attribute_coverage_percentage, $ai4seo_list_status_snapshot ); $ai4seo_can_attachment_post_get_auto_queued = $ai4seo_is_bulk_generation_activated && $ai4seo_row_readiness['selectable'] && null !== $ai4seo_this_attachment_post_is_generated && $ai4seo_should_auto_queue_bulk_generation_entries && $ai4seo_is_attachment_post_missing && ! $ai4seo_is_attachment_post_pending && ! $ai4seo_is_attachment_post_processing && ! $ai4seo_is_attachment_post_failed && ! $ai4seo_is_attachment_post_auto_queue_disallowed && ! $ai4seo_is_excluded_by_new_or_existing_filter && ( $ai4seo_this_attachment_attributes_is_not_finished || ( ! $ai4seo_this_attachment_post_is_generated && $ai4seo_do_generate_attachment_attributes_for_fully_covered_entries ) ); if ( $ai4seo_can_attachment_post_get_auto_queued ) { $ai4seo_can_attachment_post_get_auto_queued = in_array( (string) $ai4seo_this_mime_type, $ai4seo_waiting_attachment_mime_types, true ); } if ( $ai4seo_can_attachment_post_get_auto_queued ) { if ( ! $ai4seo_account_readiness['credits_ready'] ) { $ai4seo_is_attachment_post_waiting_to_get_queued = false; } elseif ( $ai4seo_current_credits_balance < $ai4seo_approximate_cost_per_attachment_post ) { $ai4seo_is_insufficient_credits = true; } else { $ai4seo_is_attachment_post_waiting_to_get_queued = true; } } // Prefer the generated thumbnail, then the filtered source URL, and use the generic icon only without real media. $ai4seo_preview_image_url = ''; $ai4seo_preview_image_alt_text = __( 'No image preview available', 'ai-for-seo' ); if ( in_array( (string) $ai4seo_this_mime_type, ai4seo_get_allowed_attachment_mime_types(), true ) ) { // SVG previews use the original image URL without requiring raster thumbnail metadata. $ai4seo_preview_image_url = 'image/svg+xml' === $ai4seo_this_mime_type ? $ai4seo_this_attachment_source_url : wp_get_attachment_image_url( $ai4seo_this_post_attachment_id, array( 48, 48 ) ); if ( ! $ai4seo_preview_image_url ) { $ai4seo_preview_image_url = $ai4seo_this_attachment_source_url; } if ( $ai4seo_preview_image_url ) { $ai4seo_this_attachment_alt_text = get_post_meta( $ai4seo_this_post_attachment_id, '_wp_attachment_image_alt', true ); $ai4seo_this_attachment_alt_text = is_scalar( $ai4seo_this_attachment_alt_text ) ? trim( (string) $ai4seo_this_attachment_alt_text ) : ''; $ai4seo_preview_image_alt_text = $ai4seo_this_attachment_alt_text ? $ai4seo_this_attachment_alt_text : $ai4seo_this_attachment_title_with_language; } } if ( ! $ai4seo_preview_image_url ) { $ai4seo_preview_image_url = ai4seo_get_assets_images_url( 'image/svg+xml' === $ai4seo_this_mime_type ? 'icons/document-svg.svg' : 'icons/document-question-48x48.png' ); } echo ''; // Use the shared renderer so the main Media table and Related Media modal retain separate labeled controls. echo "'; // Post-ID. echo ''; // Image or File Preview. echo "'; // Title. $ai4seo_this_post_title_link_label = sprintf( /* translators: 1: Visible media title, 2: Link destination and new-tab description. */ __( '%1$s — %2$s', 'ai-for-seo' ), $ai4seo_this_attachment_title_with_language, $ai4seo_this_post_link_description ); echo "'; // Generation Coverage. echo "'; // Actions. echo ''; echo ''; } echo '
"; ai4seo_echo_wp_kses( ai4seo_get_select_all_checkbox( $ai4seo_bulk_generation_queue_checkbox_name, '' ) ); echo ''; ai4seo_echo_wp_kses( ai4seo_get_content_type_sortable_column_label_html( __( 'ID', 'ai-for-seo' ), 'id', $ai4seo_filter_context ) ); echo ''; echo "" . esc_html__( 'Preview', 'ai-for-seo' ) . ''; echo ''; ai4seo_echo_wp_kses( ai4seo_get_content_type_sortable_column_label_html( __( 'Title', 'ai-for-seo' ), 'title', $ai4seo_filter_context ) ); echo ''; ai4seo_echo_wp_kses( ai4seo_get_content_type_sortable_column_label_html( __( 'SEO Coverage', 'ai-for-seo' ), 'seo_progress', $ai4seo_filter_context ) ); echo " / "; echo esc_html__( 'Title', 'ai-for-seo' ); echo ''; // Keep the global retry action aligned with the Failed status filter, including deferred AJAX hydration. if ( $ai4seo_can_administer_plugin && ! $ai4seo_is_related_attachments_modal && ( $ai4seo_should_show_retry_all_failed_attachment_attributes_generations_link || $ai4seo_should_defer_status_filters ) ) { echo ''; ai4seo_echo_wp_kses( $ai4seo_retry_all_failed_attachment_attributes_generations_link_tag ); echo ''; } echo ''; echo "" . esc_html__( 'Actions', 'ai-for-seo' ) . ''; echo '
"; ai4seo_echo_wp_kses( ai4seo_get_bulk_generation_queue_entry_checkbox( $ai4seo_bulk_generation_queue_checkbox_name, $ai4seo_this_post_attachment_id, $ai4seo_this_attachment_title_with_language, $ai4seo_row_readiness['reason'] ) ); echo ''; echo esc_html( $ai4seo_this_post_attachment_id ); echo '"; if ( $ai4seo_this_post_link ) { echo ""; } ai4seo_echo_wp_kses( ai4seo_get_attachment_preview_image_tag( $ai4seo_preview_image_url, $ai4seo_preview_image_alt_text, (string) $ai4seo_this_mime_type ) ); if ( $ai4seo_this_post_link ) { echo ''; } echo '"; echo ''; if ( $ai4seo_this_post_link ) { echo ""; } echo esc_html( $ai4seo_this_attachment_title_with_language ); if ( $ai4seo_this_post_link ) { echo ''; } echo ''; ai4seo_echo_wp_kses( $ai4seo_this_attachment_sub_info_html ); echo '"; if ( ! empty( $ai4seo_row_readiness['reason'] ) ) { echo '

' . esc_html( $ai4seo_row_readiness['reason'] ) . '

'; } if ( $ai4seo_active_attachment_attributes ) { $ai4seo_should_show_auto_queue_disallowed_note = $ai4seo_is_bulk_generation_activated && $ai4seo_should_auto_queue_bulk_generation_entries && $ai4seo_is_attachment_post_auto_queue_disallowed && $ai4seo_this_attachment_attributes_is_not_finished && ! $ai4seo_is_attachment_post_pending && ! $ai4seo_is_attachment_post_processing; $ai4seo_should_show_autopilot_pending_note = $ai4seo_is_attachment_post_pending && ! $ai4seo_is_attachment_post_processing; // Reuse the list-wide progress helper so attachment and post rows expose identical accessibility metadata. $ai4seo_this_attachment_field_states = array(); if ( $ai4seo_attachment_coverage_read_succeeded ) { foreach ( $ai4seo_attachment_attributes_coverage[ $ai4seo_this_post_attachment_id ] ?? array() as $ai4seo_attribute_identifier => $ai4seo_attribute_value ) { $ai4seo_this_attachment_field_states[ $ai4seo_attribute_identifier ] = $ai4seo_attribute_value ? 'filled' : 'missing'; } } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static progress markup escapes every dynamic attribute with esc_attr(). echo ai4seo_get_seo_coverage_progress_bar_tag( $ai4seo_this_post_attachment_id, $ai4seo_this_attachment_attribute_coverage_percentage, '', $ai4seo_this_attachment_attributes_is_not_finished, $ai4seo_media_label_singular, $ai4seo_active_attachment_attribute_names, $ai4seo_this_attachment_field_states ); if ( $ai4seo_is_attachment_post_waiting_to_get_queued ) { echo "
"; echo esc_html__( 'Waiting to get queued by SEO Autopilot...', 'ai-for-seo' ); echo '
'; } elseif ( $ai4seo_should_show_autopilot_pending_note ) { if ( $ai4seo_is_bulk_generation_activated ) { echo "
"; echo esc_html__( 'Queued for SEO Autopilot. Processing starts with a future SEO Autopilot run.', 'ai-for-seo' ); echo '
'; } else { echo "
"; echo esc_html__( 'Queued, but SEO Autopilot is deactivated for this content type.', 'ai-for-seo' ); echo '
'; } } elseif ( $ai4seo_is_attachment_post_processing ) { echo "
"; echo esc_html__( 'This entry is currently being processed.', 'ai-for-seo' ); echo '
'; } elseif ( $ai4seo_is_insufficient_credits ) { echo "
"; echo esc_html__( 'Insufficient Credits', 'ai-for-seo' ) . '. '; if ( $ai4seo_can_administer_plugin ) { ai4seo_echo_wp_kses( $ai4seo_consider_purchasing_more_credits_link_tag ); } echo '
'; } elseif ( $ai4seo_is_attachment_post_failed && $ai4seo_this_attachment_attributes_is_not_finished ) { echo "
"; echo '' . esc_html__( 'Failed to automatically fill media attributes.', 'ai-for-seo' ) . ' '; ai4seo_echo_wp_kses( ai4seo_get_small_icon_button_tag( 'arrow-up-right-from-square', __( 'Try it manually', 'ai-for-seo' ), '', 'ai4seo_open_attachment_attributes_editor_modal("' . esc_js( $ai4seo_this_post_attachment_id ) . '");' ) ); echo '
'; } elseif ( $ai4seo_is_excluded_by_new_or_existing_filter && $ai4seo_this_attachment_attributes_is_not_finished ) { $ai4seo_new_or_existing_filter_reference_timestamp_formatted = ai4seo_format_unix_timestamp( (int) $ai4seo_bulk_generation_date_filter_state['reference_timestamp'] ); echo "
"; if ( 'new' === $ai4seo_bulk_generation_new_or_existing_filter ) { printf( /* translators: %s: reference timestamp */ esc_html__( "Excluded by 'New or existing entries' filter: Uploaded before %s.", 'ai-for-seo' ), esc_html( $ai4seo_new_or_existing_filter_reference_timestamp_formatted ) ); } elseif ( 'existing' === $ai4seo_bulk_generation_new_or_existing_filter ) { printf( /* translators: %s: reference timestamp */ esc_html__( "Excluded by 'New or existing entries' filter: Uploaded after %s.", 'ai-for-seo' ), esc_html( $ai4seo_new_or_existing_filter_reference_timestamp_formatted ) ); } else { echo esc_html__( "Excluded by 'New or existing entries' filter.", 'ai-for-seo' ); } echo '
'; } elseif ( $ai4seo_this_attachment_post_is_fully_covered && false === $ai4seo_this_attachment_post_is_generated && ! $ai4seo_do_generate_attachment_attributes_for_fully_covered_entries ) { echo "
"; echo esc_html__( 'All active media attributes are filled. SEO Autopilot is leaving existing values unchanged under your overwrite settings.', 'ai-for-seo' ); echo '
'; } if ( $ai4seo_should_show_auto_queue_disallowed_note ) { echo "
"; echo esc_html__( 'Auto Queue will ignore this entry because it was excluded with a bulk action.', 'ai-for-seo' ); echo '
'; } } else { echo "
"; echo esc_html__( 'No media attributes are active.', 'ai-for-seo' ); echo '
'; } if ( $ai4seo_this_recent_activity_details_subtext_tag ) { ai4seo_echo_wp_kses( $ai4seo_this_recent_activity_details_subtext_tag ); } // Title display for mobile version. echo "
"; echo ''; echo ""; echo esc_html( $ai4seo_this_attachment_title_with_language ); echo ''; echo ''; ai4seo_echo_wp_kses( $ai4seo_this_attachment_sub_info_html ); echo '
'; echo '
'; if ( $ai4seo_active_attachment_attributes ) { ai4seo_echo_wp_kses( ai4seo_get_edit_attachment_attributes_button( $ai4seo_this_post_attachment_id, $ai4seo_current_attachment_post_ids, $ai4seo_media_label_singular, null !== $ai4seo_this_attachment_attribute_coverage_percentage && $ai4seo_attachment_coverage_read_succeeded && $ai4seo_this_attachment_attribute_coverage_percentage < 100 ) ); } echo '
'; echo '
'; // Pagination. $ai4seo_pagination_base_argument = add_query_arg( array( 'page' => AI4SEO_PLUGIN_IDENTIFIER, 'ai4seo_subpage' => $ai4seo_nice_post_type, // e.g. 'media'. 'ai4seo_page' => '%#%', // placeholder for paginate_links(). ), admin_url( 'admin.php' ) ); // Normalize page bounds and preserve WordPress's placeholder when a concrete pagination base cannot be built. $ai4seo_total_pages = max( 1, $ai4seo_total_pages ); $ai4seo_current_page = max( 1, $ai4seo_current_page ); $ai4seo_pagination_base_argument = $ai4seo_pagination_base_argument ? $ai4seo_pagination_base_argument : '%_%'; // Preserve the modal source post while reusing the media page pagination helper. if ( $ai4seo_is_related_attachments_modal && $ai4seo_related_attachments_modal_post_id > 0 ) { $ai4seo_filter_query_args['post_id'] = $ai4seo_related_attachments_modal_post_id; } $ai4seo_pagination_arguments = array( 'base' => $ai4seo_pagination_base_argument, 'total' => $ai4seo_total_pages, 'current' => $ai4seo_current_page, 'show_all' => false, 'end_size' => 2, 'mid_size' => 0, 'prev_text' => '← ' . __( 'Previous', 'ai-for-seo' ), 'next_text' => __( 'Next', 'ai-for-seo' ) . ' →', 'add_args' => ( $ai4seo_filter_query_args ? $ai4seo_filter_query_args : false ), ); $ai4seo_pagination_links = paginate_links( $ai4seo_pagination_arguments ); if ( ! empty( $ai4seo_pagination_links ) ) { $ai4seo_pagination_links = ai4seo_normalize_pagination_links( $ai4seo_pagination_links ); echo "
"; ai4seo_echo_wp_kses( $ai4seo_pagination_links ); echo '
'; }