Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
99.06% covered (success)
99.06%
105 / 106
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Likes_Section
99.06% covered (success)
99.06%
105 / 106
91.67% covered (success)
91.67%
11 / 12
21
0.00% covered (danger)
0.00%
0 / 1
 render
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
6.01
 state
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render_unsupported
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 can_offer_block
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 render_block_call_to_action
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render_off
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render_activate_form
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render_block_nudge
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 render_site_editor_link
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render_options
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 render_sitewide_default_row
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 render_reblog_option
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * The Like buttons 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
12/**
13 * Renders the Like buttons section.
14 */
15final class Likes_Section {
16
17    /**
18     * Nonce action for this section's action buttons.
19     *
20     * Deliberately not sharedaddy's `sharing-options`: that nonce triggers the
21     * sharing handlers too, and one of them clears `sharing-options[global][show]`
22     * when the request carries no `show` field.
23     */
24    public const NONCE_ACTION = 'jetpack-likes-options';
25
26    /**
27     * Anchor `jetpack_likes_configuration_url()` points the module list at.
28     */
29    public const ANCHOR = 'likes';
30
31    /**
32     * Render the section.
33     */
34    public static function render(): void {
35        printf( '<div class="jetpack-sharing-settings__section" id="%s">', esc_attr( self::ANCHOR ) );
36        printf( '<h2>%s</h2>', esc_html_x( 'Like buttons', 'Settings header', 'jetpack-sharing-likes' ) );
37
38        if ( ! Environment::likes_supported() ) {
39            self::render_unsupported();
40            echo '</div>';
41            return;
42        }
43
44        // Off with a block route points at the block; off without one offers the way
45        // back. Running with a route shows the options under a nudge; running without, the options alone.
46        switch ( self::state() ) {
47            case Section_State::BLOCK_CALL_TO_ACTION:
48                self::render_block_call_to_action();
49                break;
50            case Section_State::OFF:
51                self::render_off();
52                break;
53            case Section_State::CONFIGURE_WITH_BLOCK_NUDGE:
54                self::render_block_nudge();
55                self::render_options();
56                break;
57            default:
58                self::render_options();
59        }
60
61        echo '</div>';
62    }
63
64    /**
65     * Which variant the section renders.
66     *
67     * @return string One of the `Section_State` constants.
68     */
69    public static function state(): string {
70        return Section_State::for_section(
71            self::can_offer_block(),
72            Environment::likes_module_running(),
73            Environment::legacy_likes_switched_off()
74        );
75    }
76
77    /**
78     * Like buttons cannot render on this site at all.
79     *
80     * The section still says so rather than vanishing: the per-post Likes
81     * metabox stays visible on a disconnected site, and someone following it
82     * here should learn why there is nothing to configure.
83     */
84    private static function render_unsupported(): void {
85        printf(
86            '<p>%s</p>',
87            Environment::is_offline_mode()
88                ? esc_html__( 'Like buttons need a connection to WordPress.com, which is unavailable while your site is in offline mode.', 'jetpack-sharing-likes' )
89                : esc_html__( 'Like buttons need a connection to WordPress.com. Connect your site to turn them on and choose where they appear.', 'jetpack-sharing-likes' )
90        );
91    }
92
93    /**
94     * Whether the Like block is a route we can send this site down.
95     *
96     * A block theme alone is not enough: the block must be registered, and the
97     * theme must have a single post template to add it to.
98     */
99    private static function can_offer_block(): bool {
100        return Environment::is_block_theme()
101            && Environment::like_block_registered()
102            && '' !== Environment::single_template_editor_url();
103    }
104
105    /**
106     * The legacy buttons are off and the site could use the block instead.
107     */
108    private static function render_block_call_to_action(): void {
109        printf(
110            '<p>%s</p>',
111            esc_html__( 'Add the Like block to your theme’s template.', 'jetpack-sharing-likes' )
112        );
113        self::render_site_editor_link();
114    }
115
116    /**
117     * The module is off on a theme with no block route.
118     */
119    private static function render_off(): void {
120        printf(
121            '<p>%s</p>',
122            esc_html__( 'Like buttons are turned off for this site.', 'jetpack-sharing-likes' )
123        );
124        self::render_activate_form();
125    }
126
127    /**
128     * The way back. See `Feature_Actions::activate()`.
129     */
130    private static function render_activate_form(): void {
131        Post_Handler::render_action_form(
132            'activate-likes',
133            self::NONCE_ACTION,
134            __( 'Turn on Like buttons', 'jetpack-sharing-likes' )
135        );
136    }
137
138    /**
139     * The buttons are live, but the site would be better served by the block.
140     */
141    private static function render_block_nudge(): void {
142        echo '<div class="notice notice-info inline">';
143        printf( '<p>%s</p>', esc_html__( 'Legacy Like buttons cannot be customized on block themes. Use the Like block in your theme’s template instead.', 'jetpack-sharing-likes' ) );
144        Post_Handler::render_action_form(
145            'switch-to-block-likes',
146            self::NONCE_ACTION,
147            __( 'Switch to the Like block', 'jetpack-sharing-likes' ),
148            false
149        );
150        echo '</div>';
151    }
152
153    /**
154     * Link to the active theme's single post template, with the editor open.
155     */
156    private static function render_site_editor_link(): void {
157        printf(
158            '<p><a class="button" href="%1$s">%2$s</a></p>',
159            esc_url( Environment::single_template_editor_url() ),
160            esc_html__( 'Open Site Editor', 'jetpack-sharing-likes' )
161        );
162    }
163
164    /**
165     * The section's own settings.
166     */
167    private static function render_options(): void {
168        Placement_Section::render_summary( Placement_Section::FEATURE_LIKES );
169
170        ob_start();
171        ?>
172        <table class="form-table">
173            <tbody>
174                <?php
175                self::render_sitewide_default_row( __( 'WordPress.com Likes are', 'jetpack-sharing-likes' ) );
176
177                if ( Environment::is_simple_site() ) {
178                    self::render_reblog_option();
179                }
180                ?>
181            </tbody>
182        </table>
183        <?php
184        Settings_Form::render_fields( Settings_Form::SECTION_LIKES, (string) ob_get_clean() );
185    }
186
187    /**
188     * "On for all posts" or "Turned on per post", which Like buttons and Comment Likes both read.
189     *
190     * Whichever section renders it claims `Settings_Form::SECTION_LIKES` for it.
191     *
192     * @param string $label Row heading, naming the feature the reader is configuring.
193     */
194    public static function render_sitewide_default_row( string $label ): void {
195        $likes_enabled_sitewide = Likes_Options::likes_enabled_sitewide();
196        ?>
197        <tr>
198            <th scope="row"><label><?php echo esc_html( $label ); ?></label></th>
199            <td>
200                <div>
201                    <label>
202                        <input type="radio" name="wpl_default" value="on" <?php checked( $likes_enabled_sitewide ); ?> />
203                        <?php esc_html_e( 'On for all posts', 'jetpack-sharing-likes' ); ?>
204                    </label>
205                </div>
206                <div>
207                    <label>
208                        <input type="radio" name="wpl_default" value="off" <?php checked( ! $likes_enabled_sitewide ); ?> />
209                        <?php esc_html_e( 'Turned on per post', 'jetpack-sharing-likes' ); ?>
210                    </label>
211                </div>
212            </td>
213        </tr>
214        <?php
215    }
216
217    /**
218     * Reblog button, WordPress.com Simple only.
219     *
220     * Moves to the Like block's own settings once a site switches: the block
221     * carries a `showReblogButton` attribute.
222     */
223    private static function render_reblog_option(): void {
224        $enabled = Likes_Options::reblogs_enabled_sitewide();
225        ?>
226        <tr>
227            <th scope="row"><label><?php esc_html_e( 'WordPress.com Reblog Button', 'jetpack-sharing-likes' ); ?></label></th>
228            <td>
229                <div>
230                    <label>
231                        <input type="radio" name="jetpack_reblogs_enabled" value="on" <?php checked( $enabled ); ?> />
232                        <?php esc_html_e( 'Show the Reblog button on posts', 'jetpack-sharing-likes' ); ?>
233                    </label>
234                </div>
235                <div>
236                    <label>
237                        <input type="radio" name="jetpack_reblogs_enabled" value="off" <?php checked( ! $enabled ); ?> />
238                        <?php esc_html_e( 'Don\'t show the Reblog button on posts', 'jetpack-sharing-likes' ); ?>
239                    </label>
240                </div>
241            </td>
242        </tr>
243        <?php
244    }
245}