PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | modules/subscriptions/subscribe-modal/class-jetpack-subscribe-modal.php +121 -109 12.4.2 → 16.3-a.7 View file →
@@ -5,8 +5,11 @@
5 5 * @package automattic/jetpack-mu-wpcom
6 6 * @since 12.4
7 7 */
8 8
9 +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
10 +use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS;
11 +
9 12 /**
10 13 * Jetpack_Subscribe_Modal class.
11 14 */
12 15 class Jetpack_Subscribe_Modal {
@@ -47,9 +50,8 @@
47 50 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
48 51 add_action( 'wp_footer', array( $this, 'add_subscribe_modal_to_frontend' ) );
49 52 }
50 53 add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 );
51 - add_filter( 'get_block_templates', array( $this, 'get_block_templates_filter' ), 10, 3 );
52 54 }
53 55
54 56 /**
55 57 * Enqueues JS to load modal.
@@ -56,11 +58,52 @@
56 58 *
57 59 * @return void
58 60 */
59 61 public function enqueue_assets() {
60 - if ( $this->is_front_end_single_post() ) {
62 + if ( $this->should_user_see_modal() ) {
61 63 wp_enqueue_style( 'subscribe-modal-css', plugins_url( 'subscribe-modal.css', __FILE__ ), array(), JETPACK__VERSION );
62 64 wp_enqueue_script( 'subscribe-modal-js', plugins_url( 'subscribe-modal.js', __FILE__ ), array( 'wp-dom-ready' ), JETPACK__VERSION, true );
65 +
66 + /**
67 + * Filter how many milliseconds until the Subscribe Modal appears.
68 + *
69 + * @module subscriptions
70 + *
71 + * @since 13.4
72 + *
73 + * @param int 60000 Time in milliseconds for the Subscribe Modal to appear.
74 + */
75 + $load_time = absint( apply_filters( 'jetpack_subscribe_modal_load_time', 60000 ) );
76 +
77 + /**
78 + * Filter how many percentage of the page should be scrolled before the Subscribe Modal appears.
79 + *
80 + * @module subscriptions
81 + *
82 + * @since 13.6
83 + *
84 + * @param int Percentage of the page scrolled before the Subscribe Modal appears.
85 + */
86 + $scroll_threshold = absint( apply_filters( 'jetpack_subscribe_modal_scroll_threshold', 50 ) );
87 +
88 + /**
89 + * Filter to control the interval at which the subscribe modal is shown to the same user. The default interval is 24 hours.
90 + *
91 + * @since 13.7
92 + *
93 + * @param int 24 Hours before we show the same user the Subscribe Modal to again.
94 + */
95 + $modal_interval = absint( apply_filters( 'jetpack_subscribe_modal_interval', 24 ) );
96 +
97 + wp_localize_script(
98 + 'subscribe-modal-js',
99 + 'Jetpack_Subscriptions',
100 + array(
101 + 'modalLoadTime' => $load_time,
102 + 'modalScrollThreshold' => $scroll_threshold,
103 + 'modalInterval' => ( $modal_interval * HOUR_IN_SECONDS * 1000 ),
104 + )
105 + );
63 106 }
64 107 }
65 108
66 109 /**
@@ -68,12 +111,12 @@
68 111 *
69 112 * @return void
70 113 */
71 114 public function add_subscribe_modal_to_frontend() {
72 - if ( $this->is_front_end_single_post() ) { ?>
115 + if ( $this->should_user_see_modal() ) { ?>
73 116 <div class="jetpack-subscribe-modal">
74 117 <div class="jetpack-subscribe-modal__modal-content">
75 - <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?>
118 + <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?>
76 119 </div>
77 120 </div>
78 121 <?php
79 122 }
@@ -98,33 +141,8 @@
98 141 return $block_template;
99 142 }
100 143
101 144 /**
102 - * Makes get_block_templates return the WP_Block_Template within the results.
103 - *
104 - * @param WP_Block_Template $query_result The filter result.
105 - * @param string $query The query string.
106 - * @param string $template_type Template type: `'wp_template'` or '`wp_template_part'`.
107 - *
108 - * @return array WP_Block_Template
109 - */
110 - public function get_block_templates_filter( $query_result, $query, $template_type ) {
111 - if ( empty( $query ) && $template_type === 'wp_template_part' ) {
112 - if ( is_array( $query_result ) ) {
113 - // find the custom template and return early if we have a custom version in the results.
114 - foreach ( $query_result as $template ) {
115 - if ( $template->id === self::get_block_template_part_id() ) {
116 - return $query_result;
117 - }
118 - }
119 - }
120 - $query_result[] = $this->get_template();
121 - }
122 -
123 - return $query_result;
124 - }
125 -
126 - /**
127 145 * Returns a custom template for the Subscribe Modal.
128 146 *
129 147 * @return WP_Block_Template
130 148 */
@@ -140,27 +158,14 @@
140 158 $template->title = __( 'Jetpack Subscribe modal', 'jetpack' );
141 159 $template->status = 'publish';
142 160 $template->has_theme_file = false;
143 161 $template->is_custom = true;
144 - $template->description = __( 'A subscribe form that pops up when someone visits your site', 'jetpack' );
162 + $template->description = __( 'A subscribe form that pops up when someone visits your site.', 'jetpack' );
145 163
146 164 return $template;
147 165 }
148 166
149 167 /**
150 - * Returns true if we are on frontend of single post.
151 - * Note: Because of how WordPress works, this function will
152 - * only return the correct value after a certain point in the
153 - * WordPress loading process. You cannot, for example, use this
154 - * method in the class contructor method - it will always return false.
155 - *
156 - * @return bool
157 - */
158 - public function is_front_end_single_post() {
159 - return ! is_admin() && is_singular( 'post' );
160 - }
161 -
162 - /**
163 168 * Returns the initial content of the Subscribe Modal template.
164 169 * This can then be edited by the user.
165 170 *
166 171 * @return string
@@ -167,90 +172,97 @@
167 172 */
168 173 public function get_subscribe_template_content() {
169 174 // translators: %s is the name of the site.
170 175 $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
171 - $continue_reading = __( 'Continue Reading', 'jetpack' );
172 - $subscribe_text = __( 'Subscribe to the newsletter to keep reading and get access to the full archive.', 'jetpack' );
176 + $continue_reading = __( 'Continue reading', 'jetpack' );
177 + $subscribe_text = __( 'Subscribe now to keep reading and get access to the full archive.', 'jetpack' );
178 + $group_block_name = esc_attr__( 'Subscription pop-up container', 'jetpack' );
173 179
174 180 return <<<HTML
175 - <!-- wp:group {"style":{"spacing":{"padding":{"top":"50px","bottom":"50px","left":"20px","right":"20px"}}},"layout":{"type":"constrained"}} -->
176 - <div class='wp-block-group' style='padding-top:50px;padding-right:20px;padding-bottom:50px;padding-left:20px'>
177 - <!-- wp:group {"style":{"dimensions":{"minHeight":"0px"},"spacing":{"blockGap":"8px"}},"layout":{"type":"flex","flexWrap":"wrap","justifyContent":"center"}} -->
178 - <div class='wp-block-group' style='min-height:0px'>
179 - <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"4px"}}}} -->
180 - <h2 class="wp-block-heading has-text-align-center" style="margin-top:4px;margin-bottom:4px;font-size:26px;font-style:normal;font-weight:600">$discover_more_from</h2>
181 - <!-- /wp:heading -->
182 - </div>
183 - <!-- /wp:group -->
181 + <!-- wp:group {"metadata":{"name":"$group_block_name"},"style":{"spacing":{"padding":{"top":"32px","bottom":"32px","left":"32px","right":"32px"},"margin":{"top":"0","bottom":"0"}},"border":{"color":"#dddddd","width":"1px"}},"layout":{"type":"constrained","contentSize":"450px"}} -->
182 + <div class="wp-block-group has-border-color" style="border-color:#dddddd;border-width:1px;margin-top:0;margin-bottom:0;padding-top:32px;padding-right:32px;padding-bottom:32px;padding-left:32px">
184 183
185 - <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} -->
186 - <p class='has-text-align-center' style='margin-top:4px;margin-bottom:0px;font-size:15px'>$subscribe_text</p>
187 - <!-- /wp:paragraph -->
184 + <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
185 + <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>
186 + <!-- /wp:heading -->
188 187
189 - <!-- wp:jetpack/subscriptions {"buttonBackgroundColor":"primary","textColor":"secondary","borderRadius":50,"borderColor":"primary","className":"is-style-compact"} /-->
190 -
191 - <!-- wp:paragraph {"align":"center","style":{"color":{"text":"#666666"},"typography":{"fontSize":"14px","textDecoration":"none"}},"className":"jetpack-subscribe-modal__close"} -->
192 - <p class='has-text-align-center jetpack-subscribe-modal__close has-text-color' style='color:#666666;font-size:14px;text-decoration:none'><a href='#'>$continue_reading</a></p>
193 - <!-- /wp:paragraph -->
194 - </div>
195 - <!-- /wp:group -->
188 + <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} -->
189 + <p class='has-text-align-center' style='margin-top:4px;margin-bottom:1em;font-size:15px'>$subscribe_text</p>
190 + <!-- /wp:paragraph -->
191 +
192 + <!-- wp:jetpack/subscriptions {"borderRadius":50,"className":"is-style-compact","appSource":"subscribe-modal"} /-->
193 +
194 + <!-- wp:paragraph {"align":"center","style":{"spacing":{"margin":{"top":"20px"}},"typography":{"fontSize":"14px"}},"className":"jetpack-subscribe-modal__close"} -->
195 + <p class="has-text-align-center jetpack-subscribe-modal__close" style="margin-top:20px;margin-bottom:0;font-size:14px"><a href="#">$continue_reading</a></p>
196 + <!-- /wp:paragraph -->
197 + </div>
198 + <!-- /wp:group -->
196 199 HTML;
197 200 }
198 201
199 202 /**
200 - * Returns true if we should load Newsletter content.
201 - * This is currently limited to lettre theme or newsletter sites.
202 - * We could open it to all themes or site intents.
203 + * Returns true if a site visitor should see
204 + * the Subscribe Modal.
203 205 *
204 206 * @return bool
205 207 */
206 - public static function should_load_subscriber_modal() {
207 - if ( 'lettre' !== get_option( 'stylesheet' ) && 'newsletter' !== get_option( 'site_intent' ) ) {
208 + public function should_user_see_modal() {
209 + // Only show when viewing frontend single post.
210 + if ( is_admin() || ! is_singular( 'post' ) ) {
208 211 return false;
209 212 }
210 - if ( ! wp_is_block_theme() ) {
213 +
214 + // Needed because Elementor editor makes is_admin() return false
215 + // See https://coreysalzano.com/wordpress/why-elementor-disobeys-is_admin/
216 + // Ignore nonce warning as just checking if is set
217 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
218 + if ( isset( $_GET['elementor-preview'] ) ) {
211 219 return false;
212 220 }
221 +
222 + // Don't show when previewing blog posts or site's theme
223 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
224 + if ( isset( $_GET['preview'] ) || isset( $_GET['theme_preview'] ) || isset( $_GET['customize_preview'] ) || isset( $_GET['hide_banners'] ) ) {
225 + return false;
226 + }
227 +
228 + // Don't show if one of subscribe query params is set.
229 + // They are set when user submits the subscribe form.
230 + // The nonce is checked elsewhere before redirect back to this page with query params.
231 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
232 + if ( isset( $_GET['subscribe'] ) || isset( $_GET['blogsub'] ) ) {
233 + return false;
234 + }
235 +
236 + // Don't show if post is not a post, is for subscribers only, or has paywall block
237 + global $post;
238 + if ( ! $post instanceof WP_Post ) {
239 + return false;
240 + }
241 + if ( defined( 'Automattic\\Jetpack\\Extensions\\Subscriptions\\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS' ) ) {
242 + $access_level = get_post_meta( $post->ID, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true );
243 + } else {
244 + $access_level = get_post_meta( $post->ID, '_jetpack_newsletter_access', true );
245 + }
246 + require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
247 + $is_accessible_by_everyone = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $access_level || empty( $access_level );
248 + if ( ! $is_accessible_by_everyone ) {
249 + return false;
250 + }
251 +
252 + // Don't show if user is subscribed to blog.
253 + require_once __DIR__ . '/../views.php';
254 + if ( ! class_exists( 'Jetpack_Memberships' ) || Jetpack_Memberships::is_current_user_subscribed() ) {
255 + return false;
256 + }
213 257 return true;
214 258 }
215 259 }
216 260
217 -add_filter(
218 - 'jetpack_subscriptions_modal_enabled',
219 - array(
220 - 'Jetpack_Subscribe_Modal',
221 - 'should_load_subscriber_modal',
222 - )
261 +Jetpack_Subscribe_Modal::init();
262 +
263 +add_action(
264 + 'rest_api_switched_to_blog',
265 + function () {
266 + Jetpack_Subscribe_Modal::init();
267 + }
223 268 );
224 -
225 -/*
226 - * The following line is being added temporarily as a feature flag.
227 - *
228 - * It disables the subscribe modal feature using the
229 - * jetpack_subscriptions_modal_enabled filter. If you want to test
230 - * this feature, you'll need to override the line below by adding
231 - *
232 - * add_filter( 'jetpack_subscriptions_modal_enabled', '__return_true', 20 );
233 - *
234 - * to your test site. When we are ready for full release of this
235 - * feature, we will remove this line, but will leave the
236 - * jetpack_subscriptions_modal_enabled filter in place.
237 - * The filter is documented just below and defaults to false.
238 - * But for production purposes, the value of this filter is
239 - * determined by add_filter() call just above, which uses the
240 - * should_load_subscriber_modal() method.
241 - */
242 -add_filter( 'jetpack_subscriptions_modal_enabled', '__return_false', 11 );
243 -
244 -/**
245 - * Filter for enabling or disabling the Jetpack Subscribe Modal
246 - * feature. We use this filter here and in several other places
247 - * to conditionally load options and functionality related to
248 - * this feature.
249 - *
250 - * @since 12.4
251 - *
252 - * @param bool Defaults to false.
253 - */
254 -if ( apply_filters( 'jetpack_subscriptions_modal_enabled', false ) ) {
255 - Jetpack_Subscribe_Modal::init();
256 -}