Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Subscription_Site | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
210 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| handle_subscriber_login_block_placements | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| is_header_context | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| handle_subscriber_login_block_navigation_placement | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adds support for Jetpack Subscription Site feature. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 13.3 |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack\Extensions\Subscriber_Login; |
| 10 | |
| 11 | use Automattic\Jetpack\Extensions\Subscriptions\Jetpack_Subscription_Site as Subscriptions_Site; |
| 12 | use WP_Block_Template; |
| 13 | use WP_Post; |
| 14 | |
| 15 | require_once __DIR__ . '/../subscriptions/class-jetpack-subscription-site.php'; |
| 16 | |
| 17 | /** |
| 18 | * Jetpack_Subscription_Site class. |
| 19 | */ |
| 20 | class Jetpack_Subscription_Site { |
| 21 | /** |
| 22 | * Jetpack_Subscription_Site singleton instance. |
| 23 | * |
| 24 | * @var Jetpack_Subscription_Site|null |
| 25 | */ |
| 26 | private static $instance; |
| 27 | |
| 28 | /** |
| 29 | * Jetpack_Subscription_Site instance init. |
| 30 | */ |
| 31 | public static function init() { |
| 32 | if ( self::$instance === null ) { |
| 33 | self::$instance = new Jetpack_Subscription_Site(); |
| 34 | } |
| 35 | |
| 36 | return self::$instance; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Handles Subscriber Login block placements. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function handle_subscriber_login_block_placements() { |
| 45 | Subscriptions_Site::seed_missing_options( array( 'jetpack_subscriptions_login_navigation_enabled' ) ); |
| 46 | |
| 47 | $this->handle_subscriber_login_block_navigation_placement(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns true if context is recognized as a header element. |
| 52 | * |
| 53 | * @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to. |
| 54 | * |
| 55 | * @return bool |
| 56 | */ |
| 57 | protected function is_header_context( $context ) { |
| 58 | if ( $context instanceof WP_Post && $context->post_type === 'wp_navigation' ) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | if ( $context instanceof WP_Block_Template && $context->area === 'header' ) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Handles Subscriber Login block navigation placement. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | protected function handle_subscriber_login_block_navigation_placement() { |
| 75 | $subscriber_login_navigation_enabled = get_option( 'jetpack_subscriptions_login_navigation_enabled', false ); |
| 76 | if ( ! $subscriber_login_navigation_enabled ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if ( ! wp_is_block_theme() ) { // TODO Fallback for classic themes. |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | add_filter( |
| 85 | 'hooked_block_types', |
| 86 | function ( $hooked_blocks, $relative_position, $anchor_block, $context ) { |
| 87 | if ( |
| 88 | $anchor_block === 'core/navigation' && |
| 89 | $relative_position === 'last_child' && |
| 90 | self::is_header_context( $context ) |
| 91 | ) { |
| 92 | $hooked_blocks[] = 'jetpack/subscriber-login'; |
| 93 | } |
| 94 | |
| 95 | return $hooked_blocks; |
| 96 | }, |
| 97 | 10, |
| 98 | 4 |
| 99 | ); |
| 100 | } |
| 101 | } |