Brak opisu

flickr.php 9.2KB

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