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