Advertisement
kotuha

YouTube Embed plugin

Mar 12th, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Youtube Embed Plugin
  4.  *
  5.  * Based on Cory Webb's Youtube Embedding plugin ( http://www.corywebbmedia.com/ )
  6.  *
  7.  * - improved regular expression
  8.  * - override default width & height by adding #width,height to the youtube link
  9.  * - don't parse youtube url inside a link
  10.  *
  11.  * @version     $Id: youtubeembed.php 5 2011-03-12 21:40:29Z kotuha $
  12.  * @author      Steven Rombauts ( @kotuhaweb )
  13.  * @email       [email protected]
  14.  */
  15.  
  16. defined( '_JEXEC' ) or die( 'Restricted access' );
  17.  
  18. jimport( 'joomla.plugin.plugin' );
  19.  
  20. class plgContentYoutubeEmbed extends JPlugin
  21. {
  22.  
  23.     function plgContentYoutubeEmbed(&$subject, $params)
  24.     {
  25.         parent::__construct($subject, $params);
  26.     }
  27.  
  28.     function onPrepareContent(&$article, &$params, $limitstart)
  29.     {  
  30.         if(strpos($article->text, 'http://www.youtube.com/') === false && strpos($article->text, 'http://youtube.com/') === false) {
  31.             return;
  32.         }
  33.  
  34.         $pattern = '/(?<!href\=\"|\')(http:\/\/w{0,3}\.{0,1}youtube\.com\/watch\?([a-zA-Z0-9=&;]+)(\#?([0-9,]+))?)/ie';
  35.        
  36.         $article->text = preg_replace($pattern,
  37.                         '$this->getEmbedCode("\2", "\4")',
  38.                         $article->text
  39.                     );
  40.    
  41.         return;
  42.     }
  43.  
  44.     function getEmbedCode($query, $hash)
  45.     {
  46.         $query = str_replace('&amp;', '&', $query);
  47.         $pairs = explode('&', $query);
  48.  
  49.         $arguments = array();
  50.         foreach($pairs as $pair)
  51.         {
  52.             $parts = explode('=', $pair);
  53.             $arguments[$parts[0]] = $parts[1];
  54.         }
  55.    
  56.         $dimension = $this->getDimension($hash);
  57.    
  58.         return '<object width="'.$dimension->width.'" height="'.$dimension->height.'"><param name="movie" value="http://www.youtube.com/v/'.$arguments['v'].'"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/'.$arguments['v'].'" type="application/x-shockwave-flash" allowfullscreen="true" width="'.$dimension->width.'" height="'.$dimension->height.'"></embed></object>';
  59.     }
  60.    
  61.     function getDimension($hash)
  62.     {  
  63.         $plugin =& JPluginHelper::getPlugin('content', 'youtubeembed');
  64.         $params = new JParameter( $plugin->params );
  65.      
  66.         $dimension = new stdClass();
  67.         $dimension->width = $params->get('width', 425);
  68.         $dimension->height = $params->get('height', 344);
  69.        
  70.         if(strpos($hash, ',') !== false)
  71.         {
  72.             $dimensions = explode(',', $hash);
  73.             $dimension->width = $dimensions[0];
  74.             $dimension->height = $dimensions[1];
  75.         }
  76.        
  77.         return $dimension;
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement