PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.6.4
Jetpack – WP Security, Backup, Speed, & Growth v12.6.4
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 / likes / jetpack-likes-settings.php
jetpack-likes-settings.php
810 lines 24.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Sync\Settings;
4
5 /**
6 * Jetpack likes settings class.
7 */
8 class Jetpack_Likes_Settings {
9
10 /**
11 * False if running on WPCOM Simple
12 *
13 * @var bool
14 */
15 public $in_jetpack;
16
17 /**
18 * Constructor function.
19 */
20 public function __construct() {
21 $this->in_jetpack = ! ( defined( 'IS_WPCOM' ) && IS_WPCOM );
22 }
23
24 /**
25 * Replaces the "Sharing" title for the post screen metabox with "Likes and Shares"
26 */
27 public function add_likes_to_sharing_meta_box_title() {
28 return __( 'Likes and Shares', 'jetpack' );
29 }
30
31 /**
32 * Adds a metabox to the post screen if the sharing one doesn't currently exist.
33 */
34 public function add_meta_box() {
35 if (
36 /**
37 * Allow disabling of the Likes metabox on the post editor screen.
38 *
39 * @module likes
40 *
41 * @since 2.2.0
42 *
43 * @param bool false Should the Likes metabox be disabled? Default to false.
44 */
45 apply_filters( 'post_flair_disable', false )
46 ) {
47 return;
48 }
49
50 $post_types = get_post_types( array( 'public' => true ) );
51 /**
52 * Filters the Likes metabox title.
53 *
54 * @module likes
55 *
56 * @since 2.2.0
57 *
58 * @param string Likes metabox title. Default to "Likes".
59 */
60 $title = apply_filters( 'likes_meta_box_title', __( 'Likes', 'jetpack' ) );
61 foreach ( $post_types as $post_type ) {
62 add_meta_box( 'likes_meta', $title, array( $this, 'meta_box_content' ), $post_type, 'side', 'default', array( '__back_compat_meta_box' => true ) );
63 }
64 }
65
66 /**
67 * Shows the likes option in the post screen metabox.
68 *
69 * @param object $post - the post object.
70 */
71 public function meta_box_content( $post ) {
72 $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
73 $checked = true;
74 $disabled = ! $this->is_enabled_sitewide();
75 $switched_status = get_post_meta( $post_id, 'switch_like_status', true );
76
77 if ( $disabled && empty( $switched_status ) || ! $disabled && $switched_status === '0' ) {
78 $checked = false;
79 }
80
81 /**
82 * Fires before the Likes meta box content in the post editor.
83 *
84 * @module likes
85 *
86 * @since 2.2.0
87 *
88 * @param WP_Post|array|null $post Post data.
89 */
90 do_action( 'start_likes_meta_box_content', $post );
91 ?>
92
93 <p>
94 <label for="wpl_enable_post_likes">
95 <input type="checkbox" name="wpl_enable_post_likes" id="wpl_enable_post_likes" value="1" <?php checked( $checked ); ?>>
96 <?php esc_html_e( 'Show likes.', 'jetpack' ); ?>
97 </label>
98 <input type="hidden" name="wpl_like_status_hidden" value="1" />
99 <?php wp_nonce_field( 'likes-and-shares', '_likesharenonce' ); ?>
100 </p>
101 <?php
102 /**
103 * Fires after the Likes meta box content in the post editor.
104 *
105 * @module likes
106 *
107 * @since 2.2.0
108 *
109 * @param WP_Post|array|null $post Post data.
110 */
111 do_action( 'end_likes_meta_box_content', $post );
112 }
113
114 /**
115 * Returns the current state of the "WordPress.com Likes are" option.
116 *
117 * @return boolean true if enabled sitewide, false if not
118 */
119 public function is_enabled_sitewide() {
120 /**
121 * Filters whether Likes are enabled by default on all posts.
122 * true if enabled sitewide, false if not.
123 *
124 * @module likes
125 *
126 * @since 2.2.0
127 *
128 * @param bool $option Are Likes enabled sitewide.
129 */
130 return (bool) apply_filters( 'wpl_is_enabled_sitewide', ! Jetpack_Options::get_option_and_ensure_autoload( 'disabled_likes', 0 ) );
131 }
132
133 /**
134 * Handle meta box saving.
135 *
136 * @param int $post_id - the post ID.
137 */
138 public function meta_box_save( $post_id ) {
139 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
140 return $post_id;
141 }
142
143 if ( empty( $_POST['wpl_like_status_hidden'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- we're not changing anything on the site.
144 return $post_id;
145 }
146
147 if ( ! isset( $_POST['_likesharenonce'] ) || ! wp_verify_nonce( $_POST['_likesharenonce'], 'likes-and-shares' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- WordPress core doesn't unslash or verify nonces either.
148 return $post_id;
149 }
150
151 // Record sharing disable. Only needs to be done for WPCOM.
152 if ( ! $this->in_jetpack ) {
153 if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], get_post_types( array( 'public' => true ) ), true ) ) {
154 if ( ! isset( $_POST['wpl_enable_post_sharing'] ) ) {
155 update_post_meta( $post_id, 'sharing_disabled', 1 );
156 } else {
157 delete_post_meta( $post_id, 'sharing_disabled' );
158 }
159 }
160 }
161
162 if ( 'post' === $_POST['post_type'] ) {
163 if ( ! current_user_can( 'edit_post', $post_id ) ) {
164 return $post_id;
165 }
166 }
167
168 // Record a change in like status for this post - only if it contradicts the
169 // site like setting. If it doesn't contradict, then we delete the new individual status.
170 if ( ! $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) ) {
171 // Likes turned on for individual posts. User wants to add the button to a single post.
172 update_post_meta( $post_id, 'switch_like_status', 1 );
173 } elseif ( $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) {
174 // Likes turned on for all posts. User wants to remove the button from a single post.
175 update_post_meta( $post_id, 'switch_like_status', 0 );
176 } elseif (
177 ( ! $this->is_enabled_sitewide() && empty( $_POST['wpl_enable_post_likes'] ) ) ||
178 ( $this->is_enabled_sitewide() && ! empty( $_POST['wpl_enable_post_likes'] ) )
179 ) {
180 // User wants to update the likes button status for an individual post, but the new status
181 // is the same as if they're asking for the default behavior according to the current Likes setting.
182 // So we delete the meta.
183 delete_post_meta( $post_id, 'switch_like_status' );
184 }
185
186 return $post_id;
187 }
188
189 /**
190 * WordPress.com: Metabox option for sharing (sharedaddy will handle this on the JP blog).
191 *
192 * @param object $post - the post object.
193 */
194 public function sharing_meta_box_content( $post ) {
195 $post_id = ! empty( $post->ID ) ? (int) $post->ID : get_the_ID();
196 $disabled = get_post_meta( $post_id, 'sharing_disabled', true );
197 ?>
198 <p>
199 <label for="wpl_enable_post_sharing">
200 <input type="checkbox" name="wpl_enable_post_sharing" id="wpl_enable_post_sharing" value="1" <?php checked( ! $disabled ); ?>>
201 <?php esc_html_e( 'Show sharing buttons.', 'jetpack' ); ?>
202 </label>
203 <input type="hidden" name="wpl_sharing_status_hidden" value="1" />
204 </p>
205 <?php
206 }
207
208 /**
209 * Adds the 'sharing' menu to the settings menu.
210 * Only ran if sharedaddy and publicize are not already active.
211 */
212 public function sharing_menu() {
213 add_submenu_page( 'options-general.php', esc_html__( 'Sharing Settings', 'jetpack' ), esc_html__( 'Sharing', 'jetpack' ), 'manage_options', 'sharing', array( $this, 'sharing_page' ) );
214 }
215
216 /**
217 * Provides a sharing page with the sharing_global_options hook
218 * so we can display the setting.
219 * Only ran if sharedaddy and publicize are not already active.
220 */
221 public function sharing_page() {
222 $this->updated_message();
223 ?>
224 <div class="wrap">
225 <div class="icon32" id="icon-options-general"><br /></div>
226 <h1><?php esc_html_e( 'Sharing Settings', 'jetpack' ); ?></h1>
227 <?php
228 /** This action is documented in modules/sharedaddy/sharing.php */
229 do_action( 'pre_admin_screen_sharing' );
230 ?>
231 <?php $this->sharing_block(); ?>
232 </div>
233 <?php
234 }
235
236 /**
237 * Returns the settings have been saved message.
238 */
239 public function updated_message() {
240 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- ignoring since we are just displaying that the settings have been saved and not making any other changes to the site.
241 if ( isset( $_GET['update'] ) && 'saved' === $_GET['update'] ) {
242 echo '<div class="updated"><p>' . esc_html__( 'Settings have been saved', 'jetpack' ) . '</p></div>';
243 }
244 }
245
246 /**
247 * Returns just the "sharing buttons" w/ like option block, so it can be inserted into different sharing page contexts
248 */
249 public function sharing_block() {
250 ?>
251 <h2><?php esc_html_e( 'Sharing Buttons', 'jetpack' ); ?></h2>
252 <form method="post" action="">
253 <table class="form-table">
254 <tbody>
255 <?php
256 /** This action is documented in modules/sharedaddy/sharing.php */
257 do_action( 'sharing_global_options' );
258 ?>
259 </tbody>
260 </table>
261
262 <p class="submit">
263 <input type="submit" name="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'jetpack' ); ?>" />
264 <?php wp_nonce_field( 'sharing-options' ); ?>
265 </form>
266 <?php
267 }
268
269 /**
270 * Are likes enabled for this post?
271 *
272 * @param int $post_id - the post ID.
273 * @return bool
274 */
275 public function is_post_likeable( $post_id = 0 ) {
276 $post = get_post( $post_id );
277 if ( ! $post || is_wp_error( $post ) ) {
278 return false;
279 }
280
281 $sitewide_likes_enabled = (bool) $this->is_enabled_sitewide();
282 $post_likes_switched = get_post_meta( $post->ID, 'switch_like_status', true );
283
284 /*
285 * On WPCOM, headstart was inserting bad data for post_likes_switched.
286 * it was wrapping the boolean value in an array. The array is always truthy regardless of its contents.
287 * There was another bug where truthy values were ignored if the global like setting was false.
288 * So in effect, the values for headstart never had an inpact.
289 * Delete the $post_likes_switched flag in this case in order to keep the behaviour as it was.
290 */
291 if ( is_array( $post_likes_switched ) ) {
292 $post_likes_switched = null;
293 }
294
295 /*
296 * on WPCOM, we need to look at post edit date so we don't break old posts
297 * if post edit date predates this code, stick with the former (buggy) behavior
298 * see: p7DVsv-64H-p2
299 */
300 $last_modified_time = strtotime( $post->post_modified_gmt );
301
302 $behavior_was_changed_at = strtotime( '2019-02-22 00:40:42' );
303
304 if ( $this->in_jetpack || $last_modified_time > $behavior_was_changed_at ) {
305 /*
306 * the new and improved behavior on Jetpack and recent WPCOM posts:
307 * $post_likes_switched is empty to follow site setting,
308 * 0 if we want likes disabled, 1 if we want likes enabled.
309 */
310 return $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
311 }
312
313 // implicit else (old behavior): $post_likes_switched simply inverts the global setting.
314 return ( (bool) $post_likes_switched ) xor $sitewide_likes_enabled;
315 }
316
317 /**
318 * Is the like button itself visible (as opposed to the reblog button)
319 *
320 * If called from within The Loop or if called with a $post_id set, then the post will be checked.
321 * Otherwise the sitewide setting will be used.
322 *
323 * @param int $post_id The ID of the post being rendered. Defaults to the current post if called from within The Loop.
324 * @return bool
325 */
326 public function is_likes_button_visible( $post_id = 0 ) {
327 if ( in_the_loop() || $post_id ) {
328 // If in The Loop, is_post_likeable will check the current post.
329 return $this->is_post_likeable( $post_id );
330 } else {
331 // Otherwise, check and see if likes are enabled sitewide.
332 return $this->is_enabled_sitewide();
333 }
334 }
335
336 /**
337 * Are likes visible in this context?
338 *
339 * Some of this code was taken and modified from sharing_display() to ensure
340 * similar logic and filters apply here, too.
341 */
342 public function is_likes_visible() {
343 if ( Settings::is_syncing() ) {
344 return false;
345 }
346
347 return $this->is_likes_button_visible() && $this->is_likes_module_enabled();
348 }
349
350 /**
351 * Apply filters to determine if the likes module itself is enabled
352 *
353 * @return bool
354 */
355 public function is_likes_module_enabled() {
356 global $wp_current_filter; // Used to apply 'sharing_show' filter.
357
358 $post = get_post();
359 $enabled = true;
360
361 // Never show on feeds or previews.
362 if ( is_feed() || is_preview() ) {
363 $enabled = false;
364 // Not a feed or preview, so what is it?
365 } else {
366 if ( post_password_required() ) {
367 $enabled = false;
368 }
369
370 if ( in_array( 'get_the_excerpt', (array) $wp_current_filter, true ) ) {
371 $enabled = false;
372 }
373 // Sharing Setting Overrides ****************************************
374 // Single post including custom post types.
375 if ( is_single() ) {
376 if ( ! $this->is_single_post_enabled( ( $post instanceof WP_Post ) ? $post->post_type : 'post' ) ) {
377 $enabled = false;
378 }
379
380 // Single page.
381 } elseif ( is_page() && ! is_front_page() ) {
382 if ( ! $this->is_single_page_enabled() ) {
383 $enabled = false;
384 }
385
386 // Attachment.
387 } elseif ( is_attachment() ) {
388 if ( ! $this->is_attachment_enabled() ) {
389 $enabled = false;
390 }
391
392 // All other loops.
393 } elseif ( ! $this->is_index_enabled() ) {
394 $enabled = false;
395 }
396 }
397
398 if ( $post instanceof WP_Post ) {
399 // Check that the post is a public, published post.
400 if ( 'attachment' === $post->post_type ) {
401 $post_status = get_post_status( $post->post_parent );
402 } else {
403 $post_status = $post->post_status;
404 }
405 if ( 'publish' !== $post_status ) {
406 $enabled = false;
407 }
408 }
409
410 // Run through the sharing filters.
411 /** This filter is documented in modules/sharedaddy/sharing-service.php */
412 $enabled = apply_filters( 'sharing_show', $enabled, $post );
413
414 /**
415 * Filters whether the Likes should be visible or not.
416 * Allows overwriting the options set in Settings > Sharing.
417 *
418 * @module likes
419 *
420 * @since 2.2.0
421 *
422 * @param bool $enabled Should the Likes be visible?
423 */
424 return (bool) apply_filters( 'wpl_is_likes_visible', $enabled );
425 }
426
427 /**
428 * Are Post Likes enabled on single posts?
429 *
430 * @param string $post_type custom post type identifier.
431 * @return bool
432 */
433 public function is_single_post_enabled( $post_type = 'post' ) {
434 $options = $this->get_options();
435 return (bool) apply_filters(
436 /**
437 * Filters whether Likes should be enabled on single posts.
438 *
439 * The dynamic part of the filter, {$post_type}, allows you to specific the post type where Likes should be enabled.
440 *
441 * @module likes
442 *
443 * @since 2.2.0
444 *
445 * @param bool $enabled Are Post Likes enabled on single posts?
446 */
447 "wpl_is_single_{$post_type}_disabled",
448 (bool) in_array( $post_type, $options['show'], true )
449 );
450 }
451
452 /**
453 * Get the 'disabled_likes' option from the DB of the current blog.
454 *
455 * @return array
456 */
457 public function get_options() {
458 $setting = array();
459 $setting['disabled'] = get_option( 'disabled_likes' );
460 $sharing = get_option( 'sharing-options', array() );
461
462 // Default visibility settings
463 if ( ! isset( $sharing['global']['show'] ) ) {
464 $sharing['global']['show'] = array( 'post', 'page' );
465
466 // Scalar check
467 } elseif ( is_scalar( $sharing['global']['show'] ) ) {
468 switch ( $sharing['global']['show'] ) {
469 case 'posts':
470 $sharing['global']['show'] = array( 'post', 'page' );
471 break;
472 case 'index':
473 $sharing['global']['show'] = array( 'index' );
474 break;
475 case 'posts-index':
476 $sharing['global']['show'] = array( 'post', 'page', 'index' );
477 break;
478 }
479 }
480
481 // Ensure it's always an array (even if not previously empty or scalar)
482 $setting['show'] = ! empty( $sharing['global']['show'] ) ? (array) $sharing['global']['show'] : array();
483
484 /**
485 * Filters where the Likes are displayed.
486 *
487 * @module likes
488 *
489 * @since 2.2.0
490 *
491 * @param array $setting Array of Likes display settings.
492 */
493 return apply_filters( 'wpl_get_options', $setting );
494 }
495
496 /**
497 * Are Post Likes enabled on archive/front/search pages?
498 *
499 * @return bool
500 */
501 public function is_index_enabled() {
502 $options = $this->get_options();
503 /**
504 * Filters whether Likes should be enabled on archive/front/search pages.
505 *
506 * @module likes
507 *
508 * @since 2.2.0
509 *
510 * @param bool $enabled Are Post Likes enabled on archive/front/search pages?
511 */
512 return (bool) apply_filters( 'wpl_is_index_disabled', (bool) in_array( 'index', $options['show'], true ) );
513 }
514
515 /**
516 * Are Post Likes enabled on single pages?
517 *
518 * @return bool
519 */
520 public function is_single_page_enabled() {
521 $options = $this->get_options();
522 /**
523 * Filters whether Likes should be enabled on single pages.
524 *
525 * @module likes
526 *
527 * @since 2.2.0
528 *
529 * @param bool $enabled Are Post Likes enabled on single pages?
530 */
531 return (bool) apply_filters( 'wpl_is_single_page_disabled', (bool) in_array( 'page', $options['show'], true ) );
532 }
533
534 /**
535 * Are Media Likes enabled on single pages?
536 *
537 * @return bool
538 */
539 public function is_attachment_enabled() {
540 $options = $this->get_options();
541 /**
542 * Filters whether Likes should be enabled on attachment pages.
543 *
544 * @module likes
545 *
546 * @since 2.2.0
547 *
548 * @param bool $enabled Are Post Likes enabled on attachment pages?
549 */
550 return (bool) apply_filters( 'wpl_is_attachment_disabled', (bool) in_array( 'attachment', $options['show'], true ) );
551 }
552
553 /**
554 * The actual options block to be inserted into the sharing page.
555 */
556 public function admin_settings_init() {
557 ?>
558 <tr>
559 <th scope="row">
560 <label><?php esc_html_e( 'WordPress.com Likes are', 'jetpack' ); ?></label>
561 </th>
562 <td>
563 <div>
564 <label>
565 <input type="radio" class="code" name="wpl_default" value="on" <?php checked( $this->is_enabled_sitewide(), true ); ?> />
566 <?php esc_html_e( 'On for all posts', 'jetpack' ); ?>
567 </label>
568 </div>
569 <div>
570 <label>
571 <input type="radio" class="code" name="wpl_default" value="off" <?php checked( $this->is_enabled_sitewide(), false ); ?> />
572 <?php esc_html_e( 'Turned on per post', 'jetpack' ); ?>
573 </label>
574 <div>
575 </td>
576 </tr>
577 <?php if ( ! $this->in_jetpack ) : ?>
578 <tr>
579 <th scope="row">
580 <label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack' ); ?></label>
581 </th>
582 <td>
583 <div>
584 <label>
585 <input type="radio" class="code" name="jetpack_reblogs_enabled" value="on" <?php checked( $this->reblogs_enabled_sitewide(), true ); ?> />
586 <?php esc_html_e( 'Show the Reblog button on posts', 'jetpack' ); ?>
587 </label>
588 </div>
589 <div>
590 <label>
591 <input type="radio" class="code" name="jetpack_reblogs_enabled" value="off" <?php checked( $this->reblogs_enabled_sitewide(), false ); ?> />
592 <?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack' ); ?>
593 </label>
594 </div>
595 </td>
596 </tr>
597 <!-- WPCOM only: Comment Likes -->
598 <?php if ( ! $this->in_jetpack ) : ?>
599 <tr>
600 <th scope="row">
601 <label><?php esc_html_e( 'Comment Likes are', 'jetpack' ); ?></label>
602 </th>
603 <td>
604 <div>
605 <label>
606 <input type="checkbox" class="code" name="jetpack_comment_likes_enabled" value="1" <?php checked( $this->is_comments_enabled(), true ); ?> />
607 <?php esc_html_e( 'On for all comments', 'jetpack' ); ?>
608 </label>
609 </div>
610 </td>
611 </tr>
612 <?php endif; ?>
613 <?php endif; ?>
614 </tbody> <?php // closes the tbody attached to sharing_show_buttons_on_row_start... ?>
615 <?php
616 }
617
618 /**
619 * Returns the current state of the "WordPress.com Reblogs are" option.
620 *
621 * @return bool true if enabled sitewide, false if not
622 */
623 public function reblogs_enabled_sitewide() {
624 /**
625 * Filters whether Reblogs are enabled by default on all posts.
626 * true if enabled sitewide, false if not.
627 *
628 * @module likes
629 *
630 * @since 3.0.0
631 *
632 * @param bool $option Are Reblogs enabled sitewide.
633 */
634 return (bool) apply_filters( 'wpl_reblogging_enabled_sitewide', ! get_option( 'disabled_reblogs' ) );
635 }
636
637 /**
638 * Used for WPCOM ONLY. Comment likes are in their own module in Jetpack.
639 * Returns if comment likes are enabled. Defaults to 'off'
640 *
641 * @return boolean true if we should show comment likes, false if not
642 */
643 public function is_comments_enabled() {
644 /**
645 * Filters whether Comment Likes are enabled.
646 * true if enabled, false if not.
647 *
648 * @module comment-likes
649 *
650 * @since 2.2.0
651 *
652 * @param bool $option Are Comment Likes enabled sitewide.
653 */
654 return (bool) apply_filters( 'jetpack_comment_likes_enabled', get_option( 'jetpack_comment_likes_enabled', false ) );
655 }
656
657 /**
658 * Saves the setting in the database.
659 */
660 public function admin_settings_callback() {
661 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- WordPress core doesn't unslash or verify nonces either.
662 return;
663 }
664
665 // We're looking for these, and doing a dance to set some stats and save
666 // them together in array option.
667 if ( ! empty( $_POST['wpl_default'] ) ) {
668 $new_state = sanitize_text_field( wp_unslash( $_POST['wpl_default'] ) );
669 } else {
670 $new_state = 'on';
671 }
672
673 if ( ! empty( $_POST['jetpack_reblogs_enabled'] ) ) {
674 $reblogs_new_state = sanitize_text_field( wp_unslash( $_POST['jetpack_reblogs_enabled'] ) );
675 } else {
676 $reblogs_new_state = 'on';
677 }
678
679 // Checked (enabled)
680 switch ( $new_state ) {
681 case 'off':
682 update_option( 'disabled_likes', 1 );
683 break;
684 case 'on':
685 default:
686 delete_option( 'disabled_likes' );
687 break;
688 }
689
690 switch ( $reblogs_new_state ) {
691 case 'off':
692 update_option( 'disabled_reblogs', 1 );
693 break;
694 case 'on':
695 default:
696 delete_option( 'disabled_reblogs' );
697 break;
698 }
699
700 // WPCOM only: Comment Likes
701 if ( ! $this->in_jetpack ) {
702 if ( ! empty( $_POST['jetpack_comment_likes_enabled'] ) ) {
703 $new_comments_state = sanitize_text_field( wp_unslash( $_POST['jetpack_comment_likes_enabled'] ) );
704 } else {
705 $new_comments_state = false;
706 }
707 switch ( (bool) $new_comments_state ) {
708 case true:
709 update_option( 'jetpack_comment_likes_enabled', 1 );
710 break;
711 case false:
712 default:
713 update_option( 'jetpack_comment_likes_enabled', 0 );
714 break;
715 }
716 }
717 }
718
719 /**
720 * Adds the admin update hook so we can save settings even if Sharedaddy is not enabled.
721 */
722 public function process_update_requests_if_sharedaddy_not_loaded() {
723 if ( isset( $_GET['page'] ) && ( $_GET['page'] === 'sharing.php' || $_GET['page'] === 'sharing' ) ) {
724 if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'sharing-options' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- WordPress core doesn't unslash or verify nonces either.
725 /** This action is documented in modules/sharedaddy/sharing.php */
726 do_action( 'sharing_admin_update' );
727 wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
728 die();
729 }
730 }
731 }
732
733 /**
734 * If sharedaddy is not loaded, we don't have the "Show buttons on" yet, so we need to add that since it affects likes too.
735 */
736 public function admin_settings_showbuttonon_init() {
737 /** This action is documented in modules/sharedaddy/sharing.php */
738 echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
739 ?>
740 <th scope="row"><label><?php esc_html_e( 'Show buttons on', 'jetpack' ); ?></label></th>
741 <td>
742 <?php
743 $br = false;
744 $shows = array_values( get_post_types( array( 'public' => true ) ) );
745 array_unshift( $shows, 'index' );
746 $global = $this->get_options();
747 foreach ( $shows as $show ) :
748 if ( 'index' === $show ) {
749 $label = __( 'Front Page, Archive Pages, and Search Results', 'jetpack' );
750 } else {
751 $post_type_object = get_post_type_object( $show );
752 $label = $post_type_object->labels->name;
753 }
754
755 if ( $br ) {
756 echo '<br />';
757 }
758 ?>
759 <label><input type="checkbox"<?php checked( in_array( $show, $global['show'], true ) ); ?> name="show[]" value="<?php echo esc_attr( $show ); ?>" /> <?php echo esc_html( $label ); ?></label>
760 <?php
761 $br = true;
762 endforeach;
763 ?>
764 </td>
765 <?php
766 /** This action is documented in modules/sharedaddy/sharing.php */
767 echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
768 }
769
770 /**
771 * If sharedaddy is not loaded, we still need to save the the settings of the "Show buttons on" option.
772 */
773 public function admin_settings_showbuttonon_callback() {
774 $options = get_option( 'sharing-options' );
775 if ( ! is_array( $options ) ) {
776 $options = array();
777 }
778
779 $shows = array_values( get_post_types( array( 'public' => true ) ) );
780 $shows[] = 'index';
781 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- triggered due to the 'sharing_admin_update' action, but the code in sharing.php checks for the nonce before firing the action.
782 $data = $_POST;
783
784 if ( isset( $data['show'] ) ) {
785 if ( is_scalar( $data['show'] ) ) {
786 switch ( $data['show'] ) {
787 case 'posts':
788 $data['show'] = array( 'post', 'page' );
789 break;
790 case 'index':
791 $data['show'] = array( 'index' );
792 break;
793 case 'posts-index':
794 $data['show'] = array( 'post', 'page', 'index' );
795 break;
796 }
797 }
798
799 $data['show'] = array_intersect( $data['show'], $shows );
800 if ( $data['show'] ) {
801 $options['global']['show'] = $data['show'];
802 }
803 } else {
804 $options['global']['show'] = array();
805 }
806
807 update_option( 'sharing-options', $options );
808 }
809 }
810