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