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