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

134 lines 3.5 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 var $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 return 0 === strpos( $haystack, $needle );
95 }
96 endif;
97
98 if ( ! function_exists( 'wp_endswith' ) ) :
99 /**
100 * Check whether a string ends with a specific substring.
101 *
102 * @param var $haystack String we are filtering.
103 * @param string $needle The substring we are looking for.
104 * @return bool
105 */
106 function wp_endswith( $haystack, $needle ) {
107 return $needle === substr( $haystack, -strlen( $needle ) );
108 }
109 endif;
110
111 if ( ! function_exists( 'wp_in' ) ) :
112 /**
113 * Checks whether a string contains a specific substring.
114 *
115 * @param string $needle The substring we are looking for.
116 * @param var $haystack String we are filtering.
117 * @return bool
118 */
119 function wp_in( $needle, $haystack ) {
120 return false !== strpos( $haystack, $needle );
121 }
122 endif;
123
124 /**
125 * Deprecated connection function.
126 *
127 * @param string $text Deprecated.
128 * @deprecated 7.5 Use Connection_Manager instead.
129 */
130 function jetpack_sha1_base64( $text ) {
131 $connection = new Connection_Manager();
132 return $connection->sha1_base64( $text );
133 }
134