generator = $generator ?? new ContentGenerator(); } public function register_routes(): void { register_rest_route( self::REST_NAMESPACE . '/description-generator', '/history', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_history' ), 'permission_callback' => array( $this, 'check_permissions' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/apply', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'apply_content' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/bulk-apply', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'bulk_apply' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/approve-description', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'approve_description' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/reject-description', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'reject_description' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/bulk-approve', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'bulk_approve' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/bulk-reject', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'bulk_reject' ), 'permission_callback' => array( $this, 'check_permissions_write' ), 'args' => array( 'results' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ), 'maxItems' => self::MAX_BULK_REJECT_ITEMS, ), ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/delete-history-item', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'delete_history_item' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/bulk-delete', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'bulk_delete' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/delete-all-history', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'delete_all_history' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ) ); register_rest_route( self::REST_NAMESPACE . '/description-generator', '/cancel', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'cancel_generation' ), 'permission_callback' => array( $this, 'check_permissions_write' ), 'args' => array( 'history_id' => array( 'required' => true, 'sanitize_callback' => 'absint', ), ), ) ); } public function get_history(): WP_REST_Response { return $this->retired_route_response(); } public function apply_content( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } public function bulk_apply( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } public function approve_description( WP_REST_Request $request ): WP_REST_Response { $json_params = $request->get_json_params(); $product_id = absint( $json_params['product_id'] ?? $request->get_param( 'product_id' ) ?? 0 ); $type = sanitize_text_field( $json_params['description_type'] ?? $request->get_param( 'description_type' ) ?? '' ); $content = wp_kses_post( $json_params['description'] ?? $request->get_param( 'description' ) ?? '' ); $raw_options = $json_params['options'] ?? $request->get_param( 'options' ) ?? array(); $options = is_array( $raw_options ) ? $this->sanitize_generation_options( $raw_options ) : array(); if ( ! $product_id ) { return new WP_REST_Response( array( 'success' => false, 'message' => __( 'Product ID is required', 'ai-product-tools' ) ), 400 ); } if ( empty( $type ) || ! in_array( $type, array( 'short', 'full', 'tags' ), true ) ) { return new WP_REST_Response( array( 'success' => false, 'message' => __( 'Valid description type is required (short, full, or tags)', 'ai-product-tools' ) ), 400 ); } if ( empty( $content ) ) { return new WP_REST_Response( array( 'success' => false, 'message' => __( 'Description content is required', 'ai-product-tools' ) ), 400 ); } if ( ! current_user_can( 'edit_post', $product_id ) ) { return new WP_REST_Response( array( 'success' => false, 'message' => __( 'You do not have permission to edit this product.', 'ai-product-tools' ) ), 403 ); } $internal_type = $this->map_db_type_to_internal( $type ); $executionDecision = BlueprintIntegration::executionPath( BlueprintGenerators::DESCRIPTION, BlueprintIntegration::jobTrustedFacts() ); $options['executionPath'] = $executionDecision['path']; $blueprint = null; if ( BlueprintIntegration::PATH_BLUEPRINT === $executionDecision['path'] ) { $raw_blueprint = $json_params['blueprint'] ?? $request->get_param( 'blueprint' ) ?? null; $blueprint = BlueprintAttributionSanitizer::sanitize( $raw_blueprint ); } $result = $this->generator->applyContent( $product_id, array( $internal_type => $content ), $options, $blueprint ); if ( $result ) { $this->record_usage_event( 'advanced_desc', 'approved', 1, array( 'product_id' => $product_id ) ); return new WP_REST_Response( array( 'success' => true, 'message' => __( 'Description approved and applied', 'ai-product-tools' ) ), 200 ); } foreach ( $this->generator->getSkippedFields() as $skipped_field ) { if ( ( $skipped_field['field'] ?? '' ) === $internal_type && 'not_empty' === ( $skipped_field['reason'] ?? '' ) ) { return new WP_REST_Response( array( 'success' => true, 'skipped' => true, 'kept' => true, 'message' => __( 'Kept the existing text: this field already has content and Fill empty only is on. Switch Existing content to Replace to overwrite it.', 'ai-product-tools' ), ), 200 ); } } if ( 'tags' === $internal_type ) { return new WP_REST_Response( array( 'success' => false, 'message' => __( 'No tags were saved. The generated tags were empty, the product already has them, or saving them failed. Try again.', 'ai-product-tools' ), ), 422 ); } return new WP_REST_Response( array( 'success' => false, 'message' => __( 'Failed to approve description', 'ai-product-tools' ) ), 500 ); } public function reject_description( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } public function bulk_approve( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } public function bulk_reject( WP_REST_Request $request ): WP_REST_Response { $data = $request->get_json_params(); $results = $data['results'] ?? array(); if ( empty( $results ) ) { return new WP_REST_Response( array( 'success' => false, 'message' => __( 'No items to reject', 'ai-product-tools' ) ), 400 ); } $success_count = 0; $first_rejected_pid = 0; foreach ( $results as $item ) { $product_id = absint( $item['product_id'] ?? 0 ); $type = sanitize_text_field( $item['description_type'] ?? '' ); if ( ! $product_id || ! $type ) { continue; } $success_count++; if ( 0 === $first_rejected_pid ) { $first_rejected_pid = $product_id; } } if ( $success_count > 0 ) { $this->record_usage_event( 'advanced_desc', 'rejected', $success_count, array( 'product_id' => $first_rejected_pid ) ); } return new WP_REST_Response( array( 'success' => true, /* translators: %d: number of items rejected */ 'message' => sprintf( _n( '%d item rejected', '%d items rejected', $success_count, 'ai-product-tools' ), $success_count ), 'rejected_count' => $success_count, ), 200 ); } public function delete_history_item( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } public function bulk_delete( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } public function delete_all_history(): WP_REST_Response { return $this->retired_route_response(); } public function cancel_generation( WP_REST_Request $request ): WP_REST_Response { return $this->retired_route_response(); } /** * Standard 410 Gone response for a legacy approval-queue route. * * Matches the SeoMeta deprecation precedent, and is actionable rather * than a bare status code: a stale admin tab (bundle loaded before this * update) needs "reload the page" surfaced, not an unexplained failure. * * @return WP_REST_Response */ private function retired_route_response(): WP_REST_Response { return new WP_REST_Response( array( 'success' => false, 'code' => 'aipt_route_retired', 'message' => __( 'This action is no longer available from this tab. Please reload the page to load the latest version.', 'ai-product-tools' ), ), 410 ); } private function map_db_type_to_internal( string $type ): string { $mapping = array( 'short' => 'short_description', 'full' => 'description', 'tags' => 'tags', ); return $mapping[ $type ] ?? $type; } /** * Record usage telemetry without breaking the approval workflow. * * @param string $feature Feature identifier. * @param string $action Action identifier. * @param int $count Event count. * @param array $meta Event metadata. * @return void */ private function record_usage_event( string $feature, string $action, int $count, array $meta ): void { try { \AIPT\Core\Services\UsageTracker::record_event( $feature, $action, $count, $meta ); } catch ( \Throwable $throwable ) { do_action( 'aipt_usage_tracking_failed', $throwable, $feature, $action, $count, $meta ); } } }