Completed
Branch develop (6e04c9)
by
unknown
02:44
created

extras.php ➔ rest_get_avatar_url()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 19
rs 9.2
1
<?php
2
/**
3
 * Extra File
4
 *
5
 * Contains extra functions from plugin.php go.
6
 *
7
 * @package WordPress
8
 * @subpackage JSON API
9
 */
10
11
add_action( 'wp_enqueue_scripts', 'rest_register_scripts', -100 );
12
add_action( 'admin_enqueue_scripts', 'rest_register_scripts', -100 );
13
14
if ( ! function_exists( 'rest_register_scripts' ) ) {
15
	/**
16
	 * Registers REST API JavaScript helpers.
17
	 *
18
	 * @since 4.4.0
19
	 *
20
	 * @see wp_register_scripts()
21
	 */
22
	function rest_register_scripts() {
23
24
		// Use minified scripts if SCRIPT_DEBUG is not on.
25
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
26
27
		wp_register_script( 'wp-api', plugins_url( 'wp-api' . $suffix . '.js', __FILE__ ), array( 'jquery', 'backbone', 'underscore' ), '1.2', true );
28
29
		$settings = array(
30
			'root'          => esc_url_raw( get_rest_url() ),
31
			'nonce'         => wp_create_nonce( 'wp_rest' ),
32
			'versionString' => 'wp/v2/',
33
		);
34
		wp_localize_script( 'wp-api', 'wpApiSettings', $settings );
35
	}
36
}
37
38
if ( ! function_exists( 'rest_get_avatar_urls' ) ) {
39
	/**
40
	 * Retrieves the avatar urls in various sizes based on a given email address.
41
	 *
42
	 * @since 4.4.0
43
	 *
44
	 * @see get_avatar_url()
45
	 *
46
	 * @param string $email Email address.
47
	 * @return array $urls Gravatar url for each size.
48
	 */
49
	function rest_get_avatar_urls( $email ) {
50
		$avatar_sizes = rest_get_avatar_sizes();
51
52
		$urls = array();
53
		foreach ( $avatar_sizes as $size ) {
54
			$urls[ $size ] = get_avatar_url( $email, array( 'size' => $size ) );
55
		}
56
57
		return $urls;
58
	}
59
}
60
61
if ( ! function_exists( 'rest_get_avatar_sizes' ) ) {
62
	/**
63
	 * Retrieves the pixel sizes for avatars.
64
	 *
65
	 * @since 4.4.0
66
	 *
67
	 * @return array List of pixel sizes for avatars. Default `[ 24, 48, 96 ]`.
68
	 */
69
	function rest_get_avatar_sizes() {
70
		/**
71
		 * Filter the REST avatar sizes.
72
		 *
73
		 * Use this filter to adjust the array of sizes returned by the
74
		 * `rest_get_avatar_sizes` function.
75
		 *
76
		 * @since 4.4.0
77
		 *
78
		 * @param array $sizes An array of int values that are the pixel sizes for avatars.
79
		 *                     Default `[ 24, 48, 96 ]`.
80
		 */
81
		return apply_filters( 'rest_avatar_sizes', array( 24, 48, 96 ) );
82
	}
83
}
84
85
/**
86
 * Retrieves the avatar url for a user who provided a user ID or email address.
87
 *
88
 * get_avatar() doesn't return just the URL, so we have to extract it here.
89
 *
90
 * @since 4.4.0
91
 * @deprecated WPAPI-2.0 rest_get_avatar_urls()
92
 * @see rest_get_avatar_urls()
93
 *
94
 * @param string $email Email address.
95
 * @return string URL for the user's avatar, empty string otherwise.
96
 */
97
function rest_get_avatar_url( $email ) {
98
	_deprecated_function( 'rest_get_avatar_url', 'WPAPI-2.0', 'rest_get_avatar_urls' );
99
100
	// Use the WP Core `get_avatar_url()` function introduced in 4.2.
101
	if ( function_exists( 'get_avatar_url' ) ) {
102
		return esc_url_raw( get_avatar_url( $email ) );
103
	}
104
105
	$avatar_html = get_avatar( $email );
106
107
	// Strip the avatar url from the get_avatar img tag.
108
	preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches );
109
110
	if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
111
		return esc_url_raw( $matches[1] );
112
	}
113
114
	return '';
115
}
116
117
if ( ! function_exists( 'wp_is_numeric_array' ) ) {
118
	/**
119
	 * Determines if the variable is a numeric-indexed array.
120
	 *
121
	 * @since 4.4.0
122
	 *
123
	 * @param mixed $data Variable to check.
124
	 * @return bool Whether the variable is a list.
125
	 */
126
	function wp_is_numeric_array( $data ) {
127
		if ( ! is_array( $data ) ) {
128
			return false;
129
		}
130
131
		$keys = array_keys( $data );
132
		$string_keys = array_filter( $keys, 'is_string' );
133
		return count( $string_keys ) === 0;
134
	}
135
}
136
137
/**
138
 * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339.
139
 *
140
 * Explicitly strips timezones, as datetimes are not saved with any timezone
141
 * information. Including any information on the offset could be misleading.
142
 *
143
 * @deprecated WPAPI-2.0 mysql_to_rfc3339()
144
 *
145
 * @param string $date_string Date string to parse and format.
146
 * @return string Date formatted for ISO8601/RFC3339.
147
 */
148
function rest_mysql_to_rfc3339( $date_string ) {
149
	_deprecated_function( 'rest_mysql_to_rfc3339', 'WPAPI-2.0', 'mysql_to_rfc3339' );
150
	return mysql_to_rfc3339( $date_string );
151
}
152