-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.compat.php
161 lines (136 loc) · 4.28 KB
/
functions.compat.php
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
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
/**
* Compatibility functions for YouTube URLs and WP.com helper functions.
*
* @package automattic/jetpack
*/
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
/**
* Required for class.media-extractor.php to match expected function naming convention.
*
* @param $url Can be just the $url or the whole $atts array.
* @return bool|mixed The Youtube video ID via jetpack_get_youtube_id
*/
function jetpack_shortcode_get_youtube_id( $url ) {
return jetpack_get_youtube_id( $url );
}
/**
* Extract video ID from a YouTube url.
*
* @param string|array $url YouTube URL.
* @return bool|mixed The Youtube video ID
*/
function jetpack_get_youtube_id( $url ) {
// Do we have an $atts array? Get first att
if ( is_array( $url ) ) {
$url = reset( $url );
}
$url = youtube_sanitize_url( $url );
$url = wp_parse_url( $url );
$id = false;
if ( ! isset( $url['query'] ) ) {
return false;
}
parse_str( $url['query'], $qargs );
if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) ) {
return false;
}
if ( isset( $qargs['list'] ) ) {
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] );
}
if ( empty( $id ) ) {
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] );
}
return $id;
}
if ( ! function_exists( 'youtube_sanitize_url' ) ) :
/**
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
*
* @param string|array $url YouTube URL.
* @return string|array The normalized URL or false if input is invalid.
*/
function youtube_sanitize_url( $url ) {
if ( is_array( $url ) && isset( $url['url'] ) ) {
$url = $url['url'];
}
if ( ! is_string( $url ) ) {
return false;
}
$url = trim( $url, ' "' );
$url = trim( $url );
$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
// Replace any extra question marks with ampersands - the result of a URL like "https://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in.
$query_string_start = strpos( $url, '?' );
if ( false !== $query_string_start ) {
$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
}
return $url;
}
endif;
/**
* Merge in three string helper functions from WPCOM to make working with strings easier.
*
* @see WPCOM/wp-content/mu-plugins/string-helpers.php
*/
if ( ! function_exists( 'wp_startswith' ) ) :
/**
* Check whether a string starts with a specific substring.
*
* @param string $haystack String we are filtering.
* @param string $needle The substring we are looking for.
* @return bool
*/
function wp_startswith( $haystack, $needle ) {
if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
return false;
}
$haystack = (string) $haystack;
$needle = (string) $needle;
return str_starts_with( $haystack, $needle );
}
endif;
if ( ! function_exists( 'wp_endswith' ) ) :
/**
* Check whether a string ends with a specific substring.
*
* @param string $haystack String we are filtering.
* @param string $needle The substring we are looking for.
* @return bool
*/
function wp_endswith( $haystack, $needle ) {
if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
return false;
}
$haystack = (string) $haystack;
$needle = (string) $needle;
return str_ends_with( $haystack, $needle );
}
endif;
if ( ! function_exists( 'wp_in' ) ) :
/**
* Checks whether a string contains a specific substring.
*
* @param string $needle The substring we are looking for.
* @param string $haystack String we are filtering.
* @return bool
*/
function wp_in( $needle, $haystack ) {
if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
return false;
}
$haystack = (string) $haystack;
$needle = (string) $needle;
return str_contains( $haystack, $needle );
}
endif;
/**
* Deprecated connection function.
*
* @param string $text Deprecated.
* @deprecated 7.5 Use Connection_Manager instead.
*/
function jetpack_sha1_base64( $text ) {
$connection = new Connection_Manager();
return $connection->sha1_base64( $text );
}