Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 91
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 / 88
0.00% covered (danger)
0.00%
0 / 11
462
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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        /**
124         * Color Scheme
125         */
126        add_settings_field(
127            'jetpack_comment_form_color_scheme',
128            __( 'Color Scheme', 'jetpack' ),
129            array( $this, 'comment_form_color_scheme_setting' ),
130            'discussion',
131            'jetpack_comment_form'
132        );
133
134        register_setting(
135            'discussion',
136            'jetpack_comment_form_color_scheme',
137            array( $this, 'comment_form_color_scheme_sanitize' )
138        );
139
140        // The rebuilt comment form is what offers blocks; the package may lag this file on a staged deploy.
141        if ( ! class_exists( Block_Editor::class ) || ! Comments::is_enabled() ) {
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"><?php esc_html_e( 'Adjust your Comments form with a clever greeting and color-scheme.', 'jetpack' ); ?></p>
193
194        <?php
195    }
196
197    /**
198     * Custom Comment Greeting Text
199     *
200     * @since 1.4
201     */
202    public function comment_form_greeting_setting() {
203
204        // The greeting.
205        $greeting = get_option( 'highlander_comment_form_prompt', $this->default_greeting );
206        ?>
207
208        <input type="text" name="highlander_comment_form_prompt" id="jetpack-comment-form-greeting" value="<?php echo esc_attr( $greeting ); ?>" class="regular-text">
209        <p class="description"><?php esc_html_e( 'A few catchy words to motivate your readers to comment', 'jetpack' ); ?></p>
210
211        <?php
212    }
213
214    /**
215     * Sanitize the clever comment greeting
216     *
217     * @since 1.4
218     * @param string $val The contact form greeting string.
219     * @return string
220     */
221    public function comment_form_greeting_sanitize( $val ) {
222
223        // Delete if empty or the default.
224        if ( empty( $val ) || ( $this->default_greeting === $val ) ) {
225            delete_option( 'highlander_comment_form_prompt' );
226            return false;
227        }
228
229        return wp_kses( $val, array() );
230    }
231
232    /**
233     * Comment Form Color Scheme Setting
234     *
235     * @since 1.4
236     */
237    public function comment_form_color_scheme_setting() {
238
239        // The color scheme.
240        $scheme = get_option( 'jetpack_comment_form_color_scheme', $this->jetpack_comments->default_color_scheme );
241        ?>
242
243        <fieldset>
244            <legend class="screen-reader-text"><?php esc_html_e( 'Color Scheme', 'jetpack' ); ?></legend>
245
246            <?php foreach ( $this->color_schemes as $key => $label ) : ?>
247
248                <label>
249                    <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 ); ?>>
250                    <?php echo esc_attr( $label ); ?>
251                </label>
252                <br />
253
254            <?php endforeach; ?>
255
256        </fieldset>
257
258        <?php
259    }
260
261    /**
262     * Sanitize the color scheme
263     *
264     * @since 1.4
265     * @param string $val The color scheme string.
266     * @return string
267     */
268    public function comment_form_color_scheme_sanitize( $val ) {
269
270        // Delete the option if it's unknown, or the default.
271        if (
272            empty( $val ) || ! array_key_exists( $val, $this->color_schemes )
273        ||
274            $val === $this->jetpack_comments->default_color_scheme
275        ) {
276            delete_option( 'jetpack_comment_form_color_scheme' );
277            return false;
278        }
279
280        return $val;
281    }
282}
283
284Jetpack_Comments_Settings::init();