Nessuna descrizione

functions.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Functions and template tags for using site logos.
  4. *
  5. * @package automattic/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_id = site_logo()->logo;
  18. // Return false if no logo is set
  19. if ( ! $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. $logo_url = wp_get_attachment_url( $logo_id );
  27. return esc_url_raw( set_url_scheme( $logo_url ) );
  28. }
  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. // If the size is the default `thumbnail`, get its dimensions. Otherwise, get them from $_wp_additional_image_sizes
  51. if ( empty( $size ) ) {
  52. return false;
  53. } elseif ( 'thumbnail' == $size ) {
  54. $dimensions = array(
  55. 'width' => get_option( 'thumbnail_size_w' ),
  56. 'height' => get_option( 'thumbnail_size_h' ),
  57. );
  58. } else {
  59. global $_wp_additional_image_sizes;
  60. if ( ! isset( $_wp_additional_image_sizes[ $size ] ) ) {
  61. return false;
  62. }
  63. $dimensions = array(
  64. 'width' => $_wp_additional_image_sizes[ $size ]['width'],
  65. 'height' => $_wp_additional_image_sizes[ $size ]['height'],
  66. );
  67. }
  68. return $dimensions;
  69. }
  70. /**
  71. * Determine if a site logo is assigned or not.
  72. *
  73. * @uses get_option
  74. * @return boolean True if there is an active logo, false otherwise
  75. */
  76. function jetpack_has_site_logo() {
  77. return site_logo()->has_site_logo();
  78. }
  79. /**
  80. * Output an <img> tag of the site logo, at the size specified
  81. * in the theme's add_theme_support() declaration.
  82. *
  83. * @uses Site_Logo::logo
  84. * @uses Site_Logo::theme_size()
  85. * @uses jetpack_has_site_logo()
  86. * @uses jetpack_is_customize_preview()
  87. * @uses esc_url()
  88. * @uses home_url()
  89. * @uses esc_attr()
  90. * @uses wp_get_attachment_image()
  91. * @uses apply_filters()
  92. * @since 1.0
  93. */
  94. function jetpack_the_site_logo() {
  95. $size = site_logo()->theme_size();
  96. // If no logo is set, but we're in the Customizer, leave a placeholder (needed for the live preview).
  97. if (
  98. ! jetpack_has_site_logo()
  99. && jetpack_is_customize_preview()
  100. ) {
  101. /*
  102. * Reason: the output is escaped in the sprintf.
  103. * phpcs:disable WordPress.Security.EscapeOutput
  104. */
  105. /** This filter is documented in modules/theme-tools/site-logo/inc/functions.php */
  106. echo apply_filters(
  107. 'jetpack_the_site_logo',
  108. sprintf(
  109. '<a href="%1$s" class="site-logo-link" style="display:none;"><img class="site-logo" data-size="%2$s" /></a>',
  110. esc_url( home_url( '/' ) ),
  111. esc_attr( $size )
  112. ),
  113. array(),
  114. $size
  115. );
  116. /* phpcs:enable WordPress.Security.EscapeOutput */
  117. return;
  118. }
  119. // Check for WP 4.5 Site Logo and Jetpack logo.
  120. $logo_id = get_theme_mod( 'custom_logo' );
  121. // Get the option directly so the updated logo can be injected into customizer previews.
  122. $jetpack_logo_id = get_option( 'site_logo' );
  123. // Use WP Core logo if present and is an id (of an attachment), otherwise use Jetpack's.
  124. if ( ! is_numeric( $logo_id ) && $jetpack_logo_id ) {
  125. $logo_id = $jetpack_logo_id;
  126. }
  127. /*
  128. * Reason: the output is escaped in the sprintf.
  129. * phpcs:disable WordPress.Security.EscapeOutput
  130. */
  131. /**
  132. * Filter the Site Logo output.
  133. *
  134. * @module theme-tools
  135. *
  136. * @since 3.2.0
  137. *
  138. * @param string $html Site Logo HTML output.
  139. * @param array $jetpack_logo Array of Site Logo details.
  140. * @param string $size Size specified in add_theme_support declaration, or 'thumbnail' default.
  141. */
  142. echo apply_filters(
  143. 'jetpack_the_site_logo',
  144. sprintf(
  145. '<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>',
  146. esc_url( home_url( '/' ) ),
  147. wp_get_attachment_image(
  148. $logo_id,
  149. $size,
  150. false,
  151. array(
  152. 'class' => "site-logo attachment-$size",
  153. 'data-size' => $size,
  154. 'itemprop' => 'logo',
  155. )
  156. )
  157. ),
  158. // Return array format in filter for back compatibility.
  159. array(
  160. 'id' => $jetpack_logo_id,
  161. 'url' => wp_get_attachment_url( $jetpack_logo_id ),
  162. 'sizes' => array(),
  163. ),
  164. $size
  165. );
  166. /* phpcs:enable WordPress.Security.EscapeOutput */
  167. }
  168. /**
  169. * Whether the site is being previewed in the Customizer.
  170. * Duplicate of core function until 4.0 is released.
  171. *
  172. * @global WP_Customize_Manager $wp_customize Customizer instance.
  173. * @return bool True if the site is being previewed in the Customizer, false otherwise.
  174. */
  175. function jetpack_is_customize_preview() {
  176. global $wp_customize;
  177. return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
  178. }
  179. /**
  180. * Sanitize the string of classes used for header text.
  181. * Limit to A-Z,a-z,0-9,(space),(comma),_,-
  182. *
  183. * @return string Sanitized string of CSS classes.
  184. */
  185. function jetpack_sanitize_header_text_classes( $classes ) {
  186. $classes = preg_replace( '/[^A-Za-z0-9\,\ ._-]/', '', $classes );
  187. return $classes;
  188. }