Açıklama Yok

class.related-posts-customize.php 8.6KB

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