/modules/theme-tools/site-logo/inc/functions.php

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