← All changes
|
extensions/blocks/premium-content/premium-content.php
+71
-29
12.1.3
→
16.3-beta
View file →
| @@ -7,10 +7,11 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Extensions\Premium_Content; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Blocks; |
| 11 | -use Automattic\Jetpack\Status\Host; | |
| 12 | 11 | use Jetpack_Gutenberg; |
| 12 | +use WP_Post; | |
| 13 | +use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_CONTAINS_PAID_CONTENT; | |
| 13 | 14 | |
| 14 | 15 | require_once __DIR__ . '/_inc/access-check.php'; |
| 15 | 16 | require_once __DIR__ . '/logged-out-view/logged-out-view.php'; |
| 16 | 17 | require_once __DIR__ . '/subscriber-view/subscriber-view.php'; |
| @@ -15,11 +16,10 @@ | ||
| 15 | 16 | require_once __DIR__ . '/logged-out-view/logged-out-view.php'; |
| 16 | 17 | require_once __DIR__ . '/subscriber-view/subscriber-view.php'; |
| 17 | 18 | require_once __DIR__ . '/buttons/buttons.php'; |
| 18 | 19 | require_once __DIR__ . '/login-button/login-button.php'; |
| 20 | +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/subscriptions/constants.php'; | |
| 19 | 21 | |
| 20 | -const FEATURE_NAME = 'premium-content/container'; | |
| 21 | - | |
| 22 | 22 | /** |
| 23 | 23 | * Registers the block for use in Gutenberg |
| 24 | 24 | * This is done via an action so that we can disable |
| 25 | 25 | * registration if we need to. |
| @@ -24,34 +24,54 @@ | ||
| 24 | 24 | * This is done via an action so that we can disable |
| 25 | 25 | * registration if we need to. |
| 26 | 26 | */ |
| 27 | 27 | function register_block() { |
| 28 | - // Only load this block on WordPress.com. | |
| 29 | - if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || ( new Host() )->is_woa_site() ) { | |
| 30 | - // Determine required `context` key based on Gutenberg version. | |
| 31 | - $deprecated = function_exists( 'gutenberg_get_post_from_context' ); | |
| 32 | - $provides = $deprecated ? 'providesContext' : 'provides_context'; | |
| 33 | 28 | |
| 29 | + require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php'; | |
| 30 | + if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) { | |
| 34 | 31 | Blocks::jetpack_register_block( |
| 35 | - FEATURE_NAME, | |
| 32 | + __DIR__, | |
| 36 | 33 | array( |
| 37 | - 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 38 | - 'plan_check' => true, | |
| 39 | - 'attributes' => array( | |
| 40 | - 'isPremiumContentChild' => array( | |
| 41 | - 'type' => 'boolean', | |
| 42 | - 'default' => true, | |
| 43 | - ), | |
| 34 | + 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 35 | + 'provides_context' => array( | |
| 36 | + 'premium-content/planId' => 'selectedPlanId', // Deprecated. | |
| 37 | + 'premium-content/planIds' => 'selectedPlanIds', | |
| 38 | + 'isPremiumContentChild' => 'isPremiumContentChild', | |
| 44 | 39 | ), |
| 45 | - $provides => array( | |
| 46 | - 'premium-content/planId' => 'selectedPlanId', | |
| 47 | - 'isPremiumContentChild' => 'isPremiumContentChild', | |
| 48 | - ), | |
| 49 | 40 | ) |
| 50 | 41 | ); |
| 51 | 42 | } |
| 43 | + | |
| 44 | + register_post_meta( | |
| 45 | + 'post', | |
| 46 | + META_NAME_CONTAINS_PAID_CONTENT, | |
| 47 | + array( | |
| 48 | + 'show_in_rest' => true, | |
| 49 | + 'single' => true, | |
| 50 | + 'type' => 'boolean', | |
| 51 | + 'auth_callback' => function () { | |
| 52 | + return wp_get_current_user()->has_cap( 'edit_posts' ); | |
| 53 | + }, | |
| 54 | + ) | |
| 55 | + ); | |
| 56 | + | |
| 57 | + // This ensures Jetpack will sync this post meta to WPCOM. | |
| 58 | + add_filter( | |
| 59 | + 'jetpack_sync_post_meta_whitelist', | |
| 60 | + function ( $allowed_meta ) { | |
| 61 | + return array_merge( | |
| 62 | + $allowed_meta, | |
| 63 | + array( | |
| 64 | + META_NAME_CONTAINS_PAID_CONTENT, | |
| 65 | + ) | |
| 66 | + ); | |
| 67 | + } | |
| 68 | + ); | |
| 69 | + | |
| 70 | + add_action( 'wp_after_insert_post', __NAMESPACE__ . '\add_paid_content_post_meta', 99, 2 ); | |
| 52 | 71 | } |
| 53 | 72 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 73 | +add_action( 'template_redirect', __NAMESPACE__ . '\prewarm_premium_content_session_cookie' ); | |
| 54 | 74 | |
| 55 | 75 | /** |
| 56 | 76 | * Render callback. |
| 57 | 77 | * |
| @@ -64,13 +84,10 @@ | ||
| 64 | 84 | if ( ! pre_render_checks() ) { |
| 65 | 85 | return ''; |
| 66 | 86 | } |
| 67 | 87 | |
| 68 | - if ( | |
| 69 | - ! membership_checks() | |
| 70 | - // Only display Stripe nudge if Upgrade nudge isn't displaying. | |
| 71 | - && required_plan_checks() | |
| 72 | - ) { | |
| 88 | + // Render the Stripe nudge when Stripe is unconnected | |
| 89 | + if ( ! membership_checks() ) { | |
| 73 | 90 | $stripe_nudge = render_stripe_nudge(); |
| 74 | 91 | return $stripe_nudge . $content; |
| 75 | 92 | } |
| 76 | 93 | |
| @@ -94,9 +111,9 @@ | ||
| 94 | 111 | $settings['connect_url'], |
| 95 | 112 | __( 'Connect to Stripe to use this block on your site.', 'jetpack' ), |
| 96 | 113 | __( 'Connect', 'jetpack' ) |
| 97 | 114 | ); |
| 98 | - } elseif ( ( new Host() )->is_woa_site() ) { | |
| 115 | + } else { | |
| 99 | 116 | // On WoA sites, the Stripe connection url is not easily available |
| 100 | 117 | // server-side, so we redirect them to the post in the editor in order |
| 101 | 118 | // to connect. |
| 102 | 119 | return stripe_nudge( |
| @@ -104,11 +121,8 @@ | ||
| 104 | 121 | __( 'Connect to Stripe in the editor to use this block on your site.', 'jetpack' ), |
| 105 | 122 | __( 'Edit post', 'jetpack' ) |
| 106 | 123 | ); |
| 107 | 124 | } |
| 108 | - | |
| 109 | - // The Premium Content block is not supported on Jetpack sites. | |
| 110 | - return ''; | |
| 111 | 125 | } |
| 112 | 126 | |
| 113 | 127 | /** |
| 114 | 128 | * Render the stripe nudge. |
| @@ -127,5 +141,33 @@ | ||
| 127 | 141 | 'description' => $description, |
| 128 | 142 | 'buttonText' => $button_text, |
| 129 | 143 | ) |
| 130 | 144 | ); |
| 145 | +} | |
| 146 | + | |
| 147 | +/** | |
| 148 | + * Add a meta to prevent publication on firehose, ES AI or Reader | |
| 149 | + * | |
| 150 | + * @param int $post_id Post id. | |
| 151 | + * @param WP_Post $post Post being saved. | |
| 152 | + * @return void | |
| 153 | + */ | |
| 154 | +function add_paid_content_post_meta( int $post_id, WP_Post $post ) { | |
| 155 | + if ( $post->post_type !== 'post' && $post->post_type !== 'page' ) { | |
| 156 | + return; | |
| 157 | + } | |
| 158 | + | |
| 159 | + $contains_paid_content = has_block( 'premium-content/container', $post ); | |
| 160 | + if ( $contains_paid_content ) { | |
| 161 | + update_post_meta( | |
| 162 | + $post_id, | |
| 163 | + META_NAME_CONTAINS_PAID_CONTENT, | |
| 164 | + $contains_paid_content | |
| 165 | + ); | |
| 166 | + } | |
| 167 | + if ( ! $contains_paid_content ) { | |
| 168 | + delete_post_meta( | |
| 169 | + $post_id, | |
| 170 | + META_NAME_CONTAINS_PAID_CONTENT | |
| 171 | + ); | |
| 172 | + } | |
| 131 | 173 | } |