Bootstrap.php
495 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Domain; |
| 3 | |
| 4 | use Automattic\Jetpack\Constants; |
| 5 | use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi; |
| 6 | use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry; |
| 7 | use Automattic\WooCommerce\Blocks\AssetsController; |
| 8 | use Automattic\WooCommerce\Blocks\BlockPatterns; |
| 9 | use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry; |
| 10 | use Automattic\WooCommerce\Blocks\BlockTemplatesController; |
| 11 | use Automattic\WooCommerce\Blocks\BlockTypesController; |
| 12 | use Automattic\WooCommerce\Blocks\DependencyDetection; |
| 13 | use Automattic\WooCommerce\Blocks\Patterns\PatternRegistry; |
| 14 | use Automattic\WooCommerce\Blocks\Patterns\PTKClient; |
| 15 | use Automattic\WooCommerce\Blocks\Patterns\PTKPatternsStore; |
| 16 | use Automattic\WooCommerce\Blocks\Domain\Services\Notices; |
| 17 | use Automattic\WooCommerce\Blocks\Domain\Services\DraftOrders; |
| 18 | use Automattic\WooCommerce\Blocks\Domain\Services\GoogleAnalytics; |
| 19 | use Automattic\WooCommerce\Blocks\Domain\Services\Hydration; |
| 20 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields; |
| 21 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsAdmin; |
| 22 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsFrontend; |
| 23 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutLink; |
| 24 | use Automattic\WooCommerce\Blocks\InboxNotifications; |
| 25 | use Automattic\WooCommerce\Blocks\Installer; |
| 26 | use Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi; |
| 27 | use Automattic\WooCommerce\Blocks\Payments\Integrations\BankTransfer; |
| 28 | use Automattic\WooCommerce\Blocks\Payments\Integrations\CashOnDelivery; |
| 29 | use Automattic\WooCommerce\Blocks\Payments\Integrations\Cheque; |
| 30 | use Automattic\WooCommerce\Blocks\Payments\Integrations\PayPal; |
| 31 | use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry; |
| 32 | use Automattic\WooCommerce\Blocks\Registry\Container; |
| 33 | use Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility; |
| 34 | use Automattic\WooCommerce\StoreApi\RoutesController; |
| 35 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 36 | use Automattic\WooCommerce\StoreApi\StoreApi; |
| 37 | use Automattic\WooCommerce\Blocks\Shipping\ShippingController; |
| 38 | use Automattic\WooCommerce\Blocks\TemplateOptions; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Takes care of bootstrapping the plugin. |
| 43 | * |
| 44 | * @since 2.5.0 |
| 45 | */ |
| 46 | class Bootstrap { |
| 47 | |
| 48 | /** |
| 49 | * Holds the Dependency Injection Container |
| 50 | * |
| 51 | * @var Container |
| 52 | */ |
| 53 | private $container; |
| 54 | |
| 55 | /** |
| 56 | * Holds the Package instance |
| 57 | * |
| 58 | * @var Package |
| 59 | */ |
| 60 | private $package; |
| 61 | |
| 62 | /** |
| 63 | * Constructor |
| 64 | * |
| 65 | * @param Container $container The Dependency Injection Container. |
| 66 | */ |
| 67 | public function __construct( Container $container ) { |
| 68 | $this->container = $container; |
| 69 | $this->package = $container->get( Package::class ); |
| 70 | |
| 71 | $this->init(); |
| 72 | /** |
| 73 | * Fires when the woocommerce blocks are loaded and ready to use. |
| 74 | * |
| 75 | * This hook is intended to be used as a safe event hook for when the plugin |
| 76 | * has been loaded, and all dependency requirements have been met. |
| 77 | * |
| 78 | * To ensure blocks are initialized, you must use the `woocommerce_blocks_loaded` |
| 79 | * hook instead of the `plugins_loaded` hook. This is because the functions |
| 80 | * hooked into plugins_loaded on the same priority load in an inconsistent and unpredictable manner. |
| 81 | * |
| 82 | * @since 2.5.0 |
| 83 | */ |
| 84 | do_action( 'woocommerce_blocks_loaded' ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Init the package - load the blocks library and define constants. |
| 89 | */ |
| 90 | protected function init() { |
| 91 | $this->register_dependencies(); |
| 92 | $this->register_payment_methods(); |
| 93 | |
| 94 | add_action( |
| 95 | 'admin_init', |
| 96 | function () { |
| 97 | // Delete this notification because the blocks are included in WC Core now. This will handle any sites |
| 98 | // with lingering notices. |
| 99 | InboxNotifications::delete_surface_cart_checkout_blocks_notification(); |
| 100 | }, |
| 101 | 10, |
| 102 | 0 |
| 103 | ); |
| 104 | |
| 105 | // We need to initialize BlockTemplatesController and BlockTemplatesRegistry at the end of `after_setup_theme` |
| 106 | // so themes had the opportunity to declare support for template parts. |
| 107 | add_action( |
| 108 | 'after_setup_theme', |
| 109 | function () { |
| 110 | $is_store_api_request = wc()->is_store_api_request(); |
| 111 | |
| 112 | if ( ! $is_store_api_request && ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) ) { |
| 113 | $this->container->get( BlockTemplatesRegistry::class )->init(); |
| 114 | $this->container->get( BlockTemplatesController::class )->init(); |
| 115 | } |
| 116 | }, |
| 117 | 999 |
| 118 | ); |
| 119 | |
| 120 | $is_rest = wc()->is_rest_api_request(); |
| 121 | $is_store_api_request = wc()->is_store_api_request(); |
| 122 | |
| 123 | // Initialize Store API in non-admin context. |
| 124 | if ( ! is_admin() ) { |
| 125 | $this->container->get( StoreApi::class )->init(); |
| 126 | } |
| 127 | |
| 128 | // Load and init assets. |
| 129 | $this->container->get( PaymentsApi::class )->init(); |
| 130 | $this->container->get( DraftOrders::class )->init(); |
| 131 | $this->container->get( ShippingController::class )->init(); |
| 132 | $this->container->get( CheckoutFields::class )->init(); |
| 133 | $this->container->get( CheckoutLink::class )->init(); |
| 134 | $this->container->get( AssetDataRegistry::class ); |
| 135 | $this->container->get( AssetsController::class ); |
| 136 | $this->container->get( DependencyDetection::class ); |
| 137 | |
| 138 | // Load assets in admin and on the frontend. |
| 139 | if ( ! $is_rest ) { |
| 140 | $this->add_build_notice(); |
| 141 | $this->container->get( Installer::class )->init(); |
| 142 | $this->container->get( GoogleAnalytics::class )->init(); |
| 143 | $this->container->get( is_admin() ? CheckoutFieldsAdmin::class : CheckoutFieldsFrontend::class )->init(); |
| 144 | } |
| 145 | |
| 146 | // Load assets unless this is a request specifically for the store API. |
| 147 | if ( ! $is_store_api_request ) { |
| 148 | // Template related functionality. These won't be loaded for store API requests, but may be loaded for |
| 149 | // regular rest requests to maintain compatibility with the store editor. |
| 150 | $this->container->get( BlockPatterns::class ); |
| 151 | $this->container->get( BlockTypesController::class ); |
| 152 | $this->container->get( ClassicTemplatesCompatibility::class ); |
| 153 | $this->container->get( Notices::class )->init(); |
| 154 | |
| 155 | if ( is_admin() || $is_rest ) { |
| 156 | $this->container->get( PTKPatternsStore::class ); |
| 157 | } |
| 158 | |
| 159 | if ( is_admin() ) { |
| 160 | $this->container->get( TemplateOptions::class )->init(); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * See if files have been built or not. |
| 167 | * |
| 168 | * @return bool |
| 169 | */ |
| 170 | protected function is_built() { |
| 171 | return file_exists( |
| 172 | $this->package->get_path( 'assets/client/blocks/featured-product.js' ) |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Add a notice stating that the build has not been done yet. |
| 178 | */ |
| 179 | protected function add_build_notice() { |
| 180 | if ( $this->is_built() ) { |
| 181 | return; |
| 182 | } |
| 183 | add_action( |
| 184 | 'admin_notices', |
| 185 | function () { |
| 186 | echo '<div class="error"><p>'; |
| 187 | printf( |
| 188 | /* translators: %1$s is the install command, %2$s is the build command, %3$s is the watch command. */ |
| 189 | esc_html__( 'WooCommerce Blocks development mode requires files to be built. From the root directory, run %1$s to install dependencies, %2$s to build the files or %3$s to build the files and watch for changes.', 'woocommerce' ), |
| 190 | '<code>pnpm install</code>', |
| 191 | '<code>pnpm --filter="@woocommerce/plugin-woocommerce" build</code>', |
| 192 | '<code>pnpm --filter="@woocommerce/plugin-woocommerce" watch:build</code>' |
| 193 | ); |
| 194 | echo '</p></div>'; |
| 195 | } |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Register core dependencies with the container. |
| 201 | */ |
| 202 | protected function register_dependencies() { |
| 203 | $this->container->register( |
| 204 | AssetApi::class, |
| 205 | function ( Container $container ) { |
| 206 | return new AssetApi( $container->get( Package::class ) ); |
| 207 | } |
| 208 | ); |
| 209 | $this->container->register( |
| 210 | AssetDataRegistry::class, |
| 211 | function ( Container $container ) { |
| 212 | return new AssetDataRegistry( $container->get( AssetApi::class ) ); |
| 213 | } |
| 214 | ); |
| 215 | $this->container->register( |
| 216 | AssetsController::class, |
| 217 | function ( Container $container ) { |
| 218 | return new AssetsController( $container->get( AssetApi::class ) ); |
| 219 | } |
| 220 | ); |
| 221 | $this->container->register( |
| 222 | DependencyDetection::class, |
| 223 | function () { |
| 224 | return new DependencyDetection(); |
| 225 | } |
| 226 | ); |
| 227 | $this->container->register( |
| 228 | PaymentMethodRegistry::class, |
| 229 | function () { |
| 230 | return new PaymentMethodRegistry(); |
| 231 | } |
| 232 | ); |
| 233 | $this->container->register( |
| 234 | Installer::class, |
| 235 | function () { |
| 236 | return new Installer(); |
| 237 | } |
| 238 | ); |
| 239 | $this->container->register( |
| 240 | BlockTypesController::class, |
| 241 | function ( Container $container ) { |
| 242 | $asset_api = $container->get( AssetApi::class ); |
| 243 | $asset_data_registry = $container->get( AssetDataRegistry::class ); |
| 244 | return new BlockTypesController( $asset_api, $asset_data_registry ); |
| 245 | } |
| 246 | ); |
| 247 | $this->container->register( |
| 248 | ClassicTemplatesCompatibility::class, |
| 249 | function ( Container $container ) { |
| 250 | $asset_data_registry = $container->get( AssetDataRegistry::class ); |
| 251 | return new ClassicTemplatesCompatibility( $asset_data_registry ); |
| 252 | } |
| 253 | ); |
| 254 | $this->container->register( |
| 255 | DraftOrders::class, |
| 256 | function ( Container $container ) { |
| 257 | return new DraftOrders( $container->get( Package::class ) ); |
| 258 | } |
| 259 | ); |
| 260 | $this->container->register( |
| 261 | GoogleAnalytics::class, |
| 262 | function ( Container $container ) { |
| 263 | $asset_api = $container->get( AssetApi::class ); |
| 264 | return new GoogleAnalytics( $asset_api ); |
| 265 | } |
| 266 | ); |
| 267 | $this->container->register( |
| 268 | Notices::class, |
| 269 | function ( Container $container ) { |
| 270 | return new Notices( $container->get( Package::class ) ); |
| 271 | } |
| 272 | ); |
| 273 | $this->container->register( |
| 274 | Hydration::class, |
| 275 | function ( Container $container ) { |
| 276 | return new Hydration( $container->get( AssetDataRegistry::class ) ); |
| 277 | } |
| 278 | ); |
| 279 | $this->container->register( |
| 280 | CheckoutFields::class, |
| 281 | function ( Container $container ) { |
| 282 | return new CheckoutFields( $container->get( AssetDataRegistry::class ) ); |
| 283 | } |
| 284 | ); |
| 285 | $this->container->register( |
| 286 | CheckoutFieldsAdmin::class, |
| 287 | function ( Container $container ) { |
| 288 | $checkout_fields_controller = $container->get( CheckoutFields::class ); |
| 289 | return new CheckoutFieldsAdmin( $checkout_fields_controller ); |
| 290 | } |
| 291 | ); |
| 292 | $this->container->register( |
| 293 | CheckoutFieldsFrontend::class, |
| 294 | function ( Container $container ) { |
| 295 | $checkout_fields_controller = $container->get( CheckoutFields::class ); |
| 296 | return new CheckoutFieldsFrontend( $checkout_fields_controller ); |
| 297 | } |
| 298 | ); |
| 299 | $this->container->register( |
| 300 | PaymentsApi::class, |
| 301 | function ( Container $container ) { |
| 302 | $payment_method_registry = $container->get( PaymentMethodRegistry::class ); |
| 303 | $asset_data_registry = $container->get( AssetDataRegistry::class ); |
| 304 | return new PaymentsApi( $payment_method_registry, $asset_data_registry ); |
| 305 | } |
| 306 | ); |
| 307 | $this->container->register( |
| 308 | CheckoutLink::class, |
| 309 | function () { |
| 310 | return new CheckoutLink(); |
| 311 | } |
| 312 | ); |
| 313 | $this->container->register( |
| 314 | StoreApi::class, |
| 315 | function () { |
| 316 | return new StoreApi(); |
| 317 | } |
| 318 | ); |
| 319 | $this->container->register( |
| 320 | TemplateOptions::class, |
| 321 | function () { |
| 322 | return new TemplateOptions(); |
| 323 | } |
| 324 | ); |
| 325 | // Maintains backwards compatibility with previous Store API namespace. |
| 326 | $this->container->register( |
| 327 | 'Automattic\WooCommerce\Blocks\StoreApi\Formatters', |
| 328 | function ( Container $container ) { |
| 329 | $this->deprecated_dependency( 'Automattic\WooCommerce\Blocks\StoreApi\Formatters', '6.4.0', 'Automattic\WooCommerce\StoreApi\Formatters', '6.5.0' ); |
| 330 | return $container->get( StoreApi::class )->container()->get( \Automattic\WooCommerce\StoreApi\Formatters::class ); |
| 331 | } |
| 332 | ); |
| 333 | $this->container->register( |
| 334 | 'Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi', |
| 335 | function ( Container $container ) { |
| 336 | $this->deprecated_dependency( 'Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi', '6.4.0', 'Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema', '6.5.0' ); |
| 337 | return $container->get( StoreApi::class )->container()->get( \Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::class ); |
| 338 | } |
| 339 | ); |
| 340 | $this->container->register( |
| 341 | 'Automattic\WooCommerce\Blocks\StoreApi\SchemaController', |
| 342 | function ( Container $container ) { |
| 343 | $this->deprecated_dependency( 'Automattic\WooCommerce\Blocks\StoreApi\SchemaController', '6.4.0', 'Automattic\WooCommerce\StoreApi\SchemaController', '6.5.0' ); |
| 344 | return $container->get( StoreApi::class )->container()->get( SchemaController::class ); |
| 345 | } |
| 346 | ); |
| 347 | $this->container->register( |
| 348 | 'Automattic\WooCommerce\Blocks\StoreApi\RoutesController', |
| 349 | function ( Container $container ) { |
| 350 | $this->deprecated_dependency( 'Automattic\WooCommerce\Blocks\StoreApi\RoutesController', '6.4.0', 'Automattic\WooCommerce\StoreApi\RoutesController', '6.5.0' ); |
| 351 | return $container->get( StoreApi::class )->container()->get( RoutesController::class ); |
| 352 | } |
| 353 | ); |
| 354 | $this->container->register( |
| 355 | PTKClient::class, |
| 356 | function () { |
| 357 | return new PTKClient(); |
| 358 | } |
| 359 | ); |
| 360 | $this->container->register( |
| 361 | PTKPatternsStore::class, |
| 362 | function () { |
| 363 | return new PTKPatternsStore( $this->container->get( PTKClient::class ) ); |
| 364 | } |
| 365 | ); |
| 366 | $this->container->register( |
| 367 | BlockPatterns::class, |
| 368 | function () { |
| 369 | return new BlockPatterns( |
| 370 | $this->package, |
| 371 | new PatternRegistry(), |
| 372 | $this->container->get( PTKPatternsStore::class ) |
| 373 | ); |
| 374 | } |
| 375 | ); |
| 376 | $this->container->register( |
| 377 | ShippingController::class, |
| 378 | function ( $container ) { |
| 379 | $asset_api = $container->get( AssetApi::class ); |
| 380 | $asset_data_registry = $container->get( AssetDataRegistry::class ); |
| 381 | return new ShippingController( $asset_api, $asset_data_registry ); |
| 382 | } |
| 383 | ); |
| 384 | $this->container->register( |
| 385 | BlockTemplatesRegistry::class, |
| 386 | function () { |
| 387 | return new BlockTemplatesRegistry(); |
| 388 | } |
| 389 | ); |
| 390 | $this->container->register( |
| 391 | BlockTemplatesController::class, |
| 392 | function () { |
| 393 | return new BlockTemplatesController(); |
| 394 | } |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Throws a deprecation notice for a dependency without breaking requests. |
| 400 | * |
| 401 | * @param string $function Class or function being deprecated. |
| 402 | * @param string $version Version in which it was deprecated. |
| 403 | * @param string $replacement Replacement class or function, if applicable. |
| 404 | * @param string $trigger_error_version Optional version to start surfacing this as a PHP error rather than a log. Defaults to $version. |
| 405 | */ |
| 406 | protected function deprecated_dependency( $function, $version, $replacement = '', $trigger_error_version = '' ) { |
| 407 | if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | $trigger_error_version = $trigger_error_version ? $trigger_error_version : $version; |
| 412 | $error_message = $replacement ? sprintf( |
| 413 | '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', |
| 414 | $function, |
| 415 | $version, |
| 416 | $replacement |
| 417 | ) : sprintf( |
| 418 | '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', |
| 419 | $function, |
| 420 | $version |
| 421 | ); |
| 422 | /** |
| 423 | * Fires when a deprecated function is called. |
| 424 | * |
| 425 | * @since 7.3.0 |
| 426 | */ |
| 427 | do_action( 'deprecated_function_run', $function, $replacement, $version ); |
| 428 | |
| 429 | $log_error = false; |
| 430 | |
| 431 | // If headers have not been sent yet, log to avoid breaking the request. |
| 432 | if ( ! headers_sent() ) { |
| 433 | $log_error = true; |
| 434 | } |
| 435 | |
| 436 | // If the $trigger_error_version was not yet reached, only log the error. |
| 437 | if ( version_compare( Constants::get_constant( 'WC_VERSION' ), $trigger_error_version, '<' ) ) { |
| 438 | $log_error = true; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Filters whether to trigger an error for deprecated functions. (Same as WP core) |
| 443 | * |
| 444 | * @since 7.3.0 |
| 445 | * |
| 446 | * @param bool $trigger Whether to trigger the error for deprecated functions. Default true. |
| 447 | */ |
| 448 | if ( ! apply_filters( 'deprecated_function_trigger_error', true ) ) { |
| 449 | $log_error = true; |
| 450 | } |
| 451 | |
| 452 | if ( $log_error ) { |
| 453 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 454 | error_log( $error_message ); |
| 455 | } else { |
| 456 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.PHP.DevelopmentFunctions.error_log_trigger_error |
| 457 | trigger_error( $error_message, E_USER_DEPRECATED ); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Register payment method integrations with the container. |
| 463 | */ |
| 464 | protected function register_payment_methods() { |
| 465 | $this->container->register( |
| 466 | Cheque::class, |
| 467 | function ( Container $container ) { |
| 468 | $asset_api = $container->get( AssetApi::class ); |
| 469 | return new Cheque( $asset_api ); |
| 470 | } |
| 471 | ); |
| 472 | $this->container->register( |
| 473 | PayPal::class, |
| 474 | function ( Container $container ) { |
| 475 | $asset_api = $container->get( AssetApi::class ); |
| 476 | return new PayPal( $asset_api ); |
| 477 | } |
| 478 | ); |
| 479 | $this->container->register( |
| 480 | BankTransfer::class, |
| 481 | function ( Container $container ) { |
| 482 | $asset_api = $container->get( AssetApi::class ); |
| 483 | return new BankTransfer( $asset_api ); |
| 484 | } |
| 485 | ); |
| 486 | $this->container->register( |
| 487 | CashOnDelivery::class, |
| 488 | function ( Container $container ) { |
| 489 | $asset_api = $container->get( AssetApi::class ); |
| 490 | return new CashOnDelivery( $asset_api ); |
| 491 | } |
| 492 | ); |
| 493 | } |
| 494 | } |
| 495 |