← All changes
|
extensions/blocks/premium-content/premium-content.php
+71
-11
13.2.4
→
16.3-a.5
View file →
| @@ -8,8 +8,10 @@ | ||
| 8 | 8 | namespace Automattic\Jetpack\Extensions\Premium_Content; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Blocks; |
| 11 | 11 | use Jetpack_Gutenberg; |
| 12 | +use WP_Post; | |
| 13 | +use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_CONTAINS_PAID_CONTENT; | |
| 12 | 14 | |
| 13 | 15 | require_once __DIR__ . '/_inc/access-check.php'; |
| 14 | 16 | require_once __DIR__ . '/logged-out-view/logged-out-view.php'; |
| 15 | 17 | require_once __DIR__ . '/subscriber-view/subscriber-view.php'; |
| @@ -14,8 +16,9 @@ | ||
| 14 | 16 | require_once __DIR__ . '/logged-out-view/logged-out-view.php'; |
| 15 | 17 | require_once __DIR__ . '/subscriber-view/subscriber-view.php'; |
| 16 | 18 | require_once __DIR__ . '/buttons/buttons.php'; |
| 17 | 19 | require_once __DIR__ . '/login-button/login-button.php'; |
| 20 | +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/subscriptions/constants.php'; | |
| 18 | 21 | |
| 19 | 22 | /** |
| 20 | 23 | * Registers the block for use in Gutenberg |
| 21 | 24 | * This is done via an action so that we can disable |
| @@ -21,25 +24,54 @@ | ||
| 21 | 24 | * This is done via an action so that we can disable |
| 22 | 25 | * registration if we need to. |
| 23 | 26 | */ |
| 24 | 27 | 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 | 28 | |
| 29 | - Blocks::jetpack_register_block( | |
| 30 | - __DIR__, | |
| 29 | + require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php'; | |
| 30 | + if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) { | |
| 31 | + Blocks::jetpack_register_block( | |
| 32 | + __DIR__, | |
| 33 | + array( | |
| 34 | + 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 35 | + 'provides_context' => array( | |
| 36 | + 'premium-content/planId' => 'selectedPlanId', // Deprecated. | |
| 37 | + 'premium-content/planIds' => 'selectedPlanIds', | |
| 38 | + 'isPremiumContentChild' => 'isPremiumContentChild', | |
| 39 | + ), | |
| 40 | + ) | |
| 41 | + ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + register_post_meta( | |
| 45 | + 'post', | |
| 46 | + META_NAME_CONTAINS_PAID_CONTENT, | |
| 31 | 47 | 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 | - ), | |
| 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 | + }, | |
| 38 | 54 | ) |
| 39 | 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 ); | |
| 40 | 71 | } |
| 41 | 72 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 73 | +add_action( 'template_redirect', __NAMESPACE__ . '\prewarm_premium_content_session_cookie' ); | |
| 42 | 74 | |
| 43 | 75 | /** |
| 44 | 76 | * Render callback. |
| 45 | 77 | * |
| @@ -109,5 +141,33 @@ | ||
| 109 | 141 | 'description' => $description, |
| 110 | 142 | 'buttonText' => $button_text, |
| 111 | 143 | ) |
| 112 | 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 | + } | |
| 113 | 173 | } |