Нема описа

customizer.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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(
  56. 'jetpack_content_options',
  57. array(
  58. 'title' => esc_html__( 'Content Options', 'jetpack' ),
  59. 'theme_supports' => 'jetpack-content-options',
  60. 'priority' => 100,
  61. )
  62. );
  63. // Add Blog Display option.
  64. if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
  65. if ( 'mixed' === $blog_display ) {
  66. $blog_display_choices = array(
  67. 'content' => esc_html__( 'Full post', 'jetpack' ),
  68. 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
  69. 'mixed' => esc_html__( 'Default', 'jetpack' ),
  70. );
  71. $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' );
  72. } else {
  73. $blog_display_choices = array(
  74. 'content' => esc_html__( 'Full post', 'jetpack' ),
  75. 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
  76. );
  77. $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' );
  78. if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) {
  79. update_option( 'jetpack_content_blog_display', $blog_display );
  80. }
  81. }
  82. $wp_customize->add_setting(
  83. 'jetpack_content_blog_display',
  84. array(
  85. 'default' => $blog_display,
  86. 'type' => 'option',
  87. 'transport' => 'postMessage',
  88. 'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display',
  89. )
  90. );
  91. $wp_customize->add_control(
  92. 'jetpack_content_blog_display',
  93. array(
  94. 'section' => 'jetpack_content_options',
  95. 'label' => esc_html__( 'Blog Display', 'jetpack' ),
  96. 'description' => $blog_display_description,
  97. 'type' => 'radio',
  98. 'choices' => $blog_display_choices,
  99. )
  100. );
  101. }
  102. // Add Author Bio option.
  103. if ( true === $author_bio ) {
  104. $wp_customize->add_setting( 'jetpack_content_author_bio_title' );
  105. $wp_customize->add_control(
  106. new Jetpack_Customize_Control_Title(
  107. $wp_customize,
  108. 'jetpack_content_author_bio_title',
  109. array(
  110. 'section' => 'jetpack_content_options',
  111. 'label' => esc_html__( 'Author Bio', 'jetpack' ),
  112. 'type' => 'title',
  113. )
  114. )
  115. );
  116. $wp_customize->add_setting(
  117. 'jetpack_content_author_bio',
  118. array(
  119. 'default' => $author_bio_default,
  120. 'type' => 'option',
  121. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  122. )
  123. );
  124. $wp_customize->add_control(
  125. 'jetpack_content_author_bio',
  126. array(
  127. 'section' => 'jetpack_content_options',
  128. 'label' => esc_html__( 'Display on single posts', 'jetpack' ),
  129. 'type' => 'checkbox',
  130. )
  131. );
  132. }
  133. // Add Post Details options.
  134. if ( ( ! empty( $post_details ) )
  135. && ( ! empty( $post_details['stylesheet'] ) )
  136. && ( ! empty( $date )
  137. || ! empty( $categories )
  138. || ! empty( $tags )
  139. || ! empty( $author )
  140. || ! empty( $comment ) ) ) {
  141. $wp_customize->add_setting( 'jetpack_content_post_details_title' );
  142. $wp_customize->add_control(
  143. new Jetpack_Customize_Control_Title(
  144. $wp_customize,
  145. 'jetpack_content_post_details_title',
  146. array(
  147. 'section' => 'jetpack_content_options',
  148. 'label' => esc_html__( 'Post Details', 'jetpack' ),
  149. 'type' => 'title',
  150. )
  151. )
  152. );
  153. // Post Details: Date
  154. if ( ! empty( $date ) ) {
  155. $wp_customize->add_setting(
  156. 'jetpack_content_post_details_date',
  157. array(
  158. 'default' => 1,
  159. 'type' => 'option',
  160. 'transport' => 'postMessage',
  161. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  162. )
  163. );
  164. $wp_customize->add_control(
  165. 'jetpack_content_post_details_date',
  166. array(
  167. 'section' => 'jetpack_content_options',
  168. 'label' => esc_html__( 'Display date', 'jetpack' ),
  169. 'type' => 'checkbox',
  170. )
  171. );
  172. }
  173. // Post Details: Categories
  174. if ( ! empty( $categories ) ) {
  175. $wp_customize->add_setting(
  176. 'jetpack_content_post_details_categories',
  177. array(
  178. 'default' => 1,
  179. 'type' => 'option',
  180. 'transport' => 'postMessage',
  181. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  182. )
  183. );
  184. $wp_customize->add_control(
  185. 'jetpack_content_post_details_categories',
  186. array(
  187. 'section' => 'jetpack_content_options',
  188. 'label' => esc_html__( 'Display categories', 'jetpack' ),
  189. 'type' => 'checkbox',
  190. )
  191. );
  192. }
  193. // Post Details: Tags
  194. if ( ! empty( $tags ) ) {
  195. $wp_customize->add_setting(
  196. 'jetpack_content_post_details_tags',
  197. array(
  198. 'default' => 1,
  199. 'type' => 'option',
  200. 'transport' => 'postMessage',
  201. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  202. )
  203. );
  204. $wp_customize->add_control(
  205. 'jetpack_content_post_details_tags',
  206. array(
  207. 'section' => 'jetpack_content_options',
  208. 'label' => esc_html__( 'Display tags', 'jetpack' ),
  209. 'type' => 'checkbox',
  210. )
  211. );
  212. }
  213. // Post Details: Author
  214. if ( ! empty( $author ) ) {
  215. $wp_customize->add_setting(
  216. 'jetpack_content_post_details_author',
  217. array(
  218. 'default' => 1,
  219. 'type' => 'option',
  220. 'transport' => 'postMessage',
  221. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  222. )
  223. );
  224. $wp_customize->add_control(
  225. 'jetpack_content_post_details_author',
  226. array(
  227. 'section' => 'jetpack_content_options',
  228. 'label' => esc_html__( 'Display author', 'jetpack' ),
  229. 'type' => 'checkbox',
  230. )
  231. );
  232. }
  233. // Post Details: Comment link
  234. if ( ! empty( $comment ) ) {
  235. $wp_customize->add_setting(
  236. 'jetpack_content_post_details_comment',
  237. array(
  238. 'default' => 1,
  239. 'type' => 'option',
  240. 'transport' => 'postMessage',
  241. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  242. )
  243. );
  244. $wp_customize->add_control(
  245. 'jetpack_content_post_details_comment',
  246. array(
  247. 'section' => 'jetpack_content_options',
  248. 'label' => esc_html__( 'Display comment link', 'jetpack' ),
  249. 'type' => 'checkbox',
  250. )
  251. );
  252. }
  253. }
  254. // Add Featured Images options.
  255. if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) {
  256. $wp_customize->add_setting( 'jetpack_content_featured_images_title' );
  257. $wp_customize->add_control(
  258. new Jetpack_Customize_Control_Title(
  259. $wp_customize,
  260. 'jetpack_content_featured_images_title',
  261. array(
  262. 'section' => 'jetpack_content_options',
  263. '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' ) ),
  264. 'type' => 'title',
  265. 'active_callback' => 'jetpack_post_thumbnail_supports',
  266. )
  267. )
  268. );
  269. // Featured Images: Archive
  270. if ( true === $fi_archive ) {
  271. $wp_customize->add_setting(
  272. 'jetpack_content_featured_images_archive',
  273. array(
  274. 'default' => $fi_archive_default,
  275. 'type' => 'option',
  276. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  277. )
  278. );
  279. $wp_customize->add_control(
  280. 'jetpack_content_featured_images_archive',
  281. array(
  282. 'section' => 'jetpack_content_options',
  283. 'label' => esc_html__( 'Display on blog and archives', 'jetpack' ),
  284. 'type' => 'checkbox',
  285. 'active_callback' => 'jetpack_post_thumbnail_supports',
  286. )
  287. );
  288. }
  289. // Featured Images: Post
  290. if ( true === $fi_post ) {
  291. $wp_customize->add_setting(
  292. 'jetpack_content_featured_images_post',
  293. array(
  294. 'default' => $fi_post_default,
  295. 'type' => 'option',
  296. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  297. )
  298. );
  299. $wp_customize->add_control(
  300. 'jetpack_content_featured_images_post',
  301. array(
  302. 'section' => 'jetpack_content_options',
  303. 'label' => esc_html__( 'Display on single posts', 'jetpack' ),
  304. 'type' => 'checkbox',
  305. 'active_callback' => 'jetpack_post_thumbnail_supports',
  306. )
  307. );
  308. }
  309. // Featured Images: Page
  310. if ( true === $fi_page ) {
  311. $wp_customize->add_setting(
  312. 'jetpack_content_featured_images_page',
  313. array(
  314. 'default' => $fi_page_default,
  315. 'type' => 'option',
  316. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  317. )
  318. );
  319. $wp_customize->add_control(
  320. 'jetpack_content_featured_images_page',
  321. array(
  322. 'section' => 'jetpack_content_options',
  323. 'label' => esc_html__( 'Display on pages', 'jetpack' ),
  324. 'type' => 'checkbox',
  325. 'active_callback' => 'jetpack_post_thumbnail_supports',
  326. )
  327. );
  328. }
  329. // Featured Images: Portfolio
  330. if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) {
  331. $wp_customize->add_setting(
  332. 'jetpack_content_featured_images_portfolio',
  333. array(
  334. 'default' => $fi_portfolio_default,
  335. 'type' => 'option',
  336. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  337. )
  338. );
  339. $wp_customize->add_control(
  340. 'jetpack_content_featured_images_portfolio',
  341. array(
  342. 'section' => 'jetpack_content_options',
  343. 'label' => esc_html__( 'Display on single projects', 'jetpack' ),
  344. 'type' => 'checkbox',
  345. 'active_callback' => 'jetpack_post_thumbnail_supports',
  346. )
  347. );
  348. }
  349. // Featured Images: Fallback
  350. if ( true === $fi_fallback ) {
  351. $wp_customize->add_setting(
  352. 'jetpack_content_featured_images_fallback',
  353. array(
  354. 'default' => $fi_fallback_default,
  355. 'type' => 'option',
  356. 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
  357. )
  358. );
  359. $wp_customize->add_control(
  360. 'jetpack_content_featured_images_fallback',
  361. array(
  362. 'section' => 'jetpack_content_options',
  363. 'label' => esc_html__( 'Automatically use first image in post', 'jetpack' ),
  364. 'type' => 'checkbox',
  365. 'active_callback' => 'jetpack_post_thumbnail_supports',
  366. )
  367. );
  368. }
  369. }
  370. }
  371. add_action( 'customize_register', 'jetpack_content_options_customize_register' );
  372. /**
  373. * Return whether the theme supports Post Thumbnails.
  374. */
  375. function jetpack_post_thumbnail_supports() {
  376. return ( current_theme_supports( 'post-thumbnails' ) );
  377. }
  378. /**
  379. * Sanitize the checkbox.
  380. *
  381. * @param int $input.
  382. * @return boolean|string
  383. */
  384. function jetpack_content_options_sanitize_checkbox( $input ) {
  385. return ( 1 == $input ) ? 1 : '';
  386. }
  387. /**
  388. * Sanitize the Display value.
  389. *
  390. * @param string $display.
  391. * @return string.
  392. */
  393. function jetpack_content_options_sanitize_blog_display( $display ) {
  394. if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ) ) ) {
  395. $display = 'content';
  396. }
  397. return $display;
  398. }
  399. /**
  400. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  401. */
  402. function jetpack_content_options_customize_preview_js() {
  403. $options = get_theme_support( 'jetpack-content-options' );
  404. $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
  405. $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
  406. sort( $blog_display );
  407. $blog_display = implode( ', ', $blog_display );
  408. $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
  409. $masonry = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null;
  410. $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
  411. $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
  412. $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
  413. $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
  414. $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
  415. $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
  416. wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'customize-preview' ), '1.0', true );
  417. wp_localize_script(
  418. 'jetpack-content-options-customizer',
  419. 'blogDisplay',
  420. array(
  421. 'display' => get_option( 'jetpack_content_blog_display', $blog_display ),
  422. 'masonry' => $masonry,
  423. )
  424. );
  425. wp_localize_script(
  426. 'jetpack-content-options-customizer',
  427. 'postDetails',
  428. array(
  429. 'date' => $date,
  430. 'categories' => $categories,
  431. 'tags' => $tags,
  432. 'author' => $author,
  433. 'comment' => $comment,
  434. )
  435. );
  436. }
  437. add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' );