customizer.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * AltoFocus Theme Customizer.
  4. *
  5. * @package AltoFocus
  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 altofocus_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. $wp_customize->get_setting( 'background_color' )->transport = 'postMessage';
  17. $wp_customize->get_setting( 'background_image' )->transport = 'postMessage';
  18. $wp_customize->get_setting( 'background_position_x' )->transport = 'postMessage';
  19. $wp_customize->get_setting( 'background_position_y' )->transport = 'postMessage';
  20. $wp_customize->get_setting( 'background_size' )->transport = 'postMessage';
  21. $wp_customize->get_setting( 'background_repeat' )->transport = 'postMessage';
  22. $wp_customize->get_setting( 'background_attachment' )->transport = 'postMessage';
  23. $wp_customize->selective_refresh->add_partial( 'blogname', array(
  24. 'selector' => '.site-title a',
  25. 'render_callback' => 'altofocus_customize_partial_blogname',
  26. ) );
  27. $wp_customize->selective_refresh->add_partial( 'blogdescription', array(
  28. 'selector' => '.site-description',
  29. 'render_callback' => 'altofocus_customize_partial_blogdescription',
  30. ) );
  31. }
  32. add_action( 'customize_register', 'altofocus_customize_register' );
  33. /**
  34. * Custom background settings callback
  35. */
  36. function altofocus_custom_background_cb() {
  37. $image = get_background_image();
  38. $color = get_background_color();
  39. if ( empty ( $image ) && empty ( $color ) ) {
  40. return;
  41. }
  42. $background_color = get_theme_mod( 'background_color' );
  43. $background_image = get_theme_mod( 'background_image' );
  44. $background_position_x = get_theme_mod( 'background_position_x' );
  45. $background_position_y = get_theme_mod( 'background_position_y' );
  46. $background_size = get_theme_mod( 'background_size' ) === '' || 'auto' ? 'auto' : get_theme_mod( 'background_size' );
  47. $background_repeat = get_theme_mod( 'background_repeat' );
  48. $background_attachment = get_theme_mod( 'background_attachment' );
  49. $background_styles = '';
  50. // Make sure background images with a 'scroll' background-attachment display properly
  51. if ( !empty( $background_image ) && $background_attachment == 'scroll' ) {
  52. $background_styles = 'body.custom-background, .site-header, .site-content, .single .entry-media, .widget-area, .top-navigation > div { background-image: none !important; background-color: transparent !important; }';
  53. $background_styles .= 'html {';
  54. $background_styles .= 'background-color: #' . $background_color . '; ';
  55. $background_styles .= 'background-image: url("' . esc_url( $background_image ) . '"); ';
  56. $background_styles .= 'background-position: ' . $background_position_x . ' ' . $background_position_y . '; ';
  57. $background_styles .= 'background-size: ' . $background_size . '; ';
  58. $background_styles .= 'background-repeat: ' . $background_repeat . '; ';
  59. $background_styles .= 'background-attachment: ' . $background_attachment . '; ';
  60. $background_styles .= '}';
  61. } else {
  62. $background_styles = 'html, body.custom-background, .site-header, .site-content, .single .entry-media, .widget-area, .top-navigation > div {';
  63. $background_styles .= 'background-color: #' . $background_color . '; ';
  64. $background_styles .= 'background-image: url("' . esc_url( $background_image ) . '"); ';
  65. $background_styles .= 'background-position: ' . $background_position_x . ' ' . $background_position_y . '; ';
  66. $background_styles .= 'background-size: ' . $background_size . '; ';
  67. $background_styles .= 'background-repeat: ' . $background_repeat . '; ';
  68. $background_styles .= 'background-attachment: ' . $background_attachment . '; ';
  69. $background_styles .= '}';
  70. }
  71. wp_add_inline_style( 'altofocus-style', $background_styles );
  72. }
  73. add_action( 'wp_enqueue_scripts', 'altofocus_custom_background_cb' );
  74. /**
  75. * Render the site title for the selective refresh partial.
  76. *
  77. * @return void
  78. */
  79. function altofocus_customize_partial_blogname() {
  80. bloginfo( 'name' );
  81. }
  82. /**
  83. * Render the site tagline for the selective refresh partial.
  84. *
  85. * @return void
  86. */
  87. function altofocus_customize_partial_blogdescription() {
  88. bloginfo( 'description' );
  89. }
  90. /**
  91. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  92. */
  93. function altofocus_customize_preview_js() {
  94. wp_enqueue_script( 'altofocus_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20151215', true );
  95. }
  96. add_action( 'customize_preview_init', 'altofocus_customize_preview_js' );
  97. /**
  98. * Sets a default boolean of TRUE for Featured Content show-all setting.
  99. *
  100. * @param array $settings
  101. * @return array
  102. */
  103. function altofocus_featured_content_default_settings( $settings ) {
  104. $settings['show-all'] = 1;
  105. return $settings;
  106. }
  107. add_action( 'featured_content_default_settings', 'altofocus_featured_content_default_settings' );
  108. /**
  109. * Change featured content default settings after theme setup
  110. */
  111. function altofocus_change_featured_content_default_settings() {
  112. set_theme_mod( 'featured-content[show-all]', 1 );
  113. update_option( 'featured-content[show-all]', '1' );
  114. }
  115. add_action( 'after_setup_theme', 'altofocus_change_featured_content_default_settings' );