Skip to content

Commit

Permalink
Connection: Update the connection initial state to fallback on the ne…
Browse files Browse the repository at this point in the history
…w consolidated Jetpack script data (#38825)

* Add initial state to the consolidated jetpack script data

* Add script-data as a dependency to connection package

* Fallback to the consolidated initial state in JS components

* Add changelog

* Simplify conditionals

* Fix up versions
  • Loading branch information
manzoorwanijk authored Aug 13, 2024
1 parent 6e03a6c commit de2d680
Show file tree
Hide file tree
Showing 41 changed files with 138 additions and 40 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Updated the connection initial state to fallback on the new consolidated Jetpack script data
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import restApi from '@automattic/jetpack-api';
import { getScriptData } from '@automattic/jetpack-script-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from 'react';
import { STORE_ID } from '../../state/store';

const initialState = window?.JP_CONNECTION_INITIAL_STATE ? window.JP_CONNECTION_INITIAL_STATE : {};
const initialState = window?.JP_CONNECTION_INITIAL_STATE || getScriptData()?.connection || {};

export default ( {
registrationNonce = initialState.registrationNonce,
Expand Down
8 changes: 6 additions & 2 deletions projects/js-packages/connection/helpers/get-calypso-origin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { getScriptData } from '@automattic/jetpack-script-data';

/**
* Get the Calypso origin based on the development environment.
*
* @returns {string} The Calypso url origin.
*/
export default function getCalypsoOrigin() {
const calypsoEnv =
typeof window !== 'undefined' && window?.JP_CONNECTION_INITIAL_STATE?.calypsoEnv;
const calypsoEnv = (
( typeof window !== 'undefined' && window?.JP_CONNECTION_INITIAL_STATE ) ||
getScriptData()?.connection
)?.calypsoEnv;

switch ( calypsoEnv ) {
case 'development':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import restApi from '@automattic/jetpack-api';
import { getCalypsoOrigin } from '@automattic/jetpack-connection';
import { getScriptData } from '@automattic/jetpack-script-data';
import { useDispatch, useSelect } from '@wordpress/data';
import debugFactory from 'debug';
import { useEffect, useState, useMemo } from 'react';
Expand All @@ -13,7 +14,7 @@ const {
apiRoot,
apiNonce,
siteSuffix: defaultSiteSuffix,
} = window?.JP_CONNECTION_INITIAL_STATE ? window.JP_CONNECTION_INITIAL_STATE : {};
} = window?.JP_CONNECTION_INITIAL_STATE || getScriptData()?.connection || {};
const defaultAdminUrl =
typeof window !== 'undefined' ? window?.myJetpackInitialState?.adminUrl : null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import restApi from '@automattic/jetpack-api';
import { getScriptData } from '@automattic/jetpack-script-data';
import { useDispatch } from '@wordpress/data';
import { useEffect, useState } from 'react';
import { STORE_ID } from '../../state/store';

const { apiRoot, apiNonce } = window?.JP_CONNECTION_INITIAL_STATE
? window.JP_CONNECTION_INITIAL_STATE
: {};
const { apiRoot, apiNonce } =
window?.JP_CONNECTION_INITIAL_STATE || getScriptData()?.connection || {};

/**
* Restore connection hook.
Expand Down
3 changes: 2 additions & 1 deletion projects/js-packages/connection/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@automattic/jetpack-connection",
"version": "0.34.2",
"version": "0.35.0-alpha",
"description": "Jetpack Connection Component",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/connection/#readme",
"bugs": {
Expand All @@ -18,6 +18,7 @@
"@automattic/jetpack-api": "workspace:*",
"@automattic/jetpack-components": "workspace:*",
"@automattic/jetpack-config": "workspace:*",
"@automattic/jetpack-script-data": "workspace:*",
"@wordpress/base-styles": "5.2.0",
"@wordpress/browserslist-config": "6.2.0",
"@wordpress/components": "28.2.0",
Expand Down
3 changes: 2 additions & 1 deletion projects/js-packages/connection/state/store.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-console */

import { getScriptData } from '@automattic/jetpack-script-data';
import actions from './actions';
import controls from './controls';
import reducer from './reducers';
Expand All @@ -8,7 +9,7 @@ import selectors from './selectors';
import storeHolder from './store-holder';
import STORE_ID from './store-id';

const initialState = window.JP_CONNECTION_INITIAL_STATE;
const initialState = window.JP_CONNECTION_INITIAL_STATE || getScriptData()?.connection;

if ( ! initialState ) {
console.error(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Updated the connection initial state to fallback on the new consolidated Jetpack script data
2 changes: 1 addition & 1 deletion projects/packages/connection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"link-template": "https://github.com/Automattic/jetpack-connection/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "2.11.x-dev"
"dev-trunk": "2.12.x-dev"
},
"dependencies": {
"test-only": [
Expand Down
12 changes: 12 additions & 0 deletions projects/packages/connection/src/class-initial-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ private static function get_data() {
);
}

/**
* Set the connection script data.
*
* @param array $data The script data.
*/
public static function set_connection_script_data( $data ) {

$data['connection'] = self::get_data();

return $data;
}

/**
* Render the initial state into a JavaScript variable.
*
Expand Down
2 changes: 2 additions & 0 deletions projects/packages/connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public static function configure() {

// Initial Partner management.
Partner::init();

add_filter( 'jetpack_admin_js_script_data', array( Initial_State::class, 'set_connection_script_data' ), 10, 1 );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/connection/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '2.11.4';
const PACKAGE_VERSION = '2.12.0-alpha';

const PACKAGE_SLUG = 'connection';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/automattic-for-agencies-client/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/backup/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/boost/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/inspect/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/migration/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/mu-wpcom-plugin/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


2 changes: 1 addition & 1 deletion projects/plugins/protect/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@
"automattic/jetpack-autoloader": true,
"automattic/jetpack-composer-plugin": true
},
"autoloader-suffix": "c4802e05bbcf59fd3b6350e8d3e5482c_protectⓥ3_0_0_beta"
"autoloader-suffix": "c4802e05bbcf59fd3b6350e8d3e5482c_protectⓥ3_0_1_alpha"
}
}
4 changes: 2 additions & 2 deletions projects/plugins/protect/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions projects/plugins/protect/jetpack-protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Jetpack Protect
* Plugin URI: https://wordpress.org/plugins/jetpack-protect
* Description: Security tools that keep your site safe and sound, from posts to plugins.
* Version: 3.0.0-beta
* Version: 3.0.1-alpha
* Author: Automattic - Jetpack Security team
* Author URI: https://jetpack.com/protect/
* License: GPLv2 or later
Expand Down Expand Up @@ -32,7 +32,7 @@
exit;
}

define( 'JETPACK_PROTECT_VERSION', '3.0.0-beta' );
define( 'JETPACK_PROTECT_VERSION', '3.0.1-alpha' );
define( 'JETPACK_PROTECT_DIR', plugin_dir_path( __FILE__ ) );
define( 'JETPACK_PROTECT_ROOT_FILE', __FILE__ );
define( 'JETPACK_PROTECT_ROOT_FILE_RELATIVE_PATH', plugin_basename( __FILE__ ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


Loading

0 comments on commit de2d680

Please sign in to comment.