Skip to content

Commit 8d2caa9

Browse files
authored
Merge pull request #231 from moderntribe/feature/site-201/shipping-card-connections
Remove Shipping Wizard and migrate plugin installation to Shipping Card
2 parents 8a5ae28 + 0d2fdb4 commit 8d2caa9

File tree

5 files changed

+67
-175
lines changed

5 files changed

+67
-175
lines changed

plugins/wme-sitebuilder/wme-sitebuilder/Cards/Shipping.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Tribe\WME\Sitebuilder\Cards;
44

5+
use Tribe\WME\Sitebuilder\Concerns\InvokesCli;
56
use Tribe\WME\Sitebuilder\Plugins\Shipping as ShippingPlugins;
67

78
class Shipping extends Card {
89

10+
use InvokesCli;
11+
912
/**
1013
* @var string
1114
*/
@@ -30,6 +33,67 @@ public function __construct( ShippingPlugins $shipping_plugins ) {
3033
$this->plugins = $shipping_plugins;
3134

3235
parent::__construct();
36+
37+
add_action( 'admin_init', [$this, 'handle_plugin_install_request'], 2 );
38+
}
39+
40+
/**
41+
* Install requested plugins.
42+
*/
43+
public function handle_plugin_install_request() {
44+
// Ensure this is a request to admin and also not an ajax request.
45+
if ( ! is_admin() || wp_doing_ajax() ) {
46+
return;
47+
}
48+
49+
// Ensure this is a request to install a plugin.
50+
if ( ! isset( $_GET['page'] ) || 'sitebuilder-store-details' !== $_GET['page'] ) {
51+
return;
52+
}
53+
54+
if ( ! isset( $_GET['action'] ) || 'install-plugin' !== $_GET['action'] ) {
55+
return;
56+
}
57+
58+
// Ensure user has permission to install plugins.
59+
if ( ! current_user_can( 'install_plugins' ) ) {
60+
wp_die( __( 'You are not authorized to install plugins.', 'wme-sitebuilder' ) );
61+
}
62+
63+
$requested_plugin = sanitize_text_field( $_GET['plugin'] );
64+
65+
if ( null === $requested_plugin ) {
66+
wp_die( __( 'You are not authorized to install plugins.', 'wme-sitebuilder' ) );
67+
}
68+
69+
$supported_plugins = $this->plugins->getPlugins();
70+
71+
if ( ! isset( $supported_plugins[ $requested_plugin ] ) ) {
72+
wp_die( __( 'The requested plugin is not supported by this installer.', 'wme-sitebuilder' ) );
73+
}
74+
75+
if ( ! empty( $supported_plugins[ $requested_plugin ]['active'] ) ) {
76+
wp_die( __( 'The requested plugin is already installed and active.', 'wme-sitebuilder' ) );
77+
}
78+
79+
$response = $this->makeCommand('wp plugin install', [
80+
$requested_plugin,
81+
'--activate',
82+
])->execute();
83+
84+
if ( ! $response->wasSuccessful() ) {
85+
wp_die(
86+
sprintf(
87+
/* Translators: %1$s is the exit code from WP CLI; %2$s is output from WP CLI. */
88+
__( 'Encountered WP CLI exit code "%1$s" with output "%2$s".', 'wme-sitebuilder' ),
89+
sanitize_text_field( $response->getExitCode() ),
90+
sanitize_text_field( $response->getOutput() )
91+
)
92+
);
93+
}
94+
95+
// Redirect user back to the shipping card.
96+
wp_safe_redirect( $supported_plugins[ $requested_plugin ]['activation_redirect'] );
3397
}
3498

3599
/**
@@ -43,7 +107,7 @@ public function props() {
43107
'label' => __( 'Add USPS', 'wme-sitebuilder' ),
44108
'backgroundColor' => '#004B87',
45109
// TODO: Redirect user to an action that installs and activates the plugin.
46-
'href' => admin_url( 'admin.php?page=wc-settings&tab=shipping' ),
110+
'href' => admin_url( 'admin.php?page=sitebuilder-store-details&action=install-plugin&plugin=elex-usps-shipping-method' ),
47111
]
48112
];
49113

@@ -61,7 +125,7 @@ public function props() {
61125
'id' => 'usps',
62126
'type' => 'task',
63127
'taskCta' => __( 'USPS Settings', 'wme-sitebuilder' ),
64-
'title' => __( 'USPS', 'wme-sitebuilder' ),
128+
'title' => ! $this->plugins->isUspsActive() ? __( 'Add USPS Shipping', 'wme-sitebuilder' ) : __( 'USPS Shipping', 'wme-sitebuilder' ),
65129
'intro' => __( 'Shipping rates based on address and cart content through USPS.', 'wme-sitebuilder' ),
66130
'disabled' => false,
67131
'disableText' => '',

plugins/wme-sitebuilder/wme-sitebuilder/Container.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ public function config() {
166166
$app->make( PluginInstaller::class )
167167
);
168168
},
169-
Wizards\Shipping::class => static function ( $app ) {
170-
return new Wizards\Shipping(
171-
$app->make( Plugins\Shipping::class )
172-
);
173-
},
174169
Wizards\StoreSetup::class => null,
175170

176171
// Implementations of external interfaces.

plugins/wme-sitebuilder/wme-sitebuilder/Modules/StoreDetails.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Tribe\WME\Sitebuilder\Plugins\PaymentGateways\Stripe;
1212
use Tribe\WME\Sitebuilder\Wizards\PaymentGatewayPayPal;
1313
use Tribe\WME\Sitebuilder\Wizards\PaymentGatewayStripe;
14-
use Tribe\WME\Sitebuilder\Wizards\Shipping as ShippingWizard;
1514
use Tribe\WME\Sitebuilder\Wizards\StoreSetup as StoreSetupWizard;
1615

1716
class StoreDetails extends Module implements LoadsConditionally {
@@ -77,7 +76,6 @@ public function setup() {
7776

7877
$this->wizards = [
7978
$this->factory->make( StoreSetupWizard::class ),
80-
$this->factory->make( ShippingWizard::class ),
8179
];
8280

8381
$this->addPaymentCardsWizards();

plugins/wme-sitebuilder/wme-sitebuilder/Plugins/Shipping.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct() {
2424
*/
2525
$this->plugins['elex-usps-shipping-method'] = [
2626
'active' => (bool) $this->isPluginActive( 'elex-usps-shipping-method/usps-woocommerce-shipping.php' ),
27+
'activation_redirect' => admin_url( 'admin.php?page=wc-settings&tab=shipping&section=elex_shipping_usps' ),
2728
];
2829
}
2930

plugins/wme-sitebuilder/wme-sitebuilder/Wizards/Shipping.php

Lines changed: 0 additions & 166 deletions
This file was deleted.

0 commit comments

Comments
 (0)