related-posts.php
66 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Related Posts Block. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\RelatedPosts; |
| 9 | |
| 10 | use Automattic\Jetpack\Blocks; |
| 11 | use Automattic\Jetpack\Modules; |
| 12 | use Automattic\Jetpack\Status; |
| 13 | use Automattic\Jetpack\Status\Host; |
| 14 | use WP_Block; |
| 15 | |
| 16 | /** |
| 17 | * Registers the block for use in Gutenberg |
| 18 | * This is done via an action so that we can disable |
| 19 | * registration if we need to. |
| 20 | */ |
| 21 | function register_block() { |
| 22 | if ( |
| 23 | ( new Host() )->is_wpcom_simple() |
| 24 | || ( \Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) |
| 25 | ) { |
| 26 | Blocks::jetpack_register_block( |
| 27 | __DIR__, |
| 28 | array( |
| 29 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | } |
| 34 | add_action( 'init', __NAMESPACE__ . '\register_block', 9 ); |
| 35 | |
| 36 | /** |
| 37 | * Related Posts block render callback. |
| 38 | * |
| 39 | * @param array $attributes Array containing the Button block attributes. |
| 40 | * @param string $content The block content. |
| 41 | * @param WP_Block $block The block object. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | function render_block( $attributes, $content, $block ) { |
| 46 | // If the Related Posts module is not active, don't render the block. |
| 47 | if ( |
| 48 | ! ( new Host() )->is_wpcom_simple() |
| 49 | && ! ( new Modules() )->is_active( 'related-posts' ) |
| 50 | ) { |
| 51 | return ''; |
| 52 | } |
| 53 | |
| 54 | // If the Related Posts option is turned off, don't render the block. |
| 55 | $options = \Jetpack_Options::get_option( 'relatedposts', array() ); |
| 56 | if ( empty( $options['enabled'] ) || ! $options['enabled'] ) { |
| 57 | return ''; |
| 58 | } |
| 59 | |
| 60 | if ( ! class_exists( 'Jetpack_RelatedPosts' ) ) { |
| 61 | require_once JETPACK__PLUGIN_DIR . 'modules/related-posts/jetpack-related-posts.php'; |
| 62 | } |
| 63 | |
| 64 | return \Jetpack_RelatedPosts::init()->render_block( $attributes, $content, $block ); |
| 65 | } |
| 66 |