Exceptions
2 years ago
Formatters
1 year ago
Payments
2 years ago
Routes
1 year ago
Schemas
1 year ago
Utilities
1 year ago
Authentication.php
1 year ago
Formatters.php
2 years ago
Legacy.php
1 year ago
RoutesController.php
1 year ago
SchemaController.php
1 year ago
SessionHandler.php
2 years ago
StoreApi.php
1 year ago
deprecated.php
2 years ago
functions.php
2 years ago
StoreApi.php
134 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Registry\Container; |
| 5 | use Automattic\WooCommerce\StoreApi\Formatters; |
| 6 | use Automattic\WooCommerce\StoreApi\Authentication; |
| 7 | use Automattic\WooCommerce\StoreApi\Legacy; |
| 8 | use Automattic\WooCommerce\StoreApi\Formatters\CurrencyFormatter; |
| 9 | use Automattic\WooCommerce\StoreApi\Formatters\HtmlFormatter; |
| 10 | use Automattic\WooCommerce\StoreApi\Formatters\MoneyFormatter; |
| 11 | use Automattic\WooCommerce\StoreApi\RoutesController; |
| 12 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 13 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 14 | |
| 15 | /** |
| 16 | * StoreApi Main Class. |
| 17 | */ |
| 18 | final class StoreApi { |
| 19 | /** |
| 20 | * Init and hook in Store API functionality. |
| 21 | */ |
| 22 | public function init() { |
| 23 | add_action( |
| 24 | 'rest_api_init', |
| 25 | function () { |
| 26 | if ( ! wc_rest_should_load_namespace( 'wc/store' ) && ! wc_rest_should_load_namespace( 'wc/private' ) ) { |
| 27 | return; |
| 28 | } |
| 29 | self::container()->get( Legacy::class )->init(); |
| 30 | self::container()->get( RoutesController::class )->register_all_routes(); |
| 31 | } |
| 32 | ); |
| 33 | // Runs on priority 11 after rest_api_default_filters() which is hooked at 10. |
| 34 | add_action( |
| 35 | 'rest_api_init', |
| 36 | function () { |
| 37 | if ( ! wc_rest_should_load_namespace( 'wc/store' ) ) { |
| 38 | return; |
| 39 | } |
| 40 | self::container()->get( Authentication::class )->init(); |
| 41 | }, |
| 42 | 11 |
| 43 | ); |
| 44 | |
| 45 | add_action( |
| 46 | 'woocommerce_blocks_pre_get_routes_from_namespace', |
| 47 | function ( $routes, $ns ) { |
| 48 | if ( 'wc/store/v1' !== $ns ) { |
| 49 | return $routes; |
| 50 | } |
| 51 | |
| 52 | $routes = array_merge( |
| 53 | $routes, |
| 54 | self::container()->get( RoutesController::class )->get_all_routes( 'v1' ) |
| 55 | ); |
| 56 | |
| 57 | return $routes; |
| 58 | }, |
| 59 | 10, |
| 60 | 2 |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Loads the DI container for Store API. |
| 66 | * |
| 67 | * @internal This uses the Blocks DI container. If Store API were to move to core, this container could be replaced |
| 68 | * with a different compatible container. |
| 69 | * |
| 70 | * @param boolean $reset Used to reset the container to a fresh instance. Note: this means all dependencies will be reconstructed. |
| 71 | * @return mixed |
| 72 | */ |
| 73 | public static function container( $reset = false ) { |
| 74 | static $container; |
| 75 | |
| 76 | if ( $reset ) { |
| 77 | $container = null; |
| 78 | } |
| 79 | |
| 80 | if ( $container ) { |
| 81 | return $container; |
| 82 | } |
| 83 | |
| 84 | $container = new Container(); |
| 85 | $container->register( |
| 86 | Authentication::class, |
| 87 | function () { |
| 88 | return new Authentication(); |
| 89 | } |
| 90 | ); |
| 91 | $container->register( |
| 92 | Legacy::class, |
| 93 | function () { |
| 94 | return new Legacy(); |
| 95 | } |
| 96 | ); |
| 97 | $container->register( |
| 98 | RoutesController::class, |
| 99 | function ( $container ) { |
| 100 | return new RoutesController( |
| 101 | $container->get( SchemaController::class ) |
| 102 | ); |
| 103 | } |
| 104 | ); |
| 105 | $container->register( |
| 106 | SchemaController::class, |
| 107 | function ( $container ) { |
| 108 | return new SchemaController( |
| 109 | $container->get( ExtendSchema::class ) |
| 110 | ); |
| 111 | } |
| 112 | ); |
| 113 | $container->register( |
| 114 | ExtendSchema::class, |
| 115 | function ( $container ) { |
| 116 | return new ExtendSchema( |
| 117 | $container->get( Formatters::class ) |
| 118 | ); |
| 119 | } |
| 120 | ); |
| 121 | $container->register( |
| 122 | Formatters::class, |
| 123 | function () { |
| 124 | $formatters = new Formatters(); |
| 125 | $formatters->register( 'money', MoneyFormatter::class ); |
| 126 | $formatters->register( 'html', HtmlFormatter::class ); |
| 127 | $formatters->register( 'currency', CurrencyFormatter::class ); |
| 128 | return $formatters; |
| 129 | } |
| 130 | ); |
| 131 | return $container; |
| 132 | } |
| 133 | } |
| 134 |