class.related-posts-customize.php 8.0 KB

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