Skip to content

Commit

Permalink
Merge pull request #231 from moderntribe/feature/site-201/shipping-ca…
Browse files Browse the repository at this point in the history
…rd-connections

Remove Shipping Wizard and migrate plugin installation to Shipping Card
  • Loading branch information
bswatson authored Apr 17, 2023
2 parents 8a5ae28 + 0d2fdb4 commit 8d2caa9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 175 deletions.
68 changes: 66 additions & 2 deletions plugins/wme-sitebuilder/wme-sitebuilder/Cards/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Tribe\WME\Sitebuilder\Cards;

use Tribe\WME\Sitebuilder\Concerns\InvokesCli;
use Tribe\WME\Sitebuilder\Plugins\Shipping as ShippingPlugins;

class Shipping extends Card {

use InvokesCli;

/**
* @var string
*/
Expand All @@ -30,6 +33,67 @@ public function __construct( ShippingPlugins $shipping_plugins ) {
$this->plugins = $shipping_plugins;

parent::__construct();

add_action( 'admin_init', [$this, 'handle_plugin_install_request'], 2 );
}

/**
* Install requested plugins.
*/
public function handle_plugin_install_request() {
// Ensure this is a request to admin and also not an ajax request.
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}

// Ensure this is a request to install a plugin.
if ( ! isset( $_GET['page'] ) || 'sitebuilder-store-details' !== $_GET['page'] ) {
return;
}

if ( ! isset( $_GET['action'] ) || 'install-plugin' !== $_GET['action'] ) {
return;
}

// Ensure user has permission to install plugins.
if ( ! current_user_can( 'install_plugins' ) ) {
wp_die( __( 'You are not authorized to install plugins.', 'wme-sitebuilder' ) );
}

$requested_plugin = sanitize_text_field( $_GET['plugin'] );

if ( null === $requested_plugin ) {
wp_die( __( 'You are not authorized to install plugins.', 'wme-sitebuilder' ) );
}

$supported_plugins = $this->plugins->getPlugins();

if ( ! isset( $supported_plugins[ $requested_plugin ] ) ) {
wp_die( __( 'The requested plugin is not supported by this installer.', 'wme-sitebuilder' ) );
}

if ( ! empty( $supported_plugins[ $requested_plugin ]['active'] ) ) {
wp_die( __( 'The requested plugin is already installed and active.', 'wme-sitebuilder' ) );
}

$response = $this->makeCommand('wp plugin install', [
$requested_plugin,
'--activate',
])->execute();

if ( ! $response->wasSuccessful() ) {
wp_die(
sprintf(
/* Translators: %1$s is the exit code from WP CLI; %2$s is output from WP CLI. */
__( 'Encountered WP CLI exit code "%1$s" with output "%2$s".', 'wme-sitebuilder' ),
sanitize_text_field( $response->getExitCode() ),
sanitize_text_field( $response->getOutput() )
)
);
}

// Redirect user back to the shipping card.
wp_safe_redirect( $supported_plugins[ $requested_plugin ]['activation_redirect'] );
}

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

Expand All @@ -61,7 +125,7 @@ public function props() {
'id' => 'usps',
'type' => 'task',
'taskCta' => __( 'USPS Settings', 'wme-sitebuilder' ),
'title' => __( 'USPS', 'wme-sitebuilder' ),
'title' => ! $this->plugins->isUspsActive() ? __( 'Add USPS Shipping', 'wme-sitebuilder' ) : __( 'USPS Shipping', 'wme-sitebuilder' ),
'intro' => __( 'Shipping rates based on address and cart content through USPS.', 'wme-sitebuilder' ),
'disabled' => false,
'disableText' => '',
Expand Down
5 changes: 0 additions & 5 deletions plugins/wme-sitebuilder/wme-sitebuilder/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ public function config() {
$app->make( PluginInstaller::class )
);
},
Wizards\Shipping::class => static function ( $app ) {
return new Wizards\Shipping(
$app->make( Plugins\Shipping::class )
);
},
Wizards\StoreSetup::class => null,

// Implementations of external interfaces.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Tribe\WME\Sitebuilder\Plugins\PaymentGateways\Stripe;
use Tribe\WME\Sitebuilder\Wizards\PaymentGatewayPayPal;
use Tribe\WME\Sitebuilder\Wizards\PaymentGatewayStripe;
use Tribe\WME\Sitebuilder\Wizards\Shipping as ShippingWizard;
use Tribe\WME\Sitebuilder\Wizards\StoreSetup as StoreSetupWizard;

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

$this->wizards = [
$this->factory->make( StoreSetupWizard::class ),
$this->factory->make( ShippingWizard::class ),
];

$this->addPaymentCardsWizards();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct() {
*/
$this->plugins['elex-usps-shipping-method'] = [
'active' => (bool) $this->isPluginActive( 'elex-usps-shipping-method/usps-woocommerce-shipping.php' ),
'activation_redirect' => admin_url( 'admin.php?page=wc-settings&tab=shipping&section=elex_shipping_usps' ),
];
}

Expand Down
166 changes: 0 additions & 166 deletions plugins/wme-sitebuilder/wme-sitebuilder/Wizards/Shipping.php

This file was deleted.

0 comments on commit 8d2caa9

Please sign in to comment.