/var/www/html_us/wp-content/plugins/woocommerce/src/Blocks/Domain/Bootstrap.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
namespace Automattic\WooCommerce\Blocks\Domain;

use 
Automattic\Jetpack\Constants;
use 
Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use 
Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use 
Automattic\WooCommerce\Blocks\AssetsController;
use 
Automattic\WooCommerce\Blocks\BlockPatterns;
use 
Automattic\WooCommerce\Blocks\BlockTemplatesRegistry;
use 
Automattic\WooCommerce\Blocks\BlockTemplatesController;
use 
Automattic\WooCommerce\Blocks\BlockTypesController;
use 
Automattic\WooCommerce\Blocks\Patterns\AIPatterns;
use 
Automattic\WooCommerce\Blocks\Patterns\PatternRegistry;
use 
Automattic\WooCommerce\Blocks\Patterns\PTKClient;
use 
Automattic\WooCommerce\Blocks\Patterns\PTKPatternsStore;
use 
Automattic\WooCommerce\Blocks\QueryFilters;
use 
Automattic\WooCommerce\Blocks\Domain\Services\CreateAccount;
use 
Automattic\WooCommerce\Blocks\Domain\Services\Notices;
use 
Automattic\WooCommerce\Blocks\Domain\Services\DraftOrders;
use 
Automattic\WooCommerce\Blocks\Domain\Services\GoogleAnalytics;
use 
Automattic\WooCommerce\Blocks\Domain\Services\Hydration;
use 
Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
use 
Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsAdmin;
use 
Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsFrontend;
use 
Automattic\WooCommerce\Blocks\InboxNotifications;
use 
Automattic\WooCommerce\Blocks\Installer;
use 
Automattic\WooCommerce\Blocks\Migration;
use 
Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi;
use 
Automattic\WooCommerce\Blocks\Payments\Integrations\BankTransfer;
use 
Automattic\WooCommerce\Blocks\Payments\Integrations\CashOnDelivery;
use 
Automattic\WooCommerce\Blocks\Payments\Integrations\Cheque;
use 
Automattic\WooCommerce\Blocks\Payments\Integrations\PayPal;
use 
Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use 
Automattic\WooCommerce\Blocks\Registry\Container;
use 
Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility;
use 
Automattic\WooCommerce\StoreApi\RoutesController;
use 
Automattic\WooCommerce\StoreApi\SchemaController;
use 
Automattic\WooCommerce\StoreApi\StoreApi;
use 
Automattic\WooCommerce\Blocks\Shipping\ShippingController;
use 
Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
use 
Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility;
use 
Automattic\WooCommerce\Blocks\Domain\Services\OnboardingTasks\TasksController;
use 
Automattic\WooCommerce\Blocks\TemplateOptions;


/**
 * Takes care of bootstrapping the plugin.
 *
 * @since 2.5.0
 */
class Bootstrap {

    
/**
     * Holds the Dependency Injection Container
     *
     * @var Container
     */
    
private $container;

    
/**
     * Holds the Package instance
     *
     * @var Package
     */
    
private $package;


    
/**
     * Holds the Migration instance
     *
     * @var Migration
     */
    
private $migration;

    
/**
     * Constructor
     *
     * @param Container $container  The Dependency Injection Container.
     */
    
public function __constructContainer $container ) {
        
$this->container $container;
        
$this->package   $container->getPackage::class );
        
$this->migration $container->getMigration::class );

        
$this->init();
        
/**
         * Fires when the woocommerce blocks are loaded and ready to use.
         *
         * This hook is intended to be used as a safe event hook for when the plugin
         * has been loaded, and all dependency requirements have been met.
         *
         * To ensure blocks are initialized, you must use the `woocommerce_blocks_loaded`
         * hook instead of the `plugins_loaded` hook. This is because the functions
         * hooked into plugins_loaded on the same priority load in an inconsistent and unpredictable manner.
         *
         * @since 2.5.0
         */
        
do_action'woocommerce_blocks_loaded' );
    }

    
/**
     * Init the package - load the blocks library and define constants.
     */
    
protected function init() {
        
$this->register_dependencies();
        
$this->register_payment_methods();
        
$this->load_interactivity_api();

        
// This is just a temporary solution to make sure the migrations are run. We have to refactor this. More details: https://github.com/woocommerce/woocommerce-blocks/issues/10196.
        
if ( $this->package->get_version() !== $this->package->get_version_stored_on_db() ) {
            
$this->migration->run_migrations();
            
$this->package->set_version_stored_on_db();
        }

        
add_action(
            
'admin_init',
            function () {
                
// Delete this notification because the blocks are included in WC Core now. This will handle any sites
                // with lingering notices.
                
InboxNotifications::delete_surface_cart_checkout_blocks_notification();
            },
            
10,
            
0
        
);

        
// We need to initialize BlockTemplatesController and BlockTemplatesRegistry at the end of `after_setup_theme`
        // so themes had the opportunity to declare support for template parts.
        
add_action(
            
'after_setup_theme',
            function () {
                
$is_store_api_request wc()->is_store_api_request();

                if ( ! 
$is_store_api_request && ( wc_current_theme_is_fse_theme() || current_theme_supports'block-template-parts' ) ) ) {
                    
$this->container->getBlockTemplatesRegistry::class )->init();
                    
$this->container->getBlockTemplatesController::class )->init();
                }
            },
            
999
        
);

        
$is_rest              wc()->is_rest_api_request();
        
$is_store_api_request wc()->is_store_api_request();

        
// Load and init assets.
        
$this->container->getStoreApi::class )->init();
        
$this->container->getPaymentsApi::class )->init();
        
$this->container->getDraftOrders::class )->init();
        
$this->container->getCreateAccount::class )->init();
        
$this->container->getShippingController::class )->init();
        
$this->container->getTasksController::class )->init();
        
$this->container->getCheckoutFields::class )->init();

        
// Load assets in admin and on the frontend.
        
if ( ! $is_rest ) {
            
$this->add_build_notice();
            
$this->container->getAssetDataRegistry::class );
            
$this->container->getAssetsController::class );
            
$this->container->getInstaller::class )->init();
            
$this->container->getGoogleAnalytics::class )->init();
            
$this->container->getis_admin() ? CheckoutFieldsAdmin::class : CheckoutFieldsFrontend::class )->init();
        }

        
// Load assets unless this is a request specifically for the store API.
        
if ( ! $is_store_api_request ) {
            
// Template related functionality. These won't be loaded for store API requests, but may be loaded for
            // regular rest requests to maintain compatibility with the store editor.
            
$this->container->getAIPatterns::class );
            
$this->container->getBlockPatterns::class );
            
$this->container->getBlockTypesController::class );
            
$this->container->getClassicTemplatesCompatibility::class );
            
$this->container->getNotices::class )->init();
            
$this->container->getPTKPatternsStore::class );
            
$this->container->getTemplateOptions::class )->init();
        }

        
$this->container->getQueryFilters::class )->init();
    }

    
/**
     * See if files have been built or not.
     *
     * @return bool
     */
    
protected function is_built() {
        return 
file_exists(
            
$this->package->get_path'assets/client/blocks/featured-product.js' )
        );
    }

    
/**
     * Add a notice stating that the build has not been done yet.
     */
    
protected function add_build_notice() {
        if ( 
$this->is_built() ) {
            return;
        }
        
add_action(
            
'admin_notices',
            function () {
                echo 
'<div class="error"><p>';
                
printf(
                    
/* 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. */
                    
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' ),
                    
'<code>nvm use</code>',
                    
'<code>pnpm install</code>',
                    
'<code>pnpm --filter="@woocommerce/plugin-woocommerce" build</code>',
                    
'<code>pnpm --filter="@woocommerce/plugin-woocommerce" watch:build</code>'
                
);
                echo 
'</p></div>';
            }
        );
    }

    
/**
     * Load and set up the Interactivity API if enabled.
     */
    
protected function load_interactivity_api() {
            require_once 
__DIR__ '/../Interactivity/load.php';
    }

    
/**
     * Register core dependencies with the container.
     */
    
protected function register_dependencies() {
        
$this->container->register(
            
AssetApi::class,
            function ( 
Container $container ) {
                return new 
AssetApi$container->getPackage::class ) );
            }
        );
        
$this->container->register(
            
AssetDataRegistry::class,
            function ( 
Container $container ) {
                return new 
AssetDataRegistry$container->getAssetApi::class ) );
            }
        );
        
$this->container->register(
            
AssetsController::class,
            function ( 
Container $container ) {
                return new 
AssetsController$container->getAssetApi::class ) );
            }
        );
        
$this->container->register(
            
PaymentMethodRegistry::class,
            function () {
                return new 
PaymentMethodRegistry();
            }
        );
        
$this->container->register(
            
Installer::class,
            function () {
                return new 
Installer();
            }
        );
        
$this->container->register(
            
BlockTypesController::class,
            function ( 
Container $container ) {
                
$asset_api           $container->getAssetApi::class );
                
$asset_data_registry $container->getAssetDataRegistry::class );
                return new 
BlockTypesController$asset_api$asset_data_registry );
            }
        );
        
$this->container->register(
            
ClassicTemplatesCompatibility::class,
            function ( 
Container $container ) {
                
$asset_data_registry $container->getAssetDataRegistry::class );
                return new 
ClassicTemplatesCompatibility$asset_data_registry );
            }
        );
        
$this->container->register(
            
DraftOrders::class,
            function ( 
Container $container ) {
                return new 
DraftOrders$container->getPackage::class ) );
            }
        );
        
$this->container->register(
            
CreateAccount::class,
            function ( 
Container $container ) {
                return new 
CreateAccount$container->getPackage::class ) );
            }
        );
        
$this->container->register(
            
GoogleAnalytics::class,
            function ( 
Container $container ) {
                
$asset_api $container->getAssetApi::class );
                return new 
GoogleAnalytics$asset_api );
            }
        );
        
$this->container->register(
            
Notices::class,
            function ( 
Container $container ) {
                return new 
Notices$container->getPackage::class ) );
            }
        );
        
$this->container->register(
            
Hydration::class,
            function ( 
Container $container ) {
                return new 
Hydration$container->getAssetDataRegistry::class ) );
            }
        );
        
$this->container->register(
            
CheckoutFields::class,
            function ( 
Container $container ) {
                return new 
CheckoutFields$container->getAssetDataRegistry::class ) );
            }
        );
        
$this->container->register(
            
CheckoutFieldsAdmin::class,
            function ( 
Container $container ) {
                
$checkout_fields_controller $container->getCheckoutFields::class );
                return new 
CheckoutFieldsAdmin$checkout_fields_controller );
            }
        );
        
$this->container->register(
            
CheckoutFieldsFrontend::class,
            function ( 
Container $container ) {
                
$checkout_fields_controller $container->getCheckoutFields::class );
                return new 
CheckoutFieldsFrontend$checkout_fields_controller );
            }
        );
        
$this->container->register(
            
PaymentsApi::class,
            function ( 
Container $container ) {
                
$payment_method_registry $container->getPaymentMethodRegistry::class );
                
$asset_data_registry     $container->getAssetDataRegistry::class );
                return new 
PaymentsApi$payment_method_registry$asset_data_registry );
            }
        );
        
$this->container->register(
            
StoreApi::class,
            function () {
                return new 
StoreApi();
            }
        );
        
$this->container->register(
            
TemplateOptions::class,
            function () {
                return new 
TemplateOptions();
            }
        );
        
// Maintains backwards compatibility with previous Store API namespace.
        
$this->container->register(
            
'Automattic\WooCommerce\Blocks\StoreApi\Formatters',
            function ( 
Container $container ) {
                
$this->deprecated_dependency'Automattic\WooCommerce\Blocks\StoreApi\Formatters''6.4.0''Automattic\WooCommerce\StoreApi\Formatters''6.5.0' );
                return 
$container->getStoreApi::class )->container()->get\Automattic\WooCommerce\StoreApi\Formatters::class );
            }
        );
        
$this->container->register(
            
'Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi',
            function ( 
Container $container ) {
                
$this->deprecated_dependency'Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi''6.4.0''Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema''6.5.0' );
                return 
$container->getStoreApi::class )->container()->get\Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::class );
            }
        );
        
$this->container->register(
            
'Automattic\WooCommerce\Blocks\StoreApi\SchemaController',
            function ( 
Container $container ) {
                
$this->deprecated_dependency'Automattic\WooCommerce\Blocks\StoreApi\SchemaController''6.4.0''Automattic\WooCommerce\StoreApi\SchemaController''6.5.0' );
                return 
$container->getStoreApi::class )->container()->getSchemaController::class );
            }
        );
        
$this->container->register(
            
'Automattic\WooCommerce\Blocks\StoreApi\RoutesController',
            function ( 
Container $container ) {
                
$this->deprecated_dependency'Automattic\WooCommerce\Blocks\StoreApi\RoutesController''6.4.0''Automattic\WooCommerce\StoreApi\RoutesController''6.5.0' );
                return 
$container->getStoreApi::class )->container()->getRoutesController::class );
            }
        );
        
$this->container->register(
            
PTKClient::class,
            function () {
                return new 
PTKClient();
            }
        );
        
$this->container->register(
            
PTKPatternsStore::class,
            function () {
                return new 
PTKPatternsStore$this->container->getPTKClient::class ) );
            }
        );
        
$this->container->register(
            
BlockPatterns::class,
            function () {
                return new 
BlockPatterns(
                    
$this->package,
                    new 
PatternRegistry(),
                    
$this->container->getPTKPatternsStore::class )
                );
            }
        );
        
$this->container->register(
            
AIPatterns::class,
            function () {
                return new 
AIPatterns();
            }
        );
        
$this->container->register(
            
ShippingController::class,
            function ( 
$container ) {
                
$asset_api           $container->getAssetApi::class );
                
$asset_data_registry $container->getAssetDataRegistry::class );
                return new 
ShippingController$asset_api$asset_data_registry );
            }
        );
        
$this->container->register(
            
TasksController::class,
            function () {
                return new 
TasksController();
            }
        );
        
$this->container->register(
            
QueryFilters::class,
            function () {
                return new 
QueryFilters();
            }
        );
        
$this->container->register(
            
BlockTemplatesRegistry::class,
            function () {
                return new 
BlockTemplatesRegistry();
            }
        );
        
$this->container->register(
            
BlockTemplatesController::class,
            function () {
                return new 
BlockTemplatesController();
            }
        );
    }

    
/**
     * Throws a deprecation notice for a dependency without breaking requests.
     *
     * @param string $function Class or function being deprecated.
     * @param string $version Version in which it was deprecated.
     * @param string $replacement Replacement class or function, if applicable.
     * @param string $trigger_error_version Optional version to start surfacing this as a PHP error rather than a log. Defaults to $version.
     */
    
protected function deprecated_dependency$function$version$replacement ''$trigger_error_version '' ) {
        if ( ! 
defined'WP_DEBUG' ) || ! WP_DEBUG ) {
            return;
        }

        
$trigger_error_version $trigger_error_version $trigger_error_version $version;
        
$error_message         $replacement sprintf(
            
'%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
            
$function,
            
$version,
            
$replacement
        
) : sprintf(
            
'%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.',
            
$function,
            
$version
        
);
        
/**
         * Fires when a deprecated function is called.
         *
         * @since 7.3.0
         */
        
do_action'deprecated_function_run'$function$replacement$version );

        
$log_error false;

        
// If headers have not been sent yet, log to avoid breaking the request.
        
if ( ! headers_sent() ) {
            
$log_error true;
        }

        
// If the $trigger_error_version was not yet reached, only log the error.
        
if ( version_compareConstants::get_constant'WC_VERSION' ), $trigger_error_version'<' ) ) {
            
$log_error true;
        }

        
/**
         * Filters whether to trigger an error for deprecated functions. (Same as WP core)
         *
         * @since 7.3.0
         *
         * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
         */
        
if ( ! apply_filters'deprecated_function_trigger_error'true ) ) {
            
$log_error true;
        }

        if ( 
$log_error ) {
            
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            
error_log$error_message );
        } else {
            
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
            
trigger_error$error_messageE_USER_DEPRECATED );
        }
    }

    
/**
     * Register payment method integrations with the container.
     */
    
protected function register_payment_methods() {
        
$this->container->register(
            
Cheque::class,
            function ( 
Container $container ) {
                
$asset_api $container->getAssetApi::class );
                return new 
Cheque$asset_api );
            }
        );
        
$this->container->register(
            
PayPal::class,
            function ( 
Container $container ) {
                
$asset_api $container->getAssetApi::class );
                return new 
PayPal$asset_api );
            }
        );
        
$this->container->register(
            
BankTransfer::class,
            function ( 
Container $container ) {
                
$asset_api $container->getAssetApi::class );
                return new 
BankTransfer$asset_api );
            }
        );
        
$this->container->register(
            
CashOnDelivery::class,
            function ( 
Container $container ) {
                
$asset_api $container->getAssetApi::class );
                return new 
CashOnDelivery$asset_api );
            }
        );
    }
}