Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
24.66% covered (danger)
24.66%
18 / 73
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Placement_Section
24.66% covered (danger)
24.66%
18 / 73
28.57% covered (danger)
28.57%
2 / 7
229.00
0.00% covered (danger)
0.00%
0 / 1
 render_summary
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 render
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
6
 heading
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 selected_post_types
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 normalize_show
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 default_post_types
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 label_for
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * The shared placement 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 "Show buttons on", which governs where both sharing buttons and Like
14 * buttons appear. Its own section because it belongs to neither feature alone.
15 */
16final class Placement_Section {
17
18    /**
19     * Anchor the feature sections link to.
20     */
21    public const ANCHOR = 'jetpack-sharing-placement';
22
23    /**
24     * Identifies the Sharing buttons section to `render_summary()`.
25     */
26    public const FEATURE_SHARING = 'sharing';
27
28    /**
29     * Identifies the Like buttons section to `render_summary()`.
30     */
31    public const FEATURE_LIKES = 'likes';
32
33    /**
34     * Where a feature's buttons currently appear, stated inside that feature's
35     * own section.
36     *
37     * This setting governs both features but lives in neither, so each section
38     * says what it means for that feature and links here to change it. It also
39     * surfaces a placement that hides the buttons entirely, which is otherwise
40     * only visible on this section further down the page.
41     *
42     * Each feature gets a complete sentence: interpolating the feature name into
43     * a shared one does not translate.
44     *
45     * @param string $feature One of the FEATURE_* constants.
46     */
47    public static function render_summary( string $feature ): void {
48        $labels = array_map( array( __CLASS__, 'label_for' ), self::selected_post_types() );
49
50        if ( $labels === array() ) {
51            $summary = self::FEATURE_LIKES === $feature
52                ? __( 'Like buttons are currently not shown anywhere.', 'jetpack-sharing-likes' )
53                : __( 'Sharing buttons are currently not shown anywhere.', 'jetpack-sharing-likes' );
54        } elseif ( self::FEATURE_LIKES === $feature ) {
55            /* translators: %s: comma-separated list of places, for example "Posts, Pages". */
56            $summary = sprintf( __( 'Like buttons currently appear on: %s.', 'jetpack-sharing-likes' ), implode( ', ', $labels ) );
57        } else {
58            /* translators: %s: comma-separated list of places, for example "Posts, Pages". */
59            $summary = sprintf( __( 'Sharing buttons currently appear on: %s.', 'jetpack-sharing-likes' ), implode( ', ', $labels ) );
60        }
61
62        printf(
63            '<p class="description">%1$s <a href="#%2$s">%3$s</a></p>',
64            esc_html( $summary ),
65            esc_attr( self::ANCHOR ),
66            esc_html__( 'Change where they appear', 'jetpack-sharing-likes' )
67        );
68    }
69
70    /**
71     * Render the section.
72     */
73    public static function render(): void {
74        $shown = self::selected_post_types();
75
76        $choices = array_values( get_post_types( array( 'public' => true ) ) );
77        array_unshift( $choices, 'index' );
78        ?>
79        <div class="jetpack-sharing-settings__section" id="<?php echo esc_attr( self::ANCHOR ); ?>">
80            <h2><?php echo esc_html( self::heading() ); ?></h2>
81            <?php ob_start(); ?>
82                <table class="form-table">
83                    <tbody>
84                    <?php
85                    /**
86                     * Filters the HTML at the beginning of the "Show button on" row.
87                     *
88                     * @module sharedaddy
89                     *
90                     * @since jetpack-2.1.0
91                     *
92                     * @param string $var Opening HTML tag at the beginning of the "Show button on" row.
93                     */
94                    echo apply_filters( 'sharing_show_buttons_on_row_start', '<tr valign="top">' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
95                    ?>
96                        <th scope="row"></th>
97                        <td>
98                            <fieldset>
99                                <legend class="screen-reader-text"><span><?php echo esc_html( self::heading() ); ?></span></legend>
100                                <?php foreach ( $choices as $choice ) : ?>
101                                    <label>
102                                        <input type="checkbox" name="show[]" value="<?php echo esc_attr( $choice ); ?>" <?php checked( in_array( $choice, $shown, true ) ); ?> />
103                                        <?php echo esc_html( self::label_for( $choice ) ); ?>
104                                    </label><br />
105                                <?php endforeach; ?>
106                            </fieldset>
107                        </td>
108                    <?php
109                    /**
110                     * Filters the HTML at the end of the "Show button on" row.
111                     *
112                     * @module sharedaddy
113                     *
114                     * @since jetpack-2.1.0
115                     *
116                     * @param string $var Closing HTML tag at the end of the "Show button on" row.
117                     */
118                    echo apply_filters( 'sharing_show_buttons_on_row_end', '</tr>' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
119                    ?>
120                    </tbody>
121                </table>
122            <?php Settings_Form::render_fields( Settings_Form::SECTION_PLACEMENT, (string) ob_get_clean() ); ?>
123        </div>
124        <?php
125    }
126
127    /**
128     * Heading naming the features this section governs.
129     *
130     * "Show buttons on" sat directly below the Like buttons section and read as
131     * though it belonged to it. Naming the features makes the shared scope
132     * legible without a second line of copy.
133     */
134    private static function heading(): string {
135        $sharing = Section_State::configures( Sharing_Section::state() );
136        $likes   = Section_State::configures( Likes_Section::state() );
137
138        if ( $sharing && $likes ) {
139            return __( 'Where sharing and Like buttons appear', 'jetpack-sharing-likes' );
140        }
141
142        return $sharing
143            ? __( 'Where sharing buttons appear', 'jetpack-sharing-likes' )
144            : __( 'Where Like buttons appear', 'jetpack-sharing-likes' );
145    }
146
147    /**
148     * Post types currently set to show buttons.
149     *
150     * Falls back to the same defaults the features themselves apply when the
151     * option has never been saved. Reading it raw would render every checkbox
152     * unchecked on a site where the buttons are in fact live, and the next save
153     * would then write that back and turn them off.
154     *
155     * @return string[]
156     */
157    public static function selected_post_types(): array {
158        $sharing = get_option( 'sharing-options', array() );
159
160        if ( ! is_array( $sharing ) || ! isset( $sharing['global']['show'] ) ) {
161            return self::default_post_types();
162        }
163
164        return self::normalize_show( $sharing['global']['show'] );
165    }
166
167    /**
168     * A stored `show` value as a list of post types.
169     *
170     * Older sites stored a single keyword rather than a list, and both
171     * `Sharing_Service::get_global_options()` and `Jetpack_Likes_Settings::get_options()`
172     * still map it, so it is live data rather than a historical curiosity.
173     *
174     * @param mixed $shown Stored `sharing-options['global']['show']` value.
175     * @return string[]
176     */
177    public static function normalize_show( $shown ): array {
178        if ( is_scalar( $shown ) ) {
179            $legacy = array(
180                'posts'       => array( 'post', 'page' ),
181                'index'       => array( 'index' ),
182                'posts-index' => array( 'post', 'page', 'index' ),
183            );
184            $shown  = $legacy[ $shown ] ?? array();
185        }
186
187        return array_values( array_filter( (array) $shown, 'is_string' ) );
188    }
189
190    /**
191     * Where buttons appear on a site that has never saved this section.
192     *
193     * The two features apply different defaults:
194     * - sharing: posts and pages;
195     * - Likes: those, plus public post types that support comments.
196     *
197     * Reporting the narrower set would understate where Like buttons are, so
198     * defer to Likes whenever it is the feature running.
199     *
200     * @return string[]
201     */
202    private static function default_post_types(): array {
203        $defaults = array( 'post', 'page' );
204
205        if ( ! Environment::likes_settings_in_use() ) {
206            return $defaults;
207        }
208
209        if ( ! class_exists( 'Jetpack_Likes_Settings' ) ) {
210            return $defaults;
211        }
212
213        $options = ( new \Jetpack_Likes_Settings() )->get_options();
214
215        return isset( $options['show'] ) ? self::normalize_show( $options['show'] ) : $defaults;
216    }
217
218    /**
219     * Human-readable label for a post type choice.
220     *
221     * @param string $choice Post type slug, or 'index' for the archive pages.
222     */
223    private static function label_for( string $choice ): string {
224        if ( 'index' === $choice ) {
225            return __( 'Front Page, Archive Pages, and Search Results', 'jetpack-sharing-likes' );
226        }
227
228        $post_type = get_post_type_object( $choice );
229
230        return $post_type instanceof \WP_Post_Type ? $post_type->labels->name : $choice;
231    }
232}