Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/shortcod...
File: flickr.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Flickr Short Code
[2] Fix | Delete
* Author: kellan
[3] Fix | Delete
* License: BSD/GPL/public domain (take your pick)
[4] Fix | Delete
*
[5] Fix | Delete
* [flickr video=www.flickr.com/photos/kalakeli/49931239842]
[6] Fix | Delete
* [flickr video=49931239842]
[7] Fix | Delete
* [flickr video=49931239842 w=200 h=150]
[8] Fix | Delete
* [flickr video=49931239842 autoplay="yes" controls="no"]
[9] Fix | Delete
* [flickr video=49931239842 autoplay="no" controls="yes" w=200 h=150]
[10] Fix | Delete
*
[11] Fix | Delete
* <div class="flick_video" style="max-width: 100%;width: 500px;height: 300px;"><video src="https://www.flickr.com/photos/kalakeli/49931239842/play/360p/183f75d545/" controls autoplay ></video></div>
[12] Fix | Delete
*
[13] Fix | Delete
* @package automattic/jetpack
[14] Fix | Delete
*/
[15] Fix | Delete
[16] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[17] Fix | Delete
exit( 0 );
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Transform embed to shortcode on save.
[22] Fix | Delete
*
[23] Fix | Delete
* @param string $content Post content.
[24] Fix | Delete
*
[25] Fix | Delete
* @return string Shortcode or the embed content itself.
[26] Fix | Delete
*/
[27] Fix | Delete
function flickr_embed_to_shortcode( $content ) {
[28] Fix | Delete
if ( ! is_string( $content ) ) {
[29] Fix | Delete
return $content;
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
if ( str_contains( $content, '<div class="flickr_video"' ) && str_contains( $content, '<video' ) ) {
[33] Fix | Delete
return jetpack_flickr_video_to_shortcode( $content );
[34] Fix | Delete
} elseif ( preg_match( '/<iframe src="(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/[^\"]+\"/', $content ) ) {
[35] Fix | Delete
return jetpack_flickr_photo_to_shortcode( $content );
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
return $content;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Transforms embed to shortcode on save when the photo param is used.
[43] Fix | Delete
* If embed content can not be transformed to a valid shortcode,
[44] Fix | Delete
* the embed content itself is returned.
[45] Fix | Delete
*
[46] Fix | Delete
* @param string $content Embed output.
[47] Fix | Delete
*
[48] Fix | Delete
* @return string Shortcode or the embed content.
[49] Fix | Delete
*/
[50] Fix | Delete
function jetpack_flickr_photo_to_shortcode( $content ) {
[51] Fix | Delete
preg_match( '/<iframe src=\"([^\"]+)\"(\s+height=\"([^\"]*)\")?(\s+width=\"([^\"]*)\")?/', $content, $matches );
[52] Fix | Delete
[53] Fix | Delete
if ( empty( $matches[1] ) ) {
[54] Fix | Delete
return $content;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
$src = esc_attr( str_replace( 'player/', '', $matches[1] ) );
[58] Fix | Delete
$height = empty( $matches[3] ) ? '' : esc_attr( $matches[3] );
[59] Fix | Delete
$width = empty( $matches[5] ) ? '' : esc_attr( $matches[5] );
[60] Fix | Delete
[61] Fix | Delete
/** This action is documented in modules/shortcodes/youtube.php */
[62] Fix | Delete
do_action( 'jetpack_embed_to_shortcode', 'flickr_photo', $src );
[63] Fix | Delete
[64] Fix | Delete
return '[flickr photo="' . $src . '" w=' . $width . ' h=' . $height . ']';
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Transforms embed to shortcode on save when the video param is used.
[69] Fix | Delete
* If embed content can not be transformed to a valid shortcode,
[70] Fix | Delete
* the embed content itself is returned.
[71] Fix | Delete
*
[72] Fix | Delete
* @param string $content Embed output.
[73] Fix | Delete
*
[74] Fix | Delete
* @return string Shortcode or the embed content.
[75] Fix | Delete
*/
[76] Fix | Delete
function jetpack_flickr_video_to_shortcode( $content ) {
[77] Fix | Delete
// Get video src.
[78] Fix | Delete
preg_match( '/<video src=\"([^\"]+)\"/', $content, $matches );
[79] Fix | Delete
[80] Fix | Delete
if ( empty( $matches[1] ) ) {
[81] Fix | Delete
return $content;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
preg_match( '/(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/photos\/([^\/]+)\/\d+\//', $matches[1], $matches );
[85] Fix | Delete
[86] Fix | Delete
$video_src = esc_attr( $matches[0] );
[87] Fix | Delete
[88] Fix | Delete
// Get width and height.
[89] Fix | Delete
[90] Fix | Delete
preg_match( '/style=\"max-width: 100%;(width:\s(\d+)px;)?(height:\s(\d+)px;)?/', $content, $matches );
[91] Fix | Delete
[92] Fix | Delete
$width = empty( $matches[2] ) ? '' : 'w=' . esc_attr( $matches[2] );
[93] Fix | Delete
[94] Fix | Delete
$height = empty( $matches[4] ) ? '' : 'h=' . esc_attr( $matches[4] );
[95] Fix | Delete
[96] Fix | Delete
$controls = str_contains( $content, 'controls' ) ? 'yes' : 'no';
[97] Fix | Delete
[98] Fix | Delete
$autoplay = str_contains( $content, 'autoplay' ) ? 'yes' : 'no';
[99] Fix | Delete
[100] Fix | Delete
/** This action is documented in modules/shortcodes/youtube.php */
[101] Fix | Delete
do_action( 'jetpack_embed_to_shortcode', 'flickr_video', $video_src );
[102] Fix | Delete
[103] Fix | Delete
return '[flickr video="' . $video_src . '" ' . $width . ' ' . $height . ' controls="' . $controls . '" autoplay="' . $autoplay . '"]';
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
if ( jetpack_shortcodes_should_hook_pre_kses() ) {
[107] Fix | Delete
add_filter( 'pre_kses', 'flickr_embed_to_shortcode' );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Flickr Shortcode handler.
[112] Fix | Delete
*
[113] Fix | Delete
* @param array $atts Shortcode attributes.
[114] Fix | Delete
*
[115] Fix | Delete
* @return string Shortcode Output.
[116] Fix | Delete
*/
[117] Fix | Delete
function flickr_shortcode_handler( $atts ) {
[118] Fix | Delete
$atts = shortcode_atts(
[119] Fix | Delete
array(
[120] Fix | Delete
'video' => 0,
[121] Fix | Delete
'photo' => 0,
[122] Fix | Delete
'w' => '',
[123] Fix | Delete
'h' => '',
[124] Fix | Delete
'controls' => 'yes',
[125] Fix | Delete
'autoplay' => '',
[126] Fix | Delete
),
[127] Fix | Delete
$atts,
[128] Fix | Delete
'flickr'
[129] Fix | Delete
);
[130] Fix | Delete
[131] Fix | Delete
if ( ! empty( $atts['video'] ) ) {
[132] Fix | Delete
$showing = 'video';
[133] Fix | Delete
$src = $atts['video'];
[134] Fix | Delete
} elseif ( ! empty( $atts['photo'] ) ) {
[135] Fix | Delete
$showing = 'photo';
[136] Fix | Delete
$src = $atts['photo'];
[137] Fix | Delete
} else {
[138] Fix | Delete
return '';
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
$src = str_replace( 'http://', 'https://', $src );
[142] Fix | Delete
[143] Fix | Delete
if ( 'video' === $showing ) {
[144] Fix | Delete
[145] Fix | Delete
$video_id = flick_shortcode_video_id( $src );
[146] Fix | Delete
[147] Fix | Delete
if ( empty( $video_id ) ) {
[148] Fix | Delete
return '';
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
$atts = array_map( 'esc_attr', $atts );
[152] Fix | Delete
return flickr_shortcode_video_markup( $atts, $video_id, $src );
[153] Fix | Delete
} elseif ( 'photo' === $showing ) {
[154] Fix | Delete
[155] Fix | Delete
if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
[156] Fix | Delete
return '';
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
$height = empty( $atts['h'] ) ? 'auto' : esc_attr( $atts['h'] );
[160] Fix | Delete
[161] Fix | Delete
$src = sprintf( '%s/player/', untrailingslashit( $src ) );
[162] Fix | Delete
[163] Fix | Delete
$allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen';
[164] Fix | Delete
[165] Fix | Delete
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
[166] Fix | Delete
$allow_full_screen = str_replace( ' oallowfullscreen msallowfullscreen', '', $allow_full_screen );
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
return sprintf( '<iframe src="%s" height="%s" width="%s" frameborder="0" %s></iframe>', esc_url( $src ), $height, esc_attr( $atts['w'] ), $allow_full_screen );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
return false;
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Return HTML markup for a Flickr embed.
[177] Fix | Delete
*
[178] Fix | Delete
* @param array $atts Shortcode attributes.
[179] Fix | Delete
* @param string $id Video ID.
[180] Fix | Delete
* @param string $video_param video param of the shortcode.
[181] Fix | Delete
*
[182] Fix | Delete
* @return string Shortcode ouput for video.
[183] Fix | Delete
*/
[184] Fix | Delete
function flickr_shortcode_video_markup( $atts, $id, $video_param ) {
[185] Fix | Delete
[186] Fix | Delete
$transient_name = "flickr_video_$id";
[187] Fix | Delete
$video_src = get_transient( $transient_name );
[188] Fix | Delete
[189] Fix | Delete
if ( empty( $video_src ) ) {
[190] Fix | Delete
$video_url = '';
[191] Fix | Delete
if ( ! is_numeric( $video_param ) ) {
[192] Fix | Delete
$video_url = $video_param;
[193] Fix | Delete
} else {
[194] Fix | Delete
// Get the URL of the video from the page of the video.
[195] Fix | Delete
$video_page_content = wp_remote_get( "https://flickr.com/photo.gne?id=$video_param" );
[196] Fix | Delete
// Bail if we do not get any info from Flickr.
[197] Fix | Delete
if ( is_wp_error( $video_page_content ) ) {
[198] Fix | Delete
return '';
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
// Extract the URL from the og:url meta tag.
[202] Fix | Delete
preg_match( '/property=\"og:url\"\scontent=\"([^\"]+)\"/', $video_page_content['body'], $matches );
[203] Fix | Delete
if ( empty( $matches[1] ) ) {
[204] Fix | Delete
return '';
[205] Fix | Delete
}
[206] Fix | Delete
$video_url = $matches[1];
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
$provider = 'https://www.flickr.com/services/oembed/';
[210] Fix | Delete
$oembed = _wp_oembed_get_object();
[211] Fix | Delete
$data = (array) $oembed->fetch( $provider, $video_url );
[212] Fix | Delete
if ( empty( $data['html'] ) ) {
[213] Fix | Delete
return '';
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
// Get the embed url.
[217] Fix | Delete
preg_match( '/src=\"([^\"]+)\"/', $data['html'], $matches );
[218] Fix | Delete
[219] Fix | Delete
if ( empty( $matches[1] ) ) {
[220] Fix | Delete
return '';
[221] Fix | Delete
}
[222] Fix | Delete
$embed_url = $matches[1];
[223] Fix | Delete
[224] Fix | Delete
$embed_page = wp_remote_get( $embed_url );
[225] Fix | Delete
[226] Fix | Delete
// Bail if the request returns an error.
[227] Fix | Delete
if ( ! is_array( $embed_page ) ) {
[228] Fix | Delete
return '';
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
// Get the video url from embed html markup.
[232] Fix | Delete
[233] Fix | Delete
preg_match( '/video.+src=\"([^\"]+)\"/', $embed_page['body'], $matches );
[234] Fix | Delete
if ( ! empty( $matches[1] ) ) {
[235] Fix | Delete
$video_src = $matches[1];
[236] Fix | Delete
set_transient( $transient_name, $video_src, 2592000 ); // 30 days transient.
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
$style = 'max-width: 100%;';
[241] Fix | Delete
[242] Fix | Delete
if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) {
[243] Fix | Delete
$style .= sprintf( 'width: %dpx;', $atts['w'] );
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) {
[247] Fix | Delete
$style .= sprintf( 'height: %dpx;', $atts['h'] );
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
$controls = 'yes' === $atts['controls'] ? 'controls' : '';
[251] Fix | Delete
$autoplay = 'yes' === $atts['autoplay'] ? 'autoplay' : '';
[252] Fix | Delete
[253] Fix | Delete
return sprintf(
[254] Fix | Delete
'<div class="flick_video" style="%s"><video src="%s" %s %s /></div>',
[255] Fix | Delete
esc_attr( $style ),
[256] Fix | Delete
esc_attr( $video_src ),
[257] Fix | Delete
$controls,
[258] Fix | Delete
$autoplay
[259] Fix | Delete
);
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Extract the id of the flickr video from the video param.
[264] Fix | Delete
*
[265] Fix | Delete
* @param string $video_param Video parameter of the shortcode.
[266] Fix | Delete
*
[267] Fix | Delete
* @return string|boolean ID of the video or false in case the ID can not be extracted.
[268] Fix | Delete
*/
[269] Fix | Delete
function flick_shortcode_video_id( $video_param ) {
[270] Fix | Delete
if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $video_param ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $video_param ) ) {
[271] Fix | Delete
[272] Fix | Delete
// Extract the video id from the url.
[273] Fix | Delete
preg_match( '/\d+/', $video_param, $matches );
[274] Fix | Delete
[275] Fix | Delete
if ( empty( $matches ) ) {
[276] Fix | Delete
return false;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
return $matches[0];
[280] Fix | Delete
[281] Fix | Delete
} elseif ( is_numeric( $video_param ) ) {
[282] Fix | Delete
return $video_param;
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
return false;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
add_shortcode( 'flickr', 'flickr_shortcode_handler' );
[289] Fix | Delete
[290] Fix | Delete
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds.
[291] Fix | Delete
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
[292] Fix | Delete
[293] Fix | Delete
/**
[294] Fix | Delete
* Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
[295] Fix | Delete
*
[296] Fix | Delete
* @since 3.9
[297] Fix | Delete
*
[298] Fix | Delete
* @param array $matches Regex partial matches against the URL passed.
[299] Fix | Delete
* @param array $attr Attributes received in embed response.
[300] Fix | Delete
* @param string $url Requested URL to be embedded.
[301] Fix | Delete
*
[302] Fix | Delete
* @return string Return output of Vimeo shortcode with the proper markup.
[303] Fix | Delete
*/
[304] Fix | Delete
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
[305] Fix | Delete
/*
[306] Fix | Delete
* Legacy slideshow embeds end with /show/
[307] Fix | Delete
* e.g. https://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
[308] Fix | Delete
*/
[309] Fix | Delete
if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
[310] Fix | Delete
// These lookups need cached, as they don't use WP_Embed (which caches).
[311] Fix | Delete
$cache_key = md5( $url . wp_json_encode( $attr ) );
[312] Fix | Delete
$cache_group = 'oembed_flickr';
[313] Fix | Delete
[314] Fix | Delete
$html = wp_cache_get( $cache_key, $cache_group );
[315] Fix | Delete
[316] Fix | Delete
if ( false === $html ) {
[317] Fix | Delete
$html = _wp_oembed_get_object()->get_html( $url, $attr );
[318] Fix | Delete
[319] Fix | Delete
wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
return $html;
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
return flickr_shortcode_handler( array( 'photo' => $url ) );
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function