Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.98% |
75 / 123 |
|
40.00% |
4 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Subscribe_Modal | |
65.79% |
75 / 114 |
|
40.00% |
4 / 10 |
130.72 | |
0.00% |
0 / 1 |
| init | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| get_block_template_part_id | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| enqueue_assets | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| get_global_styles_background_css | |
100.00% |
31 / 31 |
|
100.00% |
1 / 1 |
13 | |||
| add_subscribe_modal_to_frontend | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| get_block_template_filter | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
| get_template | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| get_subscribe_template_content | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| should_user_see_modal | |
66.67% |
14 / 21 |
|
0.00% |
0 / 1 |
27.70 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adds support for Jetpack Subscribe Modal feature |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | * @since 12.4 |
| 7 | */ |
| 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 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Jetpack_Subscribe_Modal class. |
| 18 | */ |
| 19 | class Jetpack_Subscribe_Modal { |
| 20 | /** |
| 21 | * Jetpack_Subscribe_Modal singleton instance. |
| 22 | * |
| 23 | * @var Jetpack_Subscribe_Modal|null |
| 24 | */ |
| 25 | private static $instance; |
| 26 | |
| 27 | /** |
| 28 | * Jetpack_Subscribe_Modal instance init. |
| 29 | */ |
| 30 | public static function init() { |
| 31 | if ( self::$instance === null ) { |
| 32 | self::$instance = new Jetpack_Subscribe_Modal(); |
| 33 | } |
| 34 | |
| 35 | return self::$instance; |
| 36 | } |
| 37 | |
| 38 | const BLOCK_TEMPLATE_PART_SLUG = 'jetpack-subscribe-modal'; |
| 39 | |
| 40 | /** |
| 41 | * Returns the block template part ID. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | public static function get_block_template_part_id() { |
| 46 | return get_stylesheet() . '//' . self::BLOCK_TEMPLATE_PART_SLUG; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Jetpack_Subscribe_Modal class constructor. |
| 51 | */ |
| 52 | public function __construct() { |
| 53 | if ( get_option( 'sm_enabled', false ) ) { |
| 54 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 55 | add_action( 'wp_footer', array( $this, 'add_subscribe_modal_to_frontend' ) ); |
| 56 | } |
| 57 | add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Enqueues JS to load modal. |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function enqueue_assets() { |
| 66 | if ( $this->should_user_see_modal() ) { |
| 67 | wp_enqueue_style( 'subscribe-modal-css', plugins_url( 'subscribe-modal.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 68 | $background_css = self::get_global_styles_background_css(); |
| 69 | if ( $background_css ) { |
| 70 | wp_add_inline_style( 'subscribe-modal-css', $background_css ); |
| 71 | } |
| 72 | wp_enqueue_script( 'subscribe-modal-js', plugins_url( 'subscribe-modal.js', __FILE__ ), array( 'wp-dom-ready' ), JETPACK__VERSION, true ); |
| 73 | |
| 74 | /** |
| 75 | * Filter how many milliseconds until the Subscribe Modal appears. |
| 76 | * |
| 77 | * @module subscriptions |
| 78 | * |
| 79 | * @since 13.4 |
| 80 | * |
| 81 | * @param int 60000 Time in milliseconds for the Subscribe Modal to appear. |
| 82 | */ |
| 83 | $load_time = absint( apply_filters( 'jetpack_subscribe_modal_load_time', 60000 ) ); |
| 84 | |
| 85 | /** |
| 86 | * Filter how many percentage of the page should be scrolled before the Subscribe Modal appears. |
| 87 | * |
| 88 | * @module subscriptions |
| 89 | * |
| 90 | * @since 13.6 |
| 91 | * |
| 92 | * @param int Percentage of the page scrolled before the Subscribe Modal appears. |
| 93 | */ |
| 94 | $scroll_threshold = absint( apply_filters( 'jetpack_subscribe_modal_scroll_threshold', 50 ) ); |
| 95 | |
| 96 | /** |
| 97 | * Filter to control the interval at which the subscribe modal is shown to the same user. The default interval is 24 hours. |
| 98 | * |
| 99 | * @since 13.7 |
| 100 | * |
| 101 | * @param int 24 Hours before we show the same user the Subscribe Modal to again. |
| 102 | */ |
| 103 | $modal_interval = absint( apply_filters( 'jetpack_subscribe_modal_interval', 24 ) ); |
| 104 | |
| 105 | wp_localize_script( |
| 106 | 'subscribe-modal-js', |
| 107 | 'Jetpack_Subscriptions', |
| 108 | array( |
| 109 | 'modalLoadTime' => $load_time, |
| 110 | 'modalScrollThreshold' => $scroll_threshold, |
| 111 | 'modalInterval' => ( $modal_interval * HOUR_IN_SECONDS * 1000 ), |
| 112 | 'modalLabel' => __( 'Subscribe', 'jetpack' ), |
| 113 | ) |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Gives the modal the site's Global Styles page background, so the text it inherits from the page stays readable. |
| 120 | * |
| 121 | * @since 16.4 |
| 122 | * |
| 123 | * @return string CSS, or an empty string to keep the stylesheet's background. |
| 124 | */ |
| 125 | public static function get_global_styles_background_css() { |
| 126 | // Read the whole tree: for a missing path, wp_get_global_styles() returns every style instead of null. |
| 127 | $styles = wp_get_global_styles(); |
| 128 | // The server cannot tell what a page image shows. Removing a theme's image stores 'none'. |
| 129 | $image = $styles['background']['backgroundImage'] ?? null; |
| 130 | if ( ! empty( $image ) && 'none' !== $image ) { |
| 131 | return ''; |
| 132 | } |
| 133 | |
| 134 | $declarations = wp_style_engine_get_styles( |
| 135 | array( |
| 136 | 'color' => array( |
| 137 | 'background' => $styles['color']['background'] ?? null, |
| 138 | 'gradient' => $styles['color']['gradient'] ?? null, |
| 139 | ), |
| 140 | 'background' => array( 'gradient' => $styles['background']['gradient'] ?? null ), |
| 141 | ) |
| 142 | )['declarations'] ?? array(); |
| 143 | foreach ( $declarations as $property => $value ) { |
| 144 | // The style engine returns values unfiltered, so only accept those core's CSS filter leaves intact. |
| 145 | $declaration = $property . ': ' . $value; |
| 146 | if ( esc_html( $declaration ) !== $declaration || safecss_filter_attr( $declaration ) !== $declaration ) { |
| 147 | return ''; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | $css = array(); |
| 152 | if ( isset( $declarations['background-color'] ) ) { |
| 153 | $css[] = '--jetpack-subscribe-modal--page-background: ' . $declarations['background-color'] . ';'; |
| 154 | } |
| 155 | // A color.gradient value is a `background` shorthand, which resets the color the stylesheet sets. |
| 156 | if ( isset( $declarations['background'] ) ) { |
| 157 | $css[] = 'background: ' . $declarations['background'] . ';'; |
| 158 | $css[] = 'background-color: var(--jetpack-subscribe-modal--page-background, #fefefe);'; |
| 159 | } |
| 160 | if ( isset( $declarations['background-image'] ) ) { |
| 161 | $css[] = 'background-image: ' . $declarations['background-image'] . ';'; |
| 162 | } |
| 163 | if ( ! $css ) { |
| 164 | return ''; |
| 165 | } |
| 166 | |
| 167 | // A saved template whose blocks set their own colors was styled against the light panel. |
| 168 | $template = get_block_template( self::get_block_template_part_id(), 'wp_template_part' ); |
| 169 | if ( $template && 'plugin' !== $template->source && preg_match( '/\bhas-(?:text-color|link-color|inline-color|background)(?![\w-])/', $template->content ) ) { |
| 170 | return ''; |
| 171 | } |
| 172 | |
| 173 | return '.jetpack-subscribe-modal__modal-content { ' . implode( ' ', $css ) . ' }'; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Adds modal with Subscribe Modal content. |
| 178 | * |
| 179 | * @return void |
| 180 | */ |
| 181 | public function add_subscribe_modal_to_frontend() { |
| 182 | if ( $this->should_user_see_modal() ) { ?> |
| 183 | <div class="jetpack-subscribe-modal"> |
| 184 | <div class="jetpack-subscribe-modal__modal-content"> |
| 185 | <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?> |
| 186 | </div> |
| 187 | </div> |
| 188 | <?php |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Makes get_block_template return the WP_Block_Template for the Subscribe Modal. |
| 194 | * |
| 195 | * @param WP_Block_Template $block_template The block template to be returned. |
| 196 | * @param string $id Template unique identifier (example: theme_slug//template_slug). |
| 197 | * @param string $template_type Template type: `'wp_template'` or '`wp_template_part'`. |
| 198 | * |
| 199 | * @return WP_Block_Template |
| 200 | */ |
| 201 | public function get_block_template_filter( $block_template, $id, $template_type ) { |
| 202 | if ( empty( $block_template ) && $template_type === 'wp_template_part' ) { |
| 203 | if ( $id === self::get_block_template_part_id() ) { |
| 204 | return $this->get_template(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return $block_template; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns a custom template for the Subscribe Modal. |
| 213 | * |
| 214 | * @return WP_Block_Template |
| 215 | */ |
| 216 | public function get_template() { |
| 217 | $template = new WP_Block_Template(); |
| 218 | $template->theme = get_stylesheet(); |
| 219 | $template->slug = self::BLOCK_TEMPLATE_PART_SLUG; |
| 220 | $template->id = self::get_block_template_part_id(); |
| 221 | $template->area = 'uncategorized'; |
| 222 | $template->content = $this->get_subscribe_template_content(); |
| 223 | $template->source = 'plugin'; |
| 224 | $template->type = 'wp_template_part'; |
| 225 | $template->title = __( 'Jetpack Subscribe modal', 'jetpack' ); |
| 226 | $template->status = 'publish'; |
| 227 | $template->has_theme_file = false; |
| 228 | $template->is_custom = true; |
| 229 | $template->description = __( 'A subscribe form that pops up when someone visits your site.', 'jetpack' ); |
| 230 | |
| 231 | return $template; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Returns the initial content of the Subscribe Modal template. |
| 236 | * This can then be edited by the user. |
| 237 | * |
| 238 | * @return string |
| 239 | */ |
| 240 | public function get_subscribe_template_content() { |
| 241 | // translators: %s is the name of the site. |
| 242 | $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
| 243 | $continue_reading = __( 'Continue reading', 'jetpack' ); |
| 244 | $subscribe_text = __( 'Subscribe now to keep reading and get access to the full archive.', 'jetpack' ); |
| 245 | $group_block_name = esc_attr__( 'Subscription pop-up container', 'jetpack' ); |
| 246 | |
| 247 | return <<<HTML |
| 248 | <!-- 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"}} --> |
| 249 | <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"> |
| 250 | |
| 251 | <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} --> |
| 252 | <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> |
| 253 | <!-- /wp:heading --> |
| 254 | |
| 255 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} --> |
| 256 | <p class='has-text-align-center' style='margin-top:4px;margin-bottom:1em;font-size:15px'>$subscribe_text</p> |
| 257 | <!-- /wp:paragraph --> |
| 258 | |
| 259 | <!-- wp:jetpack/subscriptions {"borderRadius":50,"className":"is-style-compact","appSource":"subscribe-modal"} /--> |
| 260 | |
| 261 | <!-- wp:paragraph {"align":"center","style":{"spacing":{"margin":{"top":"20px"}},"typography":{"fontSize":"14px"}},"className":"jetpack-subscribe-modal__close"} --> |
| 262 | <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> |
| 263 | <!-- /wp:paragraph --> |
| 264 | </div> |
| 265 | <!-- /wp:group --> |
| 266 | HTML; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Returns true if a site visitor should see |
| 271 | * the Subscribe Modal. |
| 272 | * |
| 273 | * @return bool |
| 274 | */ |
| 275 | public function should_user_see_modal() { |
| 276 | // Only show when viewing frontend single post. |
| 277 | if ( is_admin() || ! is_singular( 'post' ) ) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // Needed because Elementor editor makes is_admin() return false |
| 282 | // See https://coreysalzano.com/wordpress/why-elementor-disobeys-is_admin/ |
| 283 | // Ignore nonce warning as just checking if is set |
| 284 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 285 | if ( isset( $_GET['elementor-preview'] ) ) { |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | // Don't show when previewing blog posts or site's theme |
| 290 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 291 | if ( isset( $_GET['preview'] ) || isset( $_GET['theme_preview'] ) || isset( $_GET['customize_preview'] ) || isset( $_GET['hide_banners'] ) ) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | // Don't show if one of subscribe query params is set. |
| 296 | // They are set when user submits the subscribe form. |
| 297 | // The nonce is checked elsewhere before redirect back to this page with query params. |
| 298 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 299 | if ( isset( $_GET['subscribe'] ) || isset( $_GET['blogsub'] ) ) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | // Don't show if post is not a post, is for subscribers only, or has paywall block |
| 304 | global $post; |
| 305 | if ( ! $post instanceof WP_Post ) { |
| 306 | return false; |
| 307 | } |
| 308 | if ( defined( 'Automattic\\Jetpack\\Extensions\\Subscriptions\\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS' ) ) { |
| 309 | $access_level = get_post_meta( $post->ID, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); |
| 310 | } else { |
| 311 | $access_level = get_post_meta( $post->ID, '_jetpack_newsletter_access', true ); |
| 312 | } |
| 313 | require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php'; |
| 314 | $is_accessible_by_everyone = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $access_level || empty( $access_level ); |
| 315 | if ( ! $is_accessible_by_everyone ) { |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | // Don't show if user is subscribed to blog or awaiting subscription confirmation. |
| 320 | require_once __DIR__ . '/../views.php'; |
| 321 | if ( ! class_exists( 'Jetpack_Memberships' ) || Jetpack_Memberships::is_current_user_subscribed() || Jetpack_Memberships::user_is_pending_subscriber() ) { |
| 322 | return false; |
| 323 | } |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | Jetpack_Subscribe_Modal::init(); |
| 329 | |
| 330 | add_action( |
| 331 | 'rest_api_switched_to_blog', |
| 332 | function () { |
| 333 | Jetpack_Subscribe_Modal::init(); |
| 334 | } |
| 335 | ); |