Geen omschrijving

subscriber-view.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Premium Content Subscriber 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 SUBSCRIBER_VIEW_NAME = 'premium-content/subscriber-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_subscriber_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. SUBSCRIBER_VIEW_NAME,
  25. array(
  26. 'render_callback' => __NAMESPACE__ . '\render_subscriber_view_block',
  27. $uses => array( 'premium-content/planId' ),
  28. )
  29. );
  30. }
  31. }
  32. add_action( 'init', __NAMESPACE__ . '\register_subscriber_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 the full block.
  39. *
  40. * @return string
  41. */
  42. function render_subscriber_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. Jetpack_Gutenberg::load_styles_as_required( SUBSCRIBER_VIEW_NAME );
  49. // The viewer has access to premium content, so the viewer can see the subscriber view content.
  50. return $content;
  51. }
  52. return '';
  53. }