Skip to content

Commit

Permalink
Gutenberg: add loading blocks (#10008)
Browse files Browse the repository at this point in the history
Meant for easy testing of blocks during the unstable
phase of developing Jetpack's Gutenberg extensions.

- Loading blocks is disabled by default. To enable:
    ```php
    add_filter( 'jetpack_gutenberg', '__return_true', 10 );
    ```

- When enabled, loads blocks from WordPress.com CDN. 
    To load files locally instead:
    ```php
    add_filter( 'jetpack_gutenberg_cdn', '__return_false', 10 );
    ```

- To adjust CDN cache-busting:
    ```php
    add_filter( 'jetpack_gutenberg_cdn_cache_buster', function( $version ) {
        return time();
    }, 10, 1 );
    ```

Note that you will need to build block files yourself when loaded locally:
- `_inc/blocks/jetpack-editor.css`
- `_inc/blocks/jetpack-editor.js`
  • Loading branch information
simison committed Aug 14, 2018
1 parent c1c5ce0 commit cf5e9ad
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ yarn-error.log
# From here on, we only list files that are generated by a build process, ignored in git, but should be present in SVN.
#
## Things we will need in release branches
/_inc/blocks
/_inc/build
/modules/**/*.min.css
/modules/**/*-rtl.css
Expand Down
106 changes: 106 additions & 0 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ private function __construct() {
Jetpack_Network::init();
}

// Load Gutenberg editor blocks
add_action( 'init', array( $this, 'load_jetpack_gutenberg' ) );

add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );

// Unlink user before deleting the user from .com
Expand Down Expand Up @@ -7258,4 +7261,107 @@ public static function handle_post_authorization_actions( $activate_sso = false,

Jetpack::state( 'message', 'authorized' );
}

/**
* Check if Gutenberg editor is available
*
* @since 6.5.0
*
* @return bool
*/
public static function is_gutenberg_available() {
return function_exists( 'register_block_type' );
}

/**
* Load Gutenberg editor blocks.
*
* This section meant for unstable phase of developing Jetpack's
* Gutenberg extensions. If still around after Sep. 15, 2018 then
* please file an issue to remove it; if nobody responds within one
* week then please delete the code.
*
*
* Loading blocks is disabled by default and enabled via filter:
* add_filter( 'jetpack_gutenberg', '__return_true', 10 );
*
* When enabled, blocks are loaded from CDN by default. To load locally instead:
* add_filter( 'jetpack_gutenberg_cdn', '__return_false', 10 );
*
* Note that when loaded locally, you need to build the files yourself:
* - _inc/blocks/jetpack-editor.js
* - _inc/blocks/jetpack-editor.css
*
* CDN cache is busted once a day or when Jetpack version changes. To customize it:
* add_filter( 'jetpack_gutenberg_cdn_cache_buster', function( $version ) { return time(); }, 10, 1 );
*
* @since 6.5.0
*
* @return void
*/
public static function load_jetpack_gutenberg() {
/**
* Filter to turn on loading Gutenberg blocks
*
* @since 6.5.0
*
* @param bool false Whether to load Gutenberg blocks
*/
if ( ! Jetpack::is_gutenberg_available() || ! apply_filters( 'jetpack_gutenberg', false ) ) {
return;
}

/**
* Filter to turn off serving blocks via CDN
*
* @since 6.5.0
*
* @param bool true Whether to load Gutenberg blocks from CDN
*/
if ( apply_filters( 'jetpack_gutenberg_cdn', true ) ) {
$editor_script = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks/jetpack-editor.js';
$editor_style = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks/jetpack-editor.css';

/**
* Filter to modify cache busting for Gutenberg block assets loaded from CDN
*
* @since 6.5.0
*
* @param string
*/
$version = apply_filters( 'jetpack_gutenberg_cdn_cache_buster', sprintf( '%s-%s', gmdate( 'd-m-Y' ), JETPACK__VERSION ) );
} else {
$editor_script = plugins_url( '_inc/blocks/jetpack-editor.js', JETPACK__PLUGIN_FILE );
$editor_style = plugins_url( '_inc/blocks/jetpack-editor.css', JETPACK__PLUGIN_FILE );
$version = Jetpack::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/jetpack-editor.js' ) : JETPACK__VERSION;
}

wp_register_script(
'jetpack-blocks-editor',
$editor_script,
array(
'wp-blocks',
'wp-components',
'wp-compose',
'wp-data',
'wp-editor',
'wp-element',
'wp-i18n',
'wp-plugins',
),
$version
);

wp_register_style(
'jetpack-blocks-editor',
$editor_style,
array(),
$version
);

register_block_type( 'jetpack/blocks', array(
'editor_script' => 'jetpack-blocks-editor',
'editor_style' => 'jetpack-blocks-editor',
) );
}
}

0 comments on commit cf5e9ad

Please sign in to comment.