Advertisement
designbymerovingi

Parse YouTube Embed Code for myCRED

Feb 13th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. /**
  2.  * Convert YouTube Embed Code
  3.  * Will convert an embed code for a YouTube video
  4.  * to be used with the myCRED Points for viewing videos hook.
  5.  * Will use your default payout logic set in your hook.
  6.  * @version 1.0.2
  7.  */
  8. function mycred_pro_parse_video_embed( $code = '' ) {
  9.  
  10.     preg_match( '/width="(\d*)" height="(\d*)" src="(.*?)"/i', $code, $sizes );
  11.  
  12.     if ( $sizes ) {
  13.  
  14.         // First we check if the video is a YouTube video
  15.         preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\‌​n]+|(?<=youtu.be/)[^&\n]+#", $sizes[3], $matches );
  16.  
  17.         if ( $matches ) {
  18.  
  19.             return mycred_render_shortcode_video( array(
  20.                 'width'  => $sizes[1],
  21.                 'height' => $sizes[2],
  22.                 'id'     => str_replace( '?rel=0', '', $matches[0] )
  23.             ) );
  24.  
  25.         }
  26.  
  27.         // Not a YouTube video, maybe it's a Vimeo Video? (requires myCRED Video Add-on)
  28.         else {
  29.  
  30.             preg_match( '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/', $sizes[3], $matches );
  31.  
  32.             if ( $matches ) {
  33.  
  34.                 return mycred_render_shortcode_video( array(
  35.                     'width'  => $sizes[1],
  36.                     'height' => $sizes[2],
  37.                     'id'     => str_replace( '?rel=0', '', $matches[0] )
  38.                 ) );
  39.  
  40.             }
  41.  
  42.         }
  43.  
  44.     }
  45.  
  46.     return $code;
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement