Nessuna descrizione

logged-out-view.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Premium Content Logged Out View Child Block.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. namespace Automattic\Jetpack\Extensions\Premium_Content;
  8. use Automattic\Jetpack\Blocks;
  9. use Jetpack_Gutenberg;
  10. const LOGGEDOUT_VIEW_NAME = 'premium-content/logged-out-view';
  11. require_once dirname( __DIR__ ) . '/_inc/access-check.php';
  12. /**
  13. * Registers the block for use in Gutenberg
  14. * This is done via an action so that we can disable
  15. * registration if we need to.
  16. */
  17. function register_loggedout_view_block() {
  18. // Only load this block on WordPress.com.
  19. if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || jetpack_is_atomic_site() ) {
  20. // Determine required `context` key based on Gutenberg version.
  21. $deprecated = function_exists( 'gutenberg_get_post_from_context' );
  22. $uses = $deprecated ? 'context' : 'uses_context';
  23. Blocks::jetpack_register_block(
  24. LOGGEDOUT_VIEW_NAME,
  25. array(
  26. 'render_callback' => __NAMESPACE__ . '\render_loggedout_view_block',
  27. $uses => array( 'premium-content/planId' ),
  28. )
  29. );
  30. }
  31. }
  32. add_action( 'init', __NAMESPACE__ . '\register_loggedout_view_block' );
  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. $visitor_has_access = current_visitor_can_access( $attributes, $block );
  47. if ( $visitor_has_access ) {
  48. // The viewer has access to premium content, so the viewer shouldn't see the logged out view.
  49. return '';
  50. }
  51. Jetpack_Gutenberg::load_styles_as_required( LOGGEDOUT_VIEW_NAME );
  52. // Old versions of the block were rendering the subscribe/login button server-side, so we need to still support them.
  53. if ( ! empty( $attributes['buttonClasses'] ) ) {
  54. require_once __DIR__ . '/../_inc/legacy-buttons.php';
  55. $buttons = create_legacy_buttons_markup( $attributes, $content, $block );
  56. return $content . $buttons;
  57. }
  58. return $content;
  59. }