Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | |||
| 25 | // Return the ID if specified, otherwise return the URL by default |
||
| 26 | if ( 'id' == $show ) { |
||
| 27 | return $logo['id']; |
||
| 28 | } else { |
||
| 29 | return esc_url_raw( set_url_scheme( $logo['url'] ) ); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Retrieve an array of the dimensions of the Site Logo. |
||
| 35 | * |
||
| 36 | * @uses Site_Logo::theme_size() |
||
| 37 | * @uses get_option( 'thumbnail_size_w' ) |
||
| 38 | * @uses get_option( 'thumbnail_size_h' ) |
||
| 39 | * @uses global $_wp_additional_image_sizes; |
||
| 40 | * |
||
| 41 | * @since 3.6.0 |
||
| 42 | * |
||
| 43 | * @return array $dimensions { |
||
| 44 | * An array of dimensions of the Site Logo. |
||
| 45 | * |
||
| 46 | * @type string $width Width of the logo in pixels. |
||
| 47 | * @type string $height Height of the logo in pixels. |
||
| 48 | * } |
||
| 49 | */ |
||
| 50 | function jetpack_get_site_logo_dimensions() { |
||
| 51 | // Get the image size to use with the logo. |
||
| 52 | $size = site_logo()->theme_size(); |
||
| 53 | |||
| 54 | // If the size is the default `thumbnail`, get its dimensions. Otherwise, get them from $_wp_additional_image_sizes |
||
| 55 | if ( empty( $size ) ) { |
||
| 56 | return false; |
||
| 57 | } else if ( 'thumbnail' == $size ) { |
||
| 58 | $dimensions = array( |
||
| 59 | 'width' => get_option( 'thumbnail_size_w' ), |
||
| 60 | 'height' => get_option( 'thumbnail_size_h' ), |
||
| 61 | ); |
||
| 62 | } else { |
||
| 63 | global $_wp_additional_image_sizes; |
||
| 64 | |||
| 65 | if ( ! isset( $_wp_additional_image_sizes[ $size ] ) ) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | $dimensions = array( |
||
|
0 ignored issues
–
show
|
|||
| 70 | 'width' => $_wp_additional_image_sizes[ $size ][ 'width' ], |
||
| 71 | 'height' => $_wp_additional_image_sizes[ $size ][ 'height' ], |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $dimensions; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Determine if a site logo is assigned or not. |
||
| 80 | * |
||
| 81 | * @uses get_option |
||
| 82 | * @return boolean True if there is an active logo, false otherwise |
||
| 83 | */ |
||
| 84 | function jetpack_has_site_logo() { |
||
| 85 | return site_logo()->has_site_logo(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Output an <img> tag of the site logo, at the size specified |
||
| 90 | * in the theme's add_theme_support() declaration. |
||
| 91 | * |
||
| 92 | * @uses Site_Logo::logo |
||
| 93 | * @uses Site_Logo::theme_size() |
||
| 94 | * @uses jetpack_has_site_logo() |
||
| 95 | * @uses jetpack_is_customize_preview() |
||
| 96 | * @uses esc_url() |
||
| 97 | * @uses home_url() |
||
| 98 | * @uses esc_attr() |
||
| 99 | * @uses wp_get_attachment_image() |
||
| 100 | * @uses apply_filters() |
||
| 101 | * @since 1.0 |
||
| 102 | */ |
||
| 103 | function jetpack_the_site_logo() { |
||
| 104 | $logo = site_logo()->logo; |
||
| 105 | $logo_id = get_theme_mod( 'custom_logo' ); // Check for WP 4.5 Site Logo |
||
| 106 | $logo_id = $logo_id ? $logo_id : $logo['id']; // Use WP Core logo if present, otherwise use Jetpack's. |
||
| 107 | $size = site_logo()->theme_size(); |
||
| 108 | $html = ''; |
||
| 109 | |||
| 110 | // If no logo is set, but we're in the Customizer, leave a placeholder (needed for the live preview). |
||
| 111 | if ( ! jetpack_has_site_logo() ) { |
||
| 112 | if ( jetpack_is_customize_preview() ) { |
||
| 113 | $html = sprintf( '<a href="%1$s" class="site-logo-link" style="display:none;"><img class="site-logo" data-size="%2$s" /></a>', |
||
| 114 | esc_url( home_url( '/' ) ), |
||
| 115 | esc_attr( $size ) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // We have a logo. Logo is go. |
||
| 121 | else { |
||
| 122 | $html = sprintf( '<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>', |
||
| 123 | esc_url( home_url( '/' ) ), |
||
| 124 | wp_get_attachment_image( |
||
| 125 | $logo_id, |
||
| 126 | $size, |
||
| 127 | false, |
||
| 128 | array( |
||
| 129 | 'class' => "site-logo attachment-$size", |
||
| 130 | 'data-size' => $size, |
||
| 131 | 'itemprop' => "logo" |
||
| 132 | ) |
||
| 133 | ) |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Filter the Site Logo output. |
||
| 139 | * |
||
| 140 | * @module theme-tools |
||
| 141 | * |
||
| 142 | * @since 3.2.0 |
||
| 143 | * |
||
| 144 | * @param string $html Site Logo HTML output. |
||
| 145 | * @param array $logo Array of Site Logo details. |
||
| 146 | * @param string $size Size specified in add_theme_support declaration, or 'thumbnail' default. |
||
| 147 | */ |
||
| 148 | echo apply_filters( 'jetpack_the_site_logo', $html, $logo, $size ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Whether the site is being previewed in the Customizer. |
||
| 153 | * Duplicate of core function until 4.0 is released. |
||
| 154 | * |
||
| 155 | * @global WP_Customize_Manager $wp_customize Customizer instance. |
||
| 156 | * @return bool True if the site is being previewed in the Customizer, false otherwise. |
||
| 157 | */ |
||
| 158 | function jetpack_is_customize_preview() { |
||
| 159 | global $wp_customize; |
||
| 160 | |||
| 161 | return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sanitize the string of classes used for header text. |
||
| 166 | * Limit to A-Z,a-z,0-9,(space),(comma),_,- |
||
| 167 | * |
||
| 168 | * @return string Sanitized string of CSS classes. |
||
| 169 | */ |
||
| 170 | function jetpack_sanitize_header_text_classes( $classes ) { |
||
| 171 | $classes = preg_replace( '/[^A-Za-z0-9\,\ ._-]/', '', $classes ); |
||
| 172 | |||
| 173 | return $classes; |
||
| 174 | } |
||
| 175 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.