← All changes
|
extensions/blocks/subscriptions/class-jetpack-subscription-site.php
+104
-10
13.4.5
→
16.3-a.7
View file →
| @@ -8,8 +8,10 @@ | ||
| 8 | 8 | |
| 9 | 9 | namespace Automattic\Jetpack\Extensions\Subscriptions; |
| 10 | 10 | |
| 11 | 11 | use Jetpack_Memberships; |
| 12 | +use WP_Block_Template; | |
| 13 | +use WP_Post; | |
| 12 | 14 | |
| 13 | 15 | /** |
| 14 | 16 | * Jetpack_Subscription_Site class. |
| 15 | 17 | */ |
| @@ -38,8 +40,9 @@ | ||
| 38 | 40 | * @return void |
| 39 | 41 | */ |
| 40 | 42 | public function handle_subscribe_block_placements() { |
| 41 | 43 | $this->handle_subscribe_block_post_end_placement(); |
| 44 | + $this->handle_subscribe_block_navigation_placement(); | |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | 47 | /** |
| 45 | 48 | * Returns true if current user can view the post. |
| @@ -88,27 +91,115 @@ | ||
| 88 | 91 | return $default_attrs; |
| 89 | 92 | } |
| 90 | 93 | |
| 91 | 94 | /** |
| 95 | + * Returns true if context is recognized as a header element. | |
| 96 | + * | |
| 97 | + * @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to. | |
| 98 | + * | |
| 99 | + * @return bool | |
| 100 | + */ | |
| 101 | + protected function is_header_context( $context ) { | |
| 102 | + if ( $context instanceof WP_Post && $context->post_type === 'wp_navigation' ) { | |
| 103 | + return true; | |
| 104 | + } | |
| 105 | + | |
| 106 | + if ( $context instanceof WP_Block_Template && $context->area === 'header' ) { | |
| 107 | + return true; | |
| 108 | + } | |
| 109 | + | |
| 110 | + return false; | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Returns true if context is recognized as a single post element. | |
| 115 | + * | |
| 116 | + * @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to. | |
| 117 | + * | |
| 118 | + * @return bool | |
| 119 | + */ | |
| 120 | + protected function is_single_post_context( $context ) { | |
| 121 | + /** | |
| 122 | + * Some themes are not returning a WP_Block_Template. In that case, we need to check with is_singular function. | |
| 123 | + */ | |
| 124 | + if ( is_array( $context ) ) { | |
| 125 | + return is_singular( 'post' ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + return $context instanceof WP_Block_Template && $context->slug === 'single'; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Handles Subscription block navigation placement. | |
| 133 | + * | |
| 134 | + * @return void | |
| 135 | + */ | |
| 136 | + protected function handle_subscribe_block_navigation_placement() { | |
| 137 | + $is_enabled = get_option( 'jetpack_subscriptions_subscribe_navigation_enabled', false ); | |
| 138 | + if ( ! $is_enabled ) { | |
| 139 | + return; | |
| 140 | + } | |
| 141 | + | |
| 142 | + if ( ! wp_is_block_theme() ) { // TODO Fallback for classic themes. | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 146 | + add_filter( | |
| 147 | + 'hooked_block_types', | |
| 148 | + function ( $hooked_blocks, $relative_position, $anchor_block, $context ) { | |
| 149 | + if ( | |
| 150 | + $anchor_block === 'core/navigation' && | |
| 151 | + $relative_position === 'last_child' && | |
| 152 | + self::is_header_context( $context ) | |
| 153 | + ) { | |
| 154 | + $hooked_blocks[] = 'jetpack/subscriptions'; | |
| 155 | + } | |
| 156 | + | |
| 157 | + return $hooked_blocks; | |
| 158 | + }, | |
| 159 | + 10, | |
| 160 | + 4 | |
| 161 | + ); | |
| 162 | + | |
| 163 | + add_filter( | |
| 164 | + 'hooked_block_jetpack/subscriptions', | |
| 165 | + function ( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) { | |
| 166 | + $is_navigation_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/navigation'; | |
| 167 | + | |
| 168 | + if ( $is_navigation_anchor_block ) { | |
| 169 | + $class_name = ( ! empty( $hooked_block['attrs'] ) && ! empty( $hooked_block['attrs']['className'] ) ) | |
| 170 | + ? $hooked_block['attrs']['className'] . ' is-style-button' | |
| 171 | + : 'is-style-button'; | |
| 172 | + | |
| 173 | + $hooked_block['attrs']['className'] = $class_name; | |
| 174 | + $hooked_block['attrs']['appSource'] = 'subscribe-block-navigation'; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return $hooked_block; | |
| 178 | + }, | |
| 179 | + 10, | |
| 180 | + 4 | |
| 181 | + ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 92 | 185 | * Handles Subscribe block placement at the end of each post. |
| 93 | 186 | * |
| 94 | 187 | * @return void |
| 95 | 188 | */ |
| 96 | 189 | protected function handle_subscribe_block_post_end_placement() { |
| 97 | - global $wp_version; | |
| 98 | - | |
| 99 | 190 | $subscribe_post_end_enabled = get_option( 'jetpack_subscriptions_subscribe_post_end_enabled', false ); |
| 100 | 191 | if ( ! $subscribe_post_end_enabled ) { |
| 101 | 192 | return; |
| 102 | 193 | } |
| 103 | 194 | |
| 104 | - if ( ! wp_is_block_theme() || version_compare( $wp_version, '6.5-beta2', '<' ) ) { // Fallback for classic themes and wp core < 6.5-beta2. | |
| 195 | + if ( ! wp_is_block_theme() ) { // Fallback for classic themes. | |
| 105 | 196 | add_filter( |
| 106 | 197 | 'the_content', |
| 107 | 198 | function ( $content ) { |
| 108 | 199 | // Check if we're inside the main loop in a single Post. |
| 109 | 200 | if ( |
| 110 | - is_singular() && | |
| 201 | + is_single() && | |
| 111 | 202 | in_the_loop() && |
| 112 | 203 | is_main_query() && |
| 113 | 204 | $this->user_can_view_post() |
| 114 | 205 | ) { |
| @@ -113,9 +204,9 @@ | ||
| 113 | 204 | $this->user_can_view_post() |
| 114 | 205 | ) { |
| 115 | 206 | // translators: %s is the name of the site. |
| 116 | 207 | $discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
| 117 | - $subscribe_text = __( 'Subscribe to get the latest posts to your email.', 'jetpack' ); | |
| 208 | + $subscribe_text = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' ); | |
| 118 | 209 | |
| 119 | 210 | return $content . do_blocks( |
| 120 | 211 | <<<HTML |
| 121 | 212 | <!-- wp:group {"style":{"spacing":{"padding":{"top":"0px","bottom":"0px","left":"0px","right":"0px"},"margin":{"top":"32px","bottom":"32px"}},"border":{"width":"0px","style":"none"}},"className":"has-border-color","layout":{"type":"default"}} --> |
| @@ -133,9 +224,9 @@ | ||
| 133 | 224 | <!-- /wp:paragraph --> |
| 134 | 225 | |
| 135 | 226 | <!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} --> |
| 136 | 227 | <div class="wp-block-group"> |
| 137 | - <!-- wp:jetpack/subscriptions /--> | |
| 228 | + <!-- wp:jetpack/subscriptions {"appSource":"subscribe-block-post-end"} /--> | |
| 138 | 229 | </div> |
| 139 | 230 | <!-- /wp:group --> |
| 140 | 231 | </div> |
| 141 | 232 | <!-- /wp:group --> |
| @@ -153,12 +244,13 @@ | ||
| 153 | 244 | } |
| 154 | 245 | |
| 155 | 246 | add_filter( |
| 156 | 247 | 'hooked_block_types', |
| 157 | - function ( $hooked_blocks, $relative_position, $anchor_block ) { | |
| 248 | + function ( $hooked_blocks, $relative_position, $anchor_block, $context ) { | |
| 158 | 249 | if ( |
| 159 | 250 | $anchor_block === 'core/post-content' && |
| 160 | 251 | $relative_position === 'after' && |
| 252 | + self::is_single_post_context( $context ) && | |
| 161 | 253 | $this->user_can_view_post() |
| 162 | 254 | ) { |
| 163 | 255 | $hooked_blocks[] = 'jetpack/subscriptions'; |
| 164 | 256 | } |
| @@ -165,9 +257,9 @@ | ||
| 165 | 257 | |
| 166 | 258 | return $hooked_blocks; |
| 167 | 259 | }, |
| 168 | 260 | 10, |
| 169 | - 3 | |
| 261 | + 4 | |
| 170 | 262 | ); |
| 171 | 263 | |
| 172 | 264 | add_filter( |
| 173 | 265 | 'hooked_block_jetpack/subscriptions', |
| @@ -193,9 +285,9 @@ | ||
| 193 | 285 | ); |
| 194 | 286 | |
| 195 | 287 | // translators: %s is the name of the site. |
| 196 | 288 | $discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
| 197 | - $subscribe_text = __( 'Subscribe to get the latest posts to your email.', 'jetpack' ); | |
| 289 | + $subscribe_text = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' ); | |
| 198 | 290 | $inner_content_begin = <<<HTML |
| 199 | 291 | <div class="wp-block-group" style="margin-top:48px;margin-bottom:48px;padding-top:5px;padding-bottom:5px"> |
| 200 | 292 | <!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"36px"}}},"className":"is-style-wide"} --> |
| 201 | 293 | <hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:36px"/> |
| @@ -211,13 +303,15 @@ | ||
| 211 | 303 | |
| 212 | 304 | <!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} --> |
| 213 | 305 | <div class="wp-block-group"> |
| 214 | 306 | HTML; |
| 215 | - $inner_content_end = <<<HTML | |
| 307 | + $inner_content_end = <<<'HTML' | |
| 216 | 308 | </div> |
| 217 | 309 | <!-- /wp:group --> |
| 218 | 310 | </div> |
| 219 | 311 | HTML; |
| 312 | + | |
| 313 | + $hooked_block['attrs']['appSource'] = 'subscribe-block-post-end'; | |
| 220 | 314 | |
| 221 | 315 | return array( |
| 222 | 316 | 'blockName' => 'core/group', |
| 223 | 317 | 'attrs' => $attrs, |