Skip to content

Commit

Permalink
Shortcodes: return original HTML and improve regex (#14074)
Browse files Browse the repository at this point in the history

Co-authored-by: Michael D Adams <michael.d.adams@gmail.com>
  • Loading branch information
jeherve and mdawaffe committed Nov 19, 2019
1 parent 9265ccd commit 5e38904
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
19 changes: 12 additions & 7 deletions modules/shortcodes/class.filter-embedded-html-objects.php
Expand Up @@ -222,17 +222,22 @@ public static function unregister( $match ) {
* @param array $matches Array of matches.
*/
private static function dispatch_entities( $matches ) {
$matches[0] = html_entity_decode( $matches[0] );
$orig_html = $matches[0];
$decoded_matches = array( html_entity_decode( $matches[0] ) );

return self::dispatch( $matches );
return self::dispatch( $decoded_matches, $orig_html );
}

/**
* Filter and replace HTML element.
*
* @param array $matches Array of matches.
* @param array $matches Array of matches.
* @param string $orig_html Original html. Returned if no results are found via $matches processing.
*/
private static function dispatch( $matches ) {
private static function dispatch( $matches, $orig_html = null ) {
if ( null === $orig_html ) {
$orig_html = $matches[0];
}
$html = preg_replace( '%&#0*58;//%', '://', $matches[0] );
$attrs = self::get_attrs( $html );
if ( isset( $attrs['src'] ) ) {
Expand All @@ -253,7 +258,7 @@ private static function dispatch( $matches ) {
}
}

return $matches[0];
return $orig_html;
}

$src = trim( $src );
Expand Down Expand Up @@ -299,11 +304,11 @@ private static function dispatch( $matches ) {
// Keep the failed match so we can later replace it with a link,
// but return the original content to give others a chance too.
self::$failed_embeds[] = array(
'match' => $matches[0],
'match' => $orig_html,
'src' => esc_url( $src ),
);

return $matches[0];
return $orig_html;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/shortcodes/dailymotion.php
Expand Up @@ -51,7 +51,7 @@ function dailymotion_embed_to_shortcode( $content ) {
}

$id = basename( substr( $src, strlen( 'www.dailymotion.com/swf' ) ) );
$id = preg_replace( '/[^a-z0-9].*$/i', '', $id );
$id = preg_replace( '/[^a-z0-9].*$/is', '', $id );

$content = str_replace( $match[0], "[dailymotion id=$id]", $content );
/** This action is documented in modules/shortcodes/youtube.php */
Expand Down
13 changes: 11 additions & 2 deletions modules/shortcodes/flickr.php
Expand Up @@ -69,7 +69,13 @@ function flickr_embed_to_shortcode( $content ) {
continue;
}

$code_atts = array( 'video' => $flashvars['photo_id'] );
$photo_id = preg_replace( '#[^A-Za-z0-9_./@+-]+#', '', $flashvars['photo_id'] );

if ( ! strlen( $photo_id ) ) {
continue;
}

$code_atts = array( 'video' => $photo_id );

if (
isset( $flashvars['flickr_show_info_box'] )
Expand All @@ -79,7 +85,10 @@ function flickr_embed_to_shortcode( $content ) {
}

if ( ! empty( $flashvars['photo_secret'] ) ) {
$code_atts['secret'] = $flashvars['photo_secret'];
$photo_secret = preg_replace( '#[^A-Za-z0-9_./@+-]+#', '', $flashvars['photo_secret'] );
if ( strlen( $photo_secret ) ) {
$code_atts['secret'] = $photo_secret;
}
}

if ( ! empty( $params['width']['value'] ) ) {
Expand Down

0 comments on commit 5e38904

Please sign in to comment.