Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
61 / 61
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Sharing_Section
100.00% covered (success)
100.00%
61 / 61
100.00% covered (success)
100.00%
9 / 9
17
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 state
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 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%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 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_services_config
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The Sharing 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 Sharing buttons section.
14 */
15final class Sharing_Section {
16
17    /**
18     * Nonce action for this section's action buttons.
19     */
20    public const NONCE_ACTION = 'jetpack-sharing-options';
21
22    /**
23     * Render the section.
24     */
25    public static function render(): void {
26        $state = self::state();
27
28        echo '<div class="jetpack-sharing-settings__section">';
29        printf( '<h2>%s</h2>', esc_html_x( 'Sharing buttons', 'Settings header', 'jetpack-sharing-likes' ) );
30
31        switch ( $state ) {
32            case Section_State::BLOCK_CALL_TO_ACTION:
33                self::render_block_call_to_action();
34                break;
35            case Section_State::OFF:
36                self::render_off();
37                break;
38            case Section_State::CONFIGURE_WITH_BLOCK_NUDGE:
39                self::render_block_nudge();
40                self::render_services_config();
41                break;
42            default:
43                self::render_services_config();
44        }
45
46        echo '</div>';
47    }
48
49    /**
50     * Which variant the section renders.
51     *
52     * @return string One of the `Section_State` constants.
53     */
54    public static function state(): string {
55        return Section_State::for_section(
56            self::can_offer_block(),
57            Environment::sharing_module_running(),
58            Environment::legacy_sharing_switched_off()
59        );
60    }
61
62    /**
63     * Whether the Sharing Buttons block is a route we can send this site down.
64     */
65    private static function can_offer_block(): bool {
66        return Environment::is_block_theme()
67            && Environment::sharing_block_registered()
68            && '' !== Environment::single_template_editor_url();
69    }
70
71    /**
72     * The legacy buttons are off and the site could use the block instead.
73     */
74    private static function render_block_call_to_action(): void {
75        printf(
76            '<p>%s</p>',
77            esc_html__( 'Add the Sharing Buttons block to your theme’s template.', 'jetpack-sharing-likes' )
78        );
79        self::render_site_editor_link();
80    }
81
82    /**
83     * The module is off on a theme with no block route.
84     *
85     * Nothing else on this site produces sharing buttons, so this is the one
86     * place the feature can be switched back on.
87     */
88    private static function render_off(): void {
89        printf(
90            '<p>%s</p>',
91            esc_html__( 'Sharing buttons are turned off for this site.', 'jetpack-sharing-likes' )
92        );
93
94        self::render_activate_form();
95    }
96
97    /**
98     * The way back. See `Feature_Actions::activate()`.
99     */
100    private static function render_activate_form(): void {
101        if ( ! Environment::legacy_sharing_supported() ) {
102            printf(
103                '<p>%s</p>',
104                esc_html__( 'Connect your site to WordPress.com, or enable offline mode, to turn them back on.', 'jetpack-sharing-likes' )
105            );
106            return;
107        }
108
109        Post_Handler::render_action_form(
110            'activate-sharing',
111            self::NONCE_ACTION,
112            __( 'Turn on sharing buttons', 'jetpack-sharing-likes' )
113        );
114    }
115
116    /**
117     * The buttons are live, but the site would be better served by the block.
118     */
119    private static function render_block_nudge(): void {
120        echo '<div class="notice notice-info inline">';
121        printf( '<p>%s</p>', esc_html__( 'Legacy sharing buttons cannot be customized on block themes. Use the Sharing Buttons block in your theme’s template instead.', 'jetpack-sharing-likes' ) );
122        Post_Handler::render_action_form(
123            'switch-to-block-sharing',
124            self::NONCE_ACTION,
125            __( 'Switch to the Sharing Buttons block', 'jetpack-sharing-likes' ),
126            false
127        );
128        echo '</div>';
129    }
130
131    /**
132     * Link to the active theme's single post template, with the editor open.
133     */
134    private static function render_site_editor_link(): void {
135        printf(
136            '<p><a class="button" href="%1$s">%2$s</a></p>',
137            esc_url( Environment::single_template_editor_url() ),
138            esc_html__( 'Open Site Editor', 'jetpack-sharing-likes' )
139        );
140    }
141
142    /**
143     * The services list and its settings.
144     */
145    private static function render_services_config(): void {
146        // Placement is moot with no services: the buttons appear nowhere.
147        if ( ! Environment::legacy_sharing_switched_off() ) {
148            Placement_Section::render_summary( Placement_Section::FEATURE_SHARING );
149        }
150
151        ( new Services_Config() )->render();
152    }
153}