PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.1
Jetpack – WP Security, Backup, Speed, & Growth v12.1
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 in Jetpack – WP Security, Backup, Speed, & Growth 12.1, at functions.compat.php

166 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
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 $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 $url YouTube URL.
62 * @return string The normalized URL
63 */
64 function youtube_sanitize_url( $url ) {
65 $url = trim( $url, ' "' );
66 $url = trim( $url );
67 $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
68
69 // 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.
70 $query_string_start = strpos( $url, '?' );
71
72 if ( false !== $query_string_start ) {
73 $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
74 }
75
76 return $url;
77 }
78 endif;
79
80 /**
81 * Merge in three string helper functions from WPCOM to make working with strings easier.
82 *
83 * @see WPCOM/wp-content/mu-plugins/string-helpers.php
84 */
85 if ( ! function_exists( 'wp_startswith' ) ) :
86 /**
87 * Check whether a string starts with a specific substring.
88 *
89 * @param string $haystack String we are filtering.
90 * @param string $needle The substring we are looking for.
91 * @return bool
92 */
93 function wp_startswith( $haystack, $needle ) {
94 if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
95 return false;
96 }
97
98 $haystack = (string) $haystack;
99 $needle = (string) $needle;
100
101 if ( function_exists( 'str_starts_with' ) ) { // remove when PHP 8.0 is the minimum supported.
102 return str_starts_with( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions
103 }
104 return 0 === strpos( $haystack, $needle );
105 }
106 endif;
107
108 if ( ! function_exists( 'wp_endswith' ) ) :
109 /**
110 * Check whether a string ends with a specific substring.
111 *
112 * @param string $haystack String we are filtering.
113 * @param string $needle The substring we are looking for.
114 * @return bool
115 */
116 function wp_endswith( $haystack, $needle ) {
117 if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
118 return false;
119 }
120
121 $haystack = (string) $haystack;
122 $needle = (string) $needle;
123
124 if ( function_exists( 'str_ends_with' ) ) { // remove when PHP 8.0 is the minimum supported.
125 return str_ends_with( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions
126
127 }
128 return $needle === substr( $haystack, -strlen( $needle ) );
129 }
130 endif;
131
132 if ( ! function_exists( 'wp_in' ) ) :
133 /**
134 * Checks whether a string contains a specific substring.
135 *
136 * @param string $needle The substring we are looking for.
137 * @param string $haystack String we are filtering.
138 * @return bool
139 */
140 function wp_in( $needle, $haystack ) {
141 if ( ! $haystack || ! $needle || ! is_scalar( $haystack ) || ! is_scalar( $needle ) ) {
142 return false;
143 }
144
145 $haystack = (string) $haystack;
146 $needle = (string) $needle;
147
148 if ( function_exists( 'str_contains' ) ) { // remove when PHP 8.0 is the minimum supported.
149 return str_contains( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions
150 }
151
152 return false !== strpos( $haystack, $needle );
153 }
154 endif;
155
156 /**
157 * Deprecated connection function.
158 *
159 * @param string $text Deprecated.
160 * @deprecated 7.5 Use Connection_Manager instead.
161 */
162 function jetpack_sha1_base64( $text ) {
163 $connection = new Connection_Manager();
164 return $connection->sha1_base64( $text );
165 }
166