PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.1.2
Jetpack – WP Security, Backup, Speed, & Growth v13.1.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / functions.compat.php
functions.compat.php
159 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2 /**
3 * Compatibility functions for YouTube URLs and WP.com helper functions.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
9
10 /**
11 * Required for class.media-extractor.php to match expected function naming convention.
12 *
13 * @param $url Can be just the $url or the whole $atts array.
14 * @return bool|mixed The Youtube video ID via jetpack_get_youtube_id
15 */
16 function jetpack_shortcode_get_youtube_id( $url ) {
17 return jetpack_get_youtube_id( $url );
18 }
19
20 /**
21 * Extract video ID from a YouTube url.
22 *
23 * @param string|array $url YouTube URL.
24 * @return bool|mixed The Youtube video ID
25 */
26 function jetpack_get_youtube_id( $url ) {
27 // Do we have an $atts array? Get first att
28 if ( is_array( $url ) ) {
29 $url = reset( $url );
30 }
31
32 $url = youtube_sanitize_url( $url );
33 $url = wp_parse_url( $url );
34 $id = false;
35
36 if ( ! isset( $url['query'] ) ) {
37 return false;
38 }
39
40 parse_str( $url['query'], $qargs );
41
42 if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) ) {
43 return false;
44 }
45
46 if ( isset( $qargs['list'] ) ) {
47 $id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] );
48 }
49
50 if ( empty( $id ) ) {
51 $id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] );
52 }
53
54 return $id;
55 }
56
57 if ( ! function_exists( 'youtube_sanitize_url' ) ) :
58 /**
59 * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
60 *
61 * @param string|array $url YouTube URL.
62 * @return string The normalized URL
63 */
64 function youtube_sanitize_url( $url ) {
65 if ( is_array( $url ) && isset( $url['url'] ) ) {
66 $url = $url['url'];
67 }
68
69 $url = trim( $url, ' "' );
70 $url = trim( $url );
71 $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
72
73 // 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.
74 $query_string_start = strpos( $url, '?' );
75
76 if ( false !== $query_string_start ) {
77 $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
78 }
79
80 return $url;
81 }
82 endif;
83
84 /**
85 * Merge in three string helper functions from WPCOM to make working with strings easier.
86 *
87 * @see WPCOM/wp-content/mu-plugins/string-helpers.php
88 */
89 if ( ! function_exists( 'wp_startswith' ) ) :
90 /**
91 * Check whether a string starts with a specific substring.
92 *
93 * @param string $haystack String we are filtering.
94 * @param string $needle The substring we are looking for.
95 * @return bool
96 */
97 function wp_startswith( $haystack, $needle ) {
98 if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
99 return false;
100 }
101
102 $haystack = (string) $haystack;
103 $needle = (string) $needle;
104
105 return str_starts_with( $haystack, $needle );
106 }
107 endif;
108
109 if ( ! function_exists( 'wp_endswith' ) ) :
110 /**
111 * Check whether a string ends with a specific substring.
112 *
113 * @param string $haystack String we are filtering.
114 * @param string $needle The substring we are looking for.
115 * @return bool
116 */
117 function wp_endswith( $haystack, $needle ) {
118 if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
119 return false;
120 }
121
122 $haystack = (string) $haystack;
123 $needle = (string) $needle;
124
125 return str_ends_with( $haystack, $needle );
126 }
127 endif;
128
129 if ( ! function_exists( 'wp_in' ) ) :
130 /**
131 * Checks whether a string contains a specific substring.
132 *
133 * @param string $needle The substring we are looking for.
134 * @param string $haystack String we are filtering.
135 * @return bool
136 */
137 function wp_in( $needle, $haystack ) {
138 if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
139 return false;
140 }
141
142 $haystack = (string) $haystack;
143 $needle = (string) $needle;
144
145 return str_contains( $haystack, $needle );
146 }
147 endif;
148
149 /**
150 * Deprecated connection function.
151 *
152 * @param string $text Deprecated.
153 * @deprecated 7.5 Use Connection_Manager instead.
154 */
155 function jetpack_sha1_base64( $text ) {
156 $connection = new Connection_Manager();
157 return $connection->sha1_base64( $text );
158 }
159