Aucune description

premium-content.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Premium Content Block.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. namespace Automattic\Jetpack\Extensions\Premium_Content;
  8. use Automattic\Jetpack\Blocks;
  9. use Jetpack_Gutenberg;
  10. require_once __DIR__ . '/_inc/access-check.php';
  11. require_once __DIR__ . '/logged-out-view/logged-out-view.php';
  12. require_once __DIR__ . '/subscriber-view/subscriber-view.php';
  13. require_once __DIR__ . '/buttons/buttons.php';
  14. require_once __DIR__ . '/login-button/login-button.php';
  15. const FEATURE_NAME = 'premium-content/container';
  16. /**
  17. * Registers the block for use in Gutenberg
  18. * This is done via an action so that we can disable
  19. * registration if we need to.
  20. */
  21. function register_block() {
  22. // Only load this block on WordPress.com.
  23. if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || jetpack_is_atomic_site() ) {
  24. // Determine required `context` key based on Gutenberg version.
  25. $deprecated = function_exists( 'gutenberg_get_post_from_context' );
  26. $provides = $deprecated ? 'providesContext' : 'provides_context';
  27. Blocks::jetpack_register_block(
  28. FEATURE_NAME,
  29. array(
  30. 'render_callback' => __NAMESPACE__ . '\render_block',
  31. 'plan_check' => true,
  32. 'attributes' => array(
  33. 'isPremiumContentChild' => array(
  34. 'type' => 'boolean',
  35. 'default' => true,
  36. ),
  37. ),
  38. $provides => array(
  39. 'premium-content/planId' => 'selectedPlanId',
  40. 'isPremiumContentChild' => 'isPremiumContentChild',
  41. ),
  42. )
  43. );
  44. }
  45. }
  46. add_action( 'init', __NAMESPACE__ . '\register_block' );
  47. /**
  48. * Render callback.
  49. *
  50. * @param array $attributes Array containing the block attributes.
  51. * @param string $content String containing the block content.
  52. *
  53. * @return string
  54. */
  55. function render_block( $attributes, $content ) {
  56. if ( ! pre_render_checks() ) {
  57. return '';
  58. }
  59. if (
  60. ! membership_checks()
  61. // Only display Stripe nudge if Upgrade nudge isn't displaying.
  62. && required_plan_checks()
  63. ) {
  64. $stripe_nudge = render_stripe_nudge();
  65. return $stripe_nudge . $content;
  66. }
  67. // We don't use FEATURE_NAME here because styles are not in /container folder.
  68. Jetpack_Gutenberg::load_styles_as_required( 'premium-content' );
  69. return $content;
  70. }
  71. /**
  72. * Server-side rendering for the stripe connection nudge.
  73. *
  74. * @return string Final content to render.
  75. */
  76. function render_stripe_nudge() {
  77. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  78. \jetpack_require_lib( 'memberships' );
  79. $blog_id = get_current_blog_id();
  80. $settings = (array) \get_memberships_settings_for_site( $blog_id );
  81. return stripe_nudge(
  82. $settings['connect_url'],
  83. __( 'Connect to Stripe to use this block on your site.', 'jetpack' ),
  84. __( 'Connect', 'jetpack' )
  85. );
  86. } elseif ( jetpack_is_atomic_site() ) {
  87. // On Atomic sites, the Stripe connection url is not easily available
  88. // server-side, so we redirect them to the post in the editor in order
  89. // to connect.
  90. return stripe_nudge(
  91. get_edit_post_link( get_the_ID() ),
  92. __( 'Connect to Stripe in the editor to use this block on your site.', 'jetpack' ),
  93. __( 'Edit post', 'jetpack' )
  94. );
  95. }
  96. // The Premium Content block is not supported on Jetpack sites.
  97. return '';
  98. }
  99. /**
  100. * Render the stripe nudge.
  101. *
  102. * @param string $checkout_url Url for the CTA.
  103. * @param string $description Text of the nudge.
  104. * @param string $button_text Text of the button.
  105. *
  106. * @return string Final content to render.
  107. */
  108. function stripe_nudge( $checkout_url, $description, $button_text ) {
  109. \jetpack_require_lib( 'components' );
  110. return \Jetpack_Components::render_frontend_nudge(
  111. array(
  112. 'checkoutUrl' => $checkout_url,
  113. 'description' => $description,
  114. 'buttonText' => $button_text,
  115. )
  116. );
  117. }