Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.67% covered (danger)
30.67%
50 / 163
12.50% covered (danger)
12.50%
1 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Subscription_Site
30.67% covered (danger)
30.67%
50 / 163
12.50% covered (danger)
12.50%
1 / 8
719.68
0.00% covered (danger)
0.00%
0 / 1
 init
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 handle_subscribe_block_placements
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 user_can_view_post
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_post_end_placement_block_attributes
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 is_header_context
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
5.20
 is_single_post_context
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 handle_subscribe_block_navigation_placement
92.86% covered (success)
92.86%
39 / 42
0.00% covered (danger)
0.00%
0 / 1
14.07
 handle_subscribe_block_post_end_placement
3.49% covered (danger)
3.49%
3 / 86
0.00% covered (danger)
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
9namespace Automattic\Jetpack\Extensions\Subscriptions;
10
11use Jetpack_Memberships;
12use WP_Block_Template;
13use WP_Post;
14
15/**
16 * Jetpack_Subscription_Site class.
17 */
18class 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        $this->handle_subscribe_block_post_end_placement();
44        $this->handle_subscribe_block_navigation_placement();
45    }
46
47    /**
48     * Returns true if current user can view the post.
49     *
50     * @return bool
51     */
52    protected function user_can_view_post() {
53        if ( ! class_exists( 'Jetpack_Memberships' ) ) {
54            return true;
55        }
56
57        return Jetpack_Memberships::user_can_view_post();
58    }
59
60    /**
61     * Returns post end placement hooked block attributes.
62     *
63     * @param array $default_attrs Deafult attributes.
64     * @param array $anchor_block The anchor block, in parsed block array format.
65     *
66     * @return array
67     */
68    protected function get_post_end_placement_block_attributes( $default_attrs, $anchor_block ) {
69        if ( ! empty( $anchor_block['attrs']['layout']['type'] ) ) {
70            return array_merge(
71                $default_attrs,
72                array(
73                    'layout' => array(
74                        'type' => $anchor_block['attrs']['layout']['type'],
75                    ),
76                )
77            );
78        }
79
80        if ( ! empty( $anchor_block['attrs']['layout']['inherit'] ) ) {
81            return array_merge(
82                $default_attrs,
83                array(
84                    'layout' => array(
85                        'inherit' => $anchor_block['attrs']['layout']['inherit'],
86                    ),
87                )
88            );
89        }
90
91        return $default_attrs;
92    }
93
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                if ( null === $hooked_block ) {
167                    return null;
168                }
169
170                $is_navigation_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/navigation';
171
172                if ( $is_navigation_anchor_block && $relative_position === 'last_child' && has_block( 'jetpack/subscriptions', serialize_blocks( $anchor_block['innerBlocks'] ?? array() ) ) ) {
173                    // Empty output lets core still record this type in ignoredHookedBlocks when saving.
174                    return array(
175                        'blockName'    => null,
176                        'attrs'        => array(),
177                        'innerBlocks'  => array(),
178                        'innerHTML'    => '',
179                        'innerContent' => array(),
180                    );
181                }
182
183                if ( $is_navigation_anchor_block ) {
184                    $class_name = ( ! empty( $hooked_block['attrs'] ) && ! empty( $hooked_block['attrs']['className'] ) )
185                        ? $hooked_block['attrs']['className'] . ' is-style-button'
186                        : 'is-style-button';
187
188                    $hooked_block['attrs']['className'] = $class_name;
189                    $hooked_block['attrs']['appSource'] = 'subscribe-block-navigation';
190                }
191
192                return $hooked_block;
193            },
194            10,
195            4
196        );
197    }
198
199    /**
200     * Handles Subscribe block placement at the end of each post.
201     *
202     * @return void
203     */
204    protected function handle_subscribe_block_post_end_placement() {
205        $subscribe_post_end_enabled = get_option( 'jetpack_subscriptions_subscribe_post_end_enabled', false );
206        if ( ! $subscribe_post_end_enabled ) {
207            return;
208        }
209
210        if ( ! wp_is_block_theme() ) { // Fallback for classic themes.
211            add_filter(
212                'the_content',
213                function ( $content ) {
214                    // Check if we're inside the main loop in a single Post.
215                    if (
216                        is_single() &&
217                        in_the_loop() &&
218                        is_main_query() &&
219                        $this->user_can_view_post()
220                    ) {
221                        // translators: %s is the name of the site.
222                        $discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
223                        $subscribe_text          = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' );
224
225                        return $content . do_blocks(
226                            <<<HTML
227<!-- 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"}} -->
228<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">
229    <!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"24px"}}},"className":"is-style-wide"} -->
230    <hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:24px"/>
231    <!-- /wp:separator -->
232
233    <!-- wp:heading {"textAlign":"center","level":3,"style":{"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
234    <h3 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px">$discover_more_from_text</h3>
235    <!-- /wp:heading -->
236
237    <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} -->
238    <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:15px">$subscribe_text</p>
239    <!-- /wp:paragraph -->
240
241    <!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} -->
242    <div class="wp-block-group">
243        <!-- wp:jetpack/subscriptions {"appSource":"subscribe-block-post-end"} /-->
244    </div>
245    <!-- /wp:group -->
246</div>
247<!-- /wp:group -->
248HTML
249                        );
250                    }
251
252                    return $content;
253                },
254                100, // To insert it after the sharing blocks.
255                1
256            );
257
258            return;
259        }
260
261        add_filter(
262            'hooked_block_types',
263            function ( $hooked_blocks, $relative_position, $anchor_block, $context ) {
264                if (
265                    $anchor_block === 'core/post-content' &&
266                    $relative_position === 'after' &&
267                    self::is_single_post_context( $context ) &&
268                    $this->user_can_view_post()
269                ) {
270                    $hooked_blocks[] = 'jetpack/subscriptions';
271                }
272
273                return $hooked_blocks;
274            },
275            10,
276            4
277        );
278
279        add_filter(
280            'hooked_block_jetpack/subscriptions',
281            function ( $hooked_block, $hooked_block_type, $relative_position, $anchor_block ) {
282                $is_post_content_anchor_block = isset( $anchor_block['blockName'] ) && $anchor_block['blockName'] === 'core/post-content';
283                if ( $is_post_content_anchor_block && ( $relative_position === 'after' || $relative_position === 'before' ) ) {
284                    $attrs = $this->get_post_end_placement_block_attributes(
285                        array(
286                            'style' => array(
287                                'spacing' => array(
288                                    'margin'  => array(
289                                        'top'    => '48px',
290                                        'bottom' => '48px',
291                                    ),
292                                    'padding' => array(
293                                        'top'    => '5px',
294                                        'bottom' => '5px',
295                                    ),
296                                ),
297                            ),
298                        ),
299                        $anchor_block
300                    );
301
302                    // translators: %s is the name of the site.
303                    $discover_more_from_text = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
304                    $subscribe_text          = __( 'Subscribe to get the latest posts sent to your email.', 'jetpack' );
305                    $inner_content_begin     = <<<HTML
306<div class="wp-block-group" style="margin-top:48px;margin-bottom:48px;padding-top:5px;padding-bottom:5px">
307    <!-- wp:separator {"style":{"spacing":{"margin":{"bottom":"36px"}}},"className":"is-style-wide"} -->
308    <hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-bottom:36px"/>
309    <!-- /wp:separator -->
310
311    <!-- wp:heading {"textAlign":"center","level":3,"style":{"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
312    <h3 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px">$discover_more_from_text</h3>
313    <!-- /wp:heading -->
314
315    <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} -->
316    <p class="has-text-align-center" style="margin-top:4px;margin-bottom:0px;font-size:15px">$subscribe_text</p>
317    <!-- /wp:paragraph -->
318
319    <!-- wp:group {"layout":{"type":"constrained","contentSize":"480px"}} -->
320    <div class="wp-block-group">
321HTML;
322                    $inner_content_end       = <<<'HTML'
323    </div>
324    <!-- /wp:group -->
325</div>
326HTML;
327
328                    $hooked_block['attrs']['appSource'] = 'subscribe-block-post-end';
329
330                    return array(
331                        'blockName'    => 'core/group',
332                        'attrs'        => $attrs,
333                        'innerBlocks'  => array( $hooked_block ),
334                        'innerContent' => array(
335                            $inner_content_begin,
336                            null,
337                            $inner_content_end,
338                        ),
339                    );
340                }
341
342                return $hooked_block;
343            },
344            10,
345            4
346        );
347    }
348}