| 1 |
<?php |
| 2 |
/** |
| 3 |
* Premium Content Logged Out View Child Block. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\Extensions\Premium_Content; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Blocks; |
| 11 |
use Jetpack_Gutenberg; |
| 12 |
|
| 13 |
const LOGGEDOUT_VIEW_NAME = 'premium-content/logged-out-view'; |
| 14 |
|
| 15 |
require_once dirname( __DIR__ ) . '/_inc/access-check.php'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers the block for use in Gutenberg |
| 19 |
* This is done via an action so that we can disable |
| 20 |
* registration if we need to. |
| 21 |
*/ |
| 22 |
function register_loggedout_view_block() { |
| 23 |
Blocks::jetpack_register_block( |
| 24 |
LOGGEDOUT_VIEW_NAME, |
| 25 |
array( |
| 26 |
'render_callback' => __NAMESPACE__ . '\render_loggedout_view_block', |
| 27 |
'uses_context' => array( 'premium-content/planId', 'premium-content/planIds' ), |
| 28 |
) |
| 29 |
); |
| 30 |
} |
| 31 |
add_action( 'init', __NAMESPACE__ . '\register_loggedout_view_block' ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Render callback. |
| 35 |
* |
| 36 |
* @param array $attributes Array containing the block attributes. |
| 37 |
* @param string $content String containing the block content. |
| 38 |
* @param object $block Object containing block details. |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
function render_loggedout_view_block( $attributes, $content, $block = null ) { |
| 43 |
if ( ! pre_render_checks() ) { |
| 44 |
return ''; |
| 45 |
} |
| 46 |
|
| 47 |
$visitor_has_access = current_visitor_can_access( $attributes, $block ); |
| 48 |
|
| 49 |
if ( $visitor_has_access ) { |
| 50 |
// The viewer has access to premium content, so the viewer shouldn't see the logged out view. |
| 51 |
return ''; |
| 52 |
} |
| 53 |
|
| 54 |
Jetpack_Gutenberg::load_styles_as_required( LOGGEDOUT_VIEW_NAME ); |
| 55 |
|
| 56 |
// Old versions of the block were rendering the subscribe/login button server-side, so we need to still support them. |
| 57 |
if ( ! empty( $attributes['buttonClasses'] ) ) { |
| 58 |
require_once __DIR__ . '/../_inc/legacy-buttons.php'; |
| 59 |
|
| 60 |
$buttons = create_legacy_buttons_markup( $attributes, $content, $block ); |
| 61 |
return $content . $buttons; |
| 62 |
} |
| 63 |
|
| 64 |
return $content; |
| 65 |
} |
| 66 |
|