/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/API/AI/Product.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

declare( strict_types );

namespace 
Automattic\WooCommerce\Admin\API\AI;

use 
Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
use 
WP_Error;
use 
WP_REST_Request;
use 
WP_REST_Response;

defined'ABSPATH' ) || exit;

/**
 * Product controller
 *
 * @internal
 */
class Product extends AIEndpoint {
    
/**
     * The endpoint response option name.
     *
     * @var string
     */
    
const AI_CONTENT_GENERATED 'ai_content_generated';

    
/**
     * Endpoint.
     *
     * @var string
     */
    
protected $endpoint 'product';

    
/**
     * Register routes.
     */
    
public function register_routes() {
        
$this->register(
            array(
                array(
                    
'methods'             => \WP_REST_Server::CREATABLE,
                    
'callback'            => array( $this'update_product' ),
                    
'permission_callback' => array( Middleware::class, 'is_authorized' ),
                    
'args'                => array(
                        
'products_information' => array(
                            
'description' => __'Data generated by AI for updating dummy products.''woocommerce' ),
                            
'type'        => 'object',
                        ),
                        
'last_product'         => array(
                            
'description' => __'Whether the product being updated is the last one in the loop''woocommerce' ),
                            
'type'        => 'boolean',
                        ),
                    ),
                ),
            )
        );
    }

    
/**
     * Update product with the content and images powered by AI.
     *
     * @param  WP_REST_Request $request Request object.
     *
     * @return WP_REST_Response
     */
    
public function update_productWP_REST_Request $request ) {
        
$product_information $request['products_information'] ?? array();

        if ( empty( 
$product_information ) ) {
            return 
rest_ensure_response(
                array(
                    
self::AI_CONTENT_GENERATED => true,
                )
            );
        }

        try {
            
$product_updater = new UpdateProducts();
            
$product_updater->update_product_content$product_information );
        } catch ( 
\Exception $e ) {
            return 
rest_ensure_response( array( 'ai_content_generated' => false ) );
        }

        
$last_product_to_update $request['last_product'] ?? false;

        if ( 
$last_product_to_update ) {
            
flush_rewrite_rules();
        }

        return 
rest_ensure_response(
            array(
                
self::AI_CONTENT_GENERATED => true,
            )
        );
    }
}