functions.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Functions and template tags for using site logos.
  4. *
  5. * @package Jetpack
  6. */
  7. /**
  8. * Retrieve the site logo URL or ID (URL by default). Pass in the string 'id' for ID.
  9. *
  10. * @uses get_option()
  11. * @uses esc_url_raw()
  12. * @uses set_url_scheme()
  13. * @return mixed The URL or ID of our site logo, false if not set
  14. * @since 1.0
  15. */
  16. function jetpack_get_site_logo( $show = 'url' ) {
  17. $logo = site_logo()->logo;
  18. // Return false if no logo is set
  19. if ( ! isset( $logo['id'] ) || 0 == $logo['id'] ) {
  20. return false;
  21. }
  22. // Return the ID if specified, otherwise return the URL by default
  23. if ( 'id' == $show ) {
  24. return $logo['id'];
  25. } else {
  26. return esc_url_raw( set_url_scheme( $logo['url'] ) );
  27. }
  28. }
  29. /**
  30. * Retrieve an array of the dimensions of the Site Logo.
  31. *
  32. * @uses Site_Logo::theme_size()
  33. * @uses get_option( 'thumbnail_size_w' )
  34. * @uses get_option( 'thumbnail_size_h' )
  35. * @uses global $_wp_additional_image_sizes;
  36. *
  37. * @since 3.6.0
  38. *
  39. * @return array $dimensions {
  40. * An array of dimensions of the Site Logo.
  41. *
  42. * @type string $width Width of the logo in pixels.
  43. * @type string $height Height of the logo in pixels.
  44. * }
  45. */
  46. function jetpack_get_site_logo_dimensions() {
  47. // Get the image size to use with the logo.
  48. $size = site_logo()->theme_size();
  49. // If the size is the default `thumbnail`, get its dimensions. Otherwise, get them from $_wp_additional_image_sizes
  50. if ( empty( $size ) ) {
  51. return false;
  52. } else if ( 'thumbnail' == $size ) {
  53. $dimensions = array(
  54. 'width' => get_option( 'thumbnail_size_w' ),
  55. 'height' => get_option( 'thumbnail_size_h' ),
  56. );
  57. } else {
  58. global $_wp_additional_image_sizes;
  59. if ( ! isset( $_wp_additional_image_sizes[ $size ] ) ) {
  60. return false;
  61. }
  62. $dimensions = array(
  63. 'width' => $_wp_additional_image_sizes[ $size ][ 'width' ],
  64. 'height' => $_wp_additional_image_sizes[ $size ][ 'height' ],
  65. );
  66. }
  67. return $dimensions;
  68. }
  69. /**
  70. * Determine if a site logo is assigned or not.
  71. *
  72. * @uses get_option
  73. * @return boolean True if there is an active logo, false otherwise
  74. */
  75. function jetpack_has_site_logo() {
  76. return site_logo()->has_site_logo();
  77. }
  78. /**
  79. * Output an <img> tag of the site logo, at the size specified
  80. * in the theme's add_theme_support() declaration.
  81. *
  82. * @uses Site_Logo::logo
  83. * @uses Site_Logo::theme_size()
  84. * @uses jetpack_has_site_logo()
  85. * @uses jetpack_is_customize_preview()
  86. * @uses esc_url()
  87. * @uses home_url()
  88. * @uses esc_attr()
  89. * @uses wp_get_attachment_image()
  90. * @uses apply_filters()
  91. * @since 1.0
  92. */
  93. function jetpack_the_site_logo() {
  94. $logo = site_logo()->logo;
  95. $logo_id = get_theme_mod( 'custom_logo' ); // Check for WP 4.5 Site Logo
  96. $logo_id = $logo_id ? $logo_id : $logo['id']; // Use WP Core logo if present, otherwise use Jetpack's.
  97. $size = site_logo()->theme_size();
  98. $html = '';
  99. // If no logo is set, but we're in the Customizer, leave a placeholder (needed for the live preview).
  100. if ( ! jetpack_has_site_logo() ) {
  101. if ( jetpack_is_customize_preview() ) {
  102. $html = sprintf( '<a href="%1$s" class="site-logo-link" style="display:none;"><img class="site-logo" data-size="%2$s" /></a>',
  103. esc_url( home_url( '/' ) ),
  104. esc_attr( $size )
  105. );
  106. }
  107. }
  108. // We have a logo. Logo is go.
  109. else {
  110. $html = sprintf( '<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>',
  111. esc_url( home_url( '/' ) ),
  112. wp_get_attachment_image(
  113. $logo_id,
  114. $size,
  115. false,
  116. array(
  117. 'class' => "site-logo attachment-$size",
  118. 'data-size' => $size,
  119. 'itemprop' => "logo"
  120. )
  121. )
  122. );
  123. }
  124. /**
  125. * Filter the Site Logo output.
  126. *
  127. * @module theme-tools
  128. *
  129. * @since 3.2.0
  130. *
  131. * @param string $html Site Logo HTML output.
  132. * @param array $logo Array of Site Logo details.
  133. * @param string $size Size specified in add_theme_support declaration, or 'thumbnail' default.
  134. */
  135. echo apply_filters( 'jetpack_the_site_logo', $html, $logo, $size );
  136. }
  137. /**
  138. * Whether the site is being previewed in the Customizer.
  139. * Duplicate of core function until 4.0 is released.
  140. *
  141. * @global WP_Customize_Manager $wp_customize Customizer instance.
  142. * @return bool True if the site is being previewed in the Customizer, false otherwise.
  143. */
  144. function jetpack_is_customize_preview() {
  145. global $wp_customize;
  146. return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
  147. }
  148. /**
  149. * Sanitize the string of classes used for header text.
  150. * Limit to A-Z,a-z,0-9,(space),(comma),_,-
  151. *
  152. * @return string Sanitized string of CSS classes.
  153. */
  154. function jetpack_sanitize_header_text_classes( $classes ) {
  155. $classes = preg_replace( '/[^A-Za-z0-9\,\ ._-]/', '', $classes );
  156. return $classes;
  157. }