← All changes
|
modules/subscriptions/subscribe-modal/class-jetpack-subscribe-modal.php
+268
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,268 @@ | ||
| 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 | +/** | |
| 13 | + * Jetpack_Subscribe_Modal class. | |
| 14 | + */ | |
| 15 | +class Jetpack_Subscribe_Modal { | |
| 16 | + /** | |
| 17 | + * Jetpack_Subscribe_Modal singleton instance. | |
| 18 | + * | |
| 19 | + * @var Jetpack_Subscribe_Modal|null | |
| 20 | + */ | |
| 21 | + private static $instance; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Jetpack_Subscribe_Modal instance init. | |
| 25 | + */ | |
| 26 | + public static function init() { | |
| 27 | + if ( self::$instance === null ) { | |
| 28 | + self::$instance = new Jetpack_Subscribe_Modal(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + return self::$instance; | |
| 32 | + } | |
| 33 | + | |
| 34 | + const BLOCK_TEMPLATE_PART_SLUG = 'jetpack-subscribe-modal'; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Returns the block template part ID. | |
| 38 | + * | |
| 39 | + * @return string | |
| 40 | + */ | |
| 41 | + public static function get_block_template_part_id() { | |
| 42 | + return get_stylesheet() . '//' . self::BLOCK_TEMPLATE_PART_SLUG; | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Jetpack_Subscribe_Modal class constructor. | |
| 47 | + */ | |
| 48 | + public function __construct() { | |
| 49 | + if ( get_option( 'sm_enabled', false ) ) { | |
| 50 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); | |
| 51 | + add_action( 'wp_footer', array( $this, 'add_subscribe_modal_to_frontend' ) ); | |
| 52 | + } | |
| 53 | + add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 ); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Enqueues JS to load modal. | |
| 58 | + * | |
| 59 | + * @return void | |
| 60 | + */ | |
| 61 | + public function enqueue_assets() { | |
| 62 | + if ( $this->should_user_see_modal() ) { | |
| 63 | + wp_enqueue_style( 'subscribe-modal-css', plugins_url( 'subscribe-modal.css', __FILE__ ), array(), JETPACK__VERSION ); | |
| 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 | + ); | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Adds modal with Subscribe Modal content. | |
| 111 | + * | |
| 112 | + * @return void | |
| 113 | + */ | |
| 114 | + public function add_subscribe_modal_to_frontend() { | |
| 115 | + if ( $this->should_user_see_modal() ) { ?> | |
| 116 | + <div class="jetpack-subscribe-modal"> | |
| 117 | + <div class="jetpack-subscribe-modal__modal-content"> | |
| 118 | + <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?> | |
| 119 | + </div> | |
| 120 | + </div> | |
| 121 | + <?php | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Makes get_block_template return the WP_Block_Template for the Subscribe Modal. | |
| 127 | + * | |
| 128 | + * @param WP_Block_Template $block_template The block template to be returned. | |
| 129 | + * @param string $id Template unique identifier (example: theme_slug//template_slug). | |
| 130 | + * @param string $template_type Template type: `'wp_template'` or '`wp_template_part'`. | |
| 131 | + * | |
| 132 | + * @return WP_Block_Template | |
| 133 | + */ | |
| 134 | + public function get_block_template_filter( $block_template, $id, $template_type ) { | |
| 135 | + if ( empty( $block_template ) && $template_type === 'wp_template_part' ) { | |
| 136 | + if ( $id === self::get_block_template_part_id() ) { | |
| 137 | + return $this->get_template(); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + return $block_template; | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Returns a custom template for the Subscribe Modal. | |
| 146 | + * | |
| 147 | + * @return WP_Block_Template | |
| 148 | + */ | |
| 149 | + public function get_template() { | |
| 150 | + $template = new WP_Block_Template(); | |
| 151 | + $template->theme = get_stylesheet(); | |
| 152 | + $template->slug = self::BLOCK_TEMPLATE_PART_SLUG; | |
| 153 | + $template->id = self::get_block_template_part_id(); | |
| 154 | + $template->area = 'uncategorized'; | |
| 155 | + $template->content = $this->get_subscribe_template_content(); | |
| 156 | + $template->source = 'plugin'; | |
| 157 | + $template->type = 'wp_template_part'; | |
| 158 | + $template->title = __( 'Jetpack Subscribe modal', 'jetpack' ); | |
| 159 | + $template->status = 'publish'; | |
| 160 | + $template->has_theme_file = false; | |
| 161 | + $template->is_custom = true; | |
| 162 | + $template->description = __( 'A subscribe form that pops up when someone visits your site.', 'jetpack' ); | |
| 163 | + | |
| 164 | + return $template; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Returns the initial content of the Subscribe Modal template. | |
| 169 | + * This can then be edited by the user. | |
| 170 | + * | |
| 171 | + * @return string | |
| 172 | + */ | |
| 173 | + public function get_subscribe_template_content() { | |
| 174 | + // translators: %s is the name of the site. | |
| 175 | + $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); | |
| 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' ); | |
| 179 | + | |
| 180 | + return <<<HTML | |
| 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"> | |
| 183 | + | |
| 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 --> | |
| 187 | + | |
| 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 --> | |
| 199 | +HTML; | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Returns true if a site visitor should see | |
| 204 | + * the Subscribe Modal. | |
| 205 | + * | |
| 206 | + * @return bool | |
| 207 | + */ | |
| 208 | + public function should_user_see_modal() { | |
| 209 | + // Only show when viewing frontend single post. | |
| 210 | + if ( is_admin() || ! is_singular( 'post' ) ) { | |
| 211 | + return false; | |
| 212 | + } | |
| 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 | + | |
| 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 | + } | |
| 257 | + return true; | |
| 258 | + } | |
| 259 | +} | |
| 260 | + | |
| 261 | +Jetpack_Subscribe_Modal::init(); | |
| 262 | + | |
| 263 | +add_action( | |
| 264 | + 'rest_api_switched_to_blog', | |
| 265 | + function () { | |
| 266 | + Jetpack_Subscribe_Modal::init(); | |
| 267 | + } | |
| 268 | +); | |