Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.85% covered (warning)
80.85%
38 / 47
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Comment_Likes_Section
80.85% covered (warning)
80.85%
38 / 47
66.67% covered (warning)
66.67%
2 / 3
13.01
0.00% covered (danger)
0.00%
0 / 1
 render
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 update
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 render_fields
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * The Comment Likes section of Settings > Sharing.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\Settings;
11
12use Automattic\Jetpack\Modules;
13
14/**
15 * Renders the Comment Likes section.
16 *
17 * Comment Likes run whether the Like buttons are on, off, or replaced by the
18 * block, and comments have no block to move to, so this section has no variants.
19 */
20final class Comment_Likes_Section {
21
22    /**
23     * Anchor for the section.
24     */
25    public const ANCHOR = 'comment-likes';
26
27    /**
28     * Render the section.
29     */
30    public static function render(): void {
31        printf( '<div class="jetpack-sharing-settings__section" id="%s">', esc_attr( self::ANCHOR ) );
32        printf( '<h2>%s</h2>', esc_html_x( 'Comment Likes', 'Settings header', 'jetpack-sharing-likes' ) );
33
34        // The module requires a connection, so offline mode gets no switch that would do nothing.
35        if ( ! Environment::likes_supported() ) {
36            printf(
37                '<p>%s</p>',
38                Environment::is_offline_mode()
39                    ? esc_html__( 'Comment Likes need a connection to WordPress.com, which is unavailable while your site is in offline mode.', 'jetpack-sharing-likes' )
40                    : esc_html__( 'Comment Likes need a connection to WordPress.com. Connect your site to turn them on.', 'jetpack-sharing-likes' )
41            );
42            echo '</div>';
43            return;
44        }
45
46        if ( Environment::comment_likes_follow_likes_settings() ) {
47            Placement_Section::render_summary( Placement_Section::FEATURE_COMMENT_LIKES );
48        }
49
50        self::render_fields();
51
52        echo '</div>';
53    }
54
55    /**
56     * Switch Comment Likes on or off: the option on Simple, the module on Atomic and Jetpack sites.
57     *
58     * @param bool $enabled Whether comments can be liked.
59     * @return bool Whether Comment Likes now match the request.
60     */
61    public static function update( bool $enabled ): bool {
62        if ( Environment::is_simple_site() ) {
63            Likes_Options::set_comment_likes_enabled( $enabled );
64            return true;
65        }
66
67        // `deactivate()` fires its hooks even when the module was already off.
68        if ( Environment::comment_likes_enabled() === $enabled ) {
69            return true;
70        }
71
72        if ( $enabled ) {
73            ( new Modules() )->activate( 'comment-likes', false, false );
74        } else {
75            ( new Modules() )->deactivate( 'comment-likes' );
76        }
77
78        // A host can force the module either way, and activation needs a connected owner.
79        return Environment::comment_likes_enabled() === $enabled;
80    }
81
82    /**
83     * The on/off switch, plus the sitewide Likes default when nothing else on the screen shows it.
84     *
85     * The switch is Simple's option, or the module on Atomic and Jetpack sites.
86     */
87    private static function render_fields(): void {
88        $shows_default = Environment::comment_likes_follow_likes_settings()
89            && ! Section_State::configures( Likes_Section::state() );
90
91        ob_start();
92        ?>
93        <table class="form-table">
94            <tbody>
95                <tr>
96                    <th scope="row"></th>
97                    <td>
98                        <label>
99                            <input type="checkbox" name="jetpack_comment_likes_enabled" value="1" <?php checked( Environment::comment_likes_enabled() ); ?> />
100                            <?php esc_html_e( 'Allow readers to like individual comments', 'jetpack-sharing-likes' ); ?>
101                        </label>
102                    </td>
103                </tr>
104                <?php
105                if ( $shows_default ) {
106                    Likes_Section::render_sitewide_default_row( __( 'Comment Likes are', 'jetpack-sharing-likes' ) );
107                }
108                ?>
109            </tbody>
110        </table>
111        <?php
112        Settings_Form::render_fields( Settings_Form::SECTION_COMMENT_LIKES, (string) ob_get_clean() );
113
114        if ( $shows_default ) {
115            // The default saves through the Like buttons handler, which only runs for a claimed section.
116            Settings_Form::render_fields( Settings_Form::SECTION_LIKES, '' );
117        }
118    }
119}