← All changes
|
extensions/blocks/related-posts/related-posts.php
+23
-41
12.7.3
→
16.3-a.1
View file →
| @@ -10,11 +10,13 @@ | ||
| 10 | 10 | use Automattic\Jetpack\Blocks; |
| 11 | 11 | use Automattic\Jetpack\Modules; |
| 12 | 12 | use Automattic\Jetpack\Status; |
| 13 | 13 | use Automattic\Jetpack\Status\Host; |
| 14 | +use WP_Block; | |
| 14 | 15 | |
| 15 | -const FEATURE_NAME = 'related-posts'; | |
| 16 | -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit( 0 ); | |
| 18 | +} | |
| 17 | 19 | |
| 18 | 20 | /** |
| 19 | 21 | * Registers the block for use in Gutenberg |
| 20 | 22 | * This is done via an action so that we can disable |
| @@ -20,32 +22,25 @@ | ||
| 20 | 22 | * This is done via an action so that we can disable |
| 21 | 23 | * registration if we need to. |
| 22 | 24 | */ |
| 23 | 25 | function register_block() { |
| 26 | + /* | |
| 27 | + * The block is available even when the module is not active, | |
| 28 | + * so we can display a nudge to activate the module instead of the block. | |
| 29 | + * However, since non-admins cannot activate modules, we do not display the empty block for them. | |
| 30 | + */ | |
| 31 | + if ( ! ( new Modules() )->is_active( 'related-posts' ) && ! current_user_can( 'jetpack_activate_modules' ) ) { | |
| 32 | + return; | |
| 33 | + } | |
| 34 | + | |
| 24 | 35 | if ( |
| 25 | 36 | ( new Host() )->is_wpcom_simple() |
| 26 | 37 | || ( \Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) |
| 27 | 38 | ) { |
| 28 | 39 | Blocks::jetpack_register_block( |
| 29 | - BLOCK_NAME, | |
| 40 | + __DIR__, | |
| 30 | 41 | array( |
| 31 | 42 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 32 | - 'supports' => array( | |
| 33 | - 'color' => array( | |
| 34 | - 'gradients' => true, | |
| 35 | - 'link' => true, | |
| 36 | - ), | |
| 37 | - 'spacing' => array( | |
| 38 | - 'margin' => true, | |
| 39 | - 'padding' => true, | |
| 40 | - ), | |
| 41 | - 'typography' => array( | |
| 42 | - '__experimentalFontFamily' => true, | |
| 43 | - 'fontSize' => true, | |
| 44 | - 'lineHeight' => true, | |
| 45 | - ), | |
| 46 | - 'align' => array( 'wide', 'full' ), | |
| 47 | - ), | |
| 48 | 43 | ) |
| 49 | 44 | ); |
| 50 | 45 | } |
| 51 | 46 | } |
| @@ -53,30 +48,17 @@ | ||
| 53 | 48 | |
| 54 | 49 | /** |
| 55 | 50 | * Related Posts block render callback. |
| 56 | 51 | * |
| 57 | - * @param array $attributes Array containing the Button block attributes. | |
| 58 | - * @param string $content The Button block content. | |
| 52 | + * The render implementation lives in render.php and is only loaded when the | |
| 53 | + * block is actually rendered, keeping it out of the eager front-end path. | |
| 59 | 54 | * |
| 55 | + * @param array $attributes Array containing the Button block attributes. | |
| 56 | + * @param string $content The block content. | |
| 57 | + * @param WP_Block $block The block object. | |
| 58 | + * | |
| 60 | 59 | * @return string |
| 61 | 60 | */ |
| 62 | -function render_block( $attributes, $content ) { | |
| 63 | - // If the Related Posts module is not active, don't render the block. | |
| 64 | - if ( | |
| 65 | - ! ( new Host() )->is_wpcom_simple() | |
| 66 | - && ! ( new Modules() )->is_active( FEATURE_NAME ) | |
| 67 | - ) { | |
| 68 | - return ''; | |
| 69 | - } | |
| 70 | - | |
| 71 | - // If the Related Posts option is turned off, don't render the block. | |
| 72 | - $options = \Jetpack_Options::get_option( 'relatedposts', array() ); | |
| 73 | - if ( empty( $options['enabled'] ) || ! $options['enabled'] ) { | |
| 74 | - return ''; | |
| 75 | - } | |
| 76 | - | |
| 77 | - if ( ! class_exists( 'Jetpack_RelatedPosts' ) ) { | |
| 78 | - require_once JETPACK__PLUGIN_DIR . 'modules/related-posts/jetpack-related-posts.php'; | |
| 79 | - } | |
| 80 | - | |
| 81 | - return \Jetpack_RelatedPosts::init()->render_block( $attributes, $content ); | |
| 61 | +function render_block( $attributes, $content, $block ) { | |
| 62 | + require_once __DIR__ . '/render.php'; | |
| 63 | + return render_block_implementation( $attributes, $content, $block ); | |
| 82 | 64 | } |