'history_warning' ) ); return; } } $ai4seo_manual_input = ai4seo_prepare_manual_attachment_generation_input( $ai4seo_this_attachment_post_id ); if ( is_wp_error( $ai4seo_manual_input ) ) { ai4seo_send_ajax_error( esc_html( $ai4seo_manual_input->get_error_message() ), 241823825 ); return; } $ai4seo_attachment_image_source = $ai4seo_manual_input['source']; // ___________________________________________________________________________________________ \\ // === EXECUTE CALL ========================================================================== \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ $ai4seo_attachment_attributes_generation_language = ai4seo_get_attachments_language( $ai4seo_this_attachment_post_id ); $ai4seo_wpml_language = sanitize_text_field( ai4seo_try_get_post_language_by_checking_multilanguage_plugins( $ai4seo_this_attachment_post_id ) ); $ai4seo_robhub_api_call_parameters = array( 'language' => $ai4seo_attachment_attributes_generation_language, 'system_language' => sanitize_text_field( ai4seo_get_wordpress_language() ), ); if ( $ai4seo_wpml_language ) { $ai4seo_robhub_api_call_parameters['wpml_language'] = $ai4seo_wpml_language; } $ai4seo_robhub_api_call_parameters['trigger'] = 'manual'; $ai4seo_robhub_api_call_parameters['website_context'] = ai4seo_get_website_context(); $ai4seo_source_post_id = absint( wp_unslash( $_REQUEST['source_post_id'] ?? 0 ) ); if ( $ai4seo_source_post_id && ! ai4seo_can_edit_post( $ai4seo_source_post_id ) ) { ai4seo_send_ajax_error( esc_html__( 'You are not allowed to use this context post.', 'ai-for-seo' ), 30102601 ); return; } $ai4seo_context_state = array(); $ai4seo_context_preparation = ai4seo_prepare_attachment_context_policy( $ai4seo_this_attachment_post_id, $ai4seo_source_post_id ? array( $ai4seo_source_post_id ) : array(), $ai4seo_context_state ); if ( ! $ai4seo_context_preparation['succeeded'] ) { ai4seo_send_ajax_error( esc_html__( 'The image context choice could not be saved. No generation was started.', 'ai-for-seo' ), 30102602 ); return; } $ai4seo_attachment_context_result = ai4seo_resolve_attachment_usage_context( $ai4seo_this_attachment_post_id, true, $ai4seo_context_state ); $ai4seo_attachment_usage_context = $ai4seo_context_preparation['contextless'] ? '' : $ai4seo_attachment_context_result['context']; if ( ! empty( $ai4seo_attachment_usage_context ) ) { $ai4seo_robhub_api_call_parameters['attachment_usage_context'] = $ai4seo_attachment_usage_context; } // collect and build field instructions. $ai4seo_field_instructions = array(); foreach ( $ai4seo_active_attachment_attributes as $ai4seo_this_active_attachment_attribute ) { $ai4seo_this_to_generate = in_array( $ai4seo_this_active_attachment_attribute, $ai4seo_generation_fields, true ); $ai4seo_this_old_value = $ai4seo_old_input_values[ $ai4seo_this_active_attachment_attribute ] ?? ''; $ai4seo_this_prefix = $ai4seo_attachment_attributes_prefixes[ $ai4seo_this_active_attachment_attribute ] ?? ''; $ai4seo_this_suffix = $ai4seo_attachment_attributes_suffixes[ $ai4seo_this_active_attachment_attribute ] ?? ''; if ( ! $ai4seo_this_to_generate && ! $ai4seo_this_old_value ) { continue; } $ai4seo_this_prefix = ai4seo_replace_text_placeholders( $ai4seo_this_prefix, $ai4seo_attachment_placeholder_replacements ); $ai4seo_this_suffix = ai4seo_replace_text_placeholders( $ai4seo_this_suffix, $ai4seo_attachment_placeholder_replacements ); $ai4seo_field_instructions[ $ai4seo_this_active_attachment_attribute ] = array( 'generate' => $ai4seo_this_to_generate, 'old_value' => $ai4seo_this_old_value, 'prefix' => $ai4seo_this_prefix, 'suffix' => $ai4seo_this_suffix, ); } $ai4seo_robhub_api_call_parameters['approximate_cost'] = ai4seo_calculate_attachment_attributes_credits_cost_per_attachment_post( $ai4seo_generation_fields ); $ai4seo_robhub_api_call_parameters['field_instructions'] = $ai4seo_field_instructions; // Collect the settings-level instructions with the optional unsaved editor value for this request. $ai4seo_custom_instructions = ai4seo_get_generation_custom_instructions( 'attachment_attributes', $ai4seo_this_attachment_post_id, $ai4seo_entry_custom_instructions ); if ( $ai4seo_custom_instructions ) { // Pass only non-empty custom instruction scopes to keep old API payloads unchanged when no instructions exist. $ai4seo_robhub_api_call_parameters['custom_instructions'] = $ai4seo_custom_instructions; } // === CALL ROBHUB API ONCE WITH THE PRESELECTED IMAGE INPUT METHOD ========================================== \\ // RobHub owns image recovery and model repairs, so issue one request through the shared image transport. $ai4seo_results = ai4seo_call_attachment_attributes_generation_api( $ai4seo_attachment_image_source, $ai4seo_robhub_api_call_parameters, $ai4seo_manual_input['image'] ); if ( ! ai4seo_robhub_api()->was_call_successful( $ai4seo_results ?? false ) ) { $ai4seo_context_retained_message = in_array( $ai4seo_context_preparation['reason'], array( 'fixed', 'ambiguous' ), true ) ? ' ' . __( 'The saved image context choice remains in place. Generation failed.', 'ai-for-seo' ) : ''; if ( isset( $ai4seo_results['message'] ) && $ai4seo_results['message'] && isset( $ai4seo_results['code'] ) && $ai4seo_results['code'] ) { ai4seo_send_ajax_error( esc_html( $ai4seo_results['message'] . $ai4seo_context_retained_message ), $ai4seo_results['code'] ); } else { ai4seo_send_ajax_error( esc_html( __( 'Could not execute API call.', 'ai-for-seo' ) . $ai4seo_context_retained_message ), 28127323 ); } } $ai4seo_generated_data = $ai4seo_results['data'] ?? array(); if ( ! $ai4seo_generated_data || ! is_array( $ai4seo_generated_data ) ) { ai4seo_debug_message( 28173126, 'Attachment attributes generation API call did not return valid data: ' . ai4seo_stringify( $ai4seo_results ) ); ai4seo_send_ajax_error( esc_html__( 'API call did not return valid data.', 'ai-for-seo' ), 431024824 ); } // ___________________________________________________________________________________________ \\ // === CHECK RESULTS ========================================================================= \\ // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ \\ // === PREPARE RESPONSE ====================================================================== \\ // Prepare requested attributes through the shared save contract, including final affixes and immutable hard caps. $ai4seo_prepared_generated_output = ai4seo_prepare_generated_output_fields_for_save( 'attachment_attributes', $ai4seo_generated_data, array_values( array_intersect( $ai4seo_generation_fields, $ai4seo_active_attachment_attributes ) ), $ai4seo_field_instructions, true ); $ai4seo_new_attachment_attributes = $ai4seo_prepared_generated_output['values']; $ai4seo_unresolved_generation_fields = $ai4seo_prepared_generated_output['unresolved_fields']; // Log omissions for support while allowing every usable partial attribute to continue. if ( $ai4seo_unresolved_generation_fields ) { ai4seo_debug_message( 762318905, 'Media attribute generation returned a partial response for attachment post ID ' . $ai4seo_this_attachment_post_id . '. Unresolved fields: ' . implode( ', ', $ai4seo_unresolved_generation_fields ) ); } // A soft-failed response still needs at least one usable requested attribute before anything is saved. if ( ! $ai4seo_new_attachment_attributes ) { ai4seo_send_ajax_error( esc_html__( 'No media attributes were generated.', 'ai-for-seo' ), 4111221026 ); } // === SAVE GENERATED DATA TO DATABASE ================================================================= \\ // Save one timestamp and clear stale provenance for omissions so live values survive and remain eligible later. $ai4seo_generated_at = time(); $ai4seo_save_operation_details = array(); $ai4seo_this_success = $ai4seo_history_read_succeeded && ai4seo_save_generated_data_to_postmeta( $ai4seo_this_attachment_post_id, $ai4seo_new_attachment_attributes, true, $ai4seo_generated_at, $ai4seo_unresolved_generation_fields, $ai4seo_save_operation_details ); if ( ! $ai4seo_this_success ) { ai4seo_debug_message( 30173126, 'Could not save media attributes for attachment post ID ' . $ai4seo_this_attachment_post_id . ': ' . ai4seo_stringify( $ai4seo_new_attachment_attributes ) ); } // === ADD LATEST ACTIVITY ENTRY ======================================================================= \\ // Normalize unavailable usage and record only persisted fields so activity history matches the AJAX response. $ai4seo_credits_consumed = (int) ( $ai4seo_results['credits-consumed'] ?? 0 ); if ( $ai4seo_this_success ) { ai4seo_add_latest_activity_entry( $ai4seo_this_attachment_post_id, 'success', 'attachment-attributes-manually-generated', $ai4seo_credits_consumed, '', array_keys( $ai4seo_new_attachment_attributes ) ); } // === BUILD SUCCESS RESPONSE ========================================================================== \\ $ai4seo_response = ai4seo_build_manual_generation_response( $ai4seo_new_attachment_attributes, $ai4seo_results, $ai4seo_this_success, $ai4seo_generated_at, $ai4seo_save_operation_details ); $ai4seo_response['attachment_context'] = array( 'mode' => $ai4seo_attachment_context_result['mode'], 'post_id' => $ai4seo_attachment_context_result['post_id'], 'reason' => $ai4seo_context_preparation['contextless'] ? $ai4seo_context_preparation['reason'] : $ai4seo_attachment_context_result['reason'], 'used' => '' !== $ai4seo_attachment_usage_context, ); if ( $ai4seo_this_success && ! $ai4seo_unresolved_generation_fields && '' !== $ai4seo_activity_error_token && ai4seo_resolve_recent_activity_error( $ai4seo_this_attachment_post_id, 'attachment-attributes-bulk-generated', $ai4seo_activity_error_token ) ) { $ai4seo_response['resolved_activity_tokens']['attachment-attributes-bulk-generated'] = $ai4seo_activity_error_token; } ai4seo_send_ajax_success( $ai4seo_response );