customizer.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. /**
  3. * Twenty Fifteen Theme Customizer.
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Fifteen
  7. * @since Twenty Fifteen 1.0
  8. */
  9. /**
  10. * Add postMessage support for site title and description for the Theme Customizer.
  11. *
  12. * @since Twenty Fifteen 1.0
  13. *
  14. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  15. */
  16. function twentyfifteen_customize_register( $wp_customize ) {
  17. $color_scheme = twentyfifteen_get_color_scheme();
  18. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  19. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  20. $wp_customize->get_setting( 'background_color' )->transport = 'refresh';
  21. // Add color scheme setting and control.
  22. $wp_customize->add_setting( 'color_scheme', array(
  23. 'default' => 'default',
  24. 'sanitize_callback' => 'twentyfifteen_sanitize_color_scheme',
  25. ) );
  26. $wp_customize->add_control( new Twentyfifteen_Customize_Color_Scheme_Control( $wp_customize, 'color_scheme', array(
  27. 'label' => esc_html__( 'Color Scheme', 'twentyfifteen' ),
  28. 'section' => 'colors',
  29. 'choices' => twentyfifteen_get_color_scheme_choices(),
  30. 'priority' => 1,
  31. ) ) );
  32. // Add custom sidebar text color setting and control.
  33. $wp_customize->add_setting( 'sidebar_textcolor', array(
  34. 'default' => $color_scheme[4],
  35. 'sanitize_callback' => 'sanitize_hex_color',
  36. ) );
  37. $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'sidebar_textcolor', array(
  38. 'label' => __( 'Sidebar Text Color', 'twentyfifteen' ),
  39. 'section' => 'colors',
  40. ) ) );
  41. // Add custom header background color setting and control.
  42. $wp_customize->add_setting( 'header_background_color', array(
  43. 'default' => $color_scheme[1],
  44. 'sanitize_callback' => 'sanitize_hex_color',
  45. ) );
  46. $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_background_color', array(
  47. 'label' => esc_html__( 'Header & Sidebar Background Color', 'twentyfifteen' ),
  48. 'section' => 'colors',
  49. ) ) );
  50. }
  51. add_action( 'customize_register', 'twentyfifteen_customize_register', 11 );
  52. /**
  53. * Custom control for Color Schemes
  54. *
  55. * @since Twenty Fifteen 1.0
  56. */
  57. function twentyfifteen_customize_color_scheme_control() {
  58. class Twentyfifteen_Customize_Color_Scheme_Control extends WP_Customize_Control {
  59. public $type = 'colorScheme';
  60. function enqueue() {
  61. wp_enqueue_script( 'color-scheme-control', get_template_directory_uri() . '/js/color-scheme-control.js', array( 'customize-controls' ), '', true );
  62. wp_localize_script( 'color-scheme-control', 'colorScheme', twentyfifteen_get_color_schemes() );
  63. }
  64. public function render_content() {
  65. if ( empty( $this->choices ) )
  66. return;
  67. ?>
  68. <label>
  69. <?php if ( ! empty( $this->label ) ) : ?>
  70. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  71. <?php endif;
  72. if ( ! empty( $this->description ) ) : ?>
  73. <span class="description customize-control-description"><?php echo $this->description; ?></span>
  74. <?php endif; ?>
  75. <select <?php $this->link(); ?>>
  76. <?php
  77. foreach ( $this->choices as $value => $label )
  78. echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
  79. ?>
  80. </select>
  81. </label>
  82. <?php
  83. }
  84. }
  85. }
  86. add_action( 'customize_register', 'twentyfifteen_customize_color_scheme_control', 10 );
  87. /**
  88. * Register color schemes for Twenty Fifteen.
  89. * Can be filtered with twentyfifteen_color_schemes.
  90. *
  91. * The order of colors in a colors array:
  92. * 1. Main Background Color.
  93. * 2. Sidebar Background Color.
  94. * 3. Box Background Bolor.
  95. * 4. Main Text and Link Color.
  96. * 5. Sidebar Text and Link Color.
  97. * 6. Meta Box Background Color.
  98. *
  99. * @since Twenty Fifteen 1.0
  100. *
  101. * @return array An associative array of color scheme options.
  102. */
  103. function twentyfifteen_get_color_schemes() {
  104. return apply_filters( 'twentyfifteen_color_schemes', array(
  105. 'default' => array(
  106. 'label' => esc_html__( 'Default', 'twentyfifteen' ),
  107. 'colors' => array(
  108. '#f1f1f1',
  109. '#ffffff',
  110. '#ffffff',
  111. '#333333',
  112. '#333333',
  113. '#f7f7f7',
  114. ),
  115. ),
  116. 'dark' => array(
  117. 'label' => esc_html__( 'Dark', 'twentyfifteen' ),
  118. 'colors' => array(
  119. '#111111',
  120. '#202020',
  121. '#202020',
  122. '#bebebe',
  123. '#bebebe',
  124. '#1b1b1b',
  125. ),
  126. ),
  127. 'yellow' => array(
  128. 'label' => esc_html__( 'Yellow', 'twentyfifteen' ),
  129. 'colors' => array(
  130. '#f4ca16',
  131. '#ffdf00',
  132. '#ffffff',
  133. '#111111',
  134. '#111111',
  135. '#f1f1f1',
  136. ),
  137. ),
  138. 'pink' => array(
  139. 'label' => esc_html__( 'Pink', 'twentyfifteen' ),
  140. 'colors' => array(
  141. '#ffe5d1',
  142. '#e53b51',
  143. '#ffffff',
  144. '#352712',
  145. '#ffffff',
  146. '#f1f1f1',
  147. ),
  148. ),
  149. 'purple' => array(
  150. 'label' => esc_html__( 'Purple', 'twentyfifteen' ),
  151. 'colors' => array(
  152. '#674970',
  153. '#2e2256',
  154. '#ffffff',
  155. '#2e2256',
  156. '#ffffff',
  157. '#f1f1f1',
  158. ),
  159. ),
  160. 'blue' => array(
  161. 'label' => esc_html__( 'Blue', 'twentyfifteen' ),
  162. 'colors' => array(
  163. '#e9f2f9',
  164. '#55c3dc',
  165. '#ffffff',
  166. '#22313f',
  167. '#ffffff',
  168. '#f1f1f1',
  169. ),
  170. ),
  171. ) );
  172. }
  173. if ( ! function_exists( 'twentyfifteen_get_color_scheme' ) ) :
  174. /**
  175. * Returns an array of either the current or default color scheme hex values
  176. *
  177. * @since Twenty Fifteen 1.0
  178. *
  179. * @return array
  180. */
  181. function twentyfifteen_get_color_scheme() {
  182. $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
  183. $color_schemes = twentyfifteen_get_color_schemes();
  184. if ( array_key_exists( $color_scheme_option, $color_schemes ) ) {
  185. return $color_schemes[ $color_scheme_option ]['colors'];
  186. }
  187. return $color_schemes[ 'default' ]['colors'];
  188. }
  189. endif; // twentyfifteen_get_color_scheme
  190. if ( ! function_exists( 'twentyfifteen_get_color_scheme_control_options' ) ) :
  191. /**
  192. * Returns an array of color scheme choices registered for Twenty Fifteen.
  193. *
  194. * @since Twenty Fifteen 1.0
  195. *
  196. * @return array
  197. */
  198. function twentyfifteen_get_color_scheme_choices() {
  199. $color_schemes = twentyfifteen_get_color_schemes();
  200. $color_scheme_control_options = array();
  201. foreach ( $color_schemes as $color_scheme => $value ) {
  202. $color_scheme_control_options[ $color_scheme ] = $value['label'];
  203. }
  204. return $color_scheme_control_options;
  205. }
  206. endif; // twentyfifteen_get_color_scheme_control_options
  207. if ( ! function_exists( 'twentyfifteen_sanitize_color_scheme' ) ) :
  208. /**
  209. * Sanitization callback for color schemes.
  210. *
  211. * @since Twenty Fifteen 1.0
  212. *
  213. * @param string $value Color scheme name value.
  214. *
  215. * @return string Color scheme name.
  216. */
  217. function twentyfifteen_sanitize_color_scheme( $value ) {
  218. $color_schemes = twentyfifteen_get_color_scheme_choices();
  219. if ( ! array_key_exists( $value, $color_schemes ) ) {
  220. $value = 'default';
  221. }
  222. return $value;
  223. }
  224. endif; // twentyfifteen_sanitize_color_scheme
  225. /**
  226. * Enqueues front-end CSS for color scheme.
  227. *
  228. * @since Twenty Fifteen 1.0
  229. */
  230. function twentyfifteen_color_scheme_css() {
  231. $color_scheme_option = get_theme_mod( 'color_scheme', 'default' );
  232. // Don't do anything if the default color scheme is selected.
  233. if ( 'default' === $color_scheme_option ) {
  234. return;
  235. }
  236. // If we get this far, we have custom styles. Let's do this.
  237. $color_scheme = twentyfifteen_get_color_scheme();
  238. // Convert main and sidebar text hex color to rgba.
  239. $color_main_text_rgb = twentyfifteen_hex2rgb( $color_scheme[3] );
  240. $color_sidebar_link_rgb = twentyfifteen_hex2rgb( $color_scheme[4] );
  241. $color_background = $color_scheme[0];
  242. $color_sidebar_background = $color_scheme[1];
  243. $color_box_background = $color_scheme[2];
  244. $color_main_text = $color_scheme[3];
  245. $color_secondary_text = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_main_text_rgb );
  246. $color_border = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_main_text_rgb );
  247. $color_border_focus = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_main_text_rgb );
  248. $color_sidebar_link = $color_scheme[4];
  249. $color_sidebar_text = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.7)', $color_sidebar_link_rgb );
  250. $color_sidebar_border = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.1)', $color_sidebar_link_rgb );
  251. $color_sidebar_border_focus = vsprintf( 'rgba( %1$s, %2$s, %3$s, 0.3)', $color_sidebar_link_rgb );
  252. $color_meta_box = $color_scheme[5];
  253. $css = '
  254. /* Color Scheme */
  255. /* Background Color */
  256. body {
  257. background-color: %1$s;
  258. }
  259. /* Sidebar Background Color */
  260. body:before,
  261. .site-header {
  262. background-color: %2$s;
  263. }
  264. /* Box Background Color */
  265. .post-navigation,
  266. .pagination,
  267. .secondary,
  268. .site-footer,
  269. .hentry,
  270. .page-header,
  271. .page-content,
  272. .comments-area {
  273. background-color: %3$s;
  274. }
  275. /* Box Background Color */
  276. button,
  277. input[type="button"],
  278. input[type="reset"],
  279. input[type="submit"],
  280. .pagination .prev,
  281. .pagination .next,
  282. .pagination .prev:before,
  283. .pagination .next:before,
  284. .entry-content .page-links a,
  285. .entry-content .page-links a:hover,
  286. .entry-content .page-links a:focus,
  287. .sticky-post {
  288. color: %3$s;
  289. }
  290. /* Main Text Color */
  291. button,
  292. input[type="button"],
  293. input[type="reset"],
  294. input[type="submit"],
  295. .pagination .prev,
  296. .pagination .next,
  297. .page-links a,
  298. .sticky-post {
  299. background-color: %4$s;
  300. }
  301. /* Main Text Color */
  302. body,
  303. blockquote cite,
  304. blockquote small,
  305. a,
  306. .image-navigation a:hover,
  307. .image-navigation a:focus,
  308. .comment-navigation a:hover,
  309. .comment-navigation a:focus,
  310. .entry-footer a:hover,
  311. .entry-footer a:focus,
  312. .comment-metadata a:hover,
  313. .comment-metadata a:focus,
  314. .pingback .edit-link a:hover,
  315. .pingback .edit-link a:focus,
  316. .comment-list .reply a:hover,
  317. .comment-list .reply a:focus,
  318. .site-info a:hover,
  319. .site-info a:focus {
  320. color: %4$s;
  321. }
  322. /* Main Text Color */
  323. .entry-content a,
  324. .entry-summary a,
  325. .page-content a,
  326. .comment-content a,
  327. .author-description a,
  328. .comment-list .reply a:hover,
  329. .comment-list .reply a:focus {
  330. border-color: %4$s;
  331. }
  332. /* Secondary Text Color */
  333. button:hover,
  334. button:focus,
  335. input[type="button"]:hover,
  336. input[type="button"]:focus,
  337. input[type="reset"]:hover,
  338. input[type="reset"]:focus,
  339. input[type="submit"]:hover,
  340. input[type="submit"]:focus,
  341. .pagination .prev:hover,
  342. .pagination .prev:focus,
  343. .pagination .next:hover,
  344. .pagination .next:focus,
  345. .page-links a:hover,
  346. .page-links a:focus {
  347. background-color: %4$s; /* Fallback for IE7 and IE8 */
  348. background-color: %5$s;
  349. }
  350. /* Secondary Text Color */
  351. blockquote,
  352. input[type="text"],
  353. input[type="email"],
  354. input[type="url"],
  355. input[type="password"],
  356. input[type="search"],
  357. textarea,
  358. a:hover,
  359. a:focus,
  360. .post-navigation .meta-nav,
  361. .post-navigation a:hover .post-title,
  362. .post-navigation a:focus .post-title,
  363. .image-navigation,
  364. .image-navigation a,
  365. .comment-navigation,
  366. .comment-navigation a,
  367. .author-heading,
  368. .entry-footer,
  369. .entry-footer a,
  370. .taxonomy-description,
  371. .page-links > .page-links-title,
  372. .entry-caption,
  373. .comment-author,
  374. .comment-metadata,
  375. .comment-metadata a,
  376. .pingback .comment-edit-link,
  377. .post-password-form label,
  378. .comment-form label,
  379. .comment-notes,
  380. .comment-awaiting-moderation,
  381. .logged-in-as,
  382. .form-allowed-tags,
  383. .no-comments,
  384. .site-info,
  385. .site-info a,
  386. .wp-caption-text,
  387. .gallery-caption {
  388. color: %4$s; /* Fallback for IE7 and IE8 */
  389. color: %5$s;
  390. }
  391. /* Secondary Text Color */
  392. blockquote,
  393. .entry-content a:hover,
  394. .entry-content a:focus,
  395. .entry-summary a:hover,
  396. .entry-summary a:focus,
  397. .page-content a:hover,
  398. .page-content a:focus,
  399. .comment-content a:hover,
  400. .comment-content a:focus,
  401. .author-description a:hover,
  402. .author-description a:focus {
  403. border-color: %4$s; /* Fallback for IE7 and IE8 */
  404. border-color: %5$s;
  405. }
  406. /* Border Color */
  407. hr {
  408. background-color: %4$s; /* Fallback for IE7 and IE8 */
  409. background-color: %6$s;
  410. }
  411. /* Border Color */
  412. pre,
  413. abbr[title],
  414. table,
  415. th,
  416. td,
  417. input,
  418. textarea,
  419. .post-navigation,
  420. .post-navigation .nav-previous:not(.has-post-thumbnail) + .nav-next:not(.has-post-thumbnail),
  421. .pagination,
  422. .comment-navigation,
  423. .site-header,
  424. .site-footer,
  425. .hentry + .hentry,
  426. .author-info,
  427. .entry-content .page-links a,
  428. .page-links > span,
  429. .page-header,
  430. .comments-area,
  431. .comment-list + .comment-respond,
  432. .comment-list article,
  433. .comment-list .pingback,
  434. .comment-list .trackback,
  435. .comment-list .reply a,
  436. .no-comments {
  437. border-color: %4$s; /* Fallback for IE7 and IE8 */
  438. border-color: %6$s;
  439. }
  440. /* Border Focus Color */
  441. input:focus,
  442. textarea:focus {
  443. border-color: %4$s; /* Fallback for IE7 and IE8 */
  444. border-color: %7$s;
  445. }
  446. /* Sidebar Link Color */
  447. .secondary-toggle:hover {
  448. border-color: %8$s;
  449. }
  450. .secondary-toggle:before {
  451. color: %8$s;
  452. }
  453. .secondary-toggle:focus {
  454. outline-color: %8$s;
  455. }
  456. .site-title a,
  457. .site-description {
  458. color: %8$s;
  459. }
  460. /* Sidebar Text Color */
  461. .site-title a:hover,
  462. .site-title a:focus {
  463. color: %9$s;
  464. }
  465. /* Sidebar Border Color */
  466. .secondary-toggle {
  467. border-color: %8$s; /* Fallback for IE7 and IE8 */
  468. border-color: %10$s;
  469. }
  470. /* Meta Background Color */
  471. .entry-footer {
  472. background-color: %12$s;
  473. }
  474. @media screen and (min-width: 38.75em) {
  475. /* Main Text Color */
  476. .page-header {
  477. border-color: %4$s;
  478. }
  479. }
  480. @media screen and (min-width: 59.6875em) {
  481. /* Make sure its transparent on desktop */
  482. .site-header,
  483. .secondary {
  484. background-color: transparent;
  485. }
  486. /* Sidebar Background Color */
  487. .widget button,
  488. .widget input[type="button"],
  489. .widget input[type="reset"],
  490. .widget input[type="submit"],
  491. .widget_calendar tbody a,
  492. .widget_calendar tbody a:hover,
  493. .widget_calendar tbody a:focus,
  494. .widget mark,
  495. .widget ins {
  496. color: %2$s;
  497. }
  498. /* Sidebar Link Color */
  499. .widget button,
  500. .widget input[type="button"],
  501. .widget input[type="reset"],
  502. .widget input[type="submit"],
  503. .widget_calendar tbody a,
  504. .widget mark,
  505. .widget ins {
  506. background-color: %8$s;
  507. }
  508. .secondary a,
  509. .dropdown-toggle:after,
  510. .widget-title,
  511. .widget blockquote cite,
  512. .widget blockquote small {
  513. color: %8$s;
  514. }
  515. .dropdown-toggle:focus {
  516. outline-color: %8$s;
  517. }
  518. /* Sidebar Text Color */
  519. .secondary a:hover,
  520. .secondary a:focus,
  521. .widget,
  522. .main-navigation .menu-item-description,
  523. .widget blockquote,
  524. .widget .wp-caption-text,
  525. .widget .gallery-caption {
  526. color: %9$s;
  527. }
  528. .dropdown-toggle:hover,
  529. .dropdown-toggle:focus,
  530. .widget button:hover,
  531. .widget button:focus,
  532. .widget input[type="button"]:hover,
  533. .widget input[type="button"]:focus,
  534. .widget input[type="reset"]:hover,
  535. .widget input[type="reset"]:focus,
  536. .widget input[type="submit"]:hover,
  537. .widget input[type="submit"]:focus,
  538. .widget_calendar tbody a:hover,
  539. .widget_calendar tbody a:focus {
  540. background-color: %9$s;
  541. }
  542. .widget blockquote {
  543. border-color: %9$s;
  544. }
  545. /* Sidebar Border Color */
  546. .main-navigation ul,
  547. .main-navigation li,
  548. .widget input,
  549. .widget textarea,
  550. .widget table,
  551. .widget th,
  552. .widget td,
  553. .widget input,
  554. .widget textarea,
  555. .widget pre,
  556. .widget li,
  557. .widget_categories .children,
  558. .widget_nav_menu .sub-menu,
  559. .widget_pages .children,
  560. .widget abbr[title] {
  561. border-color: %10$s;
  562. }
  563. .widget hr {
  564. background-color: %10$s;
  565. }
  566. /* Sidebar Border Focus Color */
  567. .widget input:focus,
  568. .widget textarea:focus {
  569. border-color: %11$s;
  570. }
  571. }
  572. ';
  573. wp_add_inline_style( 'twentyfifteen-style', sprintf( $css,
  574. $color_background,
  575. $color_sidebar_background,
  576. $color_box_background,
  577. $color_main_text,
  578. $color_secondary_text,
  579. $color_border,
  580. $color_border_focus,
  581. $color_sidebar_link,
  582. $color_sidebar_text,
  583. $color_sidebar_border,
  584. $color_sidebar_border_focus,
  585. $color_meta_box
  586. ) );
  587. }
  588. add_action( 'wp_enqueue_scripts', 'twentyfifteen_color_scheme_css' );
  589. /**
  590. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  591. *
  592. * @since Twenty Fifteen 1.0
  593. */
  594. function twentyfifteen_customize_preview_js() {
  595. wp_enqueue_script( 'twentyfifteen-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), '20141005', true );
  596. }
  597. add_action( 'customize_preview_init', 'twentyfifteen_customize_preview_js' );