PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 14.7.1
Jetpack – WP Security, Backup, Speed, & Growth v14.7.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 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / subscriptions / views.php
jetpack / modules / subscriptions Last commit date
newsletter-widget 1 year ago subscribe-floating-button 1 year ago subscribe-modal 1 year ago subscribe-overlay 1 year ago class-settings.php 1 year ago jetpack-user-content-link-redirection.php 1 year ago readme.md 7 years ago subscriptions.css 7 years ago views.php 1 year ago
views.php
1060 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
4
5 /**
6 * Jetpack_Subscriptions_Widget main view class.
7 */
8 class Jetpack_Subscriptions_Widget extends WP_Widget {
9
10 const ID_BASE = 'blog_subscription';
11
12 /**
13 * Track number of rendered Subscription widgets. The count is used for class names and widget IDs.
14 *
15 * @var int
16 */
17 public static $instance_count = 0;
18
19 /**
20 * When printing the submit button, what tags are allowed.
21 *
22 * @var array
23 */
24 public static $allowed_html_tags_for_submit_button = array(
25 'br' => array(),
26 's' => array(),
27 'strong' => array(),
28 'em' => array(),
29 );
30
31 /**
32 * Use this variable when printing the message after submitting an email in subscription widgets
33 *
34 * @var array what tags are allowed
35 */
36 public static $allowed_html_tags_for_message = array(
37 'a' => array(
38 'href' => array(),
39 'title' => array(),
40 'rel' => array(),
41 'target' => array(),
42 ),
43 'br' => array(),
44 );
45
46 /**
47 * Jetpack_Subscriptions_Widget constructor.
48 */
49 public function __construct() {
50 $widget_ops = array(
51 'classname' => 'widget_blog_subscription jetpack_subscription_widget',
52 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
53 'customize_selective_refresh' => true,
54 'show_instance_in_rest' => true,
55 );
56
57 $name = self::is_jetpack() ?
58 /** This filter is documented in modules/widgets/facebook-likebox.php */
59 apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) :
60 __( 'Follow Blog', 'jetpack' );
61
62 parent::__construct(
63 'blog_subscription',
64 $name,
65 $widget_ops
66 );
67
68 if ( self::is_jetpack() &&
69 (
70 is_active_widget( false, false, $this->id_base ) ||
71 is_active_widget( false, false, 'monster' ) ||
72 is_customize_preview()
73 )
74 ) {
75 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
76 }
77
78 add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
79 }
80
81 /**
82 * Remove the "Blog Subscription" widget from the Legacy Widget block
83 *
84 * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
85 * @return array $widget_types New list of widgets that will be removed.
86 */
87 public function hide_widget_in_block_editor( $widget_types ) {
88 $widget_types[] = self::ID_BASE;
89 return $widget_types;
90 }
91
92 /**
93 * Enqueue the form's CSS.
94 *
95 * @since 4.5.0
96 */
97 public function enqueue_style() {
98 wp_register_style(
99 'jetpack-subscriptions',
100 plugins_url( 'subscriptions.css', __FILE__ ),
101 array(),
102 JETPACK__VERSION
103 );
104 wp_enqueue_style( 'jetpack-subscriptions' );
105 }
106
107 /**
108 * Renders a full widget either within the context of WordPress widget, or in response to a shortcode.
109 *
110 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
111 * @param array $instance The settings for the particular instance of the widget.
112 */
113 public function widget( $args, $instance ) {
114 if ( self::is_wpcom() && ! self::wpcom_has_status_message() && self::is_current_user_subscribed() ) {
115 return null;
116 }
117 if ( self::is_jetpack() &&
118 /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */
119 false === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
120 ) {
121 $subscribe_email = '';
122 } else {
123 $current_user = wp_get_current_user();
124 if ( ! empty( $current_user->user_email ) ) {
125 $subscribe_email = esc_attr( $current_user->user_email );
126 } else {
127 $subscribe_email = '';
128 }
129 }
130
131 $stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog';
132 /** This action is documented in modules/widgets/gravatar-profile.php */
133 do_action( 'jetpack_stats_extra', 'widget_view', $stats_action );
134
135 $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
136 $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
137 $instance = wp_parse_args( (array) $instance, static::defaults() );
138
139 echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
140
141 ++self::$instance_count;
142
143 self::render_widget_title( $args, $instance );
144
145 self::render_widget_status_messages( $instance );
146
147 self::render_widget_subscription_form( $args, $instance, $subscribe_email );
148
149 echo "\n" . $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
150 }
151
152 /**
153 * Prints the widget's title. If show_only_email_and_button is true, we will not show a title.
154 *
155 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
156 * @param array $instance The settings for the particular instance of the widget.
157 */
158 public static function render_widget_title( $args, $instance ) {
159 $show_only_email_and_button = $instance['show_only_email_and_button'];
160 $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
161 $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
162 if ( self::is_wpcom() && ! $show_only_email_and_button ) {
163 if ( self::is_current_user_subscribed() ) {
164 if ( ! empty( $instance['title_following'] ) ) {
165 printf(
166 '%1$s<label for="subscribe-field%2$s">%3$s</label>%4$s%5$s',
167 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
168 ( self::$instance_count > 1 ? '-' . (int) self::$instance_count : '' ),
169 esc_html( $instance['title_following'] ),
170 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
171 "\n"
172 );
173 }
174 } elseif ( ! empty( $instance['title'] ) ) {
175 printf(
176 '%1$s<label for="subscribe-field%2$s">%3$s</label>%4$s%5$s',
177 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
178 ( self::$instance_count > 1 ? '-' . (int) self::$instance_count : '' ),
179 esc_html( $instance['title'] ),
180 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
181 "\n"
182 );
183 }
184 }
185
186 if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) {
187 printf(
188 '%1$s%2$s%3$s%4$s',
189 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
190 esc_html( $instance['title'] ),
191 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
192 "\n"
193 );
194 }
195 }
196
197 /**
198 * Prints the subscription block's status messages after someone has attempted to subscribe.
199 * Either a success message or an error message.
200 *
201 * @param array $instance The settings for the particular instance of the widget.
202 */
203 public static function render_widget_status_messages( $instance ) {
204 if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
205 $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
206 $subscribers_total = self::fetch_subscriber_count();
207
208 switch ( $_GET['subscribe'] ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended
209 case 'invalid_email':
210 ?>
211 <p class="error"><?php esc_html_e( 'Oops! The email you used is invalid. Please try again.', 'jetpack' ); ?></p>
212 <?php
213 break;
214 case 'opted_out':
215 ?>
216 <p class="error">
217 <?php
218 printf(
219 wp_kses(
220 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link. */
221 __( 'Oops! It seems that the email you used has opted out of subscriptions. You can manage your preferences from the <a href="%1$s" title="%2$s" target="_blank">Subscriptions Manager</a>', 'jetpack' ),
222 self::$allowed_html_tags_for_message
223 ),
224 'https://subscribe.wordpress.com/',
225 esc_attr__( 'Subscriptions Manager', 'jetpack' )
226 );
227 ?>
228 </p>
229 <?php
230 break;
231 case 'already':
232 ?>
233 <p class="error">
234 <?php
235 printf(
236 wp_kses(
237 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link. */
238 __( 'You have already subscribed to this site. Please check your email inbox. You can manage your preferences from the <a href="%1$s" title="%2$s" target="_blank">Subscriptions Manager</a>.', 'jetpack' ),
239 self::$allowed_html_tags_for_message
240 ),
241 'https://subscribe.wordpress.com/',
242 esc_attr__( 'Subscriptions Manager', 'jetpack' )
243 );
244 ?>
245 </p>
246 <?php
247 break;
248 case 'many_pending_subs':
249 ?>
250 <p class="error">
251 <?php
252 printf(
253 wp_kses(
254 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
255 __( 'Oops! It seems you have several subscriptions pending confirmation. You can confirm or unsubscribe some from the <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">Subscriptions Manager</a> before adding more.', 'jetpack' ),
256 self::$allowed_html_tags_for_message
257 ),
258 'https://subscribe.wordpress.com/',
259 esc_attr__( 'Subscriptions Manager', 'jetpack' )
260 );
261 ?>
262 </p>
263 <?php
264 break;
265 case 'pending':
266 ?>
267 <p class="error">
268 <?php
269 printf(
270 wp_kses(
271 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
272 __( 'It seems you already tried to subscribe with this email, but have not confirmed from the email link we sent. Please check your email inbox to confirm or you can manage your preferences from the <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">Subscriptions Manager</a>.', 'jetpack' ),
273 self::$allowed_html_tags_for_message
274 ),
275 'https://subscribe.wordpress.com/',
276 esc_attr__( 'Subscriptions Manager', 'jetpack' )
277 );
278 ?>
279 </p>
280 <?php
281 break;
282 case 'success':
283 ?>
284 <div class="success"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $success_message ) ), 'post' ); ?></div>
285 <?php
286 break;
287 default:
288 ?>
289 <p class="error"><?php esc_html_e( 'Oops! There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
290 <?php
291 break;
292 endswitch;
293 }
294
295 if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
296 global $themecolors;
297 $message = '';
298
299 switch ( $_GET['blogsub'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
300 case 'confirming':
301 $message = __( 'Thank you! You can now check your email to confirm your subscription.', 'jetpack' );
302 break;
303 case 'blocked':
304 $message = __( 'Sorry but this email has been blocked for this subscription. <a href="https://en.support.wordpress.com/contact/">Contact us</a> if needed.', 'jetpack' );
305 break;
306 case 'flooded':
307 $message = __( 'Oops! It seems you have several subscriptions pending confirmation. You can confirm or unsubscribe some from the <a href="https://subscribe.wordpress.com/">Subscriptions Manager</a> before adding more.', 'jetpack' );
308 break;
309 case 'spammed':
310 /* translators: %s is a URL */
311 $message = sprintf( __( 'Sorry but this email has been blocked. It has too many subscriptions pending confirmation. Please confirm or unsubscribe some from the <a href="%s">Subscriptions Manager</a>.', 'jetpack' ), 'https://subscribe.wordpress.com/' );
312 break;
313 case 'subscribed':
314 $message = __( 'Hey! You were already subscribed.', 'jetpack' );
315 break;
316 case 'pending':
317 $message = __( 'It seems you already tried to subscribe. We just sent you another email so you can confirm the subscription.', 'jetpack' );
318 break;
319 case 'confirmed':
320 $message = __( 'Cool! You are now subscribed. Now you can check your email for more details and how to manage the subscription.', 'jetpack' );
321 break;
322 }
323
324 $border_color = isset( $themecolors['border'] ) ? " #{$themecolors['border']}" : '';
325
326 $redirect_fragment = self::get_redirect_fragment();
327 printf(
328 '<div id="%1$s" class="jetpack-sub-notification">%3$s</div>',
329 esc_attr( $redirect_fragment ),
330 esc_attr( $border_color ),
331 wp_kses_post( $message )
332 );
333 }
334 }
335
336 /**
337 * Generates the redirect fragment used after form submission.
338 *
339 * @param string $id is the specific id that will appear in the redirect fragment. If none is provided self::$instance_count will be used.
340 */
341 protected static function get_redirect_fragment( $id = null ) {
342 if ( $id === null ) {
343 return 'subscribe-blog' . ( self::$instance_count > 1 ? '-' . self::$instance_count : '' );
344 }
345
346 return 'subscribe-blog-' . $id;
347 }
348
349 /**
350 * Renders a form allowing folks to subscribe to the blog.
351 *
352 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
353 * @param array $instance The settings for the particular instance of the widget.
354 * @param string $subscribe_email The email to use to prefill the form.
355 */
356 public static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
357 $show_only_email_and_button = $instance['show_only_email_and_button'];
358 $show_subscribers_total = (bool) $instance['show_subscribers_total'];
359 $subscribers_total = self::fetch_subscriber_count();
360 $subscribe_text = empty( $instance['show_only_email_and_button'] ) ?
361 wp_kses_post( stripslashes( $instance['subscribe_text'] ) ) :
362 false;
363 $referer = esc_url_raw( ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) );
364 $source = 'widget';
365 $widget_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : self::$instance_count;
366 $subscribe_button = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
367 $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
368 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? 'wp-block-button__link ' . $instance['submit_button_classes'] : 'wp-block-button__link';
369 $submit_button_styles = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
370 $submit_button_wrapper_styles = isset( $instance['submit_button_wrapper_styles'] ) ? $instance['submit_button_wrapper_styles'] : '';
371 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
372 $email_field_styles = isset( $instance['email_field_styles'] ) ? $instance['email_field_styles'] : '';
373
374 // We need to include those in case Jetpack blocks are disabled
375 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
376 require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
377 $post_access_level = \Jetpack_Memberships::get_post_access_level();
378
379 if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
380 global $current_blog;
381
382 $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
383 $form_id = self::get_redirect_fragment();
384 ?>
385
386 <div class="wp-block-jetpack-subscriptions__container">
387 <form
388 action="<?php echo esc_url( $url ); ?>"
389 method="post"
390 accept-charset="utf-8"
391 data-blog="<?php echo esc_attr( get_current_blog_id() ); ?>"
392 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
393 id="<?php echo esc_attr( $form_id ); ?>"
394 >
395 <?php
396 if ( ! $show_only_email_and_button ) {
397 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
398 echo wpautop( $subscribe_text );
399 }
400 $email_field_id = 'subscribe-field';
401 $email_field_id .= self::$instance_count > 1
402 ? '-' . self::$instance_count
403 : '';
404 $label_field_id = $email_field_id . '-label';
405 ?>
406 <p id="subscribe-email">
407 <label
408 id="<?php echo esc_attr( $label_field_id ); ?>"
409 for="<?php echo esc_attr( $email_field_id ); ?>"
410 class="screen-reader-text"
411 >
412 <?php echo esc_html__( 'Email Address:', 'jetpack' ); ?>
413 </label>
414
415 <?php
416 printf(
417 '<input
418 type="email"
419 name="email"
420 %1$s
421 style="%2$s"
422 placeholder="%3$s"
423 value=""
424 id="%4$s"
425 required
426 />',
427 ( ! empty( $email_field_classes )
428 ? 'class="' . esc_attr( $email_field_classes ) . '"'
429 : ''
430 ),
431 ( ! empty( $email_field_styles )
432 ? esc_attr( $email_field_styles )
433 : 'width: 95%; padding: 1px 10px'
434 ),
435 ( empty( $subscribe_placeholder ) ? esc_attr__( 'Enter your email address', 'jetpack' ) : esc_attr( $subscribe_placeholder ) ),
436 esc_attr( $email_field_id )
437 );
438 ?>
439 </p>
440
441 <p id="subscribe-submit"
442 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
443 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
444 <?php } ?>
445 >
446 <input type="hidden" name="action" value="subscribe"/>
447 <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
448 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
449 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
450 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
451 <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
452 <button type="submit"
453 <?php if ( ! empty( $submit_button_classes ) ) { ?>
454 class="<?php echo esc_attr( $submit_button_classes ); ?>"
455 <?php } ?>
456 <?php if ( ! empty( $submit_button_styles ) ) { ?>
457 style="<?php echo esc_attr( $submit_button_styles ); ?>"
458 <?php } ?>
459 >
460 <?php
461 echo wp_kses(
462 html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
463 self::$allowed_html_tags_for_submit_button
464 );
465 ?>
466 </button>
467 </p>
468 </form>
469 <?php if ( $show_subscribers_total && $subscribers_total > 0 ) { ?>
470 <div class="wp-block-jetpack-subscriptions__subscount">
471 <?php
472 echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
473 ?>
474 </div>
475 <?php } ?>
476 </div>
477 <?php
478 }
479
480 if ( self::is_jetpack() ) {
481 /**
482 * Filter the subscription form's ID prefix.
483 *
484 * @module subscriptions
485 *
486 * @since 2.7.0
487 *
488 * @param string subscribe-field Subscription form field prefix.
489 * @param int $widget_id Widget ID.
490 */
491 $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
492
493 $form_id = self::get_redirect_fragment( $widget_id );
494 ?>
495 <div class="wp-block-jetpack-subscriptions__container">
496 <form action="#" method="post" accept-charset="utf-8" id="<?php echo esc_attr( $form_id ); ?>"
497 data-blog="<?php echo esc_attr( \Jetpack_Options::get_option( 'id' ) ); ?>"
498 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" >
499 <?php
500 if ( $subscribe_text && ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
501 ?>
502 <div id="subscribe-text"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $subscribe_text ) ), 'post' ); ?></div>
503 <?php
504 }
505
506 if ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display of unsubmitted form.
507 ?>
508 <p id="subscribe-email">
509 <label id="jetpack-subscribe-label"
510 class="screen-reader-text"
511 for="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>">
512 <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
513 </label>
514 <input type="email" name="email" required="required"
515 <?php if ( ! empty( $email_field_classes ) ) { ?>
516 class="<?php echo esc_attr( $email_field_classes ); ?> required"
517 <?php } ?>
518 <?php if ( ! empty( $email_field_styles ) ) { ?>
519 style="<?php echo esc_attr( $email_field_styles ); ?>"
520 <?php } ?>
521 value="<?php echo esc_attr( $subscribe_email ); ?>"
522 id="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>"
523 placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
524 />
525 </p>
526
527 <p id="subscribe-submit"
528 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
529 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
530 <?php } ?>
531 >
532 <input type="hidden" name="action" value="subscribe"/>
533 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
534 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
535 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
536 <?php wp_nonce_field( 'blogsub_subscribe_' . \Jetpack_Options::get_option( 'id' ) ); ?>
537 <button type="submit"
538 <?php if ( ! empty( $submit_button_classes ) ) { ?>
539 class="<?php echo esc_attr( $submit_button_classes ); ?>"
540 <?php } ?>
541 <?php if ( ! empty( $submit_button_styles ) ) { ?>
542 style="<?php echo esc_attr( $submit_button_styles ); ?>"
543 <?php } ?>
544 name="jetpack_subscriptions_widget"
545 >
546 <?php
547 echo wp_kses(
548 html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
549 self::$allowed_html_tags_for_submit_button
550 );
551 ?>
552 </button>
553 </p>
554 <?php } ?>
555 </form>
556 <?php if ( $show_subscribers_total && 0 < $subscribers_total ) { ?>
557 <div class="wp-block-jetpack-subscriptions__subscount">
558 <?php
559 echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
560 ?>
561 </div>
562 <?php } ?>
563 </div>
564 <?php
565 }
566 }
567
568 /**
569 * Determines if the current user is subscribed to the blog.
570 *
571 * @return bool Is the person already subscribed.
572 */
573 public static function is_current_user_subscribed() {
574 $subscribed = isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
575
576 if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
577 $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
578 }
579
580 return $subscribed;
581 }
582
583 /**
584 * Is this script running in the wordpress.com environment?
585 *
586 * @return bool
587 */
588 public static function is_wpcom() {
589 return defined( 'IS_WPCOM' ) && IS_WPCOM;
590 }
591
592 /**
593 * Is this script running in a self-hosted environment?
594 *
595 * @return bool
596 */
597 public static function is_jetpack() {
598 return ! self::is_wpcom();
599 }
600
601 /**
602 * Used to determine if there is a valid status slug within the wordpress.com environment.
603 *
604 * @return bool
605 */
606 public static function wpcom_has_status_message() {
607 return isset( $_GET['blogsub'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
608 in_array(
609 $_GET['blogsub'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended
610 array(
611 'confirming',
612 'blocked',
613 'flooded',
614 'spammed',
615 'subscribed',
616 'pending',
617 'confirmed',
618 ),
619 true
620 );
621 }
622
623 /**
624 * Determine the amount of folks currently subscribed to the blog.
625 *
626 * @return int
627 */
628 public static function fetch_subscriber_count() {
629 $subs_count = 0;
630 if ( self::is_jetpack() ) {
631 $cache_key = 'wpcom_subscribers_total';
632 $subs_count = get_transient( $cache_key );
633 if ( false === $subs_count || 'failed' === $subs_count['status'] ) {
634 $xml = new Jetpack_IXR_Client();
635 $xml->query( 'jetpack.fetchSubscriberCount' );
636
637 if ( $xml->isError() ) { // If we get an error from .com, set the status to failed so that we will try again next time the data is requested.
638
639 $subs_count = array(
640 'status' => 'failed',
641 'code' => $xml->getErrorCode(),
642 'message' => $xml->getErrorMessage(),
643 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
644 );
645 } else {
646 $subs_count = array(
647 'status' => 'success',
648 'value' => $xml->getResponse(),
649 );
650 }
651 set_transient( $cache_key, $subs_count, 3600 ); // Try to cache the result for at least 1 hour.
652 }
653 return $subs_count['value'];
654 }
655
656 if ( self::is_wpcom() ) {
657 $subs_count = wpcom_reach_total_for_blog();
658 return $subs_count;
659 }
660 }
661
662 /**
663 * Updates a particular instance of a widget when someone saves it in wp-admin.
664 *
665 * @param array $new_instance New widget instance settings.
666 * @param array $old_instance Old widget instance settings.
667 *
668 * @return array
669 */
670 public function update( $new_instance, $old_instance ) {
671 $instance = $old_instance;
672
673 if ( self::is_jetpack() ) {
674 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
675 $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
676 $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
677 $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
678 }
679
680 if ( self::is_wpcom() ) {
681 $instance['title'] = wp_strip_all_tags( stripslashes( $new_instance['title'] ) );
682 $instance['title_following'] = wp_strip_all_tags( stripslashes( $new_instance['title_following'] ) );
683 $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
684 $instance['subscribe_button'] = wp_strip_all_tags( stripslashes( $new_instance['subscribe_button'] ) );
685 }
686
687 $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
688 $instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
689 $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
690
691 return $instance;
692 }
693
694 /**
695 * The default args for rendering a subscription form.
696 *
697 * @return array
698 */
699 public static function defaults() {
700 $defaults = array(
701 'show_subscribers_total' => true,
702 'show_only_email_and_button' => false,
703 'include_social_followers' => true,
704 );
705
706 $defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
707 $defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
708 $defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
709 $defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
710 $defaults['success_message'] = esc_html__( "Success! An email was just sent to confirm your subscription. Please find the email now and click 'Confirm' to start subscribing.", 'jetpack' );
711
712 return $defaults;
713 }
714
715 /**
716 * Renders the widget's options form in wp-admin.
717 *
718 * @param array $instance Widget instance.
719 * @return string|void
720 */
721 public function form( $instance ) {
722 $instance = wp_parse_args( (array) $instance, static::defaults() );
723 $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
724
725 if ( self::is_wpcom() ) {
726 $title = ! empty( $instance['title'] ) ? esc_attr( stripslashes( $instance['title'] ) ) : '';
727 $title_following = ! empty( $instance['title_following'] ) ? esc_attr( stripslashes( $instance['title_following'] ) ) : '';
728 $subscribe_text = ! empty( $instance['subscribe_text'] ) ? esc_attr( stripslashes( $instance['subscribe_text'] ) ) : '';
729 $subscribe_logged_in = ! empty( $instance['subscribe_logged_in'] ) ? esc_attr( stripslashes( $instance['subscribe_logged_in'] ) ) : '';
730 $subscribe_button = ! empty( $instance['subscribe_button'] ) ? esc_attr( stripslashes( $instance['subscribe_button'] ) ) : '';
731 $subscribers_total = self::fetch_subscriber_count();
732 }
733
734 if ( self::is_jetpack() ) {
735 $title = ! empty( $instance['title'] ) ? stripslashes( $instance['title'] ) : '';
736 $subscribe_text = ! empty( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : '';
737 $subscribe_placeholder = ! empty( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
738 $subscribe_button = ! empty( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : '';
739 $success_message = ! empty( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
740 $subscribers_total = self::fetch_subscriber_count();
741 }
742
743 if ( self::is_wpcom() ) :
744 ?>
745 <p>
746 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
747 <?php esc_html_e( 'Widget title for non-followers:', 'jetpack' ); ?>
748 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
749 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
750 value="<?php echo esc_attr( $title ); ?>"/>
751 </label>
752 </p>
753 <p>
754 <label for="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>">
755 <?php esc_html_e( 'Widget title for followers:', 'jetpack' ); ?>
756 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>"
757 name="<?php echo esc_attr( $this->get_field_name( 'title_following' ) ); ?>" type="text"
758 value="<?php echo esc_attr( $title_following ); ?>"/>
759 </label>
760 </p>
761 <p>
762 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>">
763 <?php esc_html_e( 'Optional text to display to logged in WordPress.com users:', 'jetpack' ); ?>
764 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>"
765 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_logged_in' ) ); ?>"
766 type="text"><?php echo esc_html( $subscribe_logged_in ); ?></textarea>
767 </label>
768 </p>
769 <p>
770 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
771 <?php esc_html_e( 'Optional text to display to non-WordPress.com users:', 'jetpack' ); ?>
772 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
773 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
774 type="text"><?php echo esc_html( $subscribe_text ); ?></textarea>
775 </label>
776 </p>
777 <p>
778 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
779 <?php esc_html_e( 'Follow Button Text:', 'jetpack' ); ?>
780 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
781 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
782 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
783 </label>
784 </p>
785 <p>
786 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
787 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
788 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
789 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
790 <?php
791 /* translators: %s: Number of followers. */
792 echo esc_html( sprintf( _n( 'Show total number of followers? (%s follower)', 'Show total number of followers? (%s followers)', $subscribers_total, 'jetpack' ), number_format_i18n( $subscribers_total ) ) );
793 ?>
794 </label>
795 </p>
796 <?php
797 endif;
798
799 if ( self::is_jetpack() ) :
800 ?>
801 <p>
802 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
803 <?php esc_html_e( 'Widget title:', 'jetpack' ); ?>
804 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
805 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
806 value="<?php echo esc_attr( $title ); ?>"/>
807 </label>
808 </p>
809 <p>
810 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
811 <?php esc_html_e( 'Optional text to display to your readers:', 'jetpack' ); ?>
812 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
813 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
814 rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
815 </label>
816 </p>
817 <p>
818 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>">
819 <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
820 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>"
821 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_placeholder' ) ); ?>" type="text"
822 value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
823 </label>
824 </p>
825 <p>
826 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
827 <?php esc_html_e( 'Subscribe Button:', 'jetpack' ); ?>
828 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
829 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
830 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
831 </label>
832 </p>
833 <p>
834 <label for="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>">
835 <?php esc_html_e( 'Success Message Text:', 'jetpack' ); ?>
836 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>"
837 name="<?php echo esc_attr( $this->get_field_name( 'success_message' ) ); ?>"
838 rows="5"><?php echo esc_html( $success_message ); ?></textarea>
839 </label>
840 </p>
841 <p>
842 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
843 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
844 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
845 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
846 <?php
847 /* translators: %s: Number of subscribers. */
848 echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) );
849 ?>
850 </label>
851 </p>
852 <?php
853 endif;
854 }
855 }
856
857 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
858 class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
859 }
860
861 /**
862 * Classname / shortcode tag to use for the Subscriptions widget.
863 *
864 * @return string
865 */
866 function get_jetpack_blog_subscriptions_widget_classname() {
867 return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
868 'Blog_Subscription_Widget' :
869 'Jetpack_Subscriptions_Widget';
870 }
871
872 /**
873 * Subscriptions widget form HTML output.
874 *
875 * @param array $instance Widget instance data.
876 */
877 function jetpack_do_subscription_form( $instance ) {
878 if ( empty( $instance ) || ! is_array( $instance ) ) {
879 $instance = array();
880 }
881
882 if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
883 $instance['show_subscribers_total'] = false;
884 } else {
885 $instance['show_subscribers_total'] = true;
886 }
887
888 // the default behavior is to include the social followers
889 if ( empty( $instance['include_social_followers'] ) || 'true' === $instance['include_social_followers'] ) {
890 $instance['include_social_followers'] = true;
891 } else {
892 $instance['include_social_followers'] = false;
893 }
894
895 $show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
896 $submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
897
898 // Build up a string with the submit button's classes and styles and set it on the instance.
899 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
900 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
901 $style = '';
902 $submit_button_styles = '';
903 $submit_button_wrapper_styles = '';
904 $email_field_styles = '';
905 $success_message = '';
906
907 if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
908 $submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
909 }
910 if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
911 $submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
912 }
913 if ( isset( $instance['custom_button_width'] ) && 'undefined' !== $instance['custom_button_width'] ) {
914 $submit_button_wrapper_styles .= 'width: ' . $instance['custom_button_width'] . '; ';
915 $submit_button_wrapper_styles .= 'max-width: 100%; ';
916
917 // Account for custom margins on inline forms.
918 if (
919 ! empty( $instance['custom_spacing'] ) &&
920 ! ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] )
921 ) {
922 $submit_button_styles .= 'width: calc(100% - ' . $instance['custom_spacing'] . 'px); ';
923 } else {
924 $submit_button_styles .= 'width: 100%; ';
925 }
926 }
927
928 if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
929 $style = 'font-size: ' . $instance['custom_font_size'];
930 $style .= is_numeric( $instance['custom_font_size'] ) ? 'px; ' : '; '; // Handle deprecated numeric font size values.
931
932 $submit_button_styles .= $style;
933 $email_field_styles .= $style;
934 }
935 if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
936 $style = 'padding: ' .
937 $instance['custom_padding'] . 'px ' .
938 round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px ' .
939 $instance['custom_padding'] . 'px ' .
940 round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px; ';
941
942 $submit_button_styles .= $style;
943 $email_field_styles .= $style;
944 }
945
946 $button_spacing = 0;
947 if ( ! empty( $instance['custom_spacing'] ) ) {
948 $button_spacing = $instance['custom_spacing'];
949 }
950 if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
951 $submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
952 } else {
953 $submit_button_styles .= 'margin: 0; '; // Reset Safari's 2px default margin for buttons affecting input and button union
954 $submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
955 }
956
957 if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
958 $style = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
959 $submit_button_styles .= $style;
960 $email_field_styles .= $style;
961 }
962 if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
963 $style = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
964 $submit_button_styles .= $style;
965 $email_field_styles .= $style;
966 }
967 if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
968 $style =
969 'border-color: ' . $instance['custom_border_color'] . '; ' .
970 'border-style: solid;';
971
972 $submit_button_styles .= $style;
973 $email_field_styles .= $style;
974 }
975 if ( isset( $instance['success_message'] ) && 'undefined' !== $instance['success_message'] ) {
976 $success_message = wp_kses( stripslashes( $instance['success_message'] ), array() );
977 }
978
979 $instance = shortcode_atts(
980 Jetpack_Subscriptions_Widget::defaults(),
981 $instance,
982 'jetpack_subscription_form'
983 );
984
985 // These must come after the call to shortcode_atts().
986 $instance['submit_button_text'] = $submit_button_text;
987 $instance['show_only_email_and_button'] = $show_only_email_and_button;
988 if ( ! empty( $submit_button_classes ) ) {
989 $instance['submit_button_classes'] = $submit_button_classes;
990 }
991 if ( ! empty( $email_field_classes ) ) {
992 $instance['email_field_classes'] = $email_field_classes;
993 }
994
995 if ( ! empty( $submit_button_styles ) ) {
996 $instance['submit_button_styles'] = trim( $submit_button_styles );
997 }
998 if ( ! empty( $submit_button_wrapper_styles ) ) {
999 $instance['submit_button_wrapper_styles'] = trim( $submit_button_wrapper_styles );
1000 }
1001 if ( ! empty( $email_field_styles ) ) {
1002 $instance['email_field_styles'] = trim( $email_field_styles );
1003 }
1004 if ( ! empty( $success_message ) ) {
1005 $instance['success_message'] = trim( $success_message );
1006 }
1007
1008 $args = array(
1009 'before_widget' => '<div class="jetpack_subscription_widget">',
1010 );
1011 ob_start();
1012 the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
1013 $output = ob_get_clean();
1014
1015 return $output;
1016 }
1017
1018 add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
1019 add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
1020
1021 /**
1022 * Register the Subscriptions widget.
1023 */
1024 function jetpack_blog_subscriptions_init() {
1025 register_widget( get_jetpack_blog_subscriptions_widget_classname() );
1026 }
1027
1028 add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
1029
1030 /**
1031 * Sets the default value for `subscription_options` site option.
1032 *
1033 * This default value is available across Simple, Atomic and Jetpack sites,
1034 * including the /sites/$site/settings endpoint.
1035 *
1036 * @param array $default Default `subscription_options` array.
1037 * @param string $option Option name.
1038 * @param bool $passed_default Whether `get_option()` passed a default value.
1039 *
1040 * @return array Default value of `subscription_options`.
1041 */
1042 function subscription_options_fallback( $default, $option, $passed_default ) {
1043 if ( $passed_default ) {
1044 return $default;
1045 }
1046
1047 $site_url = get_home_url();
1048 $display_url = preg_replace( '(^https?://)', '', untrailingslashit( $site_url ) );
1049
1050 return array(
1051 /* translators: Both %1$s and %2$s is site address */
1052 'invitation' => sprintf( __( "Howdy,\nYou recently subscribed to <a href='%1\$s'>%2\$s</a> and we need to verify the email you provided. Once you confirm below, you'll be able to receive and read new posts.\n\nIf you believe this is an error, ignore this message and nothing more will happen.", 'jetpack' ), $site_url, $display_url ),
1053 'comment_follow' => __( "Howdy.\n\nYou recently followed one of my posts. This means you will receive an email when new comments are posted.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' ),
1054 /* translators: %1$s is the site address */
1055 'welcome' => sprintf( __( 'Cool, you are now subscribed to %1$s and will receive an email notification when a new post is published.', 'jetpack' ), $display_url ),
1056 );
1057 }
1058
1059 add_filter( 'default_option_subscription_options', 'subscription_options_fallback', 10, 3 );
1060