Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.54% covered (danger)
8.54%
7 / 82
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Subscribe_Overlay
9.59% covered (danger)
9.59%
7 / 73
0.00% covered (danger)
0.00%
0 / 9
607.40
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 __construct
63.64% covered (warning)
63.64%
7 / 11
0.00% covered (danger)
0.00%
0 / 1
2.19
 get_block_template_part_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_block_template_filter
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 get_template
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 get_subscribe_overlay_template_content
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_assets
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 add_subscribe_overlay_to_frontend
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 should_user_see_overlay
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2/**
3 * Adds support for Jetpack Subscribe Overlay feature
4 *
5 * @package automattic/jetpack-subscriptions
6 * @since 13.5
7 */
8
9if ( ! defined( 'ABSPATH' ) ) {
10    exit( 0 );
11}
12
13/**
14 * Jetpack_Subscribe_Overlay class.
15 */
16class Jetpack_Subscribe_Overlay {
17    /**
18     * Jetpack_Subscribe_Overlay singleton instance.
19     *
20     * @var Jetpack_Subscribe_Overlay|null
21     */
22    private static $instance;
23
24    /**
25     * Jetpack_Subscribe_Overlay instance init.
26     */
27    public static function init() {
28        if ( self::$instance === null ) {
29            self::$instance = new Jetpack_Subscribe_Overlay();
30        }
31
32        return self::$instance;
33    }
34
35    const BLOCK_TEMPLATE_PART_SLUG = 'jetpack-subscribe-overlay';
36
37    /**
38     * Jetpack_Subscribe_Overlay class constructor.
39     */
40    public function __construct() {
41        if ( get_option( 'jetpack_subscribe_overlay_enabled', false ) ) {
42            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
43            add_action( 'wp_footer', array( $this, 'add_subscribe_overlay_to_frontend' ) );
44        }
45
46        add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 );
47
48        add_filter(
49            'jetpack_options_whitelist',
50            function ( $options ) {
51                $options[] = 'jetpack_subscribe_overlay_enabled';
52
53                return $options;
54            }
55        );
56    }
57
58    /**
59     * Returns the block template part ID.
60     *
61     * @return string
62     */
63    public static function get_block_template_part_id() {
64        return get_stylesheet() . '//' . self::BLOCK_TEMPLATE_PART_SLUG;
65    }
66
67    /**
68     * Makes get_block_template return the WP_Block_Template for the Subscribe Overlay.
69     *
70     * @param WP_Block_Template $block_template The block template to be returned.
71     * @param string            $id Template unique identifier (example: theme_slug//template_slug).
72     * @param string            $template_type Template type: `'wp_template'` or '`wp_template_part'`.
73     *
74     * @return WP_Block_Template|null
75     */
76    public function get_block_template_filter( $block_template, $id, $template_type ) {
77        if ( empty( $block_template ) && $template_type === 'wp_template_part' ) {
78            if ( $id === self::get_block_template_part_id() ) {
79                return $this->get_template();
80            }
81        }
82
83        return $block_template;
84    }
85
86    /**
87     * Returns a custom template for the Subscribe Overlay.
88     *
89     * @return WP_Block_Template
90     */
91    public function get_template() {
92        $template                 = new WP_Block_Template();
93        $template->theme          = get_stylesheet();
94        $template->slug           = self::BLOCK_TEMPLATE_PART_SLUG;
95        $template->id             = self::get_block_template_part_id();
96        $template->area           = 'uncategorized';
97        $template->content        = $this->get_subscribe_overlay_template_content();
98        $template->source         = 'plugin';
99        $template->type           = 'wp_template_part';
100        $template->title          = __( 'Jetpack Subscribe overlay', 'jetpack' );
101        $template->status         = 'publish';
102        $template->has_theme_file = false;
103        $template->is_custom      = true;
104        $template->description    = __( 'An overlay that shows up when someone visits your site.', 'jetpack' );
105
106        return $template;
107    }
108
109    /**
110     * Returns the initial content of the Subscribe Overlay template.
111     * This can then be edited by the user.
112     *
113     * @return string
114     */
115    public function get_subscribe_overlay_template_content() {
116        $home_url         = get_home_url();
117        $site_tagline     = get_bloginfo( 'description' );
118        $default_tagline  = __( 'Stay informed with curated content and the latest headlines, all delivered straight to your inbox. Subscribe now to stay ahead and never miss a beat!', 'jetpack' );
119        $tagline_block    = empty( $site_tagline )
120            ? '<!-- wp:paragraph {"align":"center","fontSize":"medium"} --><p class="has-text-align-center has-medium-font-size">' . $default_tagline . '</p><!-- /wp:paragraph -->'
121            : '<!-- wp:site-tagline {"textAlign":"center","fontSize":"medium"} /-->';
122        $skip_to_content  = __( 'Skip to content', 'jetpack' );
123        $group_block_name = esc_attr__( 'Subscribe overlay container', 'jetpack' );
124
125        return <<<HTML
126    <!-- wp:group {"metadata":{"name":"$group_block_name"},"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}}},"layout":{"type":"constrained","contentSize":"400px"}} -->
127    <div class="wp-block-group" style="padding-top:0;padding-right:0;padding-bottom:0;padding-left:0">
128        <!-- wp:site-logo {"width":90,"isLink":false,"shouldSyncIcon":true,"align":"center","className":"is-style-rounded"} /-->
129
130        <!-- wp:site-title {"textAlign":"center","isLink":false,"fontSize":"x-large"} /-->
131
132        $tagline_block
133
134        <!-- wp:jetpack/subscriptions {"appSource":"subscribe-overlay"} /-->
135
136        <!-- wp:paragraph {"align":"center","className":"jetpack-subscribe-overlay__to-content"} -->
137        <p class="has-text-align-center jetpack-subscribe-overlay__to-content"><a href="$home_url">$skip_to_content ↓</a></p>
138        <!-- /wp:paragraph -->
139    </div>
140    <!-- /wp:group -->
141HTML;
142    }
143
144    /**
145     * Enqueues JS to load overlay.
146     *
147     * @return void
148     */
149    public function enqueue_assets() {
150        if ( $this->should_user_see_overlay() ) {
151            wp_enqueue_style( 'subscribe-overlay-css', plugins_url( 'subscribe-overlay.css', __FILE__ ), array(), JETPACK__VERSION );
152            wp_enqueue_script( 'subscribe-overlay-js', plugins_url( 'subscribe-overlay.js', __FILE__ ), array( 'wp-dom-ready' ), JETPACK__VERSION, true );
153        }
154    }
155
156    /**
157     * Adds overlay with Subscribe Overlay content.
158     *
159     * @return void
160     */
161    public function add_subscribe_overlay_to_frontend() {
162        if ( $this->should_user_see_overlay() ) { ?>
163                    <div class="jetpack-subscribe-overlay">
164                        <div class="jetpack-subscribe-overlay__close">
165                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
166                                <path d="M5.40456 5L19 19M5 19L18.5954 5" stroke="currentColor" stroke-width="1.5"/>
167                            </svg>
168                        </div>
169                        <div class="jetpack-subscribe-overlay__content">
170                            <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?>
171                        </div>
172                    </div>
173            <?php
174        }
175    }
176
177    /**
178     * Returns true if a site visitor should see
179     * the Subscribe Overlay.
180     *
181     * @return bool
182     */
183    public function should_user_see_overlay() {
184        // Only show when viewing frontend.
185        if ( is_admin() ) {
186            return false;
187        }
188
189        // Needed because Elementor editor makes is_admin() return false
190        // See https://coreysalzano.com/wordpress/why-elementor-disobeys-is_admin/
191        // Ignore nonce warning as just checking if is set
192        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
193        if ( isset( $_GET['elementor-preview'] ) ) {
194            return false;
195        }
196
197        // Don't show when previewing blog posts or site's theme
198        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
199        if ( isset( $_GET['preview'] ) || isset( $_GET['theme_preview'] ) || isset( $_GET['customize_preview'] ) || isset( $_GET['hide_banners'] ) ) {
200            return false;
201        }
202
203        // Don't show if one of subscribe query params is set.
204        // They are set when user submits the subscribe form.
205        // The nonce is checked elsewhere before redirect back to this page with query params.
206        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
207        if ( isset( $_GET['subscribe'] ) || isset( $_GET['blogsub'] ) ) {
208            return false;
209        }
210
211        // Don't show if user is subscribed to blog.
212        require_once __DIR__ . '/../views.php';
213        if ( ! class_exists( 'Jetpack_Memberships' ) || Jetpack_Memberships::is_current_user_subscribed() ) {
214            return false;
215        }
216
217        return is_home() || is_front_page();
218    }
219}
220
221Jetpack_Subscribe_Overlay::init();
222
223add_action(
224    'rest_api_switched_to_blog',
225    function () {
226        Jetpack_Subscribe_Overlay::init();
227    }
228);