Completed
Push — try/instant-search-no-animatio... ( 38f8ba...dcbd6c )
by Jeremy
06:58
created

functions.php ➔ jetpack_the_site_logo()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 0
dl 0
loc 72
rs 8.2997
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	} elseif ( '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(
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
	$size = site_logo()->theme_size();
105
106
	// If no logo is set, but we're in the Customizer, leave a placeholder (needed for the live preview).
107
	if (
108
		! jetpack_has_site_logo()
109
		&& jetpack_is_customize_preview()
110
	) {
111
		/*
112
		 * Reason: the output is escaped in the sprintf.
113
		 * phpcs:disable WordPress.Security.EscapeOutput
114
		 */
115
		/** This filter is documented in modules/theme-tools/site-logo/inc/functions.php */
116
		echo apply_filters(
117
			'jetpack_the_site_logo',
118
			sprintf(
119
				'<a href="%1$s" class="site-logo-link" style="display:none;"><img class="site-logo" data-size="%2$s" /></a>',
120
				esc_url( home_url( '/' ) ),
121
				esc_attr( $size )
122
			),
123
			array(),
124
			$size
125
		);
126
		/* phpcs:enable WordPress.Security.EscapeOutput */
127
		return;
128
	}
129
130
	// Check for WP 4.5 Site Logo and Jetpack logo.
131
	$logo_id      = get_theme_mod( 'custom_logo' );
132
	$jetpack_logo = site_logo()->logo;
133
134
	// Use WP Core logo if present, otherwise use Jetpack's.
135
	if ( ! $logo_id && isset( $jetpack_logo['id'] ) ) {
136
		$logo_id = $jetpack_logo['id'];
137
	}
138
139
	/*
140
	 * Reason: the output is escaped in the sprintf.
141
	 * phpcs:disable WordPress.Security.EscapeOutput
142
	 */
143
	/**
144
	 * Filter the Site Logo output.
145
	 *
146
	 * @module theme-tools
147
	 *
148
	 * @since 3.2.0
149
	 *
150
	 * @param string $html Site Logo HTML output.
151
	 * @param array $jetpack_logo Array of Site Logo details.
152
	 * @param string $size Size specified in add_theme_support declaration, or 'thumbnail' default.
153
	 */
154
	echo apply_filters(
155
		'jetpack_the_site_logo',
156
		sprintf(
157
			'<a href="%1$s" class="site-logo-link" rel="home" itemprop="url">%2$s</a>',
158
			esc_url( home_url( '/' ) ),
159
			wp_get_attachment_image(
160
				$logo_id,
161
				$size,
162
				false,
163
				array(
164
					'class'     => "site-logo attachment-$size",
165
					'data-size' => $size,
166
					'itemprop'  => 'logo',
167
				)
168
			)
169
		),
170
		$jetpack_logo,
171
		$size
172
	);
173
	/* phpcs:enable WordPress.Security.EscapeOutput */
174
}
175
176
/**
177
 * Whether the site is being previewed in the Customizer.
178
 * Duplicate of core function until 4.0 is released.
179
 *
180
 * @global WP_Customize_Manager $wp_customize Customizer instance.
181
 * @return bool True if the site is being previewed in the Customizer, false otherwise.
182
 */
183
function jetpack_is_customize_preview() {
184
	global $wp_customize;
185
186
	return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
187
}
188
189
/**
190
 * Sanitize the string of classes used for header text.
191
 * Limit to A-Z,a-z,0-9,(space),(comma),_,-
192
 *
193
 * @return string Sanitized string of CSS classes.
194
 */
195
function jetpack_sanitize_header_text_classes( $classes ) {
196
	$classes = preg_replace( '/[^A-Za-z0-9\,\ ._-]/', '', $classes );
197
198
	return $classes;
199
}
200