Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
40.84% |
78 / 191 |
|
22.22% |
2 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Subscription_Site | |
40.84% |
78 / 191 |
|
22.22% |
2 / 9 |
681.41 | |
0.00% |
0 / 1 |
| init | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| handle_subscribe_block_placements | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| seed_missing_options | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
10 | |||
| user_can_view_post | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_post_end_placement_block_attributes | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| is_header_context | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
5.20 | |||
| is_single_post_context | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| handle_subscribe_block_navigation_placement | |
92.86% |
39 / 42 |
|
0.00% |
0 / 1 |
14.07 | |||
| handle_subscribe_block_post_end_placement | |
3.49% |
3 / 86 |
|
0.00% |
0 / 1 |
217.27 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adds support for Jetpack Subscription Site feature. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 13.2 |
| 7 | */ |
| 8 | |
| 9 | namespace Automattic\Jetpack\Extensions\Subscriptions; |
| 10 | |
| 11 | use Jetpack_Memberships; |
| 12 | use WP_Block_Template; |
| 13 | use WP_Post; |
| 14 | |
| 15 | /** |
| 16 | * Jetpack_Subscription_Site class. |
| 17 | */ |
| 18 | class Jetpack_Subscription_Site { |
| 19 | /** |
| 20 | * Jetpack_Subscription_Site singleton instance. |
| 21 | * |
| 22 | * @var Jetpack_Subscription_Site|null |
| 23 | */ |
| 24 | private static $instance; |
| 25 | |
| 26 | /** |
| 27 | * Jetpack_Subscription_Site instance init. |
| 28 | */ |
| 29 | public static function init() { |
| 30 | if ( self::$instance === null ) { |
| 31 | self::$instance = new Jetpack_Subscription_Site(); |
| 32 | } |
| 33 | |
| 34 | return self::$instance; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Handles Subscribe block placements. |
| 39 | * |
| 40 | * @return void |
| 41 | */ |
| 42 | public function handle_subscribe_block_placements() { |
| 43 | self::seed_missing_options( |
| 44 | array( |
| 45 | 'jetpack_subscriptions_subscribe_post_end_enabled', |
| 46 | 'jetpack_subscriptions_subscribe_navigation_enabled', |
| 47 | ) |
| 48 | ); |
| 49 | |
| 50 | $this->handle_subscribe_block_post_end_placement(); |
| 51 | $this->handle_subscribe_block_navigation_placement(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Stores unset placement options as disabled, so they are autoloaded instead of queried on every request. |
| 56 | * |
| 57 | * @since $$next-version$$ |
| 58 | * |
| 59 | * @param string[] $option_names Option names. |
| 60 | * @return void |
| 61 | */ |
| 62 | public static function seed_missing_options( $option_names ) { |
| 63 | // Only write from a back-end request, and never where a persistent cache already remembers the miss. |
| 64 | if ( |
| 65 | wp_using_ext_object_cache() |
| 66 | || ! ( is_admin() || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) |
| 67 | ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | global $wpdb; |
| 72 | $missing = new \stdClass(); |
| 73 | $seeded = false; |
| 74 | foreach ( $option_names as $option_name ) { |
| 75 | if ( $missing !== get_option( $option_name, $missing ) ) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- add_option() can overwrite a concurrent enable. |
| 80 | $inserted = $wpdb->query( |
| 81 | $wpdb->prepare( |
| 82 | "INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, %s )", |
| 83 | $option_name, |
| 84 | '0', |
| 85 | 'yes' |
| 86 | ) |
| 87 | ); |
| 88 | if ( false !== $inserted ) { |
| 89 | wp_cache_delete( $option_name, 'options' ); |
| 90 | $seeded = true; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if ( $seeded ) { |
| 95 | wp_cache_delete( 'notoptions', 'options' ); |
| 96 | wp_cache_delete( 'alloptions', 'options' ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns true if current user can view the post. |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | protected function user_can_view_post() { |
| 106 | if ( ! class_exists( 'Jetpack_Memberships' ) ) { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | return Jetpack_Memberships::user_can_view_post(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns post end placement hooked block attributes. |
| 115 | * |
| 116 | * @param array $default_attrs Deafult attributes. |
| 117 | * @param array $anchor_block The anchor block, in parsed block array format. |
| 118 | * |
| 119 | * @return array |
| 120 | */ |
| 121 | protected function get_post_end_placement_block_attributes( $default_attrs, $anchor_block ) { |
| 122 | if ( ! empty( $anchor_block['attrs']['layout']['type'] ) ) { |
| 123 | return array_merge( |
| 124 | $default_attrs, |
| 125 | array( |
| 126 | 'layout' => array( |
| 127 | 'type' => $anchor_block['attrs']['layout']['type'], |
| 128 | ), |
| 129 | ) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | if ( ! empty( $anchor_block['attrs']['layout']['inherit'] ) ) { |
| 134 | return array_merge( |
| 135 | $default_attrs, |
| 136 | array( |
| 137 | 'layout' => array( |
| 138 | 'inherit' => $anchor_block['attrs']['layout']['inherit'], |
| 139 | ), |
| 140 | ) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | return $default_attrs; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns true if context is recognized as a header element. |
| 149 | * |
| 150 | * @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to. |
| 151 | * |
| 152 | * @return bool |
| 153 | */ |
| 154 | protected function is_header_context( $context ) { |
| 155 | if ( $context instanceof WP_Post && $context->post_type === 'wp_navigation' ) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | if ( $context instanceof WP_Block_Template && $context->area === 'header' ) { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns true if context is recognized as a single post element. |
| 168 | * |
| 169 | * @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern the anchor block belongs to. |
| 170 | * |
| 171 | * @return bool |
| 172 | */ |
| 173 | protected function is_single_post_context( $context ) { |
| 174 | /** |
| 175 | * Some themes are not returning a WP_Block_Template. In that case, we need to check with is_singular function. |
| 176 | */ |
| 177 | if ( is_array( $context ) ) { |
| 178 | return is_singular( 'post' ); |
| 179 | } |
| 180 | |
| 181 | return $context instanceof WP_Block_Template && $context->slug === 'single'; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Handles Subscription block navigation placement. |
| 186 | * |
| 187 | * @return void |
| 188 | */ |
| 189 | protected function handle_subscribe_block_navigation_placement() { |
| 190 | $is_enabled = get_option( 'jetpack_subscriptions_subscribe_navigation_enabled', false ); |
| 191 | if ( ! $is_enabled ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | if ( ! wp_is_block_theme() ) { // TODO Fallback for classic themes. |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | add_filter( |
| 200 | 'hooked_block_types', |
| 201 | function ( $hooked_blocks, $relative_position, $anchor_block, $context ) { |
| 202 | if ( |
| 203 | $anchor_block === 'core/navigation' && |
| 204 | $relative_position === 'last_child' && |
| 205 | self::is_header_context( $context ) |
| 206 | ) { |
| 207 | $hooked_blocks[] = 'jetpack/subscriptions'; |
| 208 | } |
| 209 | |
| 210 | return $hooked_blocks; |
| 211 | }, |
| 212 | 10, |
| 213 | 4 |
| 214 | ); |
| 215 | |
| 216 | add_filter( |
| 217 | 'hooked_block_jetpack/subscriptions', |
| 218 | function ( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) { |
| 219 | if ( null === $hooked_block ) { |
| 220 | return null; |
| 221 | } |
| 222 | |
| 223 | $is_navigation_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/navigation'; |
| 224 | |
| 225 | if ( $is_navigation_anchor_block && $relative_position === 'last_child' && has_block( 'jetpack/subscriptions', serialize_blocks( $anchor_block['innerBlocks'] ?? array() ) ) ) { |
| 226 | // Empty output lets core still record this type in ignoredHookedBlocks when saving. |
| 227 | return array( |
| 228 | 'blockName' => null, |
| 229 | 'attrs' => array(), |
| 230 | 'innerBlocks' => array(), |
| 231 | 'innerHTML' => '', |
| 232 | 'innerContent' => array(), |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | if ( $is_navigation_anchor_block ) { |
| 237 | $class_name = ( ! empty( $hooked_block['attrs'] ) && ! empty( $hooked_block['attrs']['className'] ) ) |
| 238 | ? $hooked_block['attrs']['className'] . ' is-style-button' |
| 239 | : 'is-style-button'; |
| 240 | |
| 241 | $hooked_block['attrs']['className'] = $class_name; |
| 242 | $hooked_block['attrs']['appSource'] = 'subscribe-block-navigation'; |
| 243 | } |
| 244 | |
| 245 | return $hooked_block; |
| 246 | }, |
| 247 | 10, |
| 248 | 4 |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Handles Subscribe block placement at the end of each post. |
| 254 | * |
| 255 | * @return void |
| 256 | */ |
| 257 | protected function handle_subscribe_block_post_end_placement() { |
| 258 | $subscribe_post_end_enabled = get_option( 'jetpack_subscriptions_subscribe_post_end_enabled', false ); |
| 259 | if ( ! $subscribe_post_end_enabled ) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | if ( ! wp_is_block_theme() ) { // Fallback for classic themes. |
| 264 | add_filter( |
| 265 | 'the_content', |
| 266 | function ( $content ) { |
| 267 | // Check if we're inside the main loop in a single Post. |
| 268 | if ( |
| 269 | is_single() && |
| 270 | in_the_loop() && |
| 271 | is_main_query() && |
| 272 | $this->user_can_view_post() |
| 273 | ) { |
| 274 | // translators: %s is the name of the site. |
| 275 | $discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
| 276 | $subscribe_text = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' ); |
| 277 | |
| 278 | return $content . do_blocks( |
| 279 | <<<HTML |
| 280 | <!-- 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"}} --> |
| 281 | <div class="wp-block-group has-border-color" style="border-style:none;border-width:0px;margin-top:32px;margin-bottom:32px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px"> |
| 282 | <!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"24px"}}},"className":"is-style-wide"} --> |
| 283 | <hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:24px"/> |
| 284 | <!-- /wp:separator --> |
| 285 | |
| 286 | <!-- wp:heading {"textAlign":"center","level":3,"style":{"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} --> |
| 287 | <h3 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px">$discover_more_from_text</h3> |
| 288 | <!-- /wp:heading --> |
| 289 | |
| 290 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} --> |
| 291 | <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:15px">$subscribe_text</p> |
| 292 | <!-- /wp:paragraph --> |
| 293 | |
| 294 | <!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} --> |
| 295 | <div class="wp-block-group"> |
| 296 | <!-- wp:jetpack/subscriptions {"appSource":"subscribe-block-post-end"} /--> |
| 297 | </div> |
| 298 | <!-- /wp:group --> |
| 299 | </div> |
| 300 | <!-- /wp:group --> |
| 301 | HTML |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | return $content; |
| 306 | }, |
| 307 | 100, // To insert it after the sharing blocks. |
| 308 | 1 |
| 309 | ); |
| 310 | |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | add_filter( |
| 315 | 'hooked_block_types', |
| 316 | function ( $hooked_blocks, $relative_position, $anchor_block, $context ) { |
| 317 | if ( |
| 318 | $anchor_block === 'core/post-content' && |
| 319 | $relative_position === 'after' && |
| 320 | self::is_single_post_context( $context ) && |
| 321 | $this->user_can_view_post() |
| 322 | ) { |
| 323 | $hooked_blocks[] = 'jetpack/subscriptions'; |
| 324 | } |
| 325 | |
| 326 | return $hooked_blocks; |
| 327 | }, |
| 328 | 10, |
| 329 | 4 |
| 330 | ); |
| 331 | |
| 332 | add_filter( |
| 333 | 'hooked_block_jetpack/subscriptions', |
| 334 | function ( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) { |
| 335 | $is_post_content_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/post-content'; |
| 336 | if ( $is_post_content_anchor_block && ( $relative_position === 'after' || $relative_position === 'before' ) ) { |
| 337 | $attrs = $this->get_post_end_placement_block_attributes( |
| 338 | array( |
| 339 | 'style' => array( |
| 340 | 'spacing' => array( |
| 341 | 'margin' => array( |
| 342 | 'top' => '48px', |
| 343 | 'bottom' => '48px', |
| 344 | ), |
| 345 | 'padding' => array( |
| 346 | 'top' => '5px', |
| 347 | 'bottom' => '5px', |
| 348 | ), |
| 349 | ), |
| 350 | ), |
| 351 | ), |
| 352 | $anchor_block |
| 353 | ); |
| 354 | |
| 355 | // translators: %s is the name of the site. |
| 356 | $discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
| 357 | $subscribe_text = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' ); |
| 358 | $inner_content_begin = <<<HTML |
| 359 | <div class="wp-block-group" style="margin-top:48px;margin-bottom:48px;padding-top:5px;padding-bottom:5px"> |
| 360 | <!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"36px"}}},"className":"is-style-wide"} --> |
| 361 | <hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:36px"/> |
| 362 | <!-- /wp:separator --> |
| 363 | |
| 364 | <!-- wp:heading {"textAlign":"center","level":3,"style":{"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} --> |
| 365 | <h3 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px">$discover_more_from_text</h3> |
| 366 | <!-- /wp:heading --> |
| 367 | |
| 368 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} --> |
| 369 | <p class="has-text-align-center" style="margin-top:4px;margin-bottom:0px;font-size:15px">$subscribe_text</p> |
| 370 | <!-- /wp:paragraph --> |
| 371 | |
| 372 | <!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} --> |
| 373 | <div class="wp-block-group"> |
| 374 | HTML; |
| 375 | $inner_content_end = <<<'HTML' |
| 376 | </div> |
| 377 | <!-- /wp:group --> |
| 378 | </div> |
| 379 | HTML; |
| 380 | |
| 381 | $hooked_block['attrs']['appSource'] = 'subscribe-block-post-end'; |
| 382 | |
| 383 | return array( |
| 384 | 'blockName' => 'core/group', |
| 385 | 'attrs' => $attrs, |
| 386 | 'innerBlocks' => array( $hooked_block ), |
| 387 | 'innerContent' => array( |
| 388 | $inner_content_begin, |
| 389 | null, |
| 390 | $inner_content_end, |
| 391 | ), |
| 392 | ); |
| 393 | } |
| 394 | |
| 395 | return $hooked_block; |
| 396 | }, |
| 397 | 10, |
| 398 | 4 |
| 399 | ); |
| 400 | } |
| 401 | } |