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