PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
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 13.7.2 13.8.3 All 506 releases
jetpack / modules / comments / subscription-modal-on-comment / class-jetpack-subscription-modal-on-comment.php

class-jetpack-subscription-modal-on-comment.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at modules/comments/subscription-modal-on-comment/class-jetpack-subscription-modal-on-comment.php

219 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Adds support for Jetpack Subscription Modal On Comment feature
4 * Limited to Atomic sites.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 * @since 12.4
8 */
9
10 use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
11 use Automattic\Jetpack\Status\Host;
12 use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit( 0 );
16 }
17
18 /**
19 * Jetpack_Subscription_Modal_On_Comment class.
20 */
21 class Jetpack_Subscription_Modal_On_Comment {
22 /**
23 * Jetpack_Subscription_Modal_On_Comment singleton instance.
24 *
25 * @var Jetpack_Subscription_Modal_On_Comment|null
26 */
27 private static $instance;
28
29 /**
30 * Jetpack_Subscription_Modal_On_Comment instance init.
31 */
32 public static function init() {
33 if ( self::$instance === null ) {
34 self::$instance = new Jetpack_Subscription_Modal_On_Comment();
35 }
36
37 return self::$instance;
38 }
39
40 const BLOCK_TEMPLATE_PART_SLUG = 'jetpack-subscription-modal';
41
42 /**
43 * Returns the block template part ID.
44 *
45 * @return string
46 */
47 public static function get_block_template_part_id() {
48 return get_stylesheet() . '//' . self::BLOCK_TEMPLATE_PART_SLUG;
49 }
50
51 /**
52 * Jetpack_Subscription_Modal_On_Comment class constructor.
53 * Limited to Atomic sites.
54 */
55 public function __construct() {
56 if ( ( new Host() )->is_woa_site() && get_option( 'jetpack_verbum_subscription_modal', true ) ) {
57 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
58 add_action( 'wp_footer', array( $this, 'add_subscription_modal_to_frontend' ) );
59 add_filter( 'get_block_template', array( $this, 'get_block_template_filter' ), 10, 3 );
60 }
61 }
62
63 /**
64 * Enqueues JS to load modal.
65 *
66 * @return void
67 */
68 public function enqueue_assets() {
69 if ( ! $this->should_user_see_modal() ) {
70 return;
71 }
72
73 wp_enqueue_style( 'subscription-modal-css', plugins_url( 'subscription-modal.css', __FILE__ ), array(), JETPACK__VERSION );
74 wp_enqueue_script( 'subscription-modal-js', plugins_url( 'subscription-modal.js', __FILE__ ), array( 'wp-dom-ready' ), JETPACK__VERSION, true );
75 wp_localize_script(
76 'subscription-modal-js',
77 'subscriptionData',
78 array(
79 'homeUrl' => wp_parse_url( home_url(), PHP_URL_HOST ),
80 )
81 );
82 }
83
84 /**
85 * Adds modal with Subscribe Modal content.
86 *
87 * @return void
88 */
89 public function add_subscription_modal_to_frontend() {
90 if ( $this->should_user_see_modal() ) { ?>
91 <div class="jetpack-subscription-modal">
92 <div class="jetpack-subscription-modal__modal-content">
93 <?php block_template_part( self::BLOCK_TEMPLATE_PART_SLUG ); ?>
94 </div>
95 </div>
96 <?php
97 }
98 }
99
100 /**
101 * Makes get_block_template return the WP_Block_Template for the Subscribe Modal.
102 *
103 * @param WP_Block_Template $block_template The block template to be returned.
104 * @param string $id Template unique identifier (example: theme_slug//template_slug).
105 * @param string $template_type Template type: `'wp_template'` or '`wp_template_part'`.
106 *
107 * @return WP_Block_Template
108 */
109 public function get_block_template_filter( $block_template, $id, $template_type ) {
110 if ( empty( $block_template ) && $template_type === 'wp_template_part' ) {
111 if ( $id === self::get_block_template_part_id() ) {
112 return $this->get_template();
113 }
114 }
115
116 return $block_template;
117 }
118
119 /**
120 * Returns a custom template for the Subscribe Modal.
121 *
122 * @return WP_Block_Template
123 */
124 public function get_template() {
125 $template = new WP_Block_Template();
126 $template->theme = get_stylesheet();
127 $template->slug = self::BLOCK_TEMPLATE_PART_SLUG;
128 $template->id = self::get_block_template_part_id();
129 $template->area = 'uncategorized';
130 $template->content = $this->get_subscribe_template_content();
131 $template->source = 'plugin';
132 $template->type = 'wp_template_part';
133 $template->title = __( 'Jetpack Subscription modal', 'jetpack' );
134 $template->status = 'publish';
135 $template->has_theme_file = false;
136 $template->is_custom = true;
137 $template->description = __( 'A subscribe form that submit a comment.', 'jetpack' );
138
139 return $template;
140 }
141
142 /**
143 * Returns the initial content of the Subscribe Modal template.
144 * This can then be edited by the user.
145 *
146 * @return string
147 */
148 public function get_subscribe_template_content() {
149 // translators: %s is the name of the site.
150 $discover_more_from = sprintf( __( 'Discover more from %s', 'jetpack' ), get_bloginfo( 'name' ) );
151 $subscribe_text = __( 'Subscribe now to keep reading and get access to the full archive.', 'jetpack' );
152 $continue_reading = __( 'Continue reading', 'jetpack' );
153
154 return <<<HTML
155 <!-- wp:group {"style":{"spacing":{"top":"32px","bottom":"32px","left":"32px","right":"32px"},"margin":{"top":"0","bottom":"0"}},"border":{"color":"#dddddd","width":"1px"}},"layout":{"type":"constrained","contentSize":"450px"}} -->
156 <div class="wp-block-group has-border-color jetpack-subscription-modal__modal-content-form" style="border-color:#dddddd;border-width:1px;margin-top:0;margin-bottom:0;padding:32px">
157
158 <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"26px"},"layout":{"selfStretch":"fit","flexSize":null},"spacing":{"margin":{"top":"4px","bottom":"10px"}}}} -->
159 <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>
160 <!-- /wp:heading -->
161
162 <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"15px"},"spacing":{"margin":{"top":"4px","bottom":"0px"}}}} -->
163 <p class='has-text-align-center' style='margin-top:4px;margin-bottom:0px;font-size:15px'>$subscribe_text</p>
164 <!-- /wp:paragraph -->
165
166 <!-- wp:jetpack/subscriptions {"borderRadius":50,"className":"is-style-compact","appSource":"atomic-subscription-modal-lo"} /-->
167
168 <!-- wp:paragraph {"align":"center","style":{"spacing":{"margin":{"top":"20px"}},"typography":{"fontSize":"14px"}},"className":"jetpack-subscription-modal__close"} -->
169 <p class="has-text-align-center jetpack-subscription-modal__close" style="margin-top:20px;font-size:14px"><a href="#">$continue_reading</a></p>
170 <!-- /wp:paragraph -->
171 </div>
172 <!-- /wp:group -->
173 HTML;
174 }
175
176 /**
177 * Returns true if a site visitor should see
178 * the Subscribe Modal.
179 *
180 * @return bool
181 */
182 public function should_user_see_modal() {
183 // Only show when viewing frontend single post.
184 if ( is_admin() || ! is_singular( 'post' ) ) {
185 return false;
186 }
187
188 // Don't show if post is for subscribers only or has paywall block
189 global $post;
190
191 if ( ! $post instanceof WP_Post ) {
192 return false;
193 }
194
195 if ( defined( 'Automattic\\Jetpack\\Extensions\\Subscriptions\\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS' ) ) {
196 $access_level = get_post_meta( $post->ID, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true );
197 } else {
198 $access_level = get_post_meta( $post->ID, '_jetpack_newsletter_access', true );
199 }
200 require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
201 $is_accessible_by_everyone = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $access_level || empty( $access_level );
202
203 if ( ! $is_accessible_by_everyone ) {
204 return false;
205 }
206
207 return true;
208 }
209 }
210
211 Jetpack_Subscription_Modal_On_Comment::init();
212
213 add_action(
214 'rest_api_switched_to_blog',
215 function () {
216 Jetpack_Subscription_Modal_On_Comment::init();
217 }
218 );
219