Skip to content

Commit

Permalink
Connection: store persistent blog ID for multi-URL purposes (#34262)
Browse files Browse the repository at this point in the history
* Store persistent blog ID in the options.
* If the current hostname is an IP address, we generate a URL secret and pass it along with other registration data.

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/7038990403
  • Loading branch information
sergeymitr authored and matticbot committed Nov 29, 2023
1 parent 79b1ae6 commit 1283428
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 117 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"automattic/jetpack-autoloader": "^3.0.2",
"automattic/jetpack-composer-plugin": "^2.0.0",
"automattic/jetpack-config": "^2.0.0",
"automattic/jetpack-identity-crisis": "^0.12.1",
"automattic/jetpack-identity-crisis": "^0.13.0-alpha",
"automattic/jetpack-my-jetpack": "^4.0.4-alpha",
"automattic/jetpack-plugins-installer": "^0.3.0",
"automattic/jetpack-sync": "^2.1.1-alpha"
Expand Down
7 changes: 5 additions & 2 deletions jetpack_vendor/automattic/jetpack-connection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.4-alpha] - unreleased
## [2.1.0-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Added
- XMLRPC endpoint 'jetpack.idcUrlValidation' now accepts an argument to specify whether we should try to reuse existing URL secret.

## [2.0.3] - 2023-11-24

## [2.0.2] - 2023-11-21
Expand Down Expand Up @@ -935,7 +938,7 @@ This is an alpha version! The changes listed here are not final.

- Separate the connection library into its own package.

[2.0.4-alpha]: https://github.com/Automattic/jetpack-connection/compare/v2.0.3...v2.0.4-alpha
[2.1.0-alpha]: https://github.com/Automattic/jetpack-connection/compare/v2.0.3...v2.1.0-alpha
[2.0.3]: https://github.com/Automattic/jetpack-connection/compare/v2.0.2...v2.0.3
[2.0.2]: https://github.com/Automattic/jetpack-connection/compare/v2.0.1...v2.0.2
[2.0.1]: https://github.com/Automattic/jetpack-connection/compare/v2.0.0...v2.0.1
Expand Down
2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-connection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"link-template": "https://github.com/Automattic/jetpack-connection/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "2.0.x-dev"
"dev-trunk": "2.1.x-dev"
}
},
"config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '2.0.4-alpha';
const PACKAGE_VERSION = '2.1.0-alpha';

const PACKAGE_SLUG = 'connection';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.13.0-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Added
- Store persistent blog ID for multi-URL purposes.

## [0.12.1] - 2023-11-24

## [0.12.0] - 2023-11-20
Expand Down Expand Up @@ -451,6 +458,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated package dependencies.
- Use Connection/Urls for home_url and site_url functions migrated from Sync.

[0.13.0-alpha]: https://github.com/Automattic/jetpack-identity-crisis/compare/v0.12.1...v0.13.0-alpha
[0.12.1]: https://github.com/Automattic/jetpack-identity-crisis/compare/v0.12.0...v0.12.1
[0.12.0]: https://github.com/Automattic/jetpack-identity-crisis/compare/v0.11.3...v0.12.0
[0.11.3]: https://github.com/Automattic/jetpack-identity-crisis/compare/v0.11.2...v0.11.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0",
"automattic/jetpack-connection": "^2.0.4-alpha",
"automattic/jetpack-connection": "^2.1.0-alpha",
"automattic/jetpack-constants": "^2.0.0",
"automattic/jetpack-status": "^2.0.2-alpha",
"automattic/jetpack-logo": "^2.0.0",
Expand Down Expand Up @@ -57,7 +57,7 @@
"link-template": "https://github.com/Automattic/jetpack-identity-crisis/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.12.x-dev"
"dev-trunk": "0.13.x-dev"
}
},
"config": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class Identity_Crisis {
/**
* Package Version
*/
const PACKAGE_VERSION = '0.12.1';
const PACKAGE_VERSION = '0.13.0-alpha';

/**
* Persistent WPCOM blog ID that stays in the options after disconnect.
*/
const PERSISTENT_BLOG_ID_OPTION_NAME = 'jetpack_persistent_blog_id';

/**
* Instance of the object.
Expand Down Expand Up @@ -90,6 +95,9 @@ private function __construct() {

add_filter( 'jetpack_options', array( static::class, 'reverse_wpcom_urls_for_idc' ) );

add_filter( 'jetpack_register_request_body', array( static::class, 'register_request_body' ) );
add_action( 'jetpack_site_registered', array( static::class, 'site_registered' ) );

$urls_in_crisis = self::check_identity_crisis();
if ( false === $urls_in_crisis ) {
return;
Expand All @@ -113,6 +121,8 @@ public static function do_jetpack_idc_disconnect() {
$connection->disconnect_site( false );
}

delete_option( static::PERSISTENT_BLOG_ID_OPTION_NAME );

// Clear IDC options.
self::clear_all_idc_options();
}
Expand Down Expand Up @@ -1341,4 +1351,46 @@ public static function add_secret_to_url_validation_response( array $response )

return $response;
}

/**
* Add IDC-related data to the registration query.
*
* @param array $params The existing query params.
*
* @return array
*/
public static function register_request_body( array $params ) {
$persistent_blog_id = get_option( static::PERSISTENT_BLOG_ID_OPTION_NAME );
if ( $persistent_blog_id ) {
$params['persistent_blog_id'] = $persistent_blog_id;

$hostname = wp_parse_url( Urls::site_url(), PHP_URL_HOST );
if ( filter_var( $hostname, FILTER_VALIDATE_IP ) !== false ) {
try {
$secret = new URL_Secret();
$secret->create();

if ( $secret->exists() ) {
$params['url_secret'] = $secret->get_secret();
}
} catch ( Exception $e ) {
// No need to stop the registration flow, just track the error and proceed.
( new Tracking() )->record_user_event( 'registration_request_url_secret_failed', array( 'current_url' => Urls::site_url() ) );
}
}
}

return $params;
}

/**
* Set the necessary options when site gets registered.
*
* @param int $blog_id The blog ID.
*
* @return void
*/
public static function site_registered( $blog_id ) {
update_option( static::PERSISTENT_BLOG_ID_OPTION_NAME, (int) $blog_id, false );
}
}
2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-jitm/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=7.0",
"automattic/jetpack-a8c-mc-stats": "^2.0.0",
"automattic/jetpack-assets": "^2.0.1",
"automattic/jetpack-connection": "^2.0.4-alpha",
"automattic/jetpack-connection": "^2.1.0-alpha",
"automattic/jetpack-device-detection": "^2.0.1",
"automattic/jetpack-logo": "^2.0.0",
"automattic/jetpack-redirect": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-licensing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0",
"automattic/jetpack-connection": "^2.0.4-alpha"
"automattic/jetpack-connection": "^2.1.0-alpha"
},
"require-dev": {
"automattic/wordbless": "@dev",
Expand Down
2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-my-jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=7.0",
"automattic/jetpack-admin-ui": "^0.3.1",
"automattic/jetpack-assets": "^2.0.1",
"automattic/jetpack-connection": "^2.0.4-alpha",
"automattic/jetpack-connection": "^2.1.0-alpha",
"automattic/jetpack-jitm": "^3.0.0",
"automattic/jetpack-licensing": "^2.0.1",
"automattic/jetpack-plugins-installer": "^0.3.0",
Expand Down
4 changes: 2 additions & 2 deletions jetpack_vendor/automattic/jetpack-sync/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0",
"automattic/jetpack-connection": "^2.0.4-alpha",
"automattic/jetpack-connection": "^2.1.0-alpha",
"automattic/jetpack-constants": "^2.0.0",
"automattic/jetpack-identity-crisis": "^0.12.1",
"automattic/jetpack-identity-crisis": "^0.13.0-alpha",
"automattic/jetpack-password-checker": "^0.3.0",
"automattic/jetpack-ip": "^0.2.1",
"automattic/jetpack-roles": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions jetpack_vendor/i18n-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
),
'jetpack-connection' => array(
'path' => 'jetpack_vendor/automattic/jetpack-connection',
'ver' => '2.0.4-alpha1701188454',
'ver' => '2.1.0-alpha1701295077',
),
'jetpack-idc' => array(
'path' => 'jetpack_vendor/automattic/jetpack-identity-crisis',
'ver' => '0.12.1',
'ver' => '0.13.0-alpha1701295077',
),
'jetpack-ip' => array(
'path' => 'jetpack_vendor/automattic/jetpack-ip',
Expand Down
2 changes: 1 addition & 1 deletion vendor/automattic/jetpack-plans/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0",
"automattic/jetpack-connection": "^2.0.4-alpha"
"automattic/jetpack-connection": "^2.1.0-alpha"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down
Loading

0 comments on commit 1283428

Please sign in to comment.