PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 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 All 508 releases
← All changes | modules/subscriptions/subscribe-overlay/class-jetpack-subscribe-overlay.php +233 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,233 @@
1 +<?php
2 +/**
3 + * Adds support for Jetpack Subscribe Overlay feature
4 + *
5 + * @package automattic/jetpack-subscriptions
6 + * @since 13.5
7 + */
8 +
9 +if ( ! defined( 'ABSPATH' ) ) {
10 + exit( 0 );
11 +}
12 +
13 +/**
14 + * Jetpack_Subscribe_Overlay class.
15 + */
16 +class Jetpack_Subscribe_Overlay {
17 + /**
18 + * Jetpack_Subscribe_Overlay singleton instance.
19 + *
20 + * @var Jetpack_Subscribe_Overlay|null
21 + */
22 + private static $instance;
23 +
24 + /**
25 + * Jetpack_Subscribe_Overlay instance init.
26 + */
27 + public static function init() {
28 + if ( self::$instance === null ) {
29 + self::$instance = new Jetpack_Subscribe_Overlay();
30 + }
31 +
32 + return self::$instance;
33 + }
34 +
35 + const BLOCK_TEMPLATE_PART_SLUG = 'jetpack-subscribe-overlay';
36 +
37 + /**
38 + * Jetpack_Subscribe_Overlay class constructor.
39 + */
40 + public function __construct() {
41 + if ( get_option( 'jetpack_subscribe_overlay_enabled', false ) ) {
42 + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
43 + add_action( 'wp_footer', array( $this, 'add_subscribe_overlay_to_frontend' ) );
44 + }
45 +
46 + add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 );
47 +
48 + add_filter(
49 + 'jetpack_options_whitelist',
50 + function ( $options ) {
51 + $options[] = 'jetpack_subscribe_overlay_enabled';
52 +
53 + return $options;
54 + }
55 + );
56 + }
57 +
58 + /**
59 + * Returns the block template part ID.
60 + *
61 + * @return string
62 + */
63 + public static function get_block_template_part_id() {
64 + return get_stylesheet() . '//' . self::BLOCK_TEMPLATE_PART_SLUG;
65 + }
66 +
67 + /**
68 + * Makes get_block_template return the WP_Block_Template for the Subscribe Overlay.
69 + *
70 + * @param WP_Block_Template $block_template The block template to be returned.
71 + * @param string $id Template unique identifier (example: theme_slug//template_slug).
72 + * @param string $template_type Template type: `'wp_template'` or '`wp_template_part'`.
73 + *
74 + * @return WP_Block_Template|null
75 + */
76 + public function get_block_template_filter( $block_template, $id, $template_type ) {
77 + if ( empty( $block_template ) && $template_type === 'wp_template_part' ) {
78 + if ( $id === self::get_block_template_part_id() ) {
79 + return $this->get_template();
80 + }
81 + }
82 +
83 + return $block_template;
84 + }
85 +
86 + /**
87 + * Returns a custom template for the Subscribe Overlay.
88 + *
89 + * @return WP_Block_Template
90 + */
91 + public function get_template() {
92 + $template = new WP_Block_Template();
93 + $template->theme = get_stylesheet();
94 + $template->slug = self::BLOCK_TEMPLATE_PART_SLUG;
95 + $template->id = self::get_block_template_part_id();
96 + $template->area = 'uncategorized';
97 + $template->content = $this->get_subscribe_overlay_template_content();
98 + $template->source = 'plugin';
99 + $template->type = 'wp_template_part';
100 + $template->title = __( 'Jetpack Subscribe overlay', 'jetpack' );
101 + $template->status = 'publish';
102 + $template->has_theme_file = false;
103 + $template->is_custom = true;
104 + $template->description = __( 'An overlay that shows up when someone visits your site.', 'jetpack' );
105 +
106 + return $template;
107 + }
108 +
109 + /**
110 + * Returns the initial content of the Subscribe Overlay template.
111 + * This can then be edited by the user.
112 + *
113 + * @return string
114 + */
115 + public function get_subscribe_overlay_template_content() {
116 + $home_url = get_home_url();
117 + $site_tagline = get_bloginfo( 'description' );
118 + $default_tagline = __( 'Stay informed with curated content and the latest headlines, all delivered straight to your inbox. Subscribe now to stay ahead and never miss a beat!', 'jetpack' );
119 + $tagline_block = empty( $site_tagline )
120 + ? '<!-- wp:paragraph {"align":"center","fontSize":"medium"} --><p class="has-text-align-center has-medium-font-size">' . $default_tagline . '</p><!-- /wp:paragraph -->'
121 + : '<!-- wp:site-tagline {"textAlign":"center","fontSize":"medium"} /-->';
122 + $skip_to_content = __( 'Skip to content', 'jetpack' );
123 + $group_block_name = esc_attr__( 'Subscribe overlay container', 'jetpack' );
124 +
125 + return <<<HTML
126 + <!-- wp:group {"metadata":{"name":"$group_block_name"},"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}}},"layout":{"type":"constrained","contentSize":"400px"}} -->
127 + <div class="wp-block-group" style="padding-top:0;padding-right:0;padding-bottom:0;padding-left:0">
128 + <!-- wp:site-logo {"width":90,"isLink":false,"shouldSyncIcon":true,"align":"center","className":"is-style-rounded"} /-->
129 +
130 + <!-- wp:site-title {"textAlign":"center","isLink":false,"fontSize":"x-large"} /-->
131 +
132 + $tagline_block
133 +
134 + <!-- wp:jetpack/subscriptions {"appSource":"subscribe-overlay"} /-->
135 +
136 + <!-- wp:paragraph {"align":"center","className":"jetpack-subscribe-overlay__to-content"} -->
137 + <p class="has-text-align-center jetpack-subscribe-overlay__to-content"><a href="$home_url">$skip_to_content ↓</a></p>
138 + <!-- /wp:paragraph -->
139 + </div>
140 + <!-- /wp:group -->
141 +HTML;
142 + }
143 +
144 + /**
145 + * Enqueues JS to load overlay.
146 + *
147 + * @return void
148 + */
149 + public function enqueue_assets() {
150 + if ( $this->should_user_see_overlay() ) {
151 + wp_enqueue_style( 'subscribe-overlay-css', plugins_url( 'subscribe-overlay.css', __FILE__ ), array(), JETPACK__VERSION );
152 + wp_enqueue_script( 'subscribe-overlay-js', plugins_url( 'subscribe-overlay.js', __FILE__ ), array( 'wp-dom-ready' ), JETPACK__VERSION, true );
153 + }
154 + }
155 +
156 + /**
157 + * Adds overlay with Subscribe Overlay content.
158 + *
159 + * @return void
160 + */
161 + public function add_subscribe_overlay_to_frontend() {
162 + if ( $this->should_user_see_overlay() ) { ?>
163 + <div class="jetpack-subscribe-overlay">
164 + <div class="jetpack-subscribe-overlay__close">
165 + <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
166 + <path d="M5.40456 5L19 19M5 19L18.5954 5" stroke="currentColor" stroke-width="1.5"/>
167 + </svg>
168 + </div>
169 + <div class="jetpack-subscribe-overlay__content">
170 + <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?>
171 + </div>
172 + </div>
173 + <?php
174 + }
175 + }
176 +
177 + /**
178 + * Returns true if a site visitor should see
179 + * the Subscribe Overlay.
180 + *
181 + * @return bool
182 + */
183 + public function should_user_see_overlay() {
184 + // Only show when viewing frontend.
185 + if ( is_admin() ) {
186 + return false;
187 + }
188 +
189 + // The overlay only appears on the blog home and front page.
190 + if ( ! is_home() && ! is_front_page() ) {
191 + return false;
192 + }
193 +
194 + // Needed because Elementor editor makes is_admin() return false
195 + // See https://coreysalzano.com/wordpress/why-elementor-disobeys-is_admin/
196 + // Ignore nonce warning as just checking if is set
197 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
198 + if ( isset( $_GET['elementor-preview'] ) ) {
199 + return false;
200 + }
201 +
202 + // Don't show when previewing blog posts or site's theme
203 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
204 + if ( isset( $_GET['preview'] ) || isset( $_GET['theme_preview'] ) || isset( $_GET['customize_preview'] ) || isset( $_GET['hide_banners'] ) ) {
205 + return false;
206 + }
207 +
208 + // Don't show if one of subscribe query params is set.
209 + // They are set when user submits the subscribe form.
210 + // The nonce is checked elsewhere before redirect back to this page with query params.
211 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
212 + if ( isset( $_GET['subscribe'] ) || isset( $_GET['blogsub'] ) ) {
213 + return false;
214 + }
215 +
216 + // Don't show if user is subscribed to blog.
217 + require_once __DIR__ . '/../views.php';
218 + if ( ! class_exists( 'Jetpack_Memberships' ) || Jetpack_Memberships::is_current_user_subscribed() ) {
219 + return false;
220 + }
221 +
222 + return true;
223 + }
224 +}
225 +
226 +Jetpack_Subscribe_Overlay::init();
227 +
228 +add_action(
229 + 'rest_api_switched_to_blog',
230 + function () {
231 + Jetpack_Subscribe_Overlay::init();
232 + }
233 +);