'skipped', 'fields' => array(), 'message' => __( 'No FAQ content was generated.', 'ai-product-tools' ) ); } if ( 'empty_only' === $overwrite && $this->hasExistingFaq( $product_id ) ) { return array( 'status' => 'skipped', 'fields' => array( array( 'field' => 'faq', 'outcome' => 'skipped_not_empty' ) ), 'message' => __( 'FAQ content is not empty.', 'ai-product-tools' ), ); } $row = array_merge( array( 'product_id' => $product_id, 'generated_value' => wp_json_encode( $faq ), 'apply_context' => wp_json_encode( array( 'writeMode' => 'replace' ) ), ), $this->blueprintRowFields( $context ) ); $outcome = $this->callApplier( 'faq', $row ); if ( null !== $outcome['refusalMessage'] ) { return array( 'status' => 'skipped', 'fields' => array( array( 'field' => 'faq', 'outcome' => 'failed' ) ), 'message' => $outcome['refusalMessage'], 'retryable' => $this->isRetryableRefusal( $outcome ), ); } if ( ! $outcome['ok'] ) { return array( 'status' => 'skipped', 'fields' => array( array( 'field' => 'faq', 'outcome' => 'failed' ) ), 'message' => __( 'The write did not complete.', 'ai-product-tools' ) ); } $message = ''; if ( isset( $outcome['appliedFreeDefaults']['writeMode'] ) ) { $message = PlanDenial::message( (string) $outcome['appliedFreeDefaults']['writeMode'], __( 'New questions were added to the existing FAQs. Replacing the existing FAQs is included in paid plans.', 'ai-product-tools' ) ); } return array( 'status' => 'applied', 'fields' => array( array( 'field' => 'faq', 'outcome' => 'written' ) ), 'message' => $message ); } /** * @param int $product_id Product id. * @return bool */ private function hasExistingFaq( int $product_id ): bool { $raw = function_exists( 'get_post_meta' ) ? get_post_meta( $product_id, '_aipt_faq_data', true ) : ''; if ( ! is_string( $raw ) || '' === $raw ) { return false; } $decoded = json_decode( $raw, true ); return is_array( $decoded ) && ! empty( $decoded ); } }