| 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 WP_Block_Template; |
| 12 |
use WP_Post; |
| 13 |
|
| 14 |
/** |
| 15 |
* Jetpack_Subscription_Site class. |
| 16 |
*/ |
| 17 |
class Jetpack_Subscription_Site { |
| 18 |
/** |
| 19 |
* Jetpack_Subscription_Site singleton instance. |
| 20 |
* |
| 21 |
* @var Jetpack_Subscription_Site|null |
| 22 |
*/ |
| 23 |
private static $instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Jetpack_Subscription_Site instance init. |
| 27 |
*/ |
| 28 |
public static function init() { |
| 29 |
if ( self::$instance === null ) { |
| 30 |
self::$instance = new Jetpack_Subscription_Site(); |
| 31 |
} |
| 32 |
|
| 33 |
return self::$instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Handles Subscriber Login block placements. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function handle_subscriber_login_block_placements() { |
| 42 |
$this->handle_subscriber_login_block_navigation_placement(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns true if context is recognized as a header element. |
| 47 |
* |
| 48 |
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to. |
| 49 |
* |
| 50 |
* @return bool |
| 51 |
*/ |
| 52 |
protected function is_header_context( $context ) { |
| 53 |
if ( $context instanceof WP_Post && $context->post_type === 'wp_navigation' ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
if ( $context instanceof WP_Block_Template && $context->area === 'header' ) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Handles Subscriber Login block navigation placement. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
protected function handle_subscriber_login_block_navigation_placement() { |
| 70 |
$subscriber_login_navigation_enabled = get_option( 'jetpack_subscriptions_login_navigation_enabled', false ); |
| 71 |
if ( ! $subscriber_login_navigation_enabled ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! wp_is_block_theme() ) { // TODO Fallback for classic themes. |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
add_filter( |
| 80 |
'hooked_block_types', |
| 81 |
function ( $hooked_blocks, $relative_position, $anchor_block, $context ) { |
| 82 |
if ( |
| 83 |
$anchor_block === 'core/navigation' && |
| 84 |
$relative_position === 'last_child' && |
| 85 |
self::is_header_context( $context ) |
| 86 |
) { |
| 87 |
$hooked_blocks[] = 'jetpack/subscriber-login'; |
| 88 |
} |
| 89 |
|
| 90 |
return $hooked_blocks; |
| 91 |
}, |
| 92 |
10, |
| 93 |
4 |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
|