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