/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/StoreApi.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Automattic\WooCommerce\StoreApi;

use 
Automattic\WooCommerce\Blocks\Registry\Container;
use 
Automattic\WooCommerce\StoreApi\Formatters;
use 
Automattic\WooCommerce\StoreApi\Authentication;
use 
Automattic\WooCommerce\StoreApi\Legacy;
use 
Automattic\WooCommerce\StoreApi\Formatters\CurrencyFormatter;
use 
Automattic\WooCommerce\StoreApi\Formatters\HtmlFormatter;
use 
Automattic\WooCommerce\StoreApi\Formatters\MoneyFormatter;
use 
Automattic\WooCommerce\StoreApi\RoutesController;
use 
Automattic\WooCommerce\StoreApi\SchemaController;
use 
Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;

/**
 * StoreApi Main Class.
 */
final class StoreApi {
    
/**
     * Init and hook in Store API functionality.
     */
    
public function init() {
        
add_action(
            
'rest_api_init',
            function () {
                if ( ! 
wc_rest_should_load_namespace'wc/store' ) && ! wc_rest_should_load_namespace'wc/private' ) ) {
                    return;
                }
                
self::container()->getLegacy::class )->init();
                
self::container()->getRoutesController::class )->register_all_routes();
            }
        );
        
// Runs on priority 11 after rest_api_default_filters() which is hooked at 10.
        
add_action(
            
'rest_api_init',
            function () {
                if ( ! 
wc_rest_should_load_namespace'wc/store' ) ) {
                    return;
                }
                
self::container()->getAuthentication::class )->init();
            },
            
11
        
);

        
add_action(
            
'woocommerce_blocks_pre_get_routes_from_namespace',
            function ( 
$routes$ns ) {
                if ( 
'wc/store/v1' !== $ns ) {
                    return 
$routes;
                }

                
$routes array_merge(
                    
$routes,
                    
self::container()->getRoutesController::class )->get_all_routes'v1' )
                );

                return 
$routes;
            },
            
10,
            
2
        
);
    }

    
/**
     * Loads the DI container for Store API.
     *
     * @internal This uses the Blocks DI container. If Store API were to move to core, this container could be replaced
     * with a different compatible container.
     *
     * @param boolean $reset Used to reset the container to a fresh instance. Note: this means all dependencies will be reconstructed.
     * @return mixed
     */
    
public static function container$reset false ) {
        static 
$container;

        if ( 
$reset ) {
            
$container null;
        }

        if ( 
$container ) {
            return 
$container;
        }

        
$container = new Container();
        
$container->register(
            
Authentication::class,
            function () {
                return new 
Authentication();
            }
        );
        
$container->register(
            
Legacy::class,
            function () {
                return new 
Legacy();
            }
        );
        
$container->register(
            
RoutesController::class,
            function ( 
$container ) {
                return new 
RoutesController(
                    
$container->getSchemaController::class )
                );
            }
        );
        
$container->register(
            
SchemaController::class,
            function ( 
$container ) {
                return new 
SchemaController(
                    
$container->getExtendSchema::class )
                );
            }
        );
        
$container->register(
            
ExtendSchema::class,
            function ( 
$container ) {
                return new 
ExtendSchema(
                    
$container->getFormatters::class )
                );
            }
        );
        
$container->register(
            
Formatters::class,
            function () {
                
$formatters = new Formatters();
                
$formatters->register'money'MoneyFormatter::class );
                
$formatters->register'html'HtmlFormatter::class );
                
$formatters->register'currency'CurrencyFormatter::class );
                return 
$formatters;
            }
        );
        return 
$container;
    }
}