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

Social: Consolidate social initial state #38606

Merged
merged 74 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
18026f2
WIP
manzoorwanijk Jul 19, 2024
a4e0b93
Make the whole class static
manzoorwanijk Jul 22, 2024
2c677a3
Use `in-footer`
manzoorwanijk Jul 22, 2024
bb90514
Reset my-jetpack from trunk
manzoorwanijk Jul 22, 2024
59dc46c
Remove unnecessary checks for asset registration
manzoorwanijk Jul 22, 2024
590d56f
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 22, 2024
5b0ac8a
Add changelog
manzoorwanijk Jul 22, 2024
a8f41a8
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 22, 2024
713c909
Delete .eslintrc.js
manzoorwanijk Jul 22, 2024
ac4b37b
Set type to module
manzoorwanijk Jul 22, 2024
d32dc2d
Fix case
manzoorwanijk Jul 23, 2024
16a9564
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 23, 2024
1ba5b7d
Update pnpm-lock.yaml
manzoorwanijk Jul 23, 2024
f927d65
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 24, 2024
436c436
Silence filemtime warning
manzoorwanijk Jul 25, 2024
7acde2b
Use print_scripts hook instead of enqueue_scripts to render initial s…
manzoorwanijk Jul 26, 2024
d4fd7a9
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 26, 2024
14a1cd6
Reset changes in connection package
manzoorwanijk Jul 26, 2024
745918a
Have separate filters for admin and public scripts
manzoorwanijk Jul 26, 2024
066270e
Add more common fields and update types
manzoorwanijk Jul 29, 2024
7e2adfa
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 30, 2024
aadfeb3
Fix up versions
manzoorwanijk Jul 30, 2024
948e8ff
Create .gitkeep
manzoorwanijk Jul 30, 2024
d45437a
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 30, 2024
29daeaf
Fix up versions
manzoorwanijk Jul 30, 2024
7a0834f
Reset messed up versions
manzoorwanijk Jul 30, 2024
acaef37
Add the unnecessary changelog
manzoorwanijk Jul 30, 2024
d5abc90
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 31, 2024
d0fc2d6
Remove unnecessary noise of changelogs
manzoorwanijk Jul 31, 2024
bdf049a
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 1, 2024
79ec614
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 1, 2024
78271ff
Use JetpackInitialState
manzoorwanijk Aug 1, 2024
c625bfe
Update doc blocks
manzoorwanijk Aug 1, 2024
3813e96
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 1, 2024
2276331
Fix up versions
manzoorwanijk Aug 1, 2024
76022bf
Rename the package to `script-data`
manzoorwanijk Aug 2, 2024
5fb4553
Update build config
manzoorwanijk Aug 2, 2024
df778a8
Update js-package name
manzoorwanijk Aug 2, 2024
659bea0
Use separate global variables for data and module
manzoorwanijk Aug 2, 2024
7d6ccf4
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 2, 2024
84d5a5a
Update lockfile
manzoorwanijk Aug 2, 2024
0cd03bc
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 5, 2024
ea91938
Fix up versions
manzoorwanijk Aug 5, 2024
8cc9b45
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 6, 2024
5ac15db
Fix up versions
manzoorwanijk Aug 6, 2024
528fc56
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 6, 2024
d7aa3d7
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 7, 2024
13cdfec
Wire up initial state via publicize package
manzoorwanijk Jul 30, 2024
6ab848f
Use module augmentation to set types for social initial state
manzoorwanijk Jul 30, 2024
cbb4bda
Initialize publicize assets in social and Jetpack
manzoorwanijk Jul 30, 2024
e49d6e1
Update dependencies
manzoorwanijk Jul 30, 2024
8803b1d
Use the new initial state in Social admin page
manzoorwanijk Jul 30, 2024
d089d6c
Add changelog
manzoorwanijk Jul 30, 2024
8bf1756
Add more changelogs
manzoorwanijk Jul 30, 2024
ead4d9d
Fix up versions
manzoorwanijk Jul 30, 2024
606536d
Make Phan happy
manzoorwanijk Jul 30, 2024
f834580
Use existing feature flag
manzoorwanijk Jul 30, 2024
e504f32
Build assets for E2E tests
manzoorwanijk Jul 30, 2024
9b28581
debug e2e test
manzoorwanijk Jul 30, 2024
5d18f6e
Some more debugging
manzoorwanijk Jul 30, 2024
45327ce
Fix E2E tests
manzoorwanijk Jul 30, 2024
5406f7c
Use the renamed package
manzoorwanijk Aug 2, 2024
769d2c7
Create dynamic has_feature_flag method
manzoorwanijk Aug 2, 2024
12e563d
Merge branch 'trunk' into update/consolidate-social-state
manzoorwanijk Aug 7, 2024
0ac1e95
Fix up versions
manzoorwanijk Aug 7, 2024
de290e9
Remove unnecessary changelog
manzoorwanijk Aug 7, 2024
f09fecc
Comment out unused code for now
manzoorwanijk Aug 9, 2024
0739b4e
Use in-package initialization of assets
manzoorwanijk Aug 9, 2024
fe7faa7
Merge branch 'trunk' into update/consolidate-social-state
manzoorwanijk Aug 9, 2024
3c4201c
Fix up versions
manzoorwanijk Aug 9, 2024
2190351
Remove unused code for now
manzoorwanijk Aug 9, 2024
98d6408
Correct the variable names
manzoorwanijk Aug 9, 2024
9bec185
Fix namespace
manzoorwanijk Aug 9, 2024
b9f3186
Merge branch 'trunk' into update/consolidate-social-state
manzoorwanijk Aug 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 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: changed

Social: Updated intial state logic to use the new consolidated initial state
3 changes: 3 additions & 0 deletions projects/js-packages/publicize-components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//store for the components, but at the moment they're tied.
import './src/social-store';

// Ensure that module augmentation is applied
export type {} from './src/types';

export { default as Connection } from './src/components/connection';
export { default as Form } from './src/components/form';
export { default as SocialPreviewsModal } from './src/components/social-previews/modal';
Expand Down
1 change: 1 addition & 0 deletions projects/js-packages/publicize-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@automattic/jetpack-analytics": "workspace:*",
"@automattic/jetpack-components": "workspace:*",
"@automattic/jetpack-connection": "workspace:*",
"@automattic/jetpack-script-data": "workspace:*",
"@automattic/jetpack-shared-extension-utils": "workspace:*",
"@automattic/popup-monitor": "1.0.2",
"@automattic/social-previews": "2.1.0-beta.6",
Expand Down
8 changes: 8 additions & 0 deletions projects/js-packages/publicize-components/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SocialScriptData } from './types/types';

// Use module augmentation to add the social property to JetpackInitialState
declare module '@automattic/jetpack-script-data' {
interface JetpackScriptData {
social: SocialScriptData;
spsiddarthan marked this conversation as resolved.
Show resolved Hide resolved
}
}
9 changes: 9 additions & 0 deletions projects/js-packages/publicize-components/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export interface FeatureFlags {
useAdminUiV1: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have to add the new feature flag for the modal as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we need to migrate all our initial state to this new consolidated one.

}

export interface SocialScriptData {
is_publicize_enabled: boolean;
feature_flags: FeatureFlags;
}

type JetpackSettingsSelectors = {
getJetpackSettings: () => {
publicize_active: boolean;
Expand Down
4 changes: 2 additions & 2 deletions projects/packages/assets/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// drop data into `$wp_filter` for `WP_Hook::build_preinitialized_hooks()`.
if ( function_exists( 'add_action' ) ) {
add_action( 'wp_default_scripts', array( Automattic\Jetpack\Assets::class, 'wp_default_scripts_hook' ) );
add_action( 'plugins_loaded', array( Automattic\Jetpack\Script_Data::class, 'configure' ), 1 );
add_action( 'plugins_loaded', array( Automattic\Jetpack\Assets\Script_Data::class, 'configure' ), 1 );
} else {
global $wp_filter;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
Expand All @@ -20,6 +20,6 @@
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_filter['plugins_loaded'][1][] = array(
'accepted_args' => 0,
'function' => array( Automattic\Jetpack\Script_Data::class, 'configure' ),
'function' => array( Automattic\Jetpack\Assets\Script_Data::class, 'configure' ),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed variable names
16 changes: 9 additions & 7 deletions projects/packages/assets/src/class-script-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @package automattic/jetpack-assets
*/

namespace Automattic\Jetpack;
namespace Automattic\Jetpack\Assets;

use Automattic\Jetpack\Assets;

/**
* Class script data
Expand Down Expand Up @@ -90,7 +92,7 @@ protected static function get_admin_script_data() {

global $wp_version;

$state = array(
$data = array(
'site' => array(
'admin_url' => esc_url_raw( admin_url() ),
'date_format' => get_option( 'date_format' ),
Expand Down Expand Up @@ -125,9 +127,9 @@ protected static function get_admin_script_data() {
*
* @since 2.3.0
*
* @param array $state The script data.
* @param array $data The script data.
*/
return apply_filters( 'jetpack_admin_js_script_data', $state );
return apply_filters( 'jetpack_admin_js_script_data', $data );
}

/**
Expand All @@ -137,7 +139,7 @@ protected static function get_admin_script_data() {
*/
protected static function get_public_script_data() {

$state = array(
$data = array(
'site' => array(
'icon' => self::get_site_icon(),
'title' => self::get_site_title(),
Expand All @@ -151,9 +153,9 @@ protected static function get_public_script_data() {
*
* @since 2.3.0
*
* @param array $state The script data.
* @param array $data The script data.
*/
return apply_filters( 'jetpack_public_js_script_data', $state );
return apply_filters( 'jetpack_public_js_script_data', $data );
}

/**
Expand Down
19 changes: 19 additions & 0 deletions projects/packages/publicize/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Action Hooks for Jetpack Publicize module.
*
* @package automattic/jetpack-publicize
*/

// If WordPress's plugin API is available already, use it. If not,
// drop data into `$wp_filter` for `WP_Hook::build_preinitialized_hooks()`.
if ( function_exists( 'add_action' ) ) {
add_action( 'plugins_loaded', array( Automattic\Jetpack\Publicize\Publicize_Assets::class, 'configure' ), 1 );
} else {
global $wp_filter;
spsiddarthan marked this conversation as resolved.
Show resolved Hide resolved
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_filter['plugins_loaded'][1][] = array(
'accepted_args' => 0,
'function' => array( Automattic\Jetpack\Publicize\Publicize_Assets::class, 'configure' ),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Social: Updated intial state logic to use the new consolidated initial state
3 changes: 2 additions & 1 deletion projects/packages/publicize/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"src/"
],
"files": [
"actions.php",
"src/social-image-generator/utilities.php"
]
},
Expand Down Expand Up @@ -67,7 +68,7 @@
"link-template": "https://github.com/Automattic/jetpack-publicize/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "0.48.x-dev"
"dev-trunk": "0.49.x-dev"
}
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/publicize/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-publicize",
"version": "0.48.0",
"version": "0.49.0-alpha",
"description": "Publicize makes it easy to share your site’s posts on several social media networks automatically when you publish a new post.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/publicize/#readme",
"bugs": {
Expand Down
21 changes: 21 additions & 0 deletions projects/packages/publicize/src/class-publicize-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Publicize_Assets.
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize;

/**
* Publicize_Assets class.
*/
class Publicize_Assets {

/**
* Initialize the class.
*/
public static function configure() {
Publicize_Script_Data::configure();
}
}
136 changes: 136 additions & 0 deletions projects/packages/publicize/src/class-publicize-script-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Publicize_Script_Data.
*
* @package automattic/jetpack-publicize
*/

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Current_Plan;
use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;

/**
* Publicize_Script_Data class.
*/
class Publicize_Script_Data {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am a bit confused here what the class name "Script data" means. Could we have a more descriptive doc if this describes the class the best?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually the package we created in #38430 was initially named as initial-state and the class was also named as Jetpack_Initial_State but after some discussion with Garage team, we reached the conclusion to use the word script data which is what the initial state actually is. Also, WordPress uses the similar nomenclature for such data - wp_add_inline_script has calls the second argument as $data and likewise the other such functions.


/**
* Get the publicize instance - properly typed
*
* @return Publicize
*/
public static function publicize() {
/**
* Publicize instance.
*
* @var Publicize $publicize
*/
spsiddarthan marked this conversation as resolved.
Show resolved Hide resolved
global $publicize;

return $publicize;
}

/**
* Configure script data.
*/
public static function configure() {
add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'set_admin_script_data' ), 10, 1 );
}

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

$data['social'] = self::get_admin_script_data();

if ( empty( $data['site']['plan'] ) ) {
$data['site']['plan'] = Current_Plan::get();
}

return $data;
}

/**
* Get the script data for admin UI.
*
* @return array
*/
public static function get_admin_script_data() {

// Only set script data on the social settings page,
// the Jetpack settings page, or the block editor.
$should_set_script_data = Utils::is_jetpack_settings_page()
|| Utils::is_social_settings_page()
|| Utils::should_block_editor_have_social();

if ( ! $should_set_script_data ) {
return array();
}

$basic_data = array(
'is_publicize_enabled' => Utils::is_publicize_active(),
'feature_flags' => self::get_feature_flags(),
);

if ( ! Utils::is_publicize_active() || ! Utils::is_connected() ) {
return $basic_data;
}

return array_merge(
$basic_data,
array(
manzoorwanijk marked this conversation as resolved.
Show resolved Hide resolved
/**
* 'store' => self::get_store_script_data(),
* 'urls' => self::get_urls(),
* 'shares_data' => self::get_shares_data(),
*/
)
);
}

/**
* Get the feature flags.
*
* @return array
*/
public static function get_feature_flags() {
$variable_to_feature_map = array(
'useAdminUiV1' => 'connections-management',
);

$feature_flags = array();

foreach ( $variable_to_feature_map as $variable => $feature ) {
$feature_flags[ $variable ] = self::has_feature_flag( $feature );
}

return $feature_flags;
}

/**
* Whether the site has the feature flag enabled.
*
* @param string $feature The feature name to check for, without the "social-" prefix.
* @return bool
*/
public static function has_feature_flag( $feature ): bool {
$flag_name = str_replace( '-', '_', $feature );

// If the option is set, use it.
if ( get_option( 'jetpack_social_has_' . $flag_name, false ) ) {
return true;
}

$constant_name = 'JETPACK_SOCIAL_HAS_' . strtoupper( $flag_name );
// If the constant is set, use it.
if ( defined( $constant_name ) && constant( $constant_name ) ) {
return true;
}

return Current_Plan::supports( 'social-' . $feature );
}
}
Comment on lines +120 to +136
Copy link
Contributor

Choose a reason for hiding this comment

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

We have this in publicize-base with my previous changes, would that be deprecated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we will eventually move to this new consolidated logic.

Loading
Loading