customizer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * Add Content section to the Theme Customizer.
  4. *
  5. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  6. */
  7. function jetpack_content_options_customize_register( $wp_customize ) {
  8. $options = get_theme_support( 'jetpack-content-options' );
  9. $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
  10. $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
  11. sort( $blog_display );
  12. $blog_display = implode( ', ', $blog_display );
  13. $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
  14. $author_bio = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null;
  15. $author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1;
  16. $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
  17. $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
  18. $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
  19. $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
  20. $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
  21. $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
  22. $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
  23. $fi_archive = ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null;
  24. $fi_post = ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null;
  25. $fi_page = ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null;
  26. $fi_portfolio = ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null;
  27. $fi_fallback = ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null;
  28. $fi_archive_default = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1;
  29. $fi_post_default = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1;
  30. $fi_page_default = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1;
  31. $fi_portfolio_default = ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1;
  32. $fi_fallback_default = ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1;
  33. // If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', 'jetpack-content-options[ 'author-bio' ]', 'jetpack-content-options[ 'post-details' ]' and 'jetpack-content-options[ 'featured-images' ]', don't continue.
  34. if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) )
  35. && ( true !== $author_bio )
  36. && ( ( empty( $post_details['stylesheet'] ) )
  37. && ( empty( $date )
  38. || empty( $categories )
  39. || empty( $tags )
  40. || empty( $author )
  41. || empty( $comment ) ) )
  42. && ( true !== $fi_archive && true !== $fi_post && true !== $fi_page && true !== $fi_portfolio && true !== $fi_fallback ) ) {
  43. return;
  44. }
  45. // New control type: Title.
  46. class Jetpack_Customize_Control_Title extends WP_Customize_Control {
  47. public $type = 'title';
  48. public function render_content() {
  49. ?>
  50. <span class="customize-control-title"><?php echo wp_kses_post( $this->label ); ?></span>
  51. <?php
  52. }
  53. }
  54. // Add Content section.
  55. $wp_customize->add_section( 'jetpack_content_options', array(
  56. 'title' => esc_html__( 'Content Options', 'jetpack' ),
  57. 'theme_supports' => 'jetpack-content-options',
  58. 'priority' => 100,
  59. ) );
  60. // Add Blog Display option.
  61. if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
  62. if ( 'mixed' === $blog_display ) {
  63. $blog_display_choices = array(
  64. 'content' => esc_html__( 'Full post', 'jetpack' ),
  65. 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
  66. 'mixed' => esc_html__( 'Default', 'jetpack' ),
  67. );
  68. $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages, or opt for the theme\'s default combination of excerpt and full post.', 'jetpack' );
  69. } else {
  70. $blog_display_choices = array(
  71. 'content' => esc_html__( 'Full post', 'jetpack' ),
  72. 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
  73. );
  74. $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' );
  75. if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) {
  76. update_option( 'jetpack_content_blog_display', $blog_display );
  77. }
  78. }
  79. $wp_customize->add_setting( 'jetpack_content_blog_display', array(
  80. 'default' => $blog_display,
  81. 'type' => 'option',
  82. 'transport' => 'postMessage',
  83. 'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display',
  84. ) );
  85. $wp_customize->add_control( 'jetpack_content_blog_display', array(
  86. 'section' => 'jetpack_content_options',
  87. 'label' => esc_html__( 'Blog Display', 'jetpack' ),
  88. 'description' => $blog_display_description,
  89. 'type' => 'radio',
  90. 'choices' => $blog_display_choices,
  91. ) );
  92. }
  93. // Add Author Bio option.
  94. if ( true === $author_bio ) {
  95. $wp_customize->add_setting( 'jetpack_content_author_bio_title' );
  96. $wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_author_bio_title', array(
  97. 'section' => 'jetpack_content_options',
  98. 'label' => esc_html__( 'Author Bio', 'jetpack' ),
  99. 'type' => 'title',
  100. ) ) );
  101. $wp_customize->add_setting( 'jetpack_content_author_bio', array(
  102. 'default' => $author_bio_default,
  103. 'type' => 'option',
  104. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  105. ) );
  106. $wp_customize->add_control( 'jetpack_content_author_bio', array(
  107. 'section' => 'jetpack_content_options',
  108. 'label' => esc_html__( 'Display on single posts', 'jetpack' ),
  109. 'type' => 'checkbox',
  110. ) );
  111. }
  112. // Add Post Details options.
  113. if ( ( ! empty( $post_details ) )
  114. && ( ! empty( $post_details['stylesheet'] ) )
  115. && ( ! empty( $date )
  116. || ! empty( $categories )
  117. || ! empty( $tags )
  118. || ! empty( $author )
  119. || ! empty( $comment ) ) ) {
  120. $wp_customize->add_setting( 'jetpack_content_post_details_title' );
  121. $wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_post_details_title', array(
  122. 'section' => 'jetpack_content_options',
  123. 'label' => esc_html__( 'Post Details', 'jetpack' ),
  124. 'type' => 'title',
  125. ) ) );
  126. // Post Details: Date
  127. if ( ! empty( $date ) ) {
  128. $wp_customize->add_setting( 'jetpack_content_post_details_date', array(
  129. 'default' => 1,
  130. 'type' => 'option',
  131. 'transport' => 'postMessage',
  132. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  133. ) );
  134. $wp_customize->add_control( 'jetpack_content_post_details_date', array(
  135. 'section' => 'jetpack_content_options',
  136. 'label' => esc_html__( 'Display date', 'jetpack' ),
  137. 'type' => 'checkbox',
  138. ) );
  139. }
  140. // Post Details: Categories
  141. if ( ! empty( $categories ) ) {
  142. $wp_customize->add_setting( 'jetpack_content_post_details_categories', array(
  143. 'default' => 1,
  144. 'type' => 'option',
  145. 'transport' => 'postMessage',
  146. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  147. ) );
  148. $wp_customize->add_control( 'jetpack_content_post_details_categories', array(
  149. 'section' => 'jetpack_content_options',
  150. 'label' => esc_html__( 'Display categories', 'jetpack' ),
  151. 'type' => 'checkbox',
  152. ) );
  153. }
  154. // Post Details: Tags
  155. if ( ! empty( $tags ) ) {
  156. $wp_customize->add_setting( 'jetpack_content_post_details_tags', array(
  157. 'default' => 1,
  158. 'type' => 'option',
  159. 'transport' => 'postMessage',
  160. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  161. ) );
  162. $wp_customize->add_control( 'jetpack_content_post_details_tags', array(
  163. 'section' => 'jetpack_content_options',
  164. 'label' => esc_html__( 'Display tags', 'jetpack' ),
  165. 'type' => 'checkbox',
  166. ) );
  167. }
  168. // Post Details: Author
  169. if ( ! empty( $author ) ) {
  170. $wp_customize->add_setting( 'jetpack_content_post_details_author', array(
  171. 'default' => 1,
  172. 'type' => 'option',
  173. 'transport' => 'postMessage',
  174. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  175. ) );
  176. $wp_customize->add_control( 'jetpack_content_post_details_author', array(
  177. 'section' => 'jetpack_content_options',
  178. 'label' => esc_html__( 'Display author', 'jetpack' ),
  179. 'type' => 'checkbox',
  180. ) );
  181. }
  182. // Post Details: Comment link
  183. if ( ! empty( $comment ) ) {
  184. $wp_customize->add_setting( 'jetpack_content_post_details_comment', array(
  185. 'default' => 1,
  186. 'type' => 'option',
  187. 'transport' => 'postMessage',
  188. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  189. ) );
  190. $wp_customize->add_control( 'jetpack_content_post_details_comment', array(
  191. 'section' => 'jetpack_content_options',
  192. 'label' => esc_html__( 'Display comment link', 'jetpack' ),
  193. 'type' => 'checkbox',
  194. ) );
  195. }
  196. }
  197. // Add Featured Images options.
  198. if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) {
  199. $wp_customize->add_setting( 'jetpack_content_featured_images_title' );
  200. $wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_featured_images_title', array(
  201. 'section' => 'jetpack_content_options',
  202. 'label' => esc_html__( 'Featured Images', 'jetpack' ) . sprintf( '<a href="https://en.support.wordpress.com/featured-images/" class="customize-help-toggle dashicons dashicons-editor-help" title="%1$s" rel="noopener noreferrer" target="_blank"><span class="screen-reader-text">%1$s</span></a>', esc_html__( 'Learn more about Featured Images', 'jetpack' ) ),
  203. 'type' => 'title',
  204. 'active_callback' => 'jetpack_post_thumbnail_supports',
  205. ) ) );
  206. // Featured Images: Archive
  207. if ( true === $fi_archive ) {
  208. $wp_customize->add_setting( 'jetpack_content_featured_images_archive', array(
  209. 'default' => $fi_archive_default,
  210. 'type' => 'option',
  211. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  212. ) );
  213. $wp_customize->add_control( 'jetpack_content_featured_images_archive', array(
  214. 'section' => 'jetpack_content_options',
  215. 'label' => esc_html__( 'Display on blog and archives', 'jetpack' ),
  216. 'type' => 'checkbox',
  217. 'active_callback' => 'jetpack_post_thumbnail_supports',
  218. ) );
  219. }
  220. // Featured Images: Post
  221. if ( true === $fi_post ) {
  222. $wp_customize->add_setting( 'jetpack_content_featured_images_post', array(
  223. 'default' => $fi_post_default,
  224. 'type' => 'option',
  225. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  226. ) );
  227. $wp_customize->add_control( 'jetpack_content_featured_images_post', array(
  228. 'section' => 'jetpack_content_options',
  229. 'label' => esc_html__( 'Display on single posts', 'jetpack' ),
  230. 'type' => 'checkbox',
  231. 'active_callback' => 'jetpack_post_thumbnail_supports',
  232. ) );
  233. }
  234. // Featured Images: Page
  235. if ( true === $fi_page ) {
  236. $wp_customize->add_setting( 'jetpack_content_featured_images_page', array(
  237. 'default' => $fi_page_default,
  238. 'type' => 'option',
  239. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  240. ) );
  241. $wp_customize->add_control( 'jetpack_content_featured_images_page', array(
  242. 'section' => 'jetpack_content_options',
  243. 'label' => esc_html__( 'Display on pages', 'jetpack' ),
  244. 'type' => 'checkbox',
  245. 'active_callback' => 'jetpack_post_thumbnail_supports',
  246. ) );
  247. }
  248. // Featured Images: Portfolio
  249. if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) {
  250. $wp_customize->add_setting( 'jetpack_content_featured_images_portfolio', array(
  251. 'default' => $fi_portfolio_default,
  252. 'type' => 'option',
  253. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  254. ) );
  255. $wp_customize->add_control( 'jetpack_content_featured_images_portfolio', array(
  256. 'section' => 'jetpack_content_options',
  257. 'label' => esc_html__( 'Display on single projects', 'jetpack' ),
  258. 'type' => 'checkbox',
  259. 'active_callback' => 'jetpack_post_thumbnail_supports',
  260. ) );
  261. }
  262. // Featured Images: Fallback
  263. if ( true === $fi_fallback ) {
  264. $wp_customize->add_setting( 'jetpack_content_featured_images_fallback', array(
  265. 'default' => $fi_fallback_default,
  266. 'type' => 'option',
  267. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  268. ) );
  269. $wp_customize->add_control( 'jetpack_content_featured_images_fallback', array(
  270. 'section' => 'jetpack_content_options',
  271. 'label' => esc_html__( 'Automatically use first image in post', 'jetpack' ),
  272. 'type' => 'checkbox',
  273. 'active_callback' => 'jetpack_post_thumbnail_supports',
  274. ) );
  275. }
  276. }
  277. }
  278. add_action( 'customize_register', 'jetpack_content_options_customize_register' );
  279. /**
  280. * Return whether the theme supports Post Thumbnails.
  281. */
  282. function jetpack_post_thumbnail_supports() {
  283. return ( current_theme_supports( 'post-thumbnails' ) );
  284. }
  285. /**
  286. * Sanitize the checkbox.
  287. *
  288. * @param int $input.
  289. * @return boolean|string
  290. */
  291. function jetpack_content_options_sanitize_checkbox( $input ) {
  292. return ( 1 == $input ) ? 1 : '';
  293. }
  294. /**
  295. * Sanitize the Display value.
  296. *
  297. * @param string $display.
  298. * @return string.
  299. */
  300. function jetpack_content_options_sanitize_blog_display( $display ) {
  301. if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ) ) ) {
  302. $display = 'content';
  303. }
  304. return $display;
  305. }
  306. /**
  307. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  308. */
  309. function jetpack_content_options_customize_preview_js() {
  310. $options = get_theme_support( 'jetpack-content-options' );
  311. $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
  312. $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
  313. sort( $blog_display );
  314. $blog_display = implode( ', ', $blog_display );
  315. $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
  316. $masonry = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null;
  317. $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
  318. $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
  319. $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
  320. $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
  321. $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
  322. $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
  323. wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'customize-preview' ), '1.0', true );
  324. wp_localize_script( 'jetpack-content-options-customizer', 'blogDisplay', array(
  325. 'display' => get_option( 'jetpack_content_blog_display', $blog_display ),
  326. 'masonry' => $masonry,
  327. ) );
  328. wp_localize_script( 'jetpack-content-options-customizer', 'postDetails', array(
  329. 'date' => $date,
  330. 'categories' => $categories,
  331. 'tags' => $tags,
  332. 'author' => $author,
  333. 'comment' => $comment,
  334. ) );
  335. }
  336. add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' );