| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Name: Shortcode Embeds |
| 4 |
* Module Description: Easily embed rich media like YouTube videos and tweets using simple shortcodes. |
| 5 |
* Sort Order: 3 |
| 6 |
* First Introduced: 1.1 |
| 7 |
* Major Changes In: 1.2 |
| 8 |
* Requires Connection: No |
| 9 |
* Auto Activate: No |
| 10 |
* Module Tags: Photos and Videos, Social, Writing, Appearance |
| 11 |
* Feature: Writing |
| 12 |
* Additional Search Queries: shortcodes, shortcode, embeds, media, bandcamp, dailymotion, facebook, flickr, google calendars, google maps, polldaddy, recipe, recipes, scribd, slideshare, slideshow, slideshows, soundcloud, ted, twitter, vimeo, vine, youtube |
| 13 |
* |
| 14 |
* @package automattic/jetpack |
| 15 |
*/ |
| 16 |
|
| 17 |
/** |
| 18 |
* Transforms the $atts array into a string that the old functions expected |
| 19 |
* |
| 20 |
* The old way was: |
| 21 |
* [shortcode a=1&b=2&c=3] or [shortcode=1] |
| 22 |
* This is parsed as array( a => '1&b=2&c=3' ) and array( 0 => '=1' ), which is useless |
| 23 |
* |
| 24 |
* @param array $params Array of old shortcode parameters. |
| 25 |
* @param bool $old_format_support true if [shortcode=foo] format is possible. |
| 26 |
* |
| 27 |
* @return string $params |
| 28 |
*/ |
| 29 |
function shortcode_new_to_old_params( $params, $old_format_support = false ) { |
| 30 |
$str = ''; |
| 31 |
|
| 32 |
if ( $old_format_support && isset( $params[0] ) ) { |
| 33 |
$str = ltrim( $params[0], '=' ); |
| 34 |
} elseif ( is_array( $params ) ) { |
| 35 |
foreach ( array_keys( $params ) as $key ) { |
| 36 |
if ( ! is_numeric( $key ) ) { |
| 37 |
$str = $key . '=' . $params[ $key ]; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return str_replace( array( '&', '&' ), '&', $str ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Load all available Jetpack shortcode files. |
| 47 |
*/ |
| 48 |
function jetpack_load_shortcodes() { |
| 49 |
// Prevent third-party shortcode plugins when loading shortcode files. |
| 50 |
// Format: shortcode => condition_when_to_skip |
| 51 |
$shortcode_skips = array( |
| 52 |
'soundcloud' => function_exists( 'soundcloud_shortcode' ), // SoundCloud Shortcodes plugin |
| 53 |
); |
| 54 |
|
| 55 |
$shortcode_includes = array(); |
| 56 |
|
| 57 |
foreach ( Jetpack::glob_php( __DIR__ . '/shortcodes' ) as $file ) { |
| 58 |
$filename = substr( basename( $file ), 0, -4 ); |
| 59 |
|
| 60 |
if ( empty( $shortcode_skips[ $filename ] ) ) { |
| 61 |
$shortcode_includes[ $filename ] = $file; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* This filter allows other plugins to override which shortcodes Jetpack loads. |
| 67 |
* |
| 68 |
* Fires as part of the `after_setup_theme` WP hook, so modifying code needs to be in a plugin, not in a theme's functions.php. |
| 69 |
* |
| 70 |
* @module shortcodes |
| 71 |
* |
| 72 |
* @since 2.2.1 |
| 73 |
* @since 4.2.0 Added filename without extension as array key. |
| 74 |
* |
| 75 |
* @param array $shortcode_includes An array of which shortcodes to include. |
| 76 |
*/ |
| 77 |
$shortcode_includes = apply_filters( 'jetpack_shortcodes_to_include', $shortcode_includes ); |
| 78 |
|
| 79 |
foreach ( $shortcode_includes as $include ) { |
| 80 |
include_once $include; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Runs preg_replace so that replacements don't happen within open tags. |
| 86 |
* Parameters are the same as preg_replace, with an added optional search param for improved performance |
| 87 |
* |
| 88 |
* @param string $pattern Pattern to search for. |
| 89 |
* @param string $replacement String to replace. |
| 90 |
* @param string $content Post content. |
| 91 |
* @param string $search String to search for. |
| 92 |
* |
| 93 |
* @return string $content Replaced post content. |
| 94 |
*/ |
| 95 |
function jetpack_preg_replace_outside_tags( $pattern, $replacement, $content, $search = null ) { |
| 96 |
if ( $search && ! str_contains( $content, $search ) ) { |
| 97 |
return $content; |
| 98 |
} |
| 99 |
|
| 100 |
$textarr = wp_html_split( $content ); |
| 101 |
unset( $content ); |
| 102 |
foreach ( $textarr as &$element ) { |
| 103 |
if ( '' === $element || '<' === $element[0] ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
$element = preg_replace( $pattern, $replacement, $element ); |
| 107 |
} |
| 108 |
|
| 109 |
return implode( $textarr ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Runs preg_replace_callback so that replacements don't happen within open tags. |
| 114 |
* Parameters are the same as preg_replace, with an added optional search param for improved performance. |
| 115 |
* |
| 116 |
* @param string $pattern Pattern to search for. |
| 117 |
* @param string $callback Callback returning the replacement string. |
| 118 |
* @param string $content Post content. |
| 119 |
* @param string $search String to search for. |
| 120 |
* |
| 121 |
* @return string $content Replaced post content. |
| 122 |
*/ |
| 123 |
function jetpack_preg_replace_callback_outside_tags( $pattern, $callback, $content, $search = null ) { |
| 124 |
if ( $search && ! str_contains( $content, $search ) ) { |
| 125 |
return $content; |
| 126 |
} |
| 127 |
|
| 128 |
$textarr = wp_html_split( $content ); |
| 129 |
unset( $content ); |
| 130 |
foreach ( $textarr as &$element ) { |
| 131 |
if ( '' === $element || '<' === $element[0] ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
$element = preg_replace_callback( $pattern, $callback, $element ); |
| 135 |
} |
| 136 |
|
| 137 |
return implode( $textarr ); |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! function_exists( 'jetpack_shortcode_get_wpvideo_id' ) ) { |
| 141 |
/** |
| 142 |
* Get VideoPress ID from wpvideo shortcode attributes. |
| 143 |
* |
| 144 |
* @param array $atts Shortcode attributes. |
| 145 |
* |
| 146 |
* @return string|int $id VideoPress ID. |
| 147 |
*/ |
| 148 |
function jetpack_shortcode_get_wpvideo_id( $atts ) { |
| 149 |
if ( isset( $atts[0] ) ) { |
| 150 |
return $atts[0]; |
| 151 |
} else { |
| 152 |
return 0; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ( ! function_exists( 'jetpack_shortcode_get_videopress_id' ) ) { |
| 158 |
/** |
| 159 |
* Get VideoPress ID from videopress shortcode attributes. |
| 160 |
* |
| 161 |
* @param array $atts Shortcode attributes. |
| 162 |
* @return int $id VideoPress ID. |
| 163 |
*/ |
| 164 |
function jetpack_shortcode_get_videopress_id( $atts ) { |
| 165 |
if ( isset( $atts[0] ) ) { |
| 166 |
return $atts[0]; |
| 167 |
} else { |
| 168 |
return 0; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Common element attributes parsing and sanitizing for src, width and height. |
| 175 |
* |
| 176 |
* @since 4.5.0 |
| 177 |
* |
| 178 |
* @param array $attrs With original values. |
| 179 |
* |
| 180 |
* @return array $attrs With sanitized values. |
| 181 |
*/ |
| 182 |
function wpcom_shortcodereverse_parseattr( $attrs ) { |
| 183 |
$defaults = array( |
| 184 |
'src' => false, |
| 185 |
'width' => false, |
| 186 |
'height' => false, |
| 187 |
); |
| 188 |
|
| 189 |
$attrs = shortcode_atts( $defaults, $attrs ); |
| 190 |
|
| 191 |
$attrs['src'] = wp_strip_all_tags( $attrs['src'] ); // For sanity. |
| 192 |
$attrs['width'] = ( is_numeric( $attrs['width'] ) ) ? abs( (int) $attrs['width'] ) : $defaults['width']; |
| 193 |
$attrs['height'] = ( is_numeric( $attrs['height'] ) ) ? abs( (int) $attrs['height'] ) : $defaults['height']; |
| 194 |
|
| 195 |
return $attrs; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* When an embed service goes away, we can use this handler |
| 200 |
* to output a link for history's sake. |
| 201 |
* |
| 202 |
* @param array $matches Regex partial matches against the URL passed. |
| 203 |
* @param array $attr Attributes received in embed response. |
| 204 |
* @param string $url Requested URL to be embedded. |
| 205 |
* @return string Link to output. |
| 206 |
*/ |
| 207 |
function jetpack_deprecated_embed_handler( $matches, $attr, $url ) { |
| 208 |
return sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( esc_url( $url ) ) ); |
| 209 |
} |
| 210 |
|
| 211 |
jetpack_load_shortcodes(); |
| 212 |
|