Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
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 / 54
0.00% covered (danger)
0.00%
0 / 9
240
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_settings
0.00% covered (danger)
0.00%
0 / 32
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
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     * Initialize class
39     */
40    public static function init() {
41        static $instance = false;
42
43        if ( ! $instance ) {
44            $instance = new Jetpack_Comments_Settings( Jetpack_Comments::init() );
45        }
46
47        return $instance;
48    }
49
50    /**
51     * Constructor
52     *
53     * @param Highlander_Comments_Base $jetpack_comments The Jetpack Comments singleton.
54     */
55    public function __construct( Highlander_Comments_Base $jetpack_comments ) {
56        $this->jetpack_comments = $jetpack_comments;
57
58        // Setup settings.
59        add_action( 'admin_init', array( $this, 'add_settings' ) );
60        $this->setup_globals();
61    }
62
63    /** Private Methods ****************************************************** */
64
65    /**
66     * Set any global variables or class variables
67     *
68     * @since 1.4
69     */
70    protected function setup_globals() {
71        // Default option values.
72        $this->default_greeting = __( 'Leave a Reply', 'jetpack' );
73    }
74
75    /** Settings ************************************************************* */
76
77    /**
78     * Add the Jetpack settings to WordPress's discussions page
79     *
80     * @since 1.4
81     */
82    public function add_settings() {
83
84        // Create the section.
85        add_settings_section(
86            'jetpack_comment_form',
87            __( 'Comments', 'jetpack' ),
88            array( $this, 'comment_form_settings_section' ),
89            'discussion'
90        );
91
92        /**
93         * Clever Greeting
94         */
95        add_settings_field(
96            'highlander_comment_form_prompt',
97            __( 'Greeting Text', 'jetpack' ),
98            array( $this, 'comment_form_greeting_setting' ),
99            'discussion',
100            'jetpack_comment_form'
101        );
102
103        register_setting(
104            'discussion',
105            'highlander_comment_form_prompt',
106            array( $this, 'comment_form_greeting_sanitize' )
107        );
108
109        // The rebuilt comment form is what offers blocks; the package may lag this file on a staged deploy.
110        if ( ! class_exists( Block_Editor::class ) || ! Comments::is_enabled() ) {
111            return;
112        }
113
114        add_settings_field(
115            'enable_blocks_comments',
116            __( 'Allow Blocks', 'jetpack' ),
117            array( $this, 'allow_blocks_field' ),
118            'discussion',
119            'jetpack_comment_form'
120        );
121
122        register_setting(
123            'discussion',
124            'enable_blocks_comments',
125            array( $this, 'allow_blocks_sanitize' )
126        );
127    }
128
129    /**
130     * Blocks in comments setting.
131     */
132    public function allow_blocks_field() {
133        ?>
134
135        <label>
136            <input name="enable_blocks_comments" type="checkbox" value="1" <?php checked( (bool) get_option( 'enable_blocks_comments', true ) ); ?>>
137            <?php esc_html_e( 'Enable blocks in comments', 'jetpack' ); ?>
138        </label>
139
140        <?php
141    }
142
143    /**
144     * Sanitize the blocks in comments setting.
145     *
146     * @param mixed $val The submitted value.
147     * @return string
148     */
149    public function allow_blocks_sanitize( $val ) {
150        return $val ? '1' : '0';
151    }
152
153    /**
154     * Discussions setting section blurb
155     *
156     * @since 1.4
157     */
158    public function comment_form_settings_section() {
159        ?>
160
161        <p id="jetpack-comments-settings"><?php esc_html_e( 'Adjust your Comments form with a clever greeting.', 'jetpack' ); ?></p>
162
163        <?php
164    }
165
166    /**
167     * Custom Comment Greeting Text
168     *
169     * @since 1.4
170     */
171    public function comment_form_greeting_setting() {
172
173        // The greeting.
174        $greeting = get_option( 'highlander_comment_form_prompt', $this->default_greeting );
175        ?>
176
177        <input type="text" name="highlander_comment_form_prompt" id="jetpack-comment-form-greeting" value="<?php echo esc_attr( $greeting ); ?>" class="regular-text">
178        <p class="description"><?php esc_html_e( 'A few catchy words to motivate your readers to comment', 'jetpack' ); ?></p>
179
180        <?php
181    }
182
183    /**
184     * Sanitize the clever comment greeting
185     *
186     * @since 1.4
187     * @param string $val The contact form greeting string.
188     * @return string
189     */
190    public function comment_form_greeting_sanitize( $val ) {
191
192        // Delete if empty or the default.
193        if ( empty( $val ) || ( $this->default_greeting === $val ) ) {
194            delete_option( 'highlander_comment_form_prompt' );
195            return false;
196        }
197
198        return wp_kses( $val, array() );
199    }
200}
201
202Jetpack_Comments_Settings::init();