22
33namespace Tribe \WME \Sitebuilder \Cards ;
44
5+ use Tribe \WME \Sitebuilder \Concerns \InvokesCli ;
56use Tribe \WME \Sitebuilder \Plugins \Shipping as ShippingPlugins ;
67
78class 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 ' => '' ,
0 commit comments