-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start implementing class for installing WooCommerce Hosted Services * Add WooCommerce Services JITM for US and Canada-based merchants. * Remove check for testing class. * Remove “Hello World” notice. * Hook up “Install WooCommerce Services” button. * Add WooCommerce-specific JITM emblem. * Add a nonce to the WC Services install link, use “safe” redirect method after install. * Show an error message if the WooCommerce Services install fails, change entry hook to `admin_init`. * Don’t open a new tab when installing WooCommerce Services from JITM. * Remove usage of `filter_input()` in favor of the `$_GET` super global. * Ensure that JITMs are only shown to users who can install plugins. * Add a truthy return to WC Services installation method. * Prune unnecessary variable usage. * No need to explicitly call Jetpack::do_stats(). * Use the singleton pattern to allow for easier unit testing. Move hook attachment to constructor. * Use a switch statement for WC Services JITM country detection, allowing for easier addition of future target markets. * Explicitly base the post-install redirect on the referring URL. * Update WooCommerce Services plugin slug for install and activation. * Add PHPDoc comments to WooCommerce Services installer class.
- Loading branch information
1 parent
6a57df2
commit 0fe1b5f
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
class WC_Services_Installer { | ||
|
||
/** | ||
* @var WC_Services_Installer | ||
**/ | ||
private static $instance = null; | ||
|
||
static function init() { | ||
if ( is_null( self::$instance ) ) { | ||
self::$instance = new WC_Services_Installer(); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
public function __construct() { | ||
add_action( 'admin_init', array( $this, 'add_error_notice' ) ); | ||
add_action( 'admin_init', array( $this, 'try_install' ) ); | ||
} | ||
|
||
/** | ||
* Verify the intent to install WooCommerce Services, and kick off installation. | ||
*/ | ||
public function try_install() { | ||
if ( isset( $_GET['wc-services-action'] ) && ( 'install' === $_GET['wc-services-action'] ) ) { | ||
check_admin_referer( 'wc-services-install' ); | ||
|
||
if ( ! current_user_can( 'install_plugins' ) ) { | ||
return; | ||
} | ||
|
||
$result = $this->install(); | ||
$redirect = wp_get_referer(); | ||
|
||
if ( false === $result ) { | ||
$redirect = add_query_arg( 'wc-services-install-error', true, $redirect ); | ||
} | ||
|
||
wp_safe_redirect( $redirect ); | ||
|
||
exit; | ||
} | ||
} | ||
|
||
/** | ||
* Set up installation error admin notice. | ||
*/ | ||
public function add_error_notice() { | ||
if ( ! empty( $_GET['wc-services-install-error'] ) ) { | ||
add_action( 'admin_notices', array( $this, 'error_notice' ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Notify the user that the installation of WooCommerce Services failed. | ||
*/ | ||
public function error_notice() { | ||
?> | ||
<div class="notice notice-error is-dismissible"> | ||
<p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p> | ||
</div> | ||
<?php | ||
} | ||
|
||
/** | ||
* Download, install, and activate the WooCommerce Services plugin. | ||
* | ||
* @return bool result of installation/activation | ||
*/ | ||
private function install() { | ||
include_once( ABSPATH . '/wp-admin/includes/admin.php' ); | ||
include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' ); | ||
include_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | ||
include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' ); | ||
include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' ); | ||
|
||
$api = plugins_api( 'plugin_information', array( 'slug' => 'woocommerce-services' ) ); | ||
|
||
if ( is_wp_error( $api ) ) { | ||
return false; | ||
} | ||
|
||
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); | ||
$result = $upgrader->install( $api->download_link ); | ||
|
||
if ( true !== $result ) { | ||
return false; | ||
} | ||
|
||
$result = activate_plugin( 'woocommerce-services/woocommerce-services.php' ); | ||
|
||
// activate_plugin() returns null on success | ||
return is_null( $result ); | ||
} | ||
} | ||
|
||
WC_Services_Installer::init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters