Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 97
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Comments_Settings
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 11
552
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 setup_globals
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 add_settings
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
12
 allow_blocks_field
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 allow_blocks_sanitize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 comment_form_settings_section
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 comment_form_greeting_setting
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 comment_form_greeting_sanitize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 comment_form_color_scheme_setting
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 comment_form_color_scheme_sanitize
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
1<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Jetpack comments admin menu file.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Comments\Block_Editor;
9use Automattic\Jetpack\Comments\Comments;
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * Class Jetpack_Comments_Settings
17 * This class represents the comments settings functionality.
18 */
19class Jetpack_Comments_Settings {
20
21    /** Variables *************************************************************/
22
23    /**
24     * The Jetpack Comments singleton
25     *
26     * @var Highlander_Comments_Base
27     */
28    public $jetpack_comments;
29
30    /**
31     * The default comment form greeting - blank to start with
32     *
33     * @var string
34     */
35    public $default_greeting = ''; // Set in constructor.
36
37    /**
38     * The default comment form color scheme - an empty array to start with
39     *
40     * @var array
41     */
42    public $color_schemes = array();
43
44    /**
45     * Initialize class
46     */
47    public static function init() {
48        static $instance = false;
49
50        if ( ! $instance ) {
51            $instance = new Jetpack_Comments_Settings( Jetpack_Comments::init() );
52        }
53
54        return $instance;
55    }
56
57    /**
58     * Constructor
59     *
60     * @param Highlander_Comments_Base $jetpack_comments The Jetpack Comments singleton.
61     */
62    public function __construct( Highlander_Comments_Base $jetpack_comments ) {
63        $this->jetpack_comments = $jetpack_comments;
64
65        // Setup settings.
66        add_action( 'admin_init', array( $this, 'add_settings' ) );
67        $this->setup_globals();
68    }
69
70    /** Private Methods ****************************************************** */
71
72    /**
73     * Set any global variables or class variables
74     *
75     * @since 1.4
76     */
77    protected function setup_globals() {
78        // Default option values.
79        $this->default_greeting = __( 'Leave a Reply', 'jetpack' );
80
81        // Possible color schemes.
82        $this->color_schemes = array(
83            'light'       => __( 'Light', 'jetpack' ),
84            'dark'        => __( 'Dark', 'jetpack' ),
85            'transparent' => __( 'Transparent', 'jetpack' ),
86        );
87    }
88
89    /** Settings ************************************************************* */
90
91    /**
92     * Add the Jetpack settings to WordPress's discussions page
93     *
94     * @since 1.4
95     */
96    public function add_settings() {
97
98        // Create the section.
99        add_settings_section(
100            'jetpack_comment_form',
101            __( 'Comments', 'jetpack' ),
102            array( $this, 'comment_form_settings_section' ),
103            'discussion'
104        );
105
106        /**
107         * Clever Greeting
108         */
109        add_settings_field(
110            'highlander_comment_form_prompt',
111            __( 'Greeting Text', 'jetpack' ),
112            array( $this, 'comment_form_greeting_setting' ),
113            'discussion',
114            'jetpack_comment_form'
115        );
116
117        register_setting(
118            'discussion',
119            'highlander_comment_form_prompt',
120            array( $this, 'comment_form_greeting_sanitize' )
121        );
122
123        // The rebuilt comment form is what offers blocks; the package may lag this file on a staged deploy.
124        if ( ! class_exists( Block_Editor::class ) || ! Comments::is_enabled() ) {
125            /**
126             * Color Scheme, which the rebuilt form ignores for the theme's own colors.
127             */
128            add_settings_field(
129                'jetpack_comment_form_color_scheme',
130                __( 'Color Scheme', 'jetpack' ),
131                array( $this, 'comment_form_color_scheme_setting' ),
132                'discussion',
133                'jetpack_comment_form'
134            );
135
136            register_setting(
137                'discussion',
138                'jetpack_comment_form_color_scheme',
139                array( $this, 'comment_form_color_scheme_sanitize' )
140            );
141
142            return;
143        }
144
145        add_settings_field(
146            'enable_blocks_comments',
147            __( 'Allow Blocks', 'jetpack' ),
148            array( $this, 'allow_blocks_field' ),
149            'discussion',
150            'jetpack_comment_form'
151        );
152
153        register_setting(
154            'discussion',
155            'enable_blocks_comments',
156            array( $this, 'allow_blocks_sanitize' )
157        );
158    }
159
160    /**
161     * Blocks in comments setting.
162     */
163    public function allow_blocks_field() {
164        ?>
165
166        <label>
167            <input name="enable_blocks_comments" type="checkbox" value="1" <?php checked( (bool) get_option( 'enable_blocks_comments', true ) ); ?>>
168            <?php esc_html_e( 'Enable blocks in comments', 'jetpack' ); ?>
169        </label>
170
171        <?php
172    }
173
174    /**
175     * Sanitize the blocks in comments setting.
176     *
177     * @param mixed $val The submitted value.
178     * @return string
179     */
180    public function allow_blocks_sanitize( $val ) {
181        return $val ? '1' : '0';
182    }
183
184    /**
185     * Discussions setting section blurb
186     *
187     * @since 1.4
188     */
189    public function comment_form_settings_section() {
190        ?>
191
192        <p id="jetpack-comments-settings">
193            <?php
194            if ( class_exists( Block_Editor::class ) && Comments::is_enabled() ) {
195                esc_html_e( 'Adjust your Comments form with a clever greeting.', 'jetpack' );
196            } else {
197                esc_html_e( 'Adjust your Comments form with a clever greeting and color-scheme.', 'jetpack' );
198            }
199            ?>
200        </p>
201
202        <?php
203    }
204
205    /**
206     * Custom Comment Greeting Text
207     *
208     * @since 1.4
209     */
210    public function comment_form_greeting_setting() {
211
212        // The greeting.
213        $greeting = get_option( 'highlander_comment_form_prompt', $this->default_greeting );
214        ?>
215
216        <input type="text" name="highlander_comment_form_prompt" id="jetpack-comment-form-greeting" value="<?php echo esc_attr( $greeting ); ?>" class="regular-text">
217        <p class="description"><?php esc_html_e( 'A few catchy words to motivate your readers to comment', 'jetpack' ); ?></p>
218
219        <?php
220    }
221
222    /**
223     * Sanitize the clever comment greeting
224     *
225     * @since 1.4
226     * @param string $val The contact form greeting string.
227     * @return string
228     */
229    public function comment_form_greeting_sanitize( $val ) {
230
231        // Delete if empty or the default.
232        if ( empty( $val ) || ( $this->default_greeting === $val ) ) {
233            delete_option( 'highlander_comment_form_prompt' );
234            return false;
235        }
236
237        return wp_kses( $val, array() );
238    }
239
240    /**
241     * Comment Form Color Scheme Setting
242     *
243     * @since 1.4
244     */
245    public function comment_form_color_scheme_setting() {
246
247        // The color scheme.
248        $scheme = get_option( 'jetpack_comment_form_color_scheme', $this->jetpack_comments->default_color_scheme );
249        ?>
250
251        <fieldset>
252            <legend class="screen-reader-text"><?php esc_html_e( 'Color Scheme', 'jetpack' ); ?></legend>
253
254            <?php foreach ( $this->color_schemes as $key => $label ) : ?>
255
256                <label>
257                    <input type="radio" name="jetpack_comment_form_color_scheme" id="jetpack-comment-form-color-scheme" value="<?php echo esc_attr( $key ); ?>" <?php checked( $scheme, $key ); ?>>
258                    <?php echo esc_attr( $label ); ?>
259                </label>
260                <br />
261
262            <?php endforeach; ?>
263
264        </fieldset>
265
266        <?php
267    }
268
269    /**
270     * Sanitize the color scheme
271     *
272     * @since 1.4
273     * @param string $val The color scheme string.
274     * @return string
275     */
276    public function comment_form_color_scheme_sanitize( $val ) {
277
278        // Delete the option if it's unknown, or the default.
279        if (
280            empty( $val ) || ! array_key_exists( $val, $this->color_schemes )
281        ||
282            $val === $this->jetpack_comments->default_color_scheme
283        ) {
284            delete_option( 'jetpack_comment_form_color_scheme' );
285            return false;
286        }
287
288        return $val;
289    }
290}
291
292Jetpack_Comments_Settings::init();