Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display alert on WP Admin if Jetpack is not connected #391

Merged
merged 8 commits into from
Feb 18, 2020
77 changes: 52 additions & 25 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,13 @@ public static function check_plugin_dependencies( $silent ) {
return true;
}

// TODO - Remove/update when Jetpack Connection package is all we need.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we moved this todo to an issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't found any specific issue on that topic but I guess it should be part of #111 epic. Would you prefer creating a particular issue for this and adding TODO comment to the wc-payments-http class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will work well, as long as we don't forget about this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TODO with f4639fa

if ( ! self::check_for_jetpack_layer() ) {
if ( ! $silent ) {
$message = sprintf(
/* translators: %1: WooCommerce Payments version */
__( 'WooCommerce Payments %1$s requires Jetpack. Please install, activate, and connect Jetpack. This dependency will change in an upcoming release.', 'woocommerce-payments' ),
WCPAY_VERSION_NUMBER
);

self::display_admin_error( $message );
}

return false;
};

$plugin_headers = self::get_plugin_headers();

// Do not show alerts while installing plugins.
if ( ! $silent && self::is_at_plugin_install_page() ) {
return true;
}

$wc_version = $plugin_headers['WCRequires'];
$wp_version = $plugin_headers['RequiresWP'];

Expand All @@ -172,6 +162,12 @@ public static function check_plugin_dependencies( $silent ) {
'slug' => 'woocommerce-admin',
'file' => 'woocommerce-admin/woocommerce-admin.php',
),
array(
'name' => 'Jetpack',
'class' => 'Jetpack',
'slug' => 'jetpack',
'file' => 'jetpack/jetpack.php',
),
);

// Check if WooCommerce and other dependencies are installed and active.
Expand Down Expand Up @@ -242,23 +238,54 @@ public static function check_plugin_dependencies( $silent ) {
return false;
}

// Check if Jetpack is connected.
if ( ! self::is_jetpack_connected() ) {
// Do not show an alert on Jetpack admin pages.
if ( ! $silent && ! self::is_at_jetpack_admin_page() ) {
$set_up_url = wp_nonce_url( 'admin.php?page=jetpack' );
$message = sprintf(
/* translators: %1: WooCommerce Payments version, %2: Jetpack setup url */
__( 'To use WooCommerce Payments %1$s you\'ll need to <a href="%2$s">set up</a> the Jetpack plugin.', 'woocommerce-payments' ),
WCPAY_VERSION_NUMBER,
$set_up_url
);
self::display_admin_error( $message );
}

return false;
}

return true;
}

/**
* Checks whether either Jetpack transport is available and displays an admin error message if not.
* Checks if current page is plugin installation process page.
*
* @return bool true if either Jetpack transport is available, false otherwise.
* @return bool True when installing plugin.
*/
public static function check_for_jetpack_layer() {
if ( class_exists( 'Automattic\Jetpack\Connection\Client' ) ) {
return true;
}
if ( class_exists( 'Jetpack_Client' ) ) {
return true;
}
private static function is_at_plugin_install_page() {
$cur_screen = get_current_screen();
return 'update' === $cur_screen->id && 'plugins' === $cur_screen->parent_base;
}

return false;
/**
* Checks if current page is Jetpack admin page.
*
* @return bool True when current page is one of the Jetpack admin pages.
*/
private static function is_at_jetpack_admin_page() {
$cur_screen = get_current_screen();
return 'jetpack' === $cur_screen->parent_base;
}

/**
* Checks if Jetpack is connected.
*
* @return bool true if Jetpack connection is available and authenticated.
*/
public static function is_jetpack_connected() {
require_once dirname( __FILE__ ) . '/wc-payment-api/class-wc-payments-http.php';
return WC_Payments_Http::is_connected();
}

/**
Expand Down
12 changes: 12 additions & 0 deletions includes/wc-payment-api/class-wc-payments-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public function remote_request( $args, $body = null, $is_site_specific = true )
return Jetpack_Client::remote_request( $args, $body );
}
}

/**
* Checks if Jetpack is connected.
*
* Checks if connection is authenticated in the same way as Jetpack_Client or Jetpack Connection Client does.
*
* @return bool true if Jetpack connection has access token.
*/
public static function is_connected() {
return ( class_exists( 'Automattic\Jetpack\Connection\Client' ) && ( new Automattic\Jetpack\Connection\Manager() )->get_access_token() )
|| ( class_exists( 'Jetpack_Client' ) && Jetpack_Data::get_access_token() );
}
}