paywall.php
71 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Paywall Block. |
| 4 | * |
| 5 | * @since 12.5 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Paywall; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | const FEATURE_NAME = 'paywall'; |
| 19 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 20 | const BLOCK_HTML = '<!-- wp:' . BLOCK_NAME . ' /-->'; |
| 21 | const THE_EXCERPT_BLOCK = '[[[[[' . BLOCK_NAME . ']]]]]'; |
| 22 | |
| 23 | /** |
| 24 | * Registers the block for use in Gutenberg |
| 25 | * This is done via an action so that we can disable |
| 26 | * registration if we need to. |
| 27 | */ |
| 28 | function register_block() { |
| 29 | if ( ! \Jetpack::is_module_active( 'subscriptions' ) ) { |
| 30 | return; |
| 31 | } |
| 32 | if ( ! class_exists( '\Jetpack_Memberships' ) ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | Blocks::jetpack_register_block( |
| 37 | __DIR__, |
| 38 | array( |
| 39 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 40 | ) |
| 41 | ); |
| 42 | } |
| 43 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 44 | |
| 45 | /** |
| 46 | * Paywall block render callback. |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | function render_block() { |
| 51 | if ( doing_filter( 'get_the_excerpt' ) ) { |
| 52 | if ( \Jetpack_Memberships::user_can_view_post() ) { |
| 53 | return ''; |
| 54 | } |
| 55 | return THE_EXCERPT_BLOCK; |
| 56 | } |
| 57 | return ''; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Adds the Paywall block to excerpt allowed blocks. |
| 62 | * |
| 63 | * @param array $allowed_blocks The allowed blocks. |
| 64 | * |
| 65 | * @return array The allowed blocks. |
| 66 | */ |
| 67 | function excerpt_allowed_blocks( $allowed_blocks ) { |
| 68 | return array_merge( $allowed_blocks, array( Blocks::get_block_name( __DIR__ ) ) ); |
| 69 | } |
| 70 | add_filter( 'excerpt_allowed_blocks', __NAMESPACE__ . '\excerpt_allowed_blocks' ); |
| 71 |