Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Comments_Settings
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 9
272
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 / 30
0.00% covered (danger)
0.00%
0 / 1
2
 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
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Class Jetpack_Comments_Settings
14 * This class represents the comments settings functionality.
15 */
16class Jetpack_Comments_Settings {
17
18    /** Variables *************************************************************/
19
20    /**
21     * The Jetpack Comments singleton
22     *
23     * @var Highlander_Comments_Base
24     */
25    public $jetpack_comments;
26
27    /**
28     * The default comment form greeting - blank to start with
29     *
30     * @var string
31     */
32    public $default_greeting = ''; // Set in constructor.
33
34    /**
35     * The default comment form color scheme - an empty array to start with
36     *
37     * @var array
38     */
39    public $color_schemes = array();
40
41    /**
42     * Initialize class
43     */
44    public static function init() {
45        static $instance = false;
46
47        if ( ! $instance ) {
48            $instance = new Jetpack_Comments_Settings( Jetpack_Comments::init() );
49        }
50
51        return $instance;
52    }
53
54    /**
55     * Constructor
56     *
57     * @param Highlander_Comments_Base $jetpack_comments The Jetpack Comments singleton.
58     */
59    public function __construct( Highlander_Comments_Base $jetpack_comments ) {
60        $this->jetpack_comments = $jetpack_comments;
61
62        // Setup settings.
63        add_action( 'admin_init', array( $this, 'add_settings' ) );
64        $this->setup_globals();
65    }
66
67    /** Private Methods ****************************************************** */
68
69    /**
70     * Set any global variables or class variables
71     *
72     * @since 1.4
73     */
74    protected function setup_globals() {
75        // Default option values.
76        $this->default_greeting = __( 'Leave a Reply', 'jetpack' );
77
78        // Possible color schemes.
79        $this->color_schemes = array(
80            'light'       => __( 'Light', 'jetpack' ),
81            'dark'        => __( 'Dark', 'jetpack' ),
82            'transparent' => __( 'Transparent', 'jetpack' ),
83        );
84    }
85
86    /** Settings ************************************************************* */
87
88    /**
89     * Add the Jetpack settings to WordPress's discussions page
90     *
91     * @since 1.4
92     */
93    public function add_settings() {
94
95        // Create the section.
96        add_settings_section(
97            'jetpack_comment_form',
98            __( 'Comments', 'jetpack' ),
99            array( $this, 'comment_form_settings_section' ),
100            'discussion'
101        );
102
103        /**
104         * Clever Greeting
105         */
106        add_settings_field(
107            'highlander_comment_form_prompt',
108            __( 'Greeting Text', 'jetpack' ),
109            array( $this, 'comment_form_greeting_setting' ),
110            'discussion',
111            'jetpack_comment_form'
112        );
113
114        register_setting(
115            'discussion',
116            'highlander_comment_form_prompt',
117            array( $this, 'comment_form_greeting_sanitize' )
118        );
119
120        /**
121         * Color Scheme
122         */
123        add_settings_field(
124            'jetpack_comment_form_color_scheme',
125            __( 'Color Scheme', 'jetpack' ),
126            array( $this, 'comment_form_color_scheme_setting' ),
127            'discussion',
128            'jetpack_comment_form'
129        );
130
131        register_setting(
132            'discussion',
133            'jetpack_comment_form_color_scheme',
134            array( $this, 'comment_form_color_scheme_sanitize' )
135        );
136    }
137
138    /**
139     * Discussions setting section blurb
140     *
141     * @since 1.4
142     */
143    public function comment_form_settings_section() {
144        ?>
145
146        <p id="jetpack-comments-settings"><?php esc_html_e( 'Adjust your Comments form with a clever greeting and color-scheme.', 'jetpack' ); ?></p>
147
148        <?php
149    }
150
151    /**
152     * Custom Comment Greeting Text
153     *
154     * @since 1.4
155     */
156    public function comment_form_greeting_setting() {
157
158        // The greeting.
159        $greeting = get_option( 'highlander_comment_form_prompt', $this->default_greeting );
160        ?>
161
162        <input type="text" name="highlander_comment_form_prompt" id="jetpack-comment-form-greeting" value="<?php echo esc_attr( $greeting ); ?>" class="regular-text">
163        <p class="description"><?php esc_html_e( 'A few catchy words to motivate your readers to comment', 'jetpack' ); ?></p>
164
165        <?php
166    }
167
168    /**
169     * Sanitize the clever comment greeting
170     *
171     * @since 1.4
172     * @param string $val The contact form greeting string.
173     * @return string
174     */
175    public function comment_form_greeting_sanitize( $val ) {
176
177        // Delete if empty or the default.
178        if ( empty( $val ) || ( $this->default_greeting === $val ) ) {
179            delete_option( 'highlander_comment_form_prompt' );
180            return false;
181        }
182
183        return wp_kses( $val, array() );
184    }
185
186    /**
187     * Comment Form Color Scheme Setting
188     *
189     * @since 1.4
190     */
191    public function comment_form_color_scheme_setting() {
192
193        // The color scheme.
194        $scheme = get_option( 'jetpack_comment_form_color_scheme', $this->jetpack_comments->default_color_scheme );
195        ?>
196
197        <fieldset>
198            <legend class="screen-reader-text"><?php esc_html_e( 'Color Scheme', 'jetpack' ); ?></legend>
199
200            <?php foreach ( $this->color_schemes as $key => $label ) : ?>
201
202                <label>
203                    <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 ); ?>>
204                    <?php echo esc_attr( $label ); ?>
205                </label>
206                <br />
207
208            <?php endforeach; ?>
209
210        </fieldset>
211
212        <?php
213    }
214
215    /**
216     * Sanitize the color scheme
217     *
218     * @since 1.4
219     * @param string $val The color scheme string.
220     * @return string
221     */
222    public function comment_form_color_scheme_sanitize( $val ) {
223
224        // Delete the option if it's unknown, or the default.
225        if (
226            empty( $val ) || ! array_key_exists( $val, $this->color_schemes )
227        ||
228            $val === $this->jetpack_comments->default_color_scheme
229        ) {
230            delete_option( 'jetpack_comment_form_color_scheme' );
231            return false;
232        }
233
234        return $val;
235    }
236}
237
238Jetpack_Comments_Settings::init();