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 | |
---|
68 | /** |
---|
69 | * Adds modal with Subscribe Modal content. |
---|
70 | * |
---|
71 | * @return void |
---|
72 | */ |
---|
73 | public function add_subscribe_modal_to_frontend() { |
---|
74 | if ( $this->should_user_see_modal() ) { ?> |
---|
75 | <div class="jetpack-subscribe-modal"> |
---|
76 | <div class="jetpack-subscribe-modal__modal-content"> |
---|
77 | <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?> |
---|
78 | </div> |
---|
79 | </div> |
---|
80 | <?php |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Makes get_block_template return the WP_Block_Template for the Subscribe Modal. |
---|
86 | * |
---|
87 | * @param WP_Block_Template $block_template The block template to be returned. |
---|
88 | * @param string $id Template unique identifier (example: theme_slug//template_slug). |
---|
89 | * @param string $template_type Template type: `'wp_template'` or '`wp_template_part'`. |
---|
90 | * |
---|
91 | * @return WP_Block_Template |
---|
92 | */ |
---|
93 | public function get_block_template_filter( $block_template, $id, $template_type ) { |
---|
94 | if ( empty( $block_template ) && $template_type === 'wp_template_part' ) { |
---|
95 | if ( $id === self::get_block_template_part_id() ) { |
---|
96 | return $this->get_template(); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | return $block_template; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Returns a custom template for the Subscribe Modal. |
---|
105 | * |
---|
106 | * @return WP_Block_Template |
---|
107 | */ |
---|
108 | public function get_template() { |
---|
109 | $template = new WP_Block_Template(); |
---|
110 | $template->theme = get_stylesheet(); |
---|
111 | $template->slug = self::BLOCK_TEMPLATE_PART_SLUG; |
---|
112 | $template->id = self::get_block_template_part_id(); |
---|
113 | $template->area = 'uncategorized'; |
---|
114 | $template->content = $this->get_subscribe_template_content(); |
---|
115 | $template->source = 'plugin'; |
---|
116 | $template->type = 'wp_template_part'; |
---|
117 | $template->title = __( 'Jetpack Subscribe modal', 'jetpack' ); |
---|
118 | $template->status = 'publish'; |
---|
119 | $template->has_theme_file = false; |
---|
120 | $template->is_custom = true; |
---|
121 | $template->description = __( 'A subscribe form that pops up when someone visits your site', 'jetpack' ); |
---|
122 | |
---|
123 | return $template; |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Returns the initial content of the Subscribe Modal template. |
---|
128 | * This can then be edited by the user. |
---|
129 | * |
---|
130 | * @return string |
---|
131 | */ |
---|
132 | public function get_subscribe_template_content() { |
---|
133 | // translators: %s is the name of the site. |
---|
134 | $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) ); |
---|
135 | $continue_reading = __( 'Continue reading', 'jetpack' ); |
---|
136 | $subscribe_text = __( 'Subscribe now to keep reading and get access to the full archive.', 'jetpack' ); |
---|
137 | |
---|
138 | return <<<HTML |
---|
139 | <!-- wp:group {"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"}} --> |
---|
140 | <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"> |
---|
141 | |
---|
142 | <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} --> |
---|
143 | <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> |
---|
144 | <!-- /wp:heading --> |
---|
145 | |
---|
146 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} --> |
---|
147 | <p class='has-text-align-center' style='margin-top:4px;margin-bottom:0px;font-size:15px'>$subscribe_text</p> |
---|
148 | <!-- /wp:paragraph --> |
---|
149 | |
---|
150 | <!-- wp:jetpack/subscriptions {"borderRadius":50,"className":"is-style-compact"} /--> |
---|
151 | |
---|
152 | <!-- wp:paragraph {"align":"center","style":{"spacing":{"margin":{"top":"20px"}},"typography":{"fontSize":"14px"}},"className":"jetpack-subscribe-modal__close"} --> |
---|
153 | <p class="has-text-align-center jetpack-subscribe-modal__close" style="margin-top:20px;font-size:14px"><a href="#">$continue_reading</a></p> |
---|
154 | <!-- /wp:paragraph --> |
---|
155 | </div> |
---|
156 | <!-- /wp:group --> |
---|
157 | HTML; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Returns true if a site visitor should see |
---|
162 | * the Subscribe Modal. |
---|
163 | * |
---|
164 | * @return bool |
---|
165 | */ |
---|
166 | public function should_user_see_modal() { |
---|
167 | // Only show when viewing frontend single post. |
---|
168 | if ( is_admin() || ! is_singular( 'post' ) ) { |
---|
169 | return false; |
---|
170 | } |
---|
171 | |
---|
172 | // Needed because Elementor editor makes is_admin() return false |
---|
173 | // See https://coreysalzano.com/wordpress/why-elementor-disobeys-is_admin/ |
---|
174 | // Ignore nonce warning as just checking if is set |
---|
175 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
---|
176 | if ( isset( $_GET['elementor-preview'] ) ) { |
---|
177 | return false; |
---|
178 | } |
---|
179 | |
---|
180 | // Don't show when previewing blog posts or site's theme |
---|
181 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
---|
182 | if ( isset( $_GET['preview'] ) || isset( $_GET['theme_preview'] ) || isset( $_GET['customize_preview'] ) || isset( $_GET['hide_banners'] ) ) { |
---|
183 | return false; |
---|
184 | } |
---|
185 | |
---|
186 | // Don't show if one of subscribe query params is set. |
---|
187 | // They are set when user submits the subscribe form. |
---|
188 | // The nonce is checked elsewhere before redirect back to this page with query params. |
---|
189 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
---|
190 | if ( isset( $_GET['subscribe'] ) || isset( $_GET['blogsub'] ) ) { |
---|
191 | return false; |
---|
192 | } |
---|
193 | |
---|
194 | // Don't show if post is for subscribers only or has paywall block |
---|
195 | global $post; |
---|
196 | if ( defined( 'Automattic\\Jetpack\\Extensions\\Subscriptions\\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS' ) ) { |
---|
197 | $access_level = get_post_meta( $post->ID, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); |
---|
198 | } else { |
---|
199 | $access_level = get_post_meta( $post->ID, '_jetpack_newsletter_access', true ); |
---|
200 | } |
---|
201 | require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php'; |
---|
202 | $is_accessible_by_everyone = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $access_level || empty( $access_level ); |
---|
203 | if ( ! $is_accessible_by_everyone ) { |
---|
204 | return false; |
---|
205 | } |
---|
206 | |
---|
207 | // Don't show if user is subscribed to blog. |
---|
208 | require_once __DIR__ . '/../views.php'; |
---|
209 | if ( ! class_exists( 'Jetpack_Memberships' ) || Jetpack_Memberships::is_current_user_subscribed() ) { |
---|
210 | return false; |
---|
211 | } |
---|
212 | return true; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | Jetpack_Subscribe_Modal::init(); |
---|
217 | |
---|
218 | add_action( |
---|
219 | 'rest_api_switched_to_blog', |
---|
220 | function () { |
---|
221 | Jetpack_Subscribe_Modal::init(); |
---|
222 | } |
---|
223 | ); |
---|