← All changes
|
modules/subscriptions/subscribe-modal/class-jetpack-subscribe-modal.php
+79
-81
12.6.4
→
16.3-a.7
View file →
| @@ -5,10 +5,9 @@ | ||
| 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\Token_Subscription_Service; | |
| 10 | -use Automattic\Jetpack\Status\Host; | |
| 9 | +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service; | |
| 11 | 10 | use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS; |
| 12 | 11 | |
| 13 | 12 | /** |
| 14 | 13 | * Jetpack_Subscribe_Modal class. |
| @@ -62,8 +61,49 @@ | ||
| 62 | 61 | public function enqueue_assets() { |
| 63 | 62 | if ( $this->should_user_see_modal() ) { |
| 64 | 63 | wp_enqueue_style( 'subscribe-modal-css', plugins_url( 'subscribe-modal.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 65 | 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 | + ); | |
| 66 | 106 | } |
| 67 | 107 | } |
| 68 | 108 | |
| 69 | 109 | /** |
| @@ -118,9 +158,9 @@ | ||
| 118 | 158 | $template->title = __( 'Jetpack Subscribe modal', 'jetpack' ); |
| 119 | 159 | $template->status = 'publish'; |
| 120 | 160 | $template->has_theme_file = false; |
| 121 | 161 | $template->is_custom = true; |
| 122 | - $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' ); | |
| 123 | 163 | |
| 124 | 164 | return $template; |
| 125 | 165 | } |
| 126 | 166 | |
| @@ -132,27 +172,28 @@ | ||
| 132 | 172 | */ |
| 133 | 173 | public function get_subscribe_template_content() { |
| 134 | 174 | // translators: %s is the name of the site. |
| 135 | 175 | $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
| 136 | - $continue_reading = __( 'Continue Reading', 'jetpack' ); | |
| 176 | + $continue_reading = __( 'Continue reading', 'jetpack' ); | |
| 137 | 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' ); | |
| 138 | 179 | |
| 139 | 180 | return <<<HTML |
| 140 | - <!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|70","bottom":"var:preset|spacing|70","left":"var:preset|spacing|70","right":"var:preset|spacing|70"},"margin":{"top":"0","bottom":"0"}},"border":{"color":"#dddddd","width":"1px"}},"layout":{"type":"constrained","contentSize":"450px"}} --> | |
| 141 | - <div class="wp-block-group has-border-color" style="border-color:#dddddd;border-width:1px;margin-top:0;margin-bottom:0;padding-top:var(--wp--preset--spacing--70);padding-right:var(--wp--preset--spacing--70);padding-bottom:var(--wp--preset--spacing--70);padding-left:var(--wp--preset--spacing--70)"> | |
| 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"> | |
| 142 | 183 | |
| 143 | - <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"4px"}}}} --> | |
| 184 | + <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} --> | |
| 144 | 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> |
| 145 | 186 | <!-- /wp:heading --> |
| 146 | 187 | |
| 147 | 188 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} --> |
| 148 | - <p class='has-text-align-center' style='margin-top:4px;margin-bottom:0px;font-size:15px'>$subscribe_text</p> | |
| 189 | + <p class='has-text-align-center' style='margin-top:4px;margin-bottom:1em;font-size:15px'>$subscribe_text</p> | |
| 149 | 190 | <!-- /wp:paragraph --> |
| 150 | 191 | |
| 151 | - <!-- wp:jetpack/subscriptions {"buttonBackgroundColor":"primary","textColor":"secondary","borderRadius":50,"borderColor":"primary","className":"is-style-compact"} /--> | |
| 192 | + <!-- wp:jetpack/subscriptions {"borderRadius":50,"className":"is-style-compact","appSource":"subscribe-modal"} /--> | |
| 152 | 193 | |
| 153 | - <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}},"className":"jetpack-subscribe-modal__close"} --> | |
| 154 | - <p class="has-text-align-center jetpack-subscribe-modal__close" style="margin-top:20px;font-size:14px"><a href="#">$continue_reading</a></p> | |
| 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> | |
| 155 | 196 | <!-- /wp:paragraph --> |
| 156 | 197 | </div> |
| 157 | 198 | <!-- /wp:group --> |
| 158 | 199 | HTML; |
| @@ -158,20 +199,8 @@ | ||
| 158 | 199 | HTML; |
| 159 | 200 | } |
| 160 | 201 | |
| 161 | 202 | /** |
| 162 | - * Returns true if we should load Newsletter content. | |
| 163 | - * | |
| 164 | - * @return bool | |
| 165 | - */ | |
| 166 | - public static function should_load_subscriber_modal() { | |
| 167 | - // Adding extra check/flag to load only on WP.com | |
| 168 | - // When ready for Jetpack release, remove this. | |
| 169 | - $is_wpcom = ( new Host() )->is_wpcom_platform(); | |
| 170 | - return $is_wpcom; | |
| 171 | - } | |
| 172 | - | |
| 173 | - /** | |
| 174 | 203 | * Returns true if a site visitor should see |
| 175 | 204 | * the Subscribe Modal. |
| 176 | 205 | * |
| 177 | 206 | * @return bool |
| @@ -181,8 +210,22 @@ | ||
| 181 | 210 | if ( is_admin() || ! is_singular( 'post' ) ) { |
| 182 | 211 | return false; |
| 183 | 212 | } |
| 184 | 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'] ) ) { | |
| 219 | + return false; | |
| 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 | + | |
| 185 | 228 | // Don't show if one of subscribe query params is set. |
| 186 | 229 | // They are set when user submits the subscribe form. |
| 187 | 230 | // The nonce is checked elsewhere before redirect back to this page with query params. |
| 188 | 231 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| @@ -189,82 +232,37 @@ | ||
| 189 | 232 | if ( isset( $_GET['subscribe'] ) || isset( $_GET['blogsub'] ) ) { |
| 190 | 233 | return false; |
| 191 | 234 | } |
| 192 | 235 | |
| 193 | - // Don't show if post is for subscribers only or has paywall block | |
| 236 | + // Don't show if post is not a post, is for subscribers only, or has paywall block | |
| 194 | 237 | global $post; |
| 195 | - $access_level = get_post_meta( $post->ID, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); | |
| 196 | - $is_accessible_by_everyone = Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $access_level; | |
| 197 | - if ( ! $is_accessible_by_everyone ) { | |
| 238 | + if ( ! $post instanceof WP_Post ) { | |
| 198 | 239 | return false; |
| 199 | 240 | } |
| 200 | - | |
| 201 | - // Dont show if user is member of site. | |
| 202 | - if ( is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) { | |
| 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 ) { | |
| 203 | 249 | return false; |
| 204 | 250 | } |
| 205 | 251 | |
| 206 | 252 | // Don't show if user is subscribed to blog. |
| 207 | 253 | require_once __DIR__ . '/../views.php'; |
| 208 | - if ( $this->has_subscription_cookie() || Jetpack_Subscriptions_Widget::is_current_user_subscribed() ) { | |
| 254 | + if ( ! class_exists( 'Jetpack_Memberships' ) || Jetpack_Memberships::is_current_user_subscribed() ) { | |
| 209 | 255 | return false; |
| 210 | 256 | } |
| 211 | 257 | return true; |
| 212 | 258 | } |
| 213 | - | |
| 214 | - /** | |
| 215 | - * Returns true if site visitor has subscribed | |
| 216 | - * to the blog and has a subscription cookie. | |
| 217 | - * | |
| 218 | - * @return bool | |
| 219 | - */ | |
| 220 | - public function has_subscription_cookie() { | |
| 221 | - $cookies = $_COOKIE; | |
| 222 | - foreach ( $cookies as $name => $value ) { | |
| 223 | - if ( strpos( $name, 'jetpack_blog_subscribe_' ) !== false ) { | |
| 224 | - return true; | |
| 225 | - } | |
| 226 | - } | |
| 227 | - return false; | |
| 228 | - } | |
| 229 | 259 | } |
| 230 | 260 | |
| 231 | -add_filter( | |
| 232 | - 'jetpack_subscriptions_modal_enabled', | |
| 233 | - array( | |
| 234 | - 'Jetpack_Subscribe_Modal', | |
| 235 | - 'should_load_subscriber_modal', | |
| 236 | - ) | |
| 237 | -); | |
| 261 | +Jetpack_Subscribe_Modal::init(); | |
| 238 | 262 | |
| 239 | -/** | |
| 240 | - * Filter for enabling or disabling the Jetpack Subscribe Modal | |
| 241 | - * feature. We use this filter here and in several other places | |
| 242 | - * to conditionally load options and functionality related to | |
| 243 | - * this feature. | |
| 244 | - * | |
| 245 | - * @since 12.4 | |
| 246 | - * | |
| 247 | - * @param bool Defaults to false. | |
| 248 | - */ | |
| 249 | -if ( apply_filters( 'jetpack_subscriptions_modal_enabled', false ) ) { | |
| 250 | - Jetpack_Subscribe_Modal::init(); | |
| 251 | -} | |
| 252 | - | |
| 253 | 263 | add_action( |
| 254 | 264 | 'rest_api_switched_to_blog', |
| 255 | 265 | function () { |
| 256 | - /** | |
| 257 | - * Filter for enabling or disabling the Jetpack Subscribe Modal | |
| 258 | - * feature. We use this filter here and in several other places | |
| 259 | - * to conditionally load options and functionality related to | |
| 260 | - * this feature. | |
| 261 | - * | |
| 262 | - * @since 12.4 | |
| 263 | - * | |
| 264 | - * @param bool Defaults to false. | |
| 265 | - */ | |
| 266 | - if ( apply_filters( 'jetpack_subscriptions_modal_enabled', false ) ) { | |
| 267 | - Jetpack_Subscribe_Modal::init(); | |
| 268 | - } | |
| 266 | + Jetpack_Subscribe_Modal::init(); | |
| 269 | 267 | } |
| 270 | 268 | ); |