Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Sharing_Resources
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 render
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 save
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The "Disable CSS and JS" setting on 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 * Stops sharedaddy enqueuing its own CSS and JS, for themes that bundle them.
14 */
15final class Sharing_Resources {
16
17    /**
18     * Option `sharing_init()` in `modules/sharedaddy/sharedaddy.php` reads.
19     */
20    public const OPTION = 'sharedaddy_disable_resources';
21
22    /**
23     * Field name the checkbox posts.
24     */
25    private const FIELD = 'disable_resources';
26
27    /**
28     * Whether the setting has anything to configure on this site.
29     *
30     * It only affects legacy sharing buttons, and Simple never loads `sharedaddy.php`.
31     */
32    public static function is_available(): bool {
33        return ! Environment::is_simple_site() && Section_State::configures( Sharing_Section::state() );
34    }
35
36    /**
37     * Table row for the setting.
38     */
39    public static function render(): string {
40        if ( ! self::is_available() ) {
41            return '';
42        }
43
44        return sprintf(
45            '<tr valign="top">
46                <th scope="row"><label for="disable_css">%1$s</label></th>
47                <td>
48                    <input id="disable_css" type="checkbox" name="%2$s"%3$s /> <small><em>%4$s</em></small>
49                </td>
50            </tr>',
51            esc_html__( 'Disable CSS and JS', 'jetpack-sharing-likes' ),
52            esc_attr( self::FIELD ),
53            checked( '1', (string) get_option( self::OPTION ), false ),
54            esc_html__( 'Advanced. If this option is checked, you must include these files in your theme manually for the sharing links to work.', 'jetpack-sharing-likes' )
55        );
56    }
57
58    /**
59     * Save the checkbox. Callers verify the nonce.
60     *
61     * Only while it renders: an unchecked box posts nothing, so saving it from
62     * a screen that never showed it would switch the setting off.
63     */
64    public static function save(): void {
65        if ( ! self::is_available() ) {
66            return;
67        }
68
69        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by the caller.
70        self::update( isset( $_POST[ self::FIELD ] ) );
71    }
72
73    /**
74     * Store whether sharedaddy should skip its own CSS and JS.
75     *
76     * @param bool $disabled Whether the resources are disabled.
77     */
78    public static function update( bool $disabled ): void {
79        update_option( self::OPTION, $disabled ? 1 : 0 );
80    }
81}