Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Subscriptions_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Subscriptions_Widget, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Jetpack_Subscriptions_Widget extends WP_Widget { |
||
4 | static $instance_count = 0; |
||
5 | /** |
||
6 | * @var array When printing the submit button, what tags are allowed |
||
7 | */ |
||
8 | static $allowed_html_tags_for_submit_button = array( 'br' => array() ); |
||
9 | |||
10 | function __construct() { |
||
11 | $widget_ops = array( |
||
12 | 'classname' => 'widget_blog_subscription jetpack_subscription_widget', |
||
13 | 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ), |
||
14 | 'customize_selective_refresh' => true, |
||
15 | ); |
||
16 | |||
17 | $name = self::is_jetpack() ? |
||
18 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
19 | apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) : |
||
20 | __( 'Follow Blog', 'jetpack' ); |
||
21 | |||
22 | parent::__construct( |
||
23 | 'blog_subscription', |
||
24 | $name, |
||
25 | $widget_ops |
||
26 | ); |
||
27 | |||
28 | View Code Duplication | if ( self::is_jetpack() && |
|
29 | ( |
||
30 | is_active_widget( false, false, $this->id_base ) || |
||
31 | is_active_widget( false, false, 'monster' ) || |
||
32 | is_customize_preview() |
||
33 | ) |
||
34 | ) { |
||
35 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Enqueue the form's CSS. |
||
41 | * |
||
42 | * @since 4.5.0 |
||
43 | */ |
||
44 | function enqueue_style() { |
||
45 | wp_register_style( |
||
46 | 'jetpack-subscriptions', |
||
47 | plugins_url( 'subscriptions.css', __FILE__ ), |
||
48 | array(), |
||
49 | JETPACK__VERSION |
||
50 | ); |
||
51 | wp_enqueue_style( 'jetpack-subscriptions' ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Renders a full widget either within the context of WordPress widget, or in response to a shortcode. |
||
56 | * |
||
57 | * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'. |
||
58 | * @param array $instance The settings for the particular instance of the widget. |
||
59 | */ |
||
60 | function widget( $args, $instance ) { |
||
61 | if ( self::is_jetpack() && |
||
62 | /** This filter is documented in modules/contact-form/grunion-contact-form.php */ |
||
63 | false === apply_filters( 'jetpack_auto_fill_logged_in_user', false ) |
||
64 | ) { |
||
65 | $subscribe_email = ''; |
||
66 | } else { |
||
67 | $current_user = wp_get_current_user(); |
||
68 | if ( ! empty( $current_user->user_email ) ) { |
||
69 | $subscribe_email = esc_attr( $current_user->user_email ); |
||
70 | } else { |
||
71 | $subscribe_email = ''; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | $stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog'; |
||
76 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
77 | do_action( 'jetpack_stats_extra', 'widget_view', $stats_action ); |
||
78 | |||
79 | $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : ''; |
||
80 | $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : ''; |
||
81 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
82 | |||
83 | echo $before_widget; |
||
84 | |||
85 | Jetpack_Subscriptions_Widget::$instance_count ++; |
||
86 | |||
87 | self::render_widget_title( $args, $instance ); |
||
88 | |||
89 | self::render_widget_status_messages( $instance ); |
||
90 | |||
91 | if ( self::is_current_user_subscribed() ) { |
||
92 | self::render_widget_already_subscribed( $instance ); |
||
93 | } else { |
||
94 | self::render_widget_subscription_form( $args, $instance, $subscribe_email ); |
||
95 | } |
||
96 | |||
97 | echo "\n" . $after_widget; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Prints the widget's title. If show_only_email_and_button is true, we will not show a title. |
||
102 | * |
||
103 | * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'. |
||
104 | * @param array $instance The settings for the particular instance of the widget. |
||
105 | */ |
||
106 | static function render_widget_title( $args, $instance ) { |
||
107 | $show_only_email_and_button = $instance['show_only_email_and_button']; |
||
108 | $before_title = isset( $args['before_title'] ) ? $args['before_title'] : ''; |
||
109 | $after_title = isset( $args['after_title'] ) ? $args['after_title'] : ''; |
||
110 | if ( self::is_wpcom() && ! $show_only_email_and_button ) { |
||
111 | if ( self::is_current_user_subscribed() ) { |
||
112 | View Code Duplication | if ( ! empty( $instance['title_following'] ) ) { |
|
113 | echo $before_title . '<label for="subscribe-field' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ) . '">' . esc_attr( $instance['title_following'] ) . '</label>' . $after_title . "\n"; |
||
114 | } |
||
115 | View Code Duplication | } else { |
|
116 | if ( ! empty( $instance['title'] ) ) { |
||
117 | echo $before_title . '<label for="subscribe-field' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ) . '">' . esc_attr( $instance['title'] ) . '</label>' . $after_title . "\n"; |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
122 | if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) { |
||
123 | echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n"; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Prints the subscription block's status messages after someone has attempted to subscribe. |
||
129 | * Either a success message or an error message. |
||
130 | * |
||
131 | * @param array $instance The settings for the particular instance of the widget. |
||
132 | */ |
||
133 | static function render_widget_status_messages( $instance ) { |
||
134 | if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) { |
||
135 | $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : ''; |
||
136 | $subscribers_total = self::fetch_subscriber_count(); |
||
137 | switch ( $_GET['subscribe'] ) : |
||
138 | case 'invalid_email' : ?> |
||
139 | <p class="error"><?php esc_html_e( 'The email you entered was invalid. Please check and try again.', 'jetpack' ); ?></p> |
||
140 | <?php break; |
||
141 | case 'opted_out' : ?> |
||
142 | <p class="error"><?php printf( __( 'The email address has opted out of subscription emails. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a>', 'jetpack' ), |
||
143 | 'https://subscribe.wordpress.com/', |
||
144 | __( 'Manage your email preferences.', 'jetpack' ) |
||
145 | ); ?></p> |
||
146 | <?php break; |
||
147 | case 'already' : ?> |
||
148 | <p class="error"><?php printf( __( 'You have already subscribed to this site. Please check your inbox. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a>', 'jetpack' ), |
||
149 | 'https://subscribe.wordpress.com/', |
||
150 | __( 'Manage your email preferences.', 'jetpack' ) |
||
151 | ); ?></p> |
||
152 | <?php break; |
||
153 | case 'many_pending_subs' : ?> |
||
154 | <p class="error"><?php printf( __( 'You have a few pending subscriptions. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a> before continuing.', 'jetpack' ), |
||
155 | 'https://subscribe.wordpress.com/', |
||
156 | __( 'Manage your email preferences.', 'jetpack' ) |
||
157 | ); ?></p> |
||
158 | <?php break; |
||
159 | case 'success' : ?> |
||
160 | <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div> |
||
161 | <?php break; |
||
162 | default : ?> |
||
163 | <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p> |
||
164 | <?php break; |
||
165 | endswitch; |
||
166 | } |
||
167 | |||
168 | if ( self::is_wpcom() && self::wpcom_has_status_message() ) { |
||
169 | global $themecolors; |
||
170 | switch ( $_GET['blogsub'] ) { |
||
171 | View Code Duplication | case 'confirming': |
|
172 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
173 | _e( 'Thanks for subscribing! You’ll get an email with a link to confirm your subscription. If you don’t get it, please <a href="https://en.support.wordpress.com/contact/">contact us</a>.' ); |
||
174 | echo "</div>"; |
||
175 | break; |
||
176 | View Code Duplication | case 'blocked': |
|
177 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
178 | _e( 'Subscriptions have been blocked for this email address.' ); |
||
179 | echo "</div>"; |
||
180 | break; |
||
181 | View Code Duplication | case 'flooded': |
|
182 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
183 | _e( 'You already have several pending email subscriptions. Approve or delete a few through your <a href="https://subscribe.wordpress.com/">Subscription Manager</a> before attempting to subscribe to more blogs.' ); |
||
184 | echo "</div>"; |
||
185 | break; |
||
186 | case 'spammed': |
||
187 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
188 | echo wp_kses_post( sprintf( __( 'Because there are many pending subscriptions for this email address, we have blocked the subscription. Please <a href="%s">activate or delete</a> pending subscriptions before attempting to subscribe.' ), 'https://subscribe.wordpress.com/' ) ); |
||
189 | echo "</div>"; |
||
190 | break; |
||
191 | View Code Duplication | case 'subscribed': |
|
192 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
193 | _e( 'You’re already subscribed to this site.' ); |
||
194 | echo "</div>"; |
||
195 | break; |
||
196 | View Code Duplication | case 'pending': |
|
197 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
198 | _e( 'You have a pending subscription already; we just sent you another email. Click the link or <a href="https://en.support.wordpress.com/contact/">contact us</a> if you don’t receive it.' ); |
||
199 | echo "</div>"; |
||
200 | break; |
||
201 | View Code Duplication | case 'confirmed': |
|
202 | echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>"; |
||
203 | _e( 'Congrats, you’re subscribed! You’ll get an email with the details of your subscription and an unsubscribe link.' ); |
||
204 | echo "</div>"; |
||
205 | break; |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Renders a message to folks who are already subscribed. |
||
212 | * |
||
213 | * @param array $instance The settings for the particular instance of the widget. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | static function render_widget_already_subscribed( $instance ) { |
||
218 | if ( self::is_wpcom() ) { |
||
219 | $subscribers_total = self::fetch_subscriber_count(); |
||
220 | $edit_subs_url = 'https://wordpress.com/following/edit/'; |
||
221 | if ( function_exists( 'localized_wpcom_url' ) ) { |
||
222 | $edit_subs_url = localized_wpcom_url( http() . '://wordpress.com/following/edit/', get_user_locale() ); |
||
223 | } |
||
224 | $show_subscribers_total = (bool) $instance['show_subscribers_total']; |
||
225 | if ( $show_subscribers_total && $subscribers_total > 1 ) : |
||
226 | $subscribers_not_me = $subscribers_total - 1; |
||
227 | /* translators: %s: number of folks following the blog */ |
||
228 | ?> |
||
229 | <p><?php printf( _n( 'You are following this blog, along with %s other amazing person (<a href="%s">manage</a>).', 'You are following this blog, along with %s other amazing people (<a href="%s">manage</a>).', $subscribers_not_me ), number_format_i18n( $subscribers_not_me ), $edit_subs_url ) ?></p><?php |
||
230 | else : |
||
231 | ?> |
||
232 | <p><?php printf( __( 'You are following this blog (<a href="%s">manage</a>).' ), $edit_subs_url ) ?></p><?php |
||
233 | endif; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Renders a form allowing folks to subscribe to the blog. |
||
239 | * |
||
240 | * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'. |
||
241 | * @param array $instance The settings for the particular instance of the widget. |
||
242 | * @param string $subscribe_email The email to use to prefill the form. |
||
243 | */ |
||
244 | static function render_widget_subscription_form( $args, $instance, $subscribe_email ) { |
||
245 | $show_only_email_and_button = $instance['show_only_email_and_button']; |
||
246 | $subscribe_logged_in = isset( $instance['subscribe_logged_in'] ) ? stripslashes( $instance['subscribe_logged_in'] ) : ''; |
||
247 | $show_subscribers_total = (bool) $instance['show_subscribers_total']; |
||
248 | $subscribe_text = empty( $instance['show_only_email_and_button'] ) ? |
||
249 | stripslashes( $instance['subscribe_text'] ) : |
||
250 | false; |
||
251 | $referer = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
252 | $source = 'widget'; |
||
253 | $widget_id = esc_attr( ! empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) ); |
||
254 | $subscribe_button = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button']; |
||
255 | $subscribers_total = self::fetch_subscriber_count(); |
||
256 | $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : ''; |
||
257 | $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : ''; |
||
258 | $submit_button_styles = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : ''; |
||
259 | |||
260 | if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) { |
||
261 | global $current_blog; |
||
262 | $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : ''; |
||
263 | ?> |
||
264 | <form action="<?php echo $url; ?>" method="post" accept-charset="utf-8" |
||
265 | id="subscribe-blog<?php if ( Jetpack_Subscriptions_Widget::$instance_count > 1 ) { |
||
266 | echo '-' . Jetpack_Subscriptions_Widget::$instance_count; |
||
267 | } ?>"> |
||
268 | <?php if ( is_user_logged_in() ) : ?> |
||
269 | <?php |
||
270 | if ( ! $show_only_email_and_button ) { |
||
271 | echo wpautop( $subscribe_logged_in ); |
||
272 | } |
||
273 | View Code Duplication | if ( $show_subscribers_total && $subscribers_total ) { |
|
274 | /* translators: %s: number of folks following the blog */ |
||
275 | echo wpautop( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total ), number_format_i18n( $subscribers_total ) ) ); |
||
276 | } |
||
277 | ?> |
||
278 | <?php else : ?> |
||
279 | <?php |
||
280 | if ( ! $show_only_email_and_button ) { |
||
281 | echo wpautop( $subscribe_text ); |
||
282 | } |
||
283 | View Code Duplication | if ( $show_subscribers_total && $subscribers_total ) { |
|
284 | /* translators: %s: number of folks following the blog */ |
||
285 | echo wpautop( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total ), number_format_i18n( $subscribers_total ) ) ); |
||
286 | } |
||
287 | ?> |
||
288 | <p><input type="text" name="email" style="width: 95%; padding: 1px 2px" |
||
289 | placeholder="<?php esc_attr_e( 'Enter your email address' ); ?>" value="" |
||
290 | id="subscribe-field<?php if ( Jetpack_Subscriptions_Widget::$instance_count > 1 ) { |
||
291 | echo '-' . Jetpack_Subscriptions_Widget::$instance_count; |
||
292 | } ?>"/></p> |
||
293 | <?php endif; ?> |
||
294 | |||
295 | <p> |
||
296 | <input type="hidden" name="action" value="subscribe"/> |
||
297 | <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/> |
||
298 | <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/> |
||
299 | <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/> |
||
300 | <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $widget_id ); ?>"/> |
||
301 | <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?> |
||
302 | <button type="submit" |
||
303 | <?php if ( ! empty( $submit_button_classes ) ) { ?> |
||
304 | class="<?php echo esc_attr( $submit_button_classes ); ?>" |
||
305 | <?php }; ?> |
||
306 | <?php if ( ! empty( $submit_button_styles ) ) { ?> |
||
307 | style="<?php echo esc_attr( $submit_button_styles ); ?>" |
||
308 | <?php }; ?> |
||
309 | > |
||
310 | <?php |
||
311 | echo wp_kses( |
||
312 | $subscribe_button, |
||
313 | self::$allowed_html_tags_for_submit_button |
||
314 | ); |
||
315 | ?> |
||
316 | </button> |
||
317 | </p> |
||
318 | </form> |
||
319 | <?php |
||
320 | } |
||
321 | |||
322 | if ( self::is_jetpack() ) { |
||
323 | /** |
||
324 | * Filter the subscription form's ID prefix. |
||
325 | * |
||
326 | * @module subscriptions |
||
327 | * |
||
328 | * @since 2.7.0 |
||
329 | * |
||
330 | * @param string subscribe-field Subscription form field prefix. |
||
331 | * @param int $widget_id Widget ID. |
||
332 | */ |
||
333 | $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id ); |
||
334 | ?> |
||
335 | <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>"> |
||
336 | <?php |
||
337 | if ( $subscribe_text && ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) ) { |
||
|
|||
338 | ?> |
||
339 | <div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php |
||
340 | } |
||
341 | |||
342 | if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) { |
||
343 | /* translators: %s: number of folks following the blog */ |
||
344 | echo wpautop( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) ); |
||
345 | } |
||
346 | if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?> |
||
347 | <p id="subscribe-email"> |
||
348 | <label id="jetpack-subscribe-label" |
||
349 | class="screen-reader-text" |
||
350 | for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>"> |
||
351 | <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?> |
||
352 | </label> |
||
353 | <input type="email" name="email" required="required" class="required" |
||
354 | value="<?php echo esc_attr( $subscribe_email ); ?>" |
||
355 | id="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>" |
||
356 | placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"/> |
||
357 | </p> |
||
358 | |||
359 | <p id="subscribe-submit"> |
||
360 | <input type="hidden" name="action" value="subscribe"/> |
||
361 | <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/> |
||
362 | <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/> |
||
363 | <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>"/> |
||
364 | <?php |
||
365 | if ( is_user_logged_in() ) { |
||
366 | wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false ); |
||
367 | } |
||
368 | ?> |
||
369 | <button type="submit" |
||
370 | <?php if ( ! empty( $submit_button_classes ) ) { ?> |
||
371 | class="<?php echo esc_attr( $submit_button_classes ); ?>" |
||
372 | <?php }; ?> |
||
373 | <?php if ( ! empty( $submit_button_styles ) ) { ?> |
||
374 | style="<?php echo esc_attr( $submit_button_styles ); ?>" |
||
375 | <?php }; ?> |
||
376 | name="jetpack_subscriptions_widget" |
||
377 | > |
||
378 | <?php |
||
379 | echo wp_kses( |
||
380 | $subscribe_button, |
||
381 | self::$allowed_html_tags_for_submit_button |
||
382 | ); ?> |
||
383 | </button> |
||
384 | </p> |
||
385 | <?php } ?> |
||
386 | </form> |
||
387 | <?php } |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Determines if the current user is subscribed to the blog. |
||
392 | * |
||
393 | * @return bool Is the person already subscribed. |
||
394 | */ |
||
395 | static function is_current_user_subscribed() { |
||
396 | $subscribed = isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe']; |
||
397 | |||
398 | if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) { |
||
399 | $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() ); |
||
400 | } |
||
401 | |||
402 | return $subscribed; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Is this script running in the wordpress.com environment? |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | static function is_wpcom() { |
||
411 | return defined( 'IS_WPCOM' ) && IS_WPCOM; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Is this script running in a self-hosted environment? |
||
416 | * |
||
417 | * @return bool |
||
418 | */ |
||
419 | static function is_jetpack() { |
||
420 | return ! self::is_wpcom(); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Used to determine if there is a valid status slug within the wordpress.com environment. |
||
425 | * |
||
426 | * @return bool |
||
427 | */ |
||
428 | static function wpcom_has_status_message() { |
||
429 | return isset( $_GET['blogsub'] ) && |
||
430 | in_array( |
||
431 | $_GET['blogsub'], |
||
432 | array( |
||
433 | 'confirming', |
||
434 | 'blocked', |
||
435 | 'flooded', |
||
436 | 'spammed', |
||
437 | 'subscribed', |
||
438 | 'pending', |
||
439 | 'confirmed', |
||
440 | ) |
||
441 | ); |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Determine the amount of folks currently subscribed to the blog. |
||
446 | * |
||
447 | * @return int|array |
||
448 | */ |
||
449 | static function fetch_subscriber_count() { |
||
450 | $subs_count = 0; |
||
451 | |||
452 | if ( self::is_jetpack() ) { |
||
453 | $subs_count = get_transient( 'wpcom_subscribers_total' ); |
||
454 | if ( false === $subs_count || 'failed' == $subs_count['status'] ) { |
||
455 | $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) ); |
||
456 | |||
457 | $xml->query( 'jetpack.fetchSubscriberCount' ); |
||
458 | |||
459 | 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 |
||
460 | $subs_count = array( |
||
461 | 'status' => 'failed', |
||
462 | 'code' => $xml->getErrorCode(), |
||
463 | 'message' => $xml->getErrorMessage(), |
||
464 | 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0, |
||
465 | ); |
||
466 | } else { |
||
467 | $subs_count = array( |
||
468 | 'status' => 'success', |
||
469 | 'value' => $xml->getResponse(), |
||
470 | ); |
||
471 | } |
||
472 | |||
473 | set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour |
||
474 | } |
||
475 | } |
||
476 | |||
477 | if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) { |
||
478 | $subs_count = wpcom_reach_total_for_blog(); |
||
479 | } |
||
480 | |||
481 | return $subs_count; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Updates a particular instance of a widget when someone saves it in wp-admin. |
||
486 | * |
||
487 | * @param array $new_instance |
||
488 | * @param array $old_instance |
||
489 | * |
||
490 | * @return array |
||
491 | */ |
||
492 | function update( $new_instance, $old_instance ) { |
||
515 | |||
516 | /** |
||
517 | * The default args for rendering a subscription form. |
||
518 | * |
||
519 | * @return array |
||
520 | */ |
||
521 | static function defaults() { |
||
545 | |||
546 | /** |
||
547 | * Renders the widget's options form in wp-admin. |
||
548 | * |
||
549 | * @param array $instance |
||
550 | */ |
||
551 | function form( $instance ) { |
||
680 | } |
||
681 | |||
682 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'class_alias' ) ) { |
||
683 | class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' ); |
||
684 | } |
||
685 | |||
686 | function get_jetpack_blog_subscriptions_widget_classname() { |
||
687 | return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? |
||
688 | 'Blog_Subscription_Widget' : |
||
689 | 'Jetpack_Subscriptions_Widget'; |
||
690 | } |
||
691 | |||
692 | function jetpack_do_subscription_form( $instance ) { |
||
760 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: