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