summaryrefslogtreecommitdiff
blob: 3b27b32fb75d8016d7ea34782dcffbcd01b918c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
 * Functions and template tags for using site logos.
 *
 * @package Jetpack
 */

/**
 * Retrieve the site logo URL or ID (URL by default). Pass in the string 'id' for ID.
 *
 * @uses get_option()
 * @uses esc_url_raw()
 * @uses set_url_scheme()
 * @return mixed The URL or ID of our site logo, false if not set
 * @since 1.0
 */
function jetpack_get_site_logo( $show = 'url' ) {
	$logo = site_logo()->logo;

	// Return false if no logo is set
	if ( ! isset( $logo['id'] ) || 0 == $logo['id'] ) {
		return false;
	}

	// Return the ID if specified, otherwise return the URL by default
	if ( 'id' == $show ) {
		return $logo['id'];
	} else {
		return esc_url_raw( set_url_scheme( $logo['url'] ) );
	}
}

/**
 * Retrieve an array of the dimensions of the Site Logo.
 *
 * @uses Site_Logo::theme_size()
 * @uses get_option( 'thumbnail_size_w' )
 * @uses get_option( 'thumbnail_size_h' )
 * @uses global $_wp_additional_image_sizes;
 *
 * @since 3.6.0
 *
 * @return array $dimensions {
 *      An array of dimensions of the Site Logo.
 *
 *      @type string $width Width of the logo in pixels.
 *      @type string $height Height of the logo in pixels.
 * }
 */
function jetpack_get_site_logo_dimensions() {
	// Get the image size to use with the logo.
	$size = site_logo()->theme_size();

	// If the size is the default `thumbnail`, get its dimensions. Otherwise, get them from $_wp_additional_image_sizes
	if ( empty( $size ) ) {
		return false;
	} elseif ( 'thumbnail' == $size ) {
		$dimensions = array(
			'width'  => get_option( 'thumbnail_size_w' ),
			'height' => get_option( 'thumbnail_size_h' ),
		);
	} else {
		global $_wp_additional_image_sizes;

		if ( ! isset( $_wp_additional_image_sizes[ $size ] ) ) {
			return false;
		}

		$dimensions = array(
			'width'  => $_wp_additional_image_sizes[ $size ]['width'],
			'height' => $_wp_additional_image_sizes[ $size ]['height'],
		);
	}

	return $dimensions;
}

/**
 * Determine if a site logo is assigned or not.
 *
 * @uses get_option
 * @return boolean True if there is an active logo, false otherwise
 */
function jetpack_has_site_logo() {
	return site_logo()->has_site_logo();
}

/**
 * Output an <img> tag of the site logo, at the size specified
 * in the theme's add_theme_support() declaration.
 *
 * @uses Site_Logo::logo
 * @uses Site_Logo::theme_size()
 * @uses jetpack_has_site_logo()
 * @uses jetpack_is_customize_preview()
 * @uses esc_url()
 * @uses home_url()
 * @uses esc_attr()
 * @uses wp_get_attachment_image()
 * @uses apply_filters()
 * @since 1.0
 */
function jetpack_the_site_logo() {
	$logo    = site_logo()->logo;
	$logo_id = get_theme_mod( 'custom_logo' ); // Check for WP 4.5 Site Logo
	$logo_id = $logo_id ? $logo_id : $logo['id']; // Use WP Core logo if present, otherwise use Jetpack's.
	$size    = site_logo()->theme_size();
	$html    = '';

	// If no logo is set, but we're in the Customizer, leave a placeholder (needed for the live preview).
	if ( ! jetpack_has_site_logo() ) {
		if ( jetpack_is_customize_preview() ) {
			$html = sprintf(
				'<a href="%1$s" class="site-logo-link" style="display:none;"><img class="site-logo" data-size="%2$s" /></a>',
				esc_url( home_url( '/' ) ),
				esc_attr( $size )
			);
		}
	}

	// We have a logo. Logo is go.
	else {
		$html = sprintf(
			'<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>',
			esc_url( home_url( '/' ) ),
			wp_get_attachment_image(
				$logo_id,
				$size,
				false,
				array(
					'class'     => "site-logo attachment-$size",
					'data-size' => $size,
					'itemprop'  => 'logo',
				)
			)
		);
	}

	/**
	 * Filter the Site Logo output.
	 *
	 * @module theme-tools
	 *
	 * @since 3.2.0
	 *
	 * @param string $html Site Logo HTML output.
	 * @param array $logo Array of Site Logo details.
	 * @param string $size Size specified in add_theme_support declaration, or 'thumbnail' default.
	 */
	echo apply_filters( 'jetpack_the_site_logo', $html, $logo, $size );
}

/**
 * Whether the site is being previewed in the Customizer.
 * Duplicate of core function until 4.0 is released.
 *
 * @global WP_Customize_Manager $wp_customize Customizer instance.
 * @return bool True if the site is being previewed in the Customizer, false otherwise.
 */
function jetpack_is_customize_preview() {
	global $wp_customize;

	return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
}

/**
 * Sanitize the string of classes used for header text.
 * Limit to A-Z,a-z,0-9,(space),(comma),_,-
 *
 * @return string Sanitized string of CSS classes.
 */
function jetpack_sanitize_header_text_classes( $classes ) {
	$classes = preg_replace( '/[^A-Za-z0-9\,\ ._-]/', '', $classes );

	return $classes;
}