customizer.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * Pique Theme Customizer
  4. *
  5. * @package Pique
  6. */
  7. /**
  8. * Add postMessage support for site title and description for the Theme Customizer.
  9. *
  10. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  11. */
  12. function pique_customize_register( $wp_customize ) {
  13. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  14. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  15. $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
  16. /**
  17. * Add the Theme Options section
  18. */
  19. $wp_customize->add_panel( 'pique_options_panel', array(
  20. 'title' => __( 'Theme Options', 'pique' ),
  21. 'description' => __( 'Configure your theme settings', 'pique' ),
  22. ) );
  23. // General settings
  24. $wp_customize->add_section( 'pique_general_settings', array(
  25. 'title' => esc_html__( 'Menu Settings', 'pique' ),
  26. 'panel' => 'pique_options_panel',
  27. 'description' => __( 'This replaces your custom menu&mdash;on the front page only&mdash;with an automatically-generated menu that links to each of your panels.', 'pique' ),
  28. ) );
  29. $wp_customize->add_setting( 'pique_menu', array(
  30. 'default' => false,
  31. 'sanitize_callback' => 'pique_sanitize_checkbox',
  32. ) );
  33. $wp_customize->add_control( 'pique_menu', array(
  34. 'label' => esc_html__( 'Add an anchor menu to the front page.', 'pique' ),
  35. 'section' => 'pique_general_settings',
  36. 'type' => 'checkbox',
  37. ) );
  38. // Panel 1
  39. $wp_customize->add_section( 'pique_panel1', array(
  40. 'title' => esc_html__( 'Panel 1', 'pique' ),
  41. 'active_callback' => 'is_front_page',
  42. 'panel' => 'pique_options_panel',
  43. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  44. ) );
  45. $wp_customize->add_setting( 'pique_panel1', array(
  46. 'default' => false,
  47. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  48. ) );
  49. $wp_customize->add_control( 'pique_panel1', array(
  50. 'label' => esc_html__( 'Panel Content', 'pique' ),
  51. 'section' => 'pique_panel1',
  52. 'type' => 'dropdown-pages',
  53. ) );
  54. $wp_customize->add_setting( 'pique_panel1_background', array(
  55. 'default' => 'default',
  56. 'sanitize_callback' => 'sanitize_hex_color',
  57. 'transport' => 'postMessage',
  58. ) );
  59. $wp_customize->add_control( 'pique_panel1_background', array(
  60. 'label' => esc_html__( 'Background Color', 'pique' ),
  61. 'section' => 'pique_panel1',
  62. 'type' => 'color',
  63. ) );
  64. $wp_customize->add_setting( 'pique_panel1_opacity', array(
  65. 'default' => 'default',
  66. 'sanitize_callback' => 'pique_sanitize_opacity',
  67. 'transport' => 'postMessage',
  68. ) );
  69. $wp_customize->add_control( 'pique_panel1_opacity', array(
  70. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  71. 'section' => 'pique_panel1',
  72. 'type' => 'select',
  73. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  74. 'choices' => array(
  75. '0.25' => esc_html__( '25%', 'pique' ),
  76. '0.5' => esc_html__( '50%', 'pique' ),
  77. '0.75' => esc_html__( '75%', 'pique' ),
  78. '1' => esc_html__( '100%', 'pique' ),
  79. ),
  80. ) );
  81. // Panel 2
  82. $wp_customize->add_section( 'pique_panel2', array(
  83. 'title' => esc_html__( 'Panel 2', 'pique' ),
  84. 'active_callback' => 'is_front_page',
  85. 'panel' => 'pique_options_panel',
  86. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  87. ) );
  88. $wp_customize->add_setting( 'pique_panel2', array(
  89. 'default' => false,
  90. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  91. ) );
  92. $wp_customize->add_control( 'pique_panel2', array(
  93. 'label' => esc_html__( 'Panel Content', 'pique' ),
  94. 'section' => 'pique_panel2',
  95. 'type' => 'dropdown-pages',
  96. ) );
  97. $wp_customize->add_setting( 'pique_panel2_background', array(
  98. 'default' => 'default',
  99. 'sanitize_callback' => 'sanitize_hex_color',
  100. 'transport' => 'postMessage',
  101. ) );
  102. $wp_customize->add_control( 'pique_panel2_background', array(
  103. 'label' => esc_html__( 'Background Color', 'pique' ),
  104. 'section' => 'pique_panel2',
  105. 'type' => 'color',
  106. ) );
  107. $wp_customize->add_setting( 'pique_panel2_opacity', array(
  108. 'default' => 'default',
  109. 'sanitize_callback' => 'pique_sanitize_opacity',
  110. 'transport' => 'postMessage',
  111. ) );
  112. $wp_customize->add_control( 'pique_panel2_opacity', array(
  113. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  114. 'section' => 'pique_panel2',
  115. 'type' => 'select',
  116. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  117. 'choices' => array(
  118. '0.25' => esc_html__( '25%', 'pique' ),
  119. '0.5' => esc_html__( '50%', 'pique' ),
  120. '0.75' => esc_html__( '75%', 'pique' ),
  121. '1' => esc_html__( '100%', 'pique' ),
  122. ),
  123. ) );
  124. // Panel 3
  125. $wp_customize->add_section( 'pique_panel3', array(
  126. 'title' => esc_html__( 'Panel 3', 'pique' ),
  127. 'active_callback' => 'is_front_page',
  128. 'panel' => 'pique_options_panel',
  129. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  130. ) );
  131. $wp_customize->add_setting( 'pique_panel3', array(
  132. 'default' => false,
  133. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  134. ) );
  135. $wp_customize->add_control( 'pique_panel3', array(
  136. 'label' => esc_html__( 'Panel Content', 'pique' ),
  137. 'section' => 'pique_panel3',
  138. 'type' => 'dropdown-pages',
  139. ) );
  140. $wp_customize->add_setting( 'pique_panel3_background', array(
  141. 'default' => 'default',
  142. 'sanitize_callback' => 'sanitize_hex_color',
  143. 'transport' => 'postMessage',
  144. ) );
  145. $wp_customize->add_control( 'pique_panel3_background', array(
  146. 'label' => esc_html__( 'Background Color', 'pique' ),
  147. 'section' => 'pique_panel3',
  148. 'type' => 'color',
  149. ) );
  150. $wp_customize->add_setting( 'pique_panel3_opacity', array(
  151. 'default' => 'default',
  152. 'sanitize_callback' => 'pique_sanitize_opacity',
  153. 'transport' => 'postMessage',
  154. ) );
  155. $wp_customize->add_control( 'pique_panel3_opacity', array(
  156. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  157. 'section' => 'pique_panel3',
  158. 'type' => 'select',
  159. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  160. 'choices' => array(
  161. '0.25' => esc_html__( '25%', 'pique' ),
  162. '0.5' => esc_html__( '50%', 'pique' ),
  163. '0.75' => esc_html__( '75%', 'pique' ),
  164. '1' => esc_html__( '100%', 'pique' ),
  165. ),
  166. ) );
  167. // Panel 4
  168. $wp_customize->add_section( 'pique_panel4', array(
  169. 'title' => esc_html__( 'Panel 4', 'pique' ),
  170. 'active_callback' => 'is_front_page',
  171. 'panel' => 'pique_options_panel',
  172. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  173. ) );
  174. $wp_customize->add_setting( 'pique_panel4', array(
  175. 'default' => false,
  176. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  177. ) );
  178. $wp_customize->add_control( 'pique_panel4', array(
  179. 'label' => esc_html__( 'Panel Content', 'pique' ),
  180. 'section' => 'pique_panel4',
  181. 'type' => 'dropdown-pages',
  182. ) );
  183. $wp_customize->add_setting( 'pique_panel4_background', array(
  184. 'default' => 'default',
  185. 'sanitize_callback' => 'sanitize_hex_color',
  186. 'transport' => 'postMessage',
  187. ) );
  188. $wp_customize->add_control( 'pique_panel4_background', array(
  189. 'label' => esc_html__( 'Background Color', 'pique' ),
  190. 'section' => 'pique_panel4',
  191. 'type' => 'color',
  192. ) );
  193. $wp_customize->add_setting( 'pique_panel4_opacity', array(
  194. 'default' => 'default',
  195. 'sanitize_callback' => 'pique_sanitize_opacity',
  196. 'transport' => 'postMessage',
  197. ) );
  198. $wp_customize->add_control( 'pique_panel4_opacity', array(
  199. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  200. 'section' => 'pique_panel4',
  201. 'type' => 'select',
  202. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  203. 'choices' => array(
  204. '0.25' => esc_html__( '25%', 'pique' ),
  205. '0.5' => esc_html__( '50%', 'pique' ),
  206. '0.75' => esc_html__( '75%', 'pique' ),
  207. '1' => esc_html__( '100%', 'pique' ),
  208. ),
  209. ) );
  210. // Panel 5
  211. $wp_customize->add_section( 'pique_panel5', array(
  212. 'title' => esc_html__( 'Panel 5', 'pique' ),
  213. 'active_callback' => 'is_front_page',
  214. 'panel' => 'pique_options_panel',
  215. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  216. ) );
  217. $wp_customize->add_setting( 'pique_panel5', array(
  218. 'default' => false,
  219. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  220. ) );
  221. $wp_customize->add_control( 'pique_panel5', array(
  222. 'label' => esc_html__( 'Panel Content', 'pique' ),
  223. 'section' => 'pique_panel5',
  224. 'type' => 'dropdown-pages',
  225. ) );
  226. $wp_customize->add_setting( 'pique_panel5_background', array(
  227. 'default' => 'default',
  228. 'sanitize_callback' => 'sanitize_hex_color',
  229. 'transport' => 'postMessage',
  230. ) );
  231. $wp_customize->add_control( 'pique_panel5_background', array(
  232. 'label' => esc_html__( 'Background Color', 'pique' ),
  233. 'section' => 'pique_panel5',
  234. 'type' => 'color',
  235. ) );
  236. $wp_customize->add_setting( 'pique_panel5_opacity', array(
  237. 'default' => 'default',
  238. 'sanitize_callback' => 'pique_sanitize_opacity',
  239. 'transport' => 'postMessage',
  240. ) );
  241. $wp_customize->add_control( 'pique_panel5_opacity', array(
  242. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  243. 'section' => 'pique_panel5',
  244. 'type' => 'select',
  245. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  246. 'choices' => array(
  247. '0.25' => esc_html__( '25%', 'pique' ),
  248. '0.5' => esc_html__( '50%', 'pique' ),
  249. '0.75' => esc_html__( '75%', 'pique' ),
  250. '1' => esc_html__( '100%', 'pique' ),
  251. ),
  252. ) );
  253. // Panel 6
  254. $wp_customize->add_section( 'pique_panel6', array(
  255. 'title' => esc_html__( 'Panel 6', 'pique' ),
  256. 'active_callback' => 'is_front_page',
  257. 'panel' => 'pique_options_panel',
  258. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  259. ) );
  260. $wp_customize->add_setting( 'pique_panel6', array(
  261. 'default' => false,
  262. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  263. ) );
  264. $wp_customize->add_control( 'pique_panel6', array(
  265. 'label' => esc_html__( 'Panel Content', 'pique' ),
  266. 'section' => 'pique_panel6',
  267. 'type' => 'dropdown-pages',
  268. ) );
  269. $wp_customize->add_setting( 'pique_panel6_background', array(
  270. 'default' => 'default',
  271. 'sanitize_callback' => 'sanitize_hex_color',
  272. 'transport' => 'postMessage',
  273. ) );
  274. $wp_customize->add_control( 'pique_panel6_background', array(
  275. 'label' => esc_html__( 'Background Color', 'pique' ),
  276. 'section' => 'pique_panel6',
  277. 'type' => 'color',
  278. ) );
  279. $wp_customize->add_setting( 'pique_panel6_opacity', array(
  280. 'default' => 'default',
  281. 'sanitize_callback' => 'pique_sanitize_opacity',
  282. 'transport' => 'postMessage',
  283. ) );
  284. $wp_customize->add_control( 'pique_panel6_opacity', array(
  285. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  286. 'section' => 'pique_panel6',
  287. 'type' => 'select',
  288. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  289. 'choices' => array(
  290. '0.25' => esc_html__( '25%', 'pique' ),
  291. '0.5' => esc_html__( '50%', 'pique' ),
  292. '0.75' => esc_html__( '75%', 'pique' ),
  293. '1' => esc_html__( '100%', 'pique' ),
  294. ),
  295. ) );
  296. // Panel 7
  297. $wp_customize->add_section( 'pique_panel7', array(
  298. 'title' => esc_html__( 'Panel 7', 'pique' ),
  299. 'active_callback' => 'is_front_page',
  300. 'panel' => 'pique_options_panel',
  301. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  302. ) );
  303. $wp_customize->add_setting( 'pique_panel7', array(
  304. 'default' => false,
  305. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  306. ) );
  307. $wp_customize->add_control( 'pique_panel7', array(
  308. 'label' => esc_html__( 'Panel Content', 'pique' ),
  309. 'section' => 'pique_panel7',
  310. 'type' => 'dropdown-pages',
  311. ) );
  312. $wp_customize->add_setting( 'pique_panel7_background', array(
  313. 'default' => 'default',
  314. 'sanitize_callback' => 'sanitize_hex_color',
  315. 'transport' => 'postMessage',
  316. ) );
  317. $wp_customize->add_control( 'pique_panel7_background', array(
  318. 'label' => esc_html__( 'Background Color', 'pique' ),
  319. 'section' => 'pique_panel7',
  320. 'type' => 'color',
  321. ) );
  322. $wp_customize->add_setting( 'pique_panel7_opacity', array(
  323. 'default' => 'default',
  324. 'sanitize_callback' => 'pique_sanitize_opacity',
  325. 'transport' => 'postMessage',
  326. ) );
  327. $wp_customize->add_control( 'pique_panel7_opacity', array(
  328. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  329. 'section' => 'pique_panel7',
  330. 'type' => 'select',
  331. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  332. 'choices' => array(
  333. '0.25' => esc_html__( '25%', 'pique' ),
  334. '0.5' => esc_html__( '50%', 'pique' ),
  335. '0.75' => esc_html__( '75%', 'pique' ),
  336. '1' => esc_html__( '100%', 'pique' ),
  337. ),
  338. ) );
  339. // Panel 8
  340. $wp_customize->add_section( 'pique_panel8', array(
  341. 'title' => esc_html__( 'Panel 8', 'pique' ),
  342. 'active_callback' => 'is_front_page',
  343. 'panel' => 'pique_options_panel',
  344. 'description' => __( 'Add a background image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'pique' ),
  345. ) );
  346. $wp_customize->add_setting( 'pique_panel8', array(
  347. 'default' => false,
  348. 'sanitize_callback' => 'pique_sanitize_numeric_value',
  349. ) );
  350. $wp_customize->add_control( 'pique_panel8', array(
  351. 'label' => esc_html__( 'Panel Content', 'pique' ),
  352. 'section' => 'pique_panel8',
  353. 'type' => 'dropdown-pages',
  354. ) );
  355. $wp_customize->add_setting( 'pique_panel8_background', array(
  356. 'default' => 'default',
  357. 'sanitize_callback' => 'sanitize_hex_color',
  358. 'transport' => 'postMessage',
  359. ) );
  360. $wp_customize->add_control( 'pique_panel8_background', array(
  361. 'label' => esc_html__( 'Background Color', 'pique' ),
  362. 'section' => 'pique_panel8',
  363. 'type' => 'color',
  364. ) );
  365. $wp_customize->add_setting( 'pique_panel8_opacity', array(
  366. 'default' => 'default',
  367. 'sanitize_callback' => 'pique_sanitize_opacity',
  368. 'transport' => 'postMessage',
  369. ) );
  370. $wp_customize->add_control( 'pique_panel8_opacity', array(
  371. 'label' => esc_html__( 'Featured Image Opacity', 'pique' ),
  372. 'section' => 'pique_panel8',
  373. 'type' => 'select',
  374. 'description' => esc_html( 'Set the opacity of the featured image over the panel background.', 'pique' ),
  375. 'choices' => array(
  376. '0.25' => esc_html__( '25%', 'pique' ),
  377. '0.5' => esc_html__( '50%', 'pique' ),
  378. '0.75' => esc_html__( '75%', 'pique' ),
  379. '1' => esc_html__( '100%', 'pique' ),
  380. ),
  381. ) );
  382. }
  383. add_action( 'customize_register', 'pique_customize_register' );
  384. /**
  385. * Sanitize a numeric value
  386. */
  387. function pique_sanitize_numeric_value( $input ) {
  388. if ( is_numeric( $input ) ) {
  389. return intval( $input );
  390. } else {
  391. return 0;
  392. }
  393. }
  394. /**
  395. * Sanitize a true/false checkbox
  396. */
  397. function pique_sanitize_checkbox( $input ) {
  398. if ( ! in_array( $input, array( true, false ) ) ) {
  399. $input = false;
  400. }
  401. return $input;
  402. }
  403. /*
  404. * Sanitize our opacity values
  405. */
  406. function pique_sanitize_opacity( $input ) {
  407. $choices = array( 'default', 0.25, 0.5, 0.75, 1 );
  408. if ( ! in_array( $input, $choices ) ) {
  409. $input = 'default';
  410. }
  411. return $input;
  412. }
  413. /*
  414. * Output our custom CSS to change background colour/opacity of panels.
  415. * Note: not very pretty, but it works.
  416. */
  417. function pique_customizer_css() {
  418. ?>
  419. <style type="text/css">
  420. <?php
  421. // Get each panel and iterate to output the proper CSS styles
  422. $panels = array( '1', '2', '3', '4', '5', '6', '7', '8' );
  423. foreach ( $panels as $panel ) :
  424. // Adjust the background colour if a custom colour is set
  425. if ( get_theme_mod( 'pique_panel' . $panel . '_background' ) ) : ?>
  426. .pique-frontpage .pique-panel.pique-panel<?php echo absint( $panel ); ?> {
  427. background-color: <?php echo esc_attr( get_theme_mod( 'pique_panel' . $panel . '_background' ) ); ?>;
  428. }
  429. <?php endif;
  430. // Adjust the opacity of featured image if set
  431. if ( get_theme_mod( 'pique_panel' . $panel . '_opacity' ) ) : ?>
  432. .pique-frontpage .pique-panel.pique-panel<?php echo absint( $panel ); ?> .pique-panel-background {
  433. opacity: <?php echo esc_attr( get_theme_mod( 'pique_panel' . $panel . '_opacity' ) ); ?>;
  434. }
  435. <?php endif;
  436. endforeach; ?>
  437. </style>
  438. <?php
  439. }
  440. add_action( 'wp_head', 'pique_customizer_css' );
  441. /**
  442. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  443. */
  444. function pique_customize_preview_js() {
  445. wp_enqueue_script( 'pique_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20151117', true );
  446. }
  447. add_action( 'customize_preview_init', 'pique_customize_preview_js' );
  448. function pique_panels_js() {
  449. wp_enqueue_script( 'pique_extra_js', get_template_directory_uri() . '/assets/js/panel-customizer.js', array(), '20151116', true );
  450. }
  451. add_action( 'customize_controls_enqueue_scripts', 'pique_panels_js' );