-
Notifications
You must be signed in to change notification settings - Fork 20
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
refactor: remove jetpack dependency #2439
Changes from 3 commits
210c2bf
a903c8a
c3ff595
072b365
d909cd9
a41901d
ec39d7c
20bfdc0
3ccc8de
d4dfb15
68a61ee
314db9c
e17b5ee
e5e377f
97cffb2
4aef84a
9e94de0
5d1026f
d1234f3
5293e86
86a8356
5ed5d2e
09a23cc
be6de25
6f4d648
0070493
e31fd4b
0d9d56a
36d9db8
e8e7954
c13df7b
e2d8df5
44308be
754737d
0f2cd34
7fdd3ee
e6221c2
2f7f664
347dff4
4a3280a
34fad4a
94923e2
fd7b349
8e08043
ffd305d
e5e2742
2e7930e
8591173
53de389
920d1b5
42fb786
ffc4730
cd61593
8ecbf99
116aa54
93b8d2f
c589b98
1c661f3
0f648a0
6dedb1a
1a09584
3f51e55
e486487
45ca57e
8fb6d83
ee46bcf
1723baf
b0e57d2
637ced3
9cba91c
fa79986
c33c430
f9e7b87
569ad49
7cd2782
573a363
42170ab
d9bb0f0
9f319c4
21d3164
00fa75f
d1be417
e8ed1d2
e5a96d3
2104872
cf22022
4140263
07b003e
9433d62
1d462d3
627283a
702256c
5f6fc1a
1d90a66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -466,7 +466,7 @@ protected function request_body( $initial_body = array() ) { | |
'dimension_unit' => strtolower( get_option( 'woocommerce_dimension_unit' ) ), | ||
'weight_unit' => strtolower( get_option( 'woocommerce_weight_unit' ) ), | ||
'wcs_version' => WC_Connect_Loader::get_wcs_version(), | ||
'jetpack_version' => JETPACK__VERSION, | ||
'jetpack_version' => defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : 'embed', | ||
'is_atomic' => WC_Connect_Jetpack::is_atomic_site(), | ||
'wc_version' => WC()->version, | ||
'wp_version' => get_bloginfo( 'version' ), | ||
|
@@ -529,7 +529,7 @@ protected function authorization_header() { | |
} | ||
|
||
list( $token_key, $token_secret ) = explode( '.', $token->secret ); | ||
$token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); | ||
$token_key = sprintf( '%s:%d:%d', $token_key, defined( 'JETPACK__API_VERSION' ) ? JETPACK__API_VERSION : 'embed', $token->external_user_id ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The server seems ok with this 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format is Constants::get_constant( 'JETPACK__API_VERSION' ) |
||
$time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); | ||
$timestamp = time() + $time_diff; | ||
$nonce = wp_generate_password( 10, false ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ function woocommerce_debug_tools( $tools ) { | |
function test_connection() { | ||
$test_request = $this->api_client->auth_test(); | ||
if ( $test_request && ! is_wp_error( $test_request ) && $test_request->authorized ) { | ||
echo '<div class="updated inline"><p>' . __( 'Your site is succesfully communicating to the WooCommerce Shipping & Tax API.', 'woocommerce-services' ) . '</p></div>'; | ||
echo '<div class="updated inline"><p>' . __( 'Your site is successfully communicating to the WooCommerce Shipping & Tax API.', 'woocommerce-services' ) . '</p></div>'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} else { | ||
echo '<div class="error inline"><p>' . __( 'ERROR: Your site has a problem connecting to the WooCommerce Shipping & Tax API. Please make sure your Jetpack connection is working.', 'woocommerce-services' ) . '</p></div>'; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,24 +75,7 @@ protected function get_health_items() { | |
// Check that Jetpack is connected | ||
include_once ABSPATH . 'wp-admin/includes/plugin.php'; // required for is_plugin_active | ||
$is_connected = WC_Connect_Jetpack::is_active() || WC_Connect_Jetpack::is_development_mode(); | ||
if ( ! is_plugin_active( 'jetpack/jetpack.php' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for all this branching logic anymore - checking whether jetpack is installed or not won't matter, we are now using the composer packages directly. |
||
$health_item = array( | ||
'state' => 'error', | ||
'message' => sprintf( | ||
__( 'Please install and activate the Jetpack plugin, version %s or higher', 'woocommerce-services' ), | ||
WOOCOMMERCE_CONNECT_MINIMUM_JETPACK_VERSION | ||
), | ||
); | ||
} elseif ( version_compare( JETPACK__VERSION, WOOCOMMERCE_CONNECT_MINIMUM_JETPACK_VERSION, '<' ) ) { | ||
$health_item = array( | ||
'state' => 'error', | ||
'message' => sprintf( | ||
__( 'Jetpack %1$s or higher is required (You are running %2$s)', 'woocommerce-services' ), | ||
WOOCOMMERCE_CONNECT_MINIMUM_JETPACK_VERSION, | ||
JETPACK__VERSION | ||
), | ||
); | ||
} elseif ( ! $is_connected ) { | ||
if ( ! $is_connected ) { | ||
$health_item = array( | ||
'state' => 'error', | ||
'message' => __( 'Jetpack is not connected to WordPress.com. Make sure the Jetpack plugin is installed, activated, and connected.', 'woocommerce-services' ), | ||
|
@@ -102,14 +85,19 @@ protected function get_health_items() { | |
'state' => 'warning', | ||
'message' => __( 'This is a Jetpack staging site', 'woocommerce-services' ), | ||
); | ||
} else { | ||
} elseif ( defined( 'JETPACK__VERSION' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this really matter? IDK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to say no. |
||
$health_item = array( | ||
'state' => 'success', | ||
'message' => sprintf( | ||
__( 'Jetpack %s is connected and working correctly', 'woocommerce-services' ), | ||
JETPACK__VERSION | ||
), | ||
); | ||
} else { | ||
$health_item = array( | ||
'state' => 'success', | ||
'message' => __( 'Jetpack is connected and working correctly', 'woocommerce-services' ), | ||
); | ||
} | ||
$health_items['jetpack'] = $health_item; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,26 +5,19 @@ | |
|
||
if ( ! class_exists( 'WC_Connect_Jetpack' ) ) { | ||
class WC_Connect_Jetpack { | ||
const PLUGIN_SLUG = 'woocommerce-shipping-tax'; | ||
|
||
public static function get_connection_manager() { | ||
return new Manager( self::PLUGIN_SLUG ); | ||
} | ||
|
||
/** | ||
* @param $user_id | ||
* | ||
* @return stdClass|WP_Error | ||
*/ | ||
public static function get_master_user_access_token( $user_id ) { | ||
if ( class_exists( '\Automattic\Jetpack\Connection\Tokens' ) && method_exists( '\Automattic\Jetpack\Connection\Tokens', 'get_access_token' ) ) { | ||
$connection = new Tokens(); | ||
|
||
return $connection->get_access_token( $user_id ); | ||
} | ||
|
||
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) && method_exists( '\Automattic\Jetpack\Connection\Manager', 'get_access_token' ) ) { | ||
$connection = new Manager(); | ||
|
||
return $connection->get_access_token( $user_id ); | ||
} | ||
|
||
// fallback | ||
return new stdClass(); | ||
return self::get_connection_manager()->get_tokens()->get_access_token( $user_id ); | ||
} | ||
|
||
/** | ||
|
@@ -35,6 +28,7 @@ public static function get_master_user_access_token( $user_id ) { | |
public static function is_development_mode() { | ||
if ( method_exists( '\\Automattic\\Jetpack\\Status', 'is_offline_mode' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check seems to be excessive now as well, the However, it might make sense to load it directly in the plugin's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great ideas! |
||
$status = new \Automattic\Jetpack\Status(); | ||
|
||
return $status->is_offline_mode(); | ||
} | ||
|
||
|
@@ -47,14 +41,8 @@ public static function is_development_mode() { | |
* @return bool | ||
*/ | ||
public static function is_active() { | ||
if ( defined( 'WOOCOMMERCE_SERVICES_LOCAL_TEST_MODE' ) && WOOCOMMERCE_SERVICES_LOCAL_TEST_MODE ) { | ||
return true; | ||
} | ||
if ( method_exists( 'Jetpack', 'is_active' ) ) { | ||
return Jetpack::is_active(); | ||
} | ||
|
||
return false; | ||
// TODO: check | ||
return self::get_connection_manager()->is_connected(); | ||
} | ||
|
||
/** | ||
|
@@ -63,16 +51,9 @@ public static function is_active() { | |
* @return bool | ||
*/ | ||
public static function is_staging_site() { | ||
if ( method_exists( '\\Automattic\\Jetpack\\Status', 'is_staging_site' ) ) { | ||
$status = new \Automattic\Jetpack\Status(); | ||
return $status->is_staging_site(); | ||
} | ||
$jetpack_status = new \Automattic\Jetpack\Status(); | ||
|
||
if ( method_exists( 'Jetpack', 'is_staging_site' ) ) { | ||
return Jetpack::is_staging_site(); | ||
} | ||
|
||
return false; | ||
return $jetpack_status->is_staging_site(); | ||
} | ||
|
||
/** | ||
|
@@ -81,6 +62,7 @@ public static function is_staging_site() { | |
* @return bool | ||
*/ | ||
public static function is_atomic_site() { | ||
// TODO: WTH? | ||
if ( function_exists( 'jetpack_is_atomic_site' ) ) { | ||
return jetpack_is_atomic_site(); | ||
} elseif ( function_exists( 'jetpack_is_automated_transfer_site' ) ) { | ||
|
@@ -91,17 +73,7 @@ public static function is_atomic_site() { | |
} | ||
|
||
public static function get_connected_user_data( $user_id ) { | ||
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) && method_exists( '\Automattic\Jetpack\Connection\Manager', 'get_connected_user_data' ) ) { | ||
$connection = new Manager(); | ||
|
||
return $connection->get_connected_user_data( $user_id ); | ||
} | ||
|
||
if ( method_exists( 'Jetpack', 'get_connected_user_data' ) ) { | ||
return Jetpack::get_connected_user_data( $user_id ); | ||
} | ||
|
||
return false; | ||
return self::get_connection_manager()->get_connected_user_data( $user_id ); | ||
} | ||
|
||
/** | ||
|
@@ -113,6 +85,7 @@ public static function get_master_user() { | |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
if ( self::is_active() && method_exists( 'Jetpack_Options', 'get_option' ) ) { | ||
$master_user_id = Jetpack_Options::get_option( 'master_user' ); | ||
|
||
return get_userdata( $master_user_id ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend calling |
||
|
||
|
@@ -124,9 +97,11 @@ public static function get_master_user() { | |
* Builds a connect url | ||
* | ||
* @param $redirect_url | ||
* | ||
* @return string | ||
*/ | ||
public static function build_connect_url( $redirect_url ) { | ||
// TODO: gotta figure this out | ||
return Jetpack::init()->build_connect_url( | ||
true, | ||
$redirect_url, | ||
|
@@ -142,14 +117,12 @@ public static function build_connect_url( $redirect_url ) { | |
* @param | ||
*/ | ||
public static function tracks_record_event( $user, $event_type, $data ) { | ||
if ( version_compare( JETPACK__VERSION, '7.5', '<' ) ) { | ||
if ( function_exists( 'jetpack_tracks_record_event' ) ) { | ||
return jetpack_tracks_record_event( $user, $event_type, $data ); | ||
} | ||
} elseif ( class_exists( 'Automattic\\Jetpack\\Tracking' ) ) { | ||
if ( class_exists( 'Automattic\\Jetpack\\Tracking' ) ) { | ||
waclawjacek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$tracking = new Automattic\Jetpack\Tracking(); | ||
|
||
return $tracking->tracks_record_event( $user, $event_type, $data ); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
@@ -159,19 +132,7 @@ public static function tracks_record_event( $user, $event_type, $data ) { | |
* @return bool Whether or nor the current user is connected to Jetpack | ||
*/ | ||
public static function is_current_user_connected() { | ||
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) && method_exists( '\Automattic\Jetpack\Connection\Manager', 'is_user_connected' ) ) { | ||
$connection = new Manager(); | ||
|
||
return $connection->is_user_connected(); | ||
} | ||
|
||
if ( defined( 'JETPACK_MASTER_USER' ) ) { | ||
$user_token = self::get_master_user_access_token( JETPACK_MASTER_USER ); | ||
|
||
return ( isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id ); | ||
} | ||
|
||
return false; | ||
return self::get_connection_manager()->is_user_connected(); | ||
} | ||
|
||
/** | ||
|
@@ -180,19 +141,23 @@ public static function is_current_user_connected() { | |
* @return bool Whether or nor Jetpack is connected | ||
*/ | ||
public static function is_connected() { | ||
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) && method_exists( '\Automattic\Jetpack\Connection\Manager', 'is_connected' ) ) { | ||
$connection = new Manager(); | ||
|
||
return $connection->is_connected(); | ||
} | ||
$manager = self::get_connection_manager(); | ||
|
||
if ( defined( 'JETPACK_MASTER_USER' ) ) { | ||
$user_token = self::get_master_user_access_token( JETPACK_MASTER_USER ); | ||
return $manager->is_plugin_enabled() && $manager->has_connected_owner(); | ||
} | ||
|
||
return isset( $user_token->external_user_id ); | ||
/** | ||
* Check if WCS&T is installed alongside an old version of Jetpack (8.1 or earlier). Due to the autoloader code in those old | ||
* versions, the Jetpack Config initialization code would just crash the site. | ||
* | ||
* @return bool True if the plugin can keep initializing itself, false otherwise. | ||
*/ | ||
public static function is_jetpack_version_supported() { | ||
if ( defined( 'JETPACK__VERSION' ) && version_compare( JETPACK__VERSION, '8.2', '<' ) && JETPACK__VERSION !== 'wpcom' ) { | ||
return false; | ||
} | ||
|
||
return false; | ||
return true; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,8 +259,6 @@ public static function get_banner_type_to_display( $status = array() ) { | |
This is an existing user. Do nothing. | ||
*/ | ||
switch ( $status['jetpack_connection_status'] ) { | ||
case self::JETPACK_NOT_INSTALLED: | ||
case self::JETPACK_INSTALLED_NOT_ACTIVATED: | ||
case self::JETPACK_ACTIVATED_NOT_CONNECTED: | ||
return 'before_jetpack_connection'; | ||
case self::JETPACK_CONNECTED: | ||
|
@@ -288,6 +286,9 @@ public static function get_banner_type_to_display( $status = array() ) { | |
} | ||
|
||
public function get_jetpack_install_status() { | ||
// TODO: check | ||
return self::JETPACK_CONNECTED; | ||
|
||
// we need to use validate_plugin to check that Jetpack is installed | ||
include_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
|
||
|
@@ -417,10 +418,6 @@ public function set_up_nux_notices() { | |
// Admins might not be able to install or activate plugins, but Jetpack might already have been installed by a superadmin. | ||
// If this is the case, the admin can connect the site on their own, and should be able to use WCS as ususal | ||
$jetpack_install_status = $this->get_jetpack_install_status(); | ||
if ( ( self::JETPACK_NOT_INSTALLED === $jetpack_install_status && ! current_user_can( 'install_plugins' ) ) | ||
|| ( self::JETPACK_INSTALLED_NOT_ACTIVATED === $jetpack_install_status && ! current_user_can( 'activate_plugins' ) ) ) { | ||
return; | ||
} | ||
|
||
$banner_to_display = self::get_banner_type_to_display( | ||
array( | ||
|
@@ -446,10 +443,6 @@ public function set_up_nux_notices() { | |
); | ||
wp_enqueue_script( 'wc_connect_banner' ); | ||
wp_localize_script( 'wc_connect_banner', 'wcs_nux_notice', $ajax_data ); | ||
add_action( | ||
'wp_ajax_woocommerce_services_activate_jetpack', | ||
array( $this, 'ajax_activate_jetpack' ) | ||
); | ||
add_action( | ||
'wp_ajax_woocommerce_services_get_jetpack_connect_url', | ||
array( $this, 'ajax_get_jetpack_connect_url' ) | ||
|
@@ -508,14 +501,13 @@ public function show_banner_before_connection() { | |
|
||
$country = WC()->countries->get_base_country(); | ||
/* translators: %s: list of features, potentially comma separated */ | ||
$description_base = __( "WooCommerce Shipping & Tax is almost ready to go! Once you connect Jetpack you'll have access to %s.", 'woocommerce-services' ); | ||
$description_base = __( "WooCommerce Shipping & Tax is almost ready to go! Once you connect your site you'll have access to %s.", 'woocommerce-services' ); | ||
$feature_list = $this->get_feature_list_for_country( $country ); | ||
$banner_content = array( | ||
'title' => $banner_title, | ||
'description' => sprintf( $description_base, $feature_list ), | ||
'button_text' => $button_text, | ||
'image_url' => $image_url, | ||
'should_show_jp' => true, | ||
'should_show_terms' => true, | ||
); | ||
|
||
|
@@ -563,7 +555,6 @@ public function show_banner_after_connection() { | |
'images/wcs-notice.png', | ||
dirname( __FILE__ ) | ||
), | ||
'should_show_jp' => false, | ||
'should_show_terms' => false, | ||
) | ||
); | ||
|
@@ -606,7 +597,6 @@ public function show_tos_banner() { | |
'images/wcs-notice.png', | ||
dirname( __FILE__ ) | ||
), | ||
'should_show_jp' => false, | ||
'should_show_terms' => true, | ||
) | ||
); | ||
|
@@ -620,12 +610,6 @@ public function show_nux_banner( $content ) { | |
?> | ||
<div class="notice wcs-nux__notice <?php echo isset( $content['dismissible_id'] ) ? 'is-dismissible' : ''; ?>"> | ||
<div class="wcs-nux__notice-logo <?php echo isset( $content['compact_logo'] ) && $content['compact_logo'] ? 'is-compact' : ''; ?>"> | ||
<?php if ( $content['should_show_jp'] ) : ?> | ||
<img | ||
class="wcs-nux__notice-logo-jetpack" | ||
src="<?php echo esc_url( plugins_url( 'images/jetpack-logo.png', dirname( __FILE__ ) ) ); ?>" | ||
> | ||
<?php endif; ?> | ||
<img class="wcs-nux__notice-logo-graphic" src="<?php echo esc_url( $content['image_url'] ); ?>"> | ||
</div> | ||
<div class="wcs-nux__notice-content"> | ||
|
@@ -691,28 +675,6 @@ class="woocommerce-services__connect-jetpack wcs-nux__notice-content-button butt | |
endif; | ||
} | ||
|
||
/** | ||
* Activates Jetpack after an ajax request | ||
*/ | ||
public function ajax_activate_jetpack() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to activate jetpack anymore |
||
check_ajax_referer( 'wcs_nux_notice' ); | ||
|
||
$result = activate_plugin( 'jetpack/jetpack.php' ); | ||
|
||
if ( is_null( $result ) ) { | ||
// The function activate_plugin() returns NULL on success. | ||
echo 'success'; | ||
} else { | ||
if ( is_wp_error( $result ) ) { | ||
echo esc_html( $result->get_error_message() ); | ||
} else { | ||
echo 'error'; | ||
} | ||
} | ||
|
||
wp_die(); | ||
} | ||
|
||
/** | ||
* Get Jetpack connection URL. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like we're doing anything on the server with this string. However, the server is still requiring it in the payload.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it was only ever used for troubleshooting. If it's easy to get the version of the
jetpack-connection
package I would include it, otherwiseembed
is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, just checked and we are not using it anywhere aside from Tracks calls. We require it to be a string if it is present.
I've changed this from
embed
toembed-{jetpack_connection_package_version}
.