admin.php 4.9 KB

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