Skip to content

Commit 5ff420b

Browse files
authored
Merge pull request #3643 from woocommerce/add-woocommerce-blueprint-support
Add WooCommerce Blueprints support (5238)
2 parents 61e4d36 + b04f5ec commit 5ff420b

12 files changed

Lines changed: 704 additions & 1 deletion

File tree

modules/ppcp-compat/services.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use WooCommerce\PayPalCommerce\Assets\AssetGetter;
1313
use WooCommerce\PayPalCommerce\Assets\AssetGetterFactory;
1414
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
15+
use WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint\PayPalBlueprintBootstrap;
16+
use WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint\PayPalSettingsExporter;
17+
use WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint\PayPalSettingsImporter;
1518
use WooCommerce\PayPalCommerce\Settings\Data\SettingsProvider;
1619
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
1720

@@ -115,4 +118,20 @@
115118
$container->get( 'api.bearer' )
116119
);
117120
},
121+
122+
'compat.blueprint.is_available' => function (): bool {
123+
return interface_exists( 'Automattic\WooCommerce\Blueprint\Exporters\StepExporter' );
124+
},
125+
'compat.blueprint.paypal_settings_exporter' => static function ( ContainerInterface $container ): PayPalSettingsExporter {
126+
return new PayPalSettingsExporter();
127+
},
128+
'compat.blueprint.paypal_settings_importer' => static function ( ContainerInterface $container ): PayPalSettingsImporter {
129+
return new PayPalSettingsImporter();
130+
},
131+
'compat.blueprint.bootstrap' => static function ( ContainerInterface $container ): PayPalBlueprintBootstrap {
132+
return new PayPalBlueprintBootstrap(
133+
$container->get( 'compat.blueprint.paypal_settings_exporter' ),
134+
$container->get( 'compat.blueprint.paypal_settings_importer' )
135+
);
136+
},
118137
);

modules/ppcp-compat/src/CompatModule.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ function () use ( $c ) {
8080
$this->initialize_wc_bookings_compat_layer( $c );
8181
}
8282

83+
$this->initialize_blueprint_compat_layer( $c );
84+
8385
add_action( 'woocommerce_paypal_payments_gateway_migrate', static fn() => delete_transient( 'ppcp_has_ppec_subscriptions' ) );
8486

8587
$this->legacy_ui_card_payment_mapping( $c );
@@ -543,6 +545,22 @@ static function ( WC_Order $wc_order, CartData $cart_data ) use ( $container ):
543545
);
544546
}
545547

548+
/**
549+
* Sets up the WooCommerce Blueprint compatibility layer.
550+
*
551+
* @param ContainerInterface $container The Container.
552+
* @return void
553+
*/
554+
private function initialize_blueprint_compat_layer( ContainerInterface $container ): void {
555+
$is_blueprint_available = $container->get( 'compat.blueprint.is_available' );
556+
if ( ! $is_blueprint_available ) {
557+
return;
558+
}
559+
560+
$blueprint_bootstrap = $container->get( 'compat.blueprint.bootstrap' );
561+
$blueprint_bootstrap->init();
562+
}
563+
546564
/**
547565
* Responsible to keep the credit card payment configuration backwards
548566
* compatible with the legacy UI.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* PayPal Blueprint Bootstrap - Registers exporters and importers.
4+
*
5+
* @package WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint;
11+
12+
/**
13+
* Bootstrap class for PayPal Blueprint functionality.
14+
*/
15+
class PayPalBlueprintBootstrap {
16+
17+
/**
18+
* PayPal Settings Exporter instance.
19+
*
20+
* @var PayPalSettingsExporter
21+
*/
22+
private PayPalSettingsExporter $exporter;
23+
24+
/**
25+
* PayPal Settings Importer instance.
26+
*
27+
* @var PayPalSettingsImporter
28+
*/
29+
private PayPalSettingsImporter $importer;
30+
31+
/**
32+
* Constructor.
33+
*
34+
* @param PayPalSettingsExporter $exporter PayPal settings exporter.
35+
* @param PayPalSettingsImporter $importer PayPal settings importer.
36+
*/
37+
public function __construct(
38+
PayPalSettingsExporter $exporter,
39+
PayPalSettingsImporter $importer
40+
) {
41+
$this->exporter = $exporter;
42+
$this->importer = $importer;
43+
}
44+
45+
/**
46+
* Initialize the PayPal Blueprint functionality.
47+
*
48+
* @return void
49+
*/
50+
public function init(): void {
51+
$this->register_hooks();
52+
}
53+
54+
/**
55+
* Register WordPress hooks.
56+
*
57+
* @return void
58+
*/
59+
private function register_hooks(): void {
60+
add_filter( 'wooblueprint_exporters', array( $this, 'register_exporters' ) );
61+
add_filter( 'wooblueprint_importers', array( $this, 'register_importers' ) );
62+
}
63+
64+
/**
65+
* Register PayPal exporters.
66+
*
67+
* @param array $exporters Existing exporters.
68+
* @return array
69+
*/
70+
public function register_exporters( array $exporters ): array {
71+
$exporters[] = $this->exporter;
72+
return $exporters;
73+
}
74+
75+
/**
76+
* Register PayPal importers.
77+
*
78+
* @param array $importers Existing importers.
79+
* @return array
80+
*/
81+
public function register_importers( array $importers ): array {
82+
$importers[] = $this->importer;
83+
return $importers;
84+
}
85+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* PayPal Settings Blueprint Exporter.
4+
*
5+
* @package WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WooCommerce\PayPalCommerce\Compat\WooCommerceBlueprint;
11+
12+
use Automattic\WooCommerce\Blueprint\Exporters\StepExporter;
13+
use Automattic\WooCommerce\Blueprint\Exporters\HasAlias;
14+
use Automattic\WooCommerce\Blueprint\Steps\SetSiteOptions;
15+
use Automattic\WooCommerce\Blueprint\Steps\Step;
16+
17+
/**
18+
* PayPal Settings Exporter for WooCommerce Blueprint.
19+
*/
20+
class PayPalSettingsExporter implements StepExporter, HasAlias {
21+
22+
/**
23+
* Sentinel value to detect if option doesn't exist.
24+
*/
25+
private const OPTION_NOT_FOUND = '__PAYPAL_OPTION_NOT_FOUND__';
26+
27+
/**
28+
* PayPal-related options to export (excluding transients and plugin metadata).
29+
*
30+
* @var array<string>
31+
*/
32+
private const PAYPAL_OPTIONS = array(
33+
// Core PPCP data settings (new settings).
34+
'woocommerce-ppcp-data-common',
35+
'woocommerce-ppcp-data-onboarding',
36+
'woocommerce-ppcp-data-payment',
37+
'woocommerce-ppcp-data-settings',
38+
'woocommerce-ppcp-data-styling',
39+
// Legacy settings (maintained for backward compatibility during migration).
40+
'woocommerce-ppcp-settings',
41+
// Merchant state flags.
42+
'woocommerce-ppcp-is-new-merchant',
43+
// UI and migration state flags (prevent re-migration and control UI display).
44+
'woocommerce_ppcp-settings-should-use-old-ui',
45+
'woocommerce_ppcp-is_pay_later_settings_migrated',
46+
'woocommerce_ppcp-is_smart_button_settings_migrated',
47+
// Individual payment method settings (gateway titles/descriptions).
48+
'woocommerce_venmo_settings',
49+
'woocommerce_pay-later_settings',
50+
);
51+
52+
/**
53+
* Export PayPal settings.
54+
*
55+
* @return Step
56+
*/
57+
public function export(): Step {
58+
$paypal_options = array();
59+
60+
foreach ( self::PAYPAL_OPTIONS as $option_name ) {
61+
$value = get_option( $option_name, self::OPTION_NOT_FOUND );
62+
if ( self::OPTION_NOT_FOUND !== $value ) {
63+
$paypal_options[ $option_name ] = $value;
64+
}
65+
}
66+
67+
return new SetSiteOptions( $paypal_options );
68+
}
69+
70+
/**
71+
* Get step name.
72+
*
73+
* @return string
74+
*/
75+
public function get_step_name(): string {
76+
return SetSiteOptions::get_step_name();
77+
}
78+
79+
/**
80+
* Get alias for this exporter.
81+
*
82+
* @return string
83+
*/
84+
public function get_alias(): string {
85+
return 'paypalSettings';
86+
}
87+
88+
/**
89+
* Return label used in the frontend.
90+
*
91+
* @return string
92+
*/
93+
public function get_label(): string {
94+
return __( 'PayPal Settings', 'woocommerce-paypal-payments' );
95+
}
96+
97+
/**
98+
* Return the description used in the frontend.
99+
*
100+
* @return string
101+
*/
102+
public function get_description(): string {
103+
return __( 'Exports PayPal Payments settings and configuration options.', 'woocommerce-paypal-payments' );
104+
}
105+
106+
/**
107+
* Check if user has capability to export PayPal settings.
108+
*
109+
* @return bool
110+
*/
111+
public function check_step_capabilities(): bool {
112+
return current_user_can( 'manage_woocommerce' ) && current_user_can( 'manage_options' );
113+
}
114+
}

0 commit comments

Comments
 (0)