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