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

Blocks: various optimizations #39734

Open
wants to merge 21 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5ff452d
Blocks: various optimizations
kraftbj Oct 9, 2024
c07017c
Blocks: various optimizations
kraftbj Oct 9, 2024
ed49bfb
Merge branch 'add/performance-improvements' of github.com:Automattic/…
kraftbj Oct 9, 2024
dd7892a
Avoid str_starts_with calls when there's a set value already
kraftbj Oct 9, 2024
d8a851f
changelog
kraftbj Oct 9, 2024
8b2ed10
Merge remote-tracking branch 'origin/trunk' into add/performance-impr…
kraftbj Oct 10, 2024
65cb3d6
Merge branch 'add/performance-improvements' of github.com:Automattic/…
kraftbj Oct 10, 2024
2c48075
Blocks: There are no php files in any of the stores
kraftbj Oct 10, 2024
c1bf0ce
Blocks: There are no php files in any of shared directories
kraftbj Oct 10, 2024
bbd97ca
Merge branch 'trunk' into add/performance-improvements
kraftbj Oct 11, 2024
b92d27d
Merge branch 'add/performance-improvements' of github.com:Automattic/…
kraftbj Oct 15, 2024
91a34a0
Merge remote-tracking branch 'origin/trunk' into add/performance-impr…
kraftbj Oct 15, 2024
b5219ac
Remove debugging code
kraftbj Oct 15, 2024
3c41cdd
Dirty hack for tests
kraftbj Oct 15, 2024
4ff7eab
Merge remote-tracking branch 'origin/trunk' into add/performance-impr…
kraftbj Oct 17, 2024
b7470d6
Merge remote-tracking branch 'origin/trunk' into add/performance-impr…
kraftbj Oct 24, 2024
7d88eb8
Merge branch 'trunk' into add/performance-improvements
kraftbj Nov 4, 2024
acd640d
Merge remote-tracking branch 'origin/trunk' into add/performance-impr…
kraftbj Nov 18, 2024
e721e80
Ensure older prefixed form is still accepted
kraftbj Nov 18, 2024
fb7ef6b
Merge branch 'trunk' into add/performance-improvements
kraftbj Nov 20, 2024
d4e8ec5
Merge remote-tracking branch 'origin/trunk' into add/performance-impr…
kraftbj Nov 20, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: No functional changes.


1 change: 1 addition & 0 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ public static function is_gutenberg_version_available( $version_requirements, $s
! $version_available
&& ! self::is_standalone_block() // This is only useful in Jetpack.
) {
$slug = Jetpack_Gutenberg::remove_extension_prefix( $slug );
Jetpack_Gutenberg::set_extension_unavailable(
$slug,
'incorrect_gutenberg_version',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: No functional changes.


Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ public static function disable_jetpack_donate() {

// Tell Jetpack to mark the donations feature as unavailable.
Jetpack_Gutenberg::set_extension_unavailable(
'jetpack/donations',
'donations',
esc_html__( 'Jetpack donations is disabled in favour of Newspack donations.', 'jetpack-mu-wpcom' )
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Blocks: improved performance.
49 changes: 36 additions & 13 deletions projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class Jetpack_Gutenberg {
'jetpack/revue',
);

/**
* Storing the contents of the preset file.
*
* Already been json_decode.
*
* @var null|object JSON decoded object after first usage.
*/
private static $preset_cache = null;

/**
* Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in
* php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can
Expand Down Expand Up @@ -101,6 +110,7 @@ public static function is_gutenberg_version_available( $version_requirements, $s
}

if ( ! $version_available ) {
$slug = self::remove_extension_prefix( $slug );
self::set_extension_unavailable(
$slug,
'incorrect_gutenberg_version',
Expand Down Expand Up @@ -161,7 +171,8 @@ protected static function share_items( $a, $b ) {
* @param string $slug Slug of the extension.
*/
public static function set_extension_available( $slug ) {
self::$availability[ self::remove_extension_prefix( $slug ) ] = true;
$slug = self::remove_extension_prefix( $slug );
self::$availability[ $slug ] = true;
}

/**
Expand Down Expand Up @@ -196,8 +207,8 @@ public static function set_extension_unavailable( $slug, $reason, $details = arr
// Add a descriptive suffix to disable behavior but provide informative reason.
$reason .= '__nudge_disabled';
}

self::$availability[ self::remove_extension_prefix( $slug ) ] = array(
$slug = self::remove_extension_prefix( $slug );
self::$availability[ $slug ] = array(
'reason' => $reason,
'details' => $details,
);
Expand Down Expand Up @@ -245,26 +256,40 @@ public static function get_blocks_directory() {
/**
* Checks for a given .json file in the blocks folder.
*
* @deprecated $$next-version$$
*
* @param string $preset The name of the .json file to look for.
*
* @return bool True if the file is found.
*/
public static function preset_exists( $preset ) {
_deprecated_function( __METHOD__, '$$next-version$$' );
return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
}

/**
* Decodes JSON loaded from a preset file in the blocks folder
* Decodes JSON loaded from the preset file in the blocks folder
*
* @since $$next-version$$ Deprecated argument. Only one value is ever used.
*
* @param string $preset The name of the .json file to load.
* @param null $deprecated No longer used.
*
* @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
*/
public static function get_preset( $preset ) {
return json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
public static function get_preset( $deprecated = null ) {
if ( $deprecated ) {
_deprecated_argument( __METHOD__, '$$next-version', 'The $preset argument is no longer needed or used.' );
}

if ( self::$preset_cache ) {
return self::$preset_cache;
}

self::$preset_cache = json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . 'index.json' )
);
return self::$preset_cache;
}

/**
Expand All @@ -273,9 +298,7 @@ public static function get_preset( $preset ) {
* @return array A list of blocks: eg [ 'publicize', 'markdown' ]
*/
public static function get_jetpack_gutenberg_extensions_allowed_list() {
$preset_extensions_manifest = self::preset_exists( 'index' )
? self::get_preset( 'index' )
: (object) array();
$preset_extensions_manifest = ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) ? array() : self::get_preset();
$blocks_variation = self::blocks_variation();

return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation );
Expand Down Expand Up @@ -808,7 +831,7 @@ public static function load_independent_blocks() {
* Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
* If available, load them.
*/
$directories = array( 'blocks', 'plugins', 'extended-blocks', 'shared', 'store' );
$directories = array( 'blocks', 'plugins', 'extended-blocks' );

foreach ( static::get_extensions() as $extension ) {
foreach ( $directories as $dirname ) {
Expand Down
18 changes: 3 additions & 15 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,23 +624,11 @@ private function __construct() {

/**
* Prepare Gutenberg Editor functionality
*/
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'load_independent_blocks' ) );
add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'load_block_editor_extensions' ), 9 );
add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'register_block_metadata_collection' ) );
/**
* We've switched from enqueue_block_editor_assets to enqueue_block_assets in WP-Admin because the assets with the former are loaded on the main site-editor.php.
*
* With the latter, the assets are now loaded in the SE iframe; the implementation is now faster because Gutenberg doesn't need to inject the assets in the iframe on client-side.
* The hooks previously here have been moved to modules/blocks.php but leaving this here pending
* a longer investigation to see if code is expecting the Gutenberg class to always be available.
*/
if ( is_admin() ) {
add_action( 'enqueue_block_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
} else {
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
}
add_filter( 'render_block', array( 'Jetpack_Gutenberg', 'display_deprecated_block_message' ), 10, 2 );

require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );

add_action( 'jetpack_event_log', array( 'Jetpack', 'log' ), 10, 2 );
Expand Down
149 changes: 15 additions & 134 deletions projects/plugins/jetpack/extensions/blocks/ai-assistant/ai-assistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,145 +55,26 @@ function load_assets( $attr, $content ) {
}

/**
* Register the `ai-assistant-support` extension.
* Register extensions.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-assistant-support' );
}
}
);

/**
* Register the `ai-assistant-form-support` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-assistant-form-support' );
}
}
);

/**
* Register the `ai-content-lens` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-content-lens' );
}
}
);

/**
* Register the `ai-assistant-backend-prompts` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-assistant-backend-prompts' );
}
}
);

/**
* Register the `ai-assistant-usage-panel` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-assistant-usage-panel' );
}
}
);

/**
* Register the `ai-featured-image-generator` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-featured-image-generator' );
}
}
);

/**
* Register the `ai-title-optimization` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-title-optimization' );
}
}
);

/**
* Register the `ai-assistant-experimental-image-generation-support` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-assistant-experimental-image-generation-support' );
}
}
);

/**
* Register the `ai-general-purpose-image-generator` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-general-purpose-image-generator' );
}
}
);

/**
* Register the `ai-proofread-breve` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) && apply_filters( 'breve_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-proofread-breve' );
}
}
);

/**
* Register the `ai-assistant-site-logo-support` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-assistant-site-logo-support' );
}
}
);

/**
* Register the `ai-title-optimization-keywords-support` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) ) {
\Jetpack_Gutenberg::set_extension_available( 'ai-title-optimization-keywords-support' );
Jetpack_Gutenberg::set_extension_available( 'ai-assistant-support' );
Jetpack_Gutenberg::set_extension_available( 'ai-assistant-form-support' );
Jetpack_Gutenberg::set_extension_available( 'ai-content-lens' );
Jetpack_Gutenberg::set_extension_available( 'ai-assistant-backend-prompts' );
Jetpack_Gutenberg::set_extension_available( 'ai-assistant-usage-panel' );
Jetpack_Gutenberg::set_extension_available( 'ai-featured-image-generator' );
Jetpack_Gutenberg::set_extension_available( 'ai-title-optimization' );
Jetpack_Gutenberg::set_extension_available( 'ai-assistant-experimental-image-generation-support' );
Jetpack_Gutenberg::set_extension_available( 'ai-general-purpose-image-generator' );
Jetpack_Gutenberg::set_extension_available( 'ai-assistant-site-logo-support' );
Jetpack_Gutenberg::set_extension_available( 'ai-title-optimization-keywords-support' );
Comment on lines +64 to +74
Copy link
Member

Choose a reason for hiding this comment

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

Do you think you could order them alphabetically? It would be easier to quickly parse, I think.

Comment on lines +64 to +74
Copy link
Member

Choose a reason for hiding this comment

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

We now have an extra one, ai-list-to-table-transform, with an extra filter:

/**
* Register the `ai-list-to-table-transform` extension.
*/
add_action(
'jetpack_register_gutenberg_extensions',
function () {
if ( apply_filters( 'jetpack_ai_enabled', true ) &&
apply_filters( 'list_to_table_transform_enabled', false )
) {
\Jetpack_Gutenberg::set_extension_available( 'ai-list-to-table-transform' );
}
}
);

Maybe you can add it to the same function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't looked, but I wonder if there's any chance of renaming the filter to be prefixed with jetpack_ while we're in there.

if ( apply_filters( 'breve_enabled', true ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe that's a good opportunity to add a docblock for this filter?

Jetpack_Gutenberg::set_extension_available( 'ai-proofread-breve' );
}
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function register_block() {
} else {
$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'personal-bundle' : 'jetpack_personal';
\Jetpack_Gutenberg::set_extension_unavailable(
Blocks::get_block_name( __DIR__ ),
'payment-buttons',
'missing_plan',
array(
'required_feature' => 'memberships',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function register() {
* Set if the WordAds block is available.
*/
public static function set_availability() {
$block_name = Blocks::get_block_name( __DIR__ );
$block_name = 'wordads';

if ( ! self::is_available() ) {
Jetpack_Gutenberg::set_extension_unavailable( $block_name, 'WordAds unavailable' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function register_plugins() {
if (
! current_user_can( $capability )
) {
Jetpack_Gutenberg::set_extension_unavailable( 'jetpack/publicize', 'unauthorized' );
Jetpack_Gutenberg::set_extension_unavailable( 'publicize', 'unauthorized' );
return;
}

Expand All @@ -35,7 +35,7 @@ function register_plugins() {
|| ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() )
) {
// Register Publicize.
Jetpack_Gutenberg::set_extension_available( 'jetpack/publicize' );
Jetpack_Gutenberg::set_extension_available( 'publicize' );

// Set the republicize availability, depending on the site plan.
Jetpack_Gutenberg::set_availability_for_plan( 'republicize' );
Expand Down
16 changes: 16 additions & 0 deletions projects/plugins/jetpack/modules/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@
function jetpack_blocks_activate_module() {
delete_option( 'jetpack_blocks_disabled' ); // The function will check and return early if not present.
}

Jetpack_Gutenberg::load_independent_blocks();
Jetpack_Gutenberg::load_block_editor_extensions();
Comment on lines +32 to +33
Copy link
Member

Choose a reason for hiding this comment

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

Those 2 methods were hooked into plugins_loaded in class.jetpack.php. If we can switch them to being loaded in modules/blocks.php (does this mean loading them on after_setup_theme on the frontend), can we do the same with register_block_metadata_collection below?


/**
* We've switched from enqueue_block_editor_assets to enqueue_block_assets in WP-Admin because the assets with the former are loaded on the main site-editor.php.
*
* With the latter, the assets are now loaded in the SE iframe; the implementation is now faster because Gutenberg doesn't need to inject the assets in the iframe on client-side.
*/
if ( is_admin() ) {
add_action( 'enqueue_block_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
} else {
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
}
add_filter( 'render_block', array( 'Jetpack_Gutenberg', 'display_deprecated_block_message' ), 10, 2 );
add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'register_block_metadata_collection' ) );
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public function register_gutenberg_block() {
);
} else {
Jetpack_Gutenberg::set_extension_unavailable(
'jetpack/recurring-payments',
'recurring-payments',
'missing_plan',
array(
'required_feature' => 'memberships',
Expand Down
Loading
Loading