Jetpack_Related_Posts_Customize

Class to include elements to modify Related Posts look in Customizer.

Defined (1)

The class is defined in the following location(s).

/modules/related-posts/class.related-posts-customize.php  
  1. class Jetpack_Related_Posts_Customize { 
  2.  
  3. /** 
  4. * Key for panel, section and prefix for options. Same option name than in Options > Reading. 
  5. * @var string 
  6. */ 
  7. var $prefix = 'jetpack_relatedposts'; 
  8.  
  9. /** 
  10. * @var string Control to focus when customizer loads. 
  11. */ 
  12. var $focus = ''; 
  13.  
  14. /** 
  15. * Class initialization. 
  16. * @since 4.4.0 
  17. */ 
  18. function __construct() { 
  19. add_action( 'customize_register', array( $this, 'customize_register' ) ); 
  20. add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) ); 
  21.  
  22. /** 
  23. * Initialize Customizer controls. 
  24. * @since 4.4.0 
  25. * @param WP_Customize_Manager $wp_customize Customizer instance. 
  26. */ 
  27. function customize_register( $wp_customize ) { 
  28.  
  29. $wp_customize->add_section( $this->prefix,  
  30. array( 
  31. 'title' => esc_html__( 'Related Posts', 'jetpack' ),  
  32. 'description' => '',  
  33. 'capability' => 'edit_theme_options',  
  34. 'priority' => 200,  
  35. ); 
  36.  
  37. $selective_options = array(); 
  38.  
  39. foreach ( $this->get_options( $wp_customize ) as $key => $field ) { 
  40. $control_id = "$this->prefix[$key]"; 
  41. $selective_options[] = $control_id; 
  42. $wp_customize->add_setting( $control_id,  
  43. array( 
  44. 'default' => isset( $field['default'] ) ? $field['default'] : '',  
  45. 'type' => isset( $field['setting_type'] ) ? $field['setting_type'] : 'option',  
  46. 'capability' => isset( $field['capability'] ) ? $field['capability'] : 'edit_theme_options',  
  47. 'transport' => isset( $field['transport'] ) ? $field['transport'] : 'postMessage',  
  48. ); 
  49. $control_settings = array( 
  50. 'label' => isset( $field['label'] ) ? $field['label'] : '',  
  51. 'description' => isset( $field['description'] ) ? $field['description'] : '',  
  52. 'settings' => $control_id,  
  53. 'type' => isset( $field['control_type'] ) ? $field['control_type'] : 'text',  
  54. 'section' => $this->prefix,  
  55. 'priority' => 10,  
  56. 'active_callback' => isset( $field['active_callback'] ) ? $field['active_callback'] : __CLASS__ . '::is_single',  
  57. ); 
  58. switch ( $field['control_type'] ) { 
  59. case 'text': 
  60. case 'checkbox': 
  61. default: 
  62. $wp_customize->add_control( new WP_Customize_Control( $wp_customize, $control_id, $control_settings ) ); 
  63. break; 
  64. case 'select': 
  65. if ( isset( $field['choices'] ) ) { 
  66. $control_settings['choices'] = $field['choices']; 
  67. $wp_customize->add_control( new WP_Customize_Control( $wp_customize, $control_id, $control_settings ) ); 
  68. break; 
  69. case 'message': 
  70. $wp_customize->add_control( new Jetpack_Message_Control( $wp_customize, $control_id, $control_settings ) ); 
  71. break; 
  72.  
  73. // If selective refresh is available, implement it. 
  74. if ( isset( $wp_customize->selective_refresh ) ) { 
  75. $wp_customize->selective_refresh->add_partial( "$this->prefix", array( 
  76. 'selector' => '.jp-relatedposts',  
  77. 'settings' => $selective_options,  
  78. 'render_callback' => __CLASS__ . '::render_callback',  
  79. 'container_inclusive' => false,  
  80. ) ); 
  81.  
  82.  
  83. /** 
  84. * Callback that outputs the headline based on user choice. 
  85. * @since 4.4.0 
  86. */ 
  87. public static function render_callback() { 
  88. echo Jetpack_RelatedPosts::init()->get_headline(); 
  89.  
  90. /** 
  91. * Check that we're in a single post view. 
  92. * @since 4.4.0 
  93. * @return bool 
  94. */ 
  95. public static function is_single() { 
  96. return is_single(); 
  97.  
  98. /** 
  99. * Check that we're not in a single post view. 
  100. * @since 4.4.0 
  101. * @return bool 
  102. */ 
  103. public static function is_not_single() { 
  104. return ! is_single(); 
  105.  
  106. /** 
  107. * Return list of options to modify. 
  108. * @since 4.4.0 
  109. * @param object $wp_customize Instance of WP Customizer 
  110. * @return mixed|void 
  111. */ 
  112. function get_options( $wp_customize ) { 
  113. $transport = isset( $wp_customize->selective_refresh ) ? 'postMessage' : 'refresh'; 
  114.  
  115. // Get the correct translated string for preview in WP 4.7 and later. 
  116. $switched_locale = function_exists( 'switch_to_locale' ) 
  117. ? switch_to_locale( get_user_locale() ) 
  118. : false; 
  119. $headline = __( 'Related', 'jetpack' ); 
  120. if ( $switched_locale ) { 
  121. restore_previous_locale(); 
  122.  
  123. return apply_filters( 
  124. 'jetpack_related_posts_customize_options', array( 
  125. 'enabled' => array( 
  126. 'control_type' => 'hidden',  
  127. 'default' => 1,  
  128. 'setting_type' => 'option',  
  129. 'transport' => $transport,  
  130. ),  
  131. 'show_headline' => array( 
  132. 'label' => esc_html__( 'Show a headline', 'jetpack' ),  
  133. 'description' => esc_html__( 'This helps to clearly separate the related posts from post content.', 'jetpack' ),  
  134. 'control_type' => 'checkbox',  
  135. 'default' => 1,  
  136. 'setting_type' => 'option',  
  137. 'transport' => $transport,  
  138. ),  
  139. 'headline' => array( 
  140. 'label' => '',  
  141. 'description' => esc_html__( 'Enter text to use as headline.', 'jetpack' ),  
  142. 'control_type' => 'text',  
  143. 'default' => esc_html( $headline ),  
  144. 'setting_type' => 'option',  
  145. 'transport' => $transport,  
  146. ),  
  147. 'show_thumbnails' => array( 
  148. 'label' => esc_html__( 'Show thumbnails', 'jetpack' ),  
  149. 'description' => esc_html__( 'Use a large and visually striking layout.', 'jetpack' ),  
  150. 'control_type' => 'checkbox',  
  151. 'default' => 1,  
  152. 'setting_type' => 'option',  
  153. 'transport' => $transport,  
  154. ),  
  155. 'show_date' => array( 
  156. 'label' => esc_html__( 'Show date', 'jetpack' ),  
  157. 'description' => esc_html__( 'Display date when entry was published.', 'jetpack' ),  
  158. 'control_type' => 'checkbox',  
  159. 'default' => 1,  
  160. 'setting_type' => 'option',  
  161. 'transport' => $transport,  
  162. ),  
  163. 'show_context' => array( 
  164. 'label' => esc_html__( 'Show context', 'jetpack' ),  
  165. 'description' => esc_html__( "Display entry's category or tag.", 'jetpack' ),  
  166. 'control_type' => 'checkbox',  
  167. 'default' => 1,  
  168. 'setting_type' => 'option',  
  169. 'transport' => $transport,  
  170. ),  
  171. 'layout' => array( 
  172. 'label' => esc_html__( 'Layout', 'jetpack' ),  
  173. 'description' => esc_html__( 'Arrange entries in different layouts.', 'jetpack' ),  
  174. 'control_type' => 'select',  
  175. 'choices' => array( 
  176. 'grid' => esc_html__( 'Grid', 'jetpack' ),  
  177. 'list' => esc_html__( 'List', 'jetpack' ),  
  178. ),  
  179. 'default' => 'grid',  
  180. 'setting_type' => 'option',  
  181. 'transport' => $transport,  
  182. ),  
  183. 'msg_go_to_single' => array( 
  184. 'description' => esc_html__( 'Please visit a single post view to reveal the customization options.', 'jetpack' ),  
  185. 'control_type' => 'message',  
  186. 'active_callback' => __CLASS__ . '::is_not_single',  
  187. ),  
  188. 'msg_example' => array( 
  189. 'description' => esc_html__( 'Please note that the related posts displayed now are only for previewing purposes.', 'jetpack' ),  
  190. 'control_type' => 'message',  
  191. ),  
  192. ); 
  193.  
  194. /** 
  195. * Enqueue assets for Customizer controls. 
  196. * @since 4.4.0 
  197. */ 
  198. function customize_controls_enqueue_scripts() { 
  199. wp_enqueue_script( 'jetpack_related-posts-customizer', plugins_url( 'related-posts-customizer.js', __FILE__ ), array( 'customize-controls' ), JETPACK__VERSION); 
  200.  
  201. } // class end