Skip to content

Commit

Permalink
Blocks: convert to module (#39449)
Browse files Browse the repository at this point in the history
* Blocks: convert to a module

Co-authored-by: Brad Jorsch <[email protected]>
  • Loading branch information
kraftbj and anomiex authored Sep 27, 2024
1 parent 3d62db2 commit b03243a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 27 deletions.
44 changes: 21 additions & 23 deletions projects/plugins/jetpack/_inc/client/writing/composing.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chip, ToggleControl, getRedirectUrl } from '@automattic/jetpack-components';
import { Chip, getRedirectUrl } from '@automattic/jetpack-components';
import { __, _x } from '@wordpress/i18n';
import CompactCard from 'components/card/compact';
import { FormFieldset } from 'components/forms';
Expand Down Expand Up @@ -173,29 +173,27 @@ export class Composing extends React.Component {
} }
>
<FormFieldset>
<ToggleControl
checked={ ! this.props.getOptionValue( 'jetpack_blocks_disabled' ) }
toggling={ this.props.isSavingAnyOption( [ 'jetpack_blocks_disabled' ] ) }
onChange={ this.toggleBlocks }
label={
<>
<span className="jp-form-toggle-explanation">
{ __(
'Jetpack Blocks give you the power to deliver quality content that hooks website visitors without needing to hire a developer or learn a single line of code.',
'jetpack'
) }
</span>
{ ! this.props.getOptionValue( 'jetpack_blocks_disabled' ) && (
<span className="jp-form-setting-explanation">
{ __(
'Caution: if there are Jetpack blocks used in existing posts or pages, disabling this setting will cause those blocks to stop working.',
'jetpack'
) }
</span>
<ModuleToggle
slug="blocks"
activated={ !! this.props.getOptionValue( 'blocks' ) }
toggling={ this.props.isSavingAnyOption( [ 'blocks' ] ) }
toggleModule={ this.props.toggleModuleNow }
>
<span className="jp-form-toggle-explanation">
{ __(
'Jetpack Blocks give you the power to deliver quality content that hooks website visitors without needing to hire a developer or learn a single line of code.',
'jetpack'
) }
</span>
{ this.props.getOptionValue( 'blocks' ) && (
<span className="jp-form-setting-explanation">
{ __(
'Caution: if there are Jetpack blocks used in existing posts or pages, disabling this setting will cause those blocks to stop working.',
'jetpack'
) }
</>
}
/>
</span>
) }
</ModuleToggle>
</FormFieldset>
</SettingsGroup>
<CompactCard
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-blocks-module
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Blocks: transition from an option to a module to improve caching
14 changes: 10 additions & 4 deletions projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
use Automattic\Jetpack\Modules;
use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Dismissed_Notices;
use Automattic\Jetpack\Status;
Expand Down Expand Up @@ -432,18 +433,23 @@ public static function should_load() {
return false;
}

if ( get_option( 'jetpack_blocks_disabled', false ) ) {
return false;
$return = true;

if ( ! ( new Modules() )->is_active( 'blocks' ) ) {
$return = false;
}

/**
* Filter to disable Gutenberg blocks
* Filter to enable Gutenberg blocks.
*
* Defaults to true if (connected or in offline mode) and the Blocks module is active.
*
* @since 6.5.0
* @since $$next-version$$ Filter is able to activate or deactivate Gutenberg blocks.
*
* @param bool true Whether to load Gutenberg blocks
*/
return (bool) apply_filters( 'jetpack_gutenberg', true );
return (bool) apply_filters( 'jetpack_gutenberg', $return );
}

/**
Expand Down
9 changes: 9 additions & 0 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,15 @@ public function filter_default_modules( $modules ) {
}
}

// Special case to convert block setting to a block module.
$block_key = array_search( 'blocks', $modules, true );
if ( $block_key !== false ) { // Only care if 'blocks' made it through the previous filters.
$block_option = get_option( 'jetpack_blocks_disabled', null );
if ( $block_option ) {
unset( $modules[ $block_key ] );
}
}

return $modules;
}

Expand Down
30 changes: 30 additions & 0 deletions projects/plugins/jetpack/modules/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Module Name: Blocks
* Module Description: Add additional blocks to your site and post editors.
* Sort Order: 5
* First Introduced: 13.9-a.8
* Requires Connection: No
* Auto Activate: Yes
* Module Tags: blocks
* Feature: Writing
*
* @package automattic/jetpack
*/

add_action( 'jetpack_activate_module_blocks', 'jetpack_blocks_activate_module' );

/**
* Actions needed upon activating the blocks module.
*
* There is a legacy option to disable Jetpack blocks that we'll delete when this module is activated.
* Via jetpack_get_default_modules filter, we remove blocks from the default if the option is true.
* We'll leave that in place so _until the module is activated_ we will be sure to respect the previous
* setting.
*
* @since $$next-version$$
* @return void
*/
function jetpack_blocks_activate_module() {
delete_option( 'jetpack_blocks_disabled' ); // The function will check and return early if not present.
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function set_up() {
Jetpack_Options::update_option( 'master_user', $this->master_user_id );
Jetpack_Options::update_option( 'id', 1234 );
Jetpack_Options::update_option( 'blog_token', 'asd.asd.1' );
Jetpack::activate_default_modules();

add_filter( 'jetpack_set_available_extensions', array( __CLASS__, 'get_extensions_whitelist' ) );
delete_option( 'jetpack_excluded_extensions' );
Expand Down

0 comments on commit b03243a

Please sign in to comment.