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