logo-resizer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Add Logo Size option in the customizer.
  4. */
  5. function photos_logo_resizer_customize_register( $wp_customize ) {
  6. // Logo Resizer additions
  7. $wp_customize->add_setting( 'logo_size', array(
  8. 'default' => 50,
  9. 'type' => 'theme_mod',
  10. 'theme_supports' => 'custom-logo',
  11. 'transport' => 'postMessage',
  12. 'sanitize_callback' => 'absint',
  13. 'sanitize_js_callback' => 'absint',
  14. ) );
  15. $wp_customize->add_control( 'logo_size', array(
  16. 'label' => esc_html__( 'Logo Size', 'photos' ),
  17. 'section' => 'title_tagline',
  18. 'priority' => 9,
  19. 'type' => 'range',
  20. 'settings' => 'logo_size',
  21. 'input_attrs' => array(
  22. 'step' => 5,
  23. 'min' => 0,
  24. 'max' => 100,
  25. 'aria-valuemin' => 0,
  26. 'aria-valuemax' => 100,
  27. 'aria-valuenow' => 50,
  28. 'aria-orientation' => 'horizontal',
  29. ),
  30. ) );
  31. }
  32. add_action( 'customize_register', 'photos_logo_resizer_customize_register' );
  33. /**
  34. * Add support for logo resizing by filtering `get_custom_logo`.
  35. */
  36. function photos_customize_logo_resize( $html ) {
  37. $max = [];
  38. $img = [];
  39. $size = get_theme_mod( 'logo_size' );
  40. $custom_logo_id = get_theme_mod( 'custom_logo' );
  41. // set the short side minimum
  42. $min = 48;
  43. // don't use empty() because we can still use a 0
  44. if ( is_numeric( $size ) && is_numeric( $custom_logo_id ) ) {
  45. // we're looking for $img['width'] and $img['height'] of original image
  46. $logo = wp_get_attachment_metadata( $custom_logo_id );
  47. if ( ! $logo ) return $html;
  48. // get the logo support size
  49. $sizes = get_theme_support( 'custom-logo' );
  50. // Check for max height and width, default to image sizes if none set in theme
  51. $max['height'] = isset( $sizes[0]['height'] ) ? $sizes[0]['height'] : $logo['height'];
  52. $max['width'] = isset( $sizes[0]['width'] ) ? $sizes[0]['width'] : $logo['width'];
  53. // landscape or square
  54. if ( $logo['width'] >= $logo['height'] ) {
  55. $output = photos_logo_resize_min_max( $logo['height'], $logo['width'], $max['height'], $max['width'], $size, $min );
  56. $img = array(
  57. 'height' => $output['short'],
  58. 'width' => $output['long']
  59. );
  60. // portrait
  61. } else if ( $logo['width'] < $logo['height'] ) {
  62. $output = photos_logo_resize_min_max( $logo['width'], $logo['height'], $max['width'], $max['height'], $size, $min );
  63. $img = array(
  64. 'height' => $output['long'],
  65. 'width' => $output['short']
  66. );
  67. }
  68. // add the CSS
  69. $css = '
  70. <style>
  71. .custom-logo {
  72. height: ' . $img['height'] . 'px;
  73. max-height: ' . $max['height'] . 'px;
  74. max-width: ' . $max['width'] . 'px;
  75. width: ' . $img['width'] . 'px;
  76. }
  77. </style>';
  78. $html = $css . $html;
  79. }
  80. return $html;
  81. }
  82. add_filter( 'get_custom_logo', 'photos_customize_logo_resize' );
  83. /* Helper function to determine the max size of the logo */
  84. function photos_logo_resize_min_max( $short, $long, $short_max, $long_max, $percent, $min ){
  85. $max = [];
  86. $size = [];
  87. $ratio = ( $long / $short );
  88. $max['long'] = ( $long_max >= $long ) ? $long : $long_max;
  89. $max['short'] = ( $short_max >= ( $max['long'] / $ratio ) ) ? floor( $max['long'] / $ratio ) : $short_max;
  90. $ppp = ( $max['short'] - $min ) / 100;
  91. $size['short'] = round( $min + ( $percent * $ppp ) );
  92. $size['long'] = round( $size['short'] / ( $short / $long ) );
  93. return $size;
  94. }
  95. /**
  96. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  97. */
  98. function photos_logo_resizer_customize_preview_js() {
  99. wp_enqueue_script( 'photo-blog-logo-resizer-customizer', get_template_directory_uri() . '/js/logo/customize-preview.js', array( 'jquery', 'customize-preview' ), '20180724', true );
  100. }
  101. add_action( 'customize_preview_init', 'photos_logo_resizer_customize_preview_js' );
  102. /**
  103. * JS handlers for Customizer Controls
  104. */
  105. function photos_logo_resizer_customize_controls_js() {
  106. wp_enqueue_script( 'photo-blog-logo-resizer-customizer-controls', get_template_directory_uri() . '/js/logo/customize-controls.js', array( 'jquery', 'customize-controls' ), '20180724', true );
  107. }
  108. add_action( 'customize_controls_enqueue_scripts', 'photos_logo_resizer_customize_controls_js' );
  109. /**
  110. * Adds CSS to the Customizer controls.
  111. */
  112. function photos_logo_resizer_customize_css() {
  113. wp_add_inline_style( 'customize-controls', '#customize-control-logo_size input[type="range"] { width: 100%; }' );
  114. }
  115. add_action( 'customize_controls_enqueue_scripts', 'photos_logo_resizer_customize_css' );