Skip to content

Commit

Permalink
JITM: Detect when WooCommerce Services is installed, but not active (#…
Browse files Browse the repository at this point in the history
…6382)

* Make "yarn build-client" work on Windows. The full path "./node_modules/.bin/something" is not needed in NPM scripts.

* Detect when the WooCommerce Services plugin is installed but deactivated, and change the copy & behaviour of the JITM to activate only instead of install & activate

* Changed get_plugins() call for the more efficient validate_plugin(). Fixed the title attr for the JITM when the action is "activate plugin"
  • Loading branch information
DanReyLop authored and singerb committed Feb 20, 2017
1 parent 8f63395 commit e19683d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.DS_Store
languages/messages.pot
.idea
*.iml
npm-debug.log
tests/php/files/jetpack-150x150.jpg
*.unison.tmp
Expand Down
66 changes: 43 additions & 23 deletions 3rd-party/woocommerce-services.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,43 @@ public function __construct() {
* 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;
}
if ( ! isset( $_GET['wc-services-action'] ) ) {
return;
}
check_admin_referer( 'wc-services-install' );

$result = false;

switch ( $_GET['wc-services-action'] ) {
case 'install':
if ( current_user_can( 'install_plugins' ) ) {
$this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION );
$result = $this->install();
if ( $result ) {
$result = $this->activate();
}
}
break;

case 'activate':
if ( current_user_can( 'activate_plugins' ) ) {
$this->jetpack->stat( 'jitm', 'wooservices-activate-' . JETPACK__VERSION );
$result = $this->activate();
}
break;
}

$result = $this->install();
$redirect = wp_get_referer();
$redirect = wp_get_referer();

if ( false === $result ) {
$redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
} else {
$this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION );
}
if ( $result ) {
$this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION );
} else {
$redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
}

wp_safe_redirect( $redirect );
wp_safe_redirect( $redirect );

exit;
}
exit;
}

/**
Expand All @@ -77,13 +94,11 @@ public function error_notice() {
}

/**
* Download, install, and activate the WooCommerce Services plugin.
* Download and install the WooCommerce Services plugin.
*
* @return bool result of installation/activation
* @return bool result of installation
*/
private function install() {
$this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION );

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' );
Expand All @@ -99,10 +114,15 @@ private function install() {
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
$result = $upgrader->install( $api->download_link );

if ( true !== $result ) {
return false;
}
return true === $result;
}

/**
* Activate the WooCommerce Services plugin.
*
* @return bool result of activation
*/
private function activate() {
$result = activate_plugin( 'woocommerce-services/woocommerce-services.php' );

// activate_plugin() returns null on success
Expand Down
13 changes: 9 additions & 4 deletions class.jetpack-jitm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ private function __construct() {
}
add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
}

function get_emblem()
{
return '<div class="jp-emblem">' . Jetpack::get_jp_emblem() . '</div>';
}

/**
* Prepare actions according to screen and post type.
*
Expand Down Expand Up @@ -457,7 +457,10 @@ function woocommerce_services_msg() {
return;
}

$install_url = wp_nonce_url( add_query_arg( array( 'wc-services-action' => 'install' ) ), 'wc-services-install' );
// If plugin dir exists, means it's installed but not activated
$already_installed = ( 0 === validate_plugin( 'woocommerce-services/woocommerce-services.php' ) );

$install_url = wp_nonce_url( add_query_arg( array( 'wc-services-action' => $already_installed ? 'activate' : 'install' ) ), 'wc-services-install' );

?>
<div class="jp-jitm woo-jitm">
Expand All @@ -471,7 +474,9 @@ function woocommerce_services_msg() {
<?php echo esc_html( $message ); ?>
</p>
<p>
<a href="<?php echo esc_url( $install_url ); ?>" title="<?php esc_attr_e( 'Install WooCommerce Services', 'jetpack' ); ?>" data-module="wooservices" class="button button-jetpack show-after-enable"><?php esc_html_e( 'Install WooCommerce Services', 'jetpack' ); ?></a>
<a href="<?php echo esc_url( $install_url ); ?>" title="<?php $already_installed ? esc_attr_e( 'Activate WooCommerce Services', 'jetpack' ) : esc_attr_e( 'Install WooCommerce Services', 'jetpack' ); ?>" data-module="wooservices" class="button button-jetpack show-after-enable">
<?php $already_installed ? esc_html_e( 'Activate WooCommerce Services', 'jetpack' ) : esc_html_e( 'Install WooCommerce Services', 'jetpack' ); ?>
</a>
</p>
</div>
<?php
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"clean-client": "rm -rf _inc/build/*.js _inc/build/*.css _inc/build/*.map _inc/build/*.html",
"distclean": "rm -rf node_modules && yarn cache clean",
"build": "yarn && yarn build-client",
"build-client": "./node_modules/.bin/gulp",
"build-production": "yarn clean-client && yarn build-languages && ./node_modules/.bin/gulp languages:extract && NODE_ENV=production BABEL_ENV=production yarn build",
"build-languages": "./node_modules/.bin/gulp languages",
"build-client": "gulp",
"build-production": "yarn clean-client && yarn build-languages && gulp languages:extract && NODE_ENV=production BABEL_ENV=production yarn build",
"build-languages": "gulp languages",
"lint": "eslint _inc/client -c .eslintrc",
"test-client": "NODE_ENV=test NODE_PATH=tests:_inc/client:node_modules/@automattic/dops-components/client tests/runner.js",
"add-textdomain": "./node_modules/.bin/grunt addtextdomain",
"build-pot": "./node_modules/.bin/grunt makepot",
"add-textdomain": "grunt addtextdomain",
"build-pot": "grunt makepot",
"test-gui": "NODE_ENV=test NODE_PATH=tests:_inc/client:node_modules/@automattic/dops-components/client tests/runner.js gui"
},
"dependencies": {
Expand Down

0 comments on commit e19683d

Please sign in to comment.