Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Subscription_Modal_On_Comment
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 9
600
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
 get_block_template_part_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 enqueue_assets
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 add_subscription_modal_to_frontend
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 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_template_content
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 should_user_see_modal
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * Adds support for Jetpack Subscription Modal On Comment feature
4 * Limited to Atomic sites.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 * @since 12.4
8 */
9
10use Automattic\Jetpack\Comments\Comments;
11use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
12use Automattic\Jetpack\Status\Host;
13use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19/**
20 * Jetpack_Subscription_Modal_On_Comment class.
21 */
22class Jetpack_Subscription_Modal_On_Comment {
23    /**
24     * Jetpack_Subscription_Modal_On_Comment singleton instance.
25     *
26     * @var Jetpack_Subscription_Modal_On_Comment|null
27     */
28    private static $instance;
29
30    /**
31     * Jetpack_Subscription_Modal_On_Comment instance init.
32     */
33    public static function init() {
34        if ( self::$instance === null ) {
35            self::$instance = new Jetpack_Subscription_Modal_On_Comment();
36        }
37
38        return self::$instance;
39    }
40
41    const BLOCK_TEMPLATE_PART_SLUG = 'jetpack-subscription-modal';
42
43    /**
44     * Returns the block template part ID.
45     *
46     * @return string
47     */
48    public static function get_block_template_part_id() {
49        return get_stylesheet() . '//' . self::BLOCK_TEMPLATE_PART_SLUG;
50    }
51
52    /**
53     * Jetpack_Subscription_Modal_On_Comment class constructor.
54     * Limited to Atomic sites.
55     */
56    public function __construct() {
57        // Only the legacy comment iframe opens this modal, so the new form would load it for nothing.
58        if ( Comments::is_enabled() ) {
59            return;
60        }
61
62        if ( ( new Host() )->is_woa_site() && get_option( 'jetpack_verbum_subscription_modal', true ) ) {
63            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
64            add_action( 'wp_footer', array( $this, 'add_subscription_modal_to_frontend' ) );
65            add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 );
66        }
67    }
68
69    /**
70     * Enqueues JS to load modal.
71     *
72     * @return void
73     */
74    public function enqueue_assets() {
75        if ( ! $this->should_user_see_modal() ) {
76            return;
77        }
78
79        wp_enqueue_style( 'subscription-modal-css', plugins_url( 'subscription-modal.css', __FILE__ ), array(), JETPACK__VERSION );
80        wp_enqueue_script( 'subscription-modal-js', plugins_url( 'subscription-modal.js', __FILE__ ), array( 'wp-dom-ready' ), JETPACK__VERSION, true );
81        wp_localize_script(
82            'subscription-modal-js',
83            'subscriptionData',
84            array(
85                'homeUrl' => wp_parse_url( home_url(), PHP_URL_HOST ),
86            )
87        );
88    }
89
90    /**
91     * Adds modal with Subscribe Modal content.
92     *
93     * @return void
94     */
95    public function add_subscription_modal_to_frontend() {
96        if ( $this->should_user_see_modal() ) { ?>
97                    <div class="jetpack-subscription-modal">
98                        <div class="jetpack-subscription-modal__modal-content">
99                            <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?>
100                        </div>
101                    </div>
102            <?php
103        }
104    }
105
106    /**
107     * Makes get_block_template return the WP_Block_Template for the Subscribe Modal.
108     *
109     * @param WP_Block_Template $block_template The block template to be returned.
110     * @param string            $id Template unique identifier (example: theme_slug//template_slug).
111     * @param string            $template_type Template type: `'wp_template'` or '`wp_template_part'`.
112     *
113     * @return WP_Block_Template
114     */
115    public function get_block_template_filter( $block_template, $id, $template_type ) {
116        if ( empty( $block_template ) && $template_type === 'wp_template_part' ) {
117            if ( $id === self::get_block_template_part_id() ) {
118                return $this->get_template();
119            }
120        }
121
122        return $block_template;
123    }
124
125    /**
126     * Returns a custom template for the Subscribe Modal.
127     *
128     * @return WP_Block_Template
129     */
130    public function get_template() {
131        $template                 = new WP_Block_Template();
132        $template->theme          = get_stylesheet();
133        $template->slug           = self::BLOCK_TEMPLATE_PART_SLUG;
134        $template->id             = self::get_block_template_part_id();
135        $template->area           = 'uncategorized';
136        $template->content        = $this->get_subscribe_template_content();
137        $template->source         = 'plugin';
138        $template->type           = 'wp_template_part';
139        $template->title          = __( 'Jetpack Subscription modal', 'jetpack' );
140        $template->status         = 'publish';
141        $template->has_theme_file = false;
142        $template->is_custom      = true;
143        $template->description    = __( 'A subscribe form that submit a comment.', 'jetpack' );
144
145        return $template;
146    }
147
148    /**
149     * Returns the initial content of the Subscribe Modal template.
150     * This can then be edited by the user.
151     *
152     * @return string
153     */
154    public function get_subscribe_template_content() {
155        // translators: %s is the name of the site.
156        $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
157        $subscribe_text     = __( 'Subscribe now to keep reading and get access to the full archive.', 'jetpack' );
158        $continue_reading   = __( 'Continue reading', 'jetpack' );
159
160        return <<<HTML
161    <!-- wp:group {"style":{"spacing":{"top":"32px","bottom":"32px","left":"32px","right":"32px"},"margin":{"top":"0","bottom":"0"}},"border":{"color":"#dddddd","width":"1px"}},"layout":{"type":"constrained","contentSize":"450px"}} -->
162    <div class="wp-block-group has-border-color jetpack-subscription-modal__modal-content-form" style="border-color:#dddddd;border-width:1px;margin-top:0;margin-bottom:0;padding:32px">
163
164        <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
165        <h2 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:10px;font-size:26px;font-style:normal;font-weight:600">$discover_more_from</h2>
166        <!-- /wp:heading -->
167
168        <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} -->
169        <p class='has-text-align-center' style='margin-top:4px;margin-bottom:0px;font-size:15px'>$subscribe_text</p>
170        <!-- /wp:paragraph -->
171
172        <!-- wp:jetpack/subscriptions {"borderRadius":50,"className":"is-style-compact","appSource":"atomic-subscription-modal-lo"} /-->
173
174        <!-- wp:paragraph {"align":"center","style":{"spacing":{"margin":{"top":"20px"}},"typography":{"fontSize":"14px"}},"className":"jetpack-subscription-modal__close"} -->
175        <p class="has-text-align-center jetpack-subscription-modal__close" style="margin-top:20px;font-size:14px"><a href="#">$continue_reading</a></p>
176        <!-- /wp:paragraph -->
177    </div>
178    <!-- /wp:group -->
179HTML;
180    }
181
182    /**
183     * Returns true if a site visitor should see
184     * the Subscribe Modal.
185     *
186     * @return bool
187     */
188    public function should_user_see_modal() {
189        // Only show when viewing frontend single post.
190        if ( is_admin() || ! is_singular( 'post' ) ) {
191            return false;
192        }
193
194        // Don't show if post is for subscribers only or has paywall block
195        global $post;
196
197        if ( ! $post instanceof WP_Post ) {
198            return false;
199        }
200
201        if ( defined( 'Automattic\\Jetpack\\Extensions\\Subscriptions\\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS' ) ) {
202            $access_level = get_post_meta( $post->ID, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true );
203        } else {
204            $access_level = get_post_meta( $post->ID, '_jetpack_newsletter_access', true );
205        }
206        require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
207        $is_accessible_by_everyone = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $access_level || empty( $access_level );
208
209        if ( ! $is_accessible_by_everyone ) {
210            return false;
211        }
212
213        return true;
214    }
215}
216
217Jetpack_Subscription_Modal_On_Comment::init();
218
219add_action(
220    'rest_api_switched_to_blog',
221    function () {
222        Jetpack_Subscription_Modal_On_Comment::init();
223    }
224);