YouTube and Google Video as well as videos you host yourself, into WordPress. Credits: Owen Winkler for ButtonSnap and An-archos for help with WP 2.1+ button code. Author: Viper007Bond Author URI: http://www.viper007bond.com/ */ # Nothing to see here! Please use the plugin's options page. You can configure everything there. class VipersVideoQuicktags { var $version = '5.0.0'; var $folder = '/wp-content/plugins/vipers-video-quicktags'; // You shouldn't need to change this ;) var $fullfolderurl; var $settings = array(); var $defaultsettings = array(); var $twopointoneplus; // Initialization stuff function VipersVideoQuicktags() { global $wp_version; $this->fullfolderurl = get_bloginfo('wpurl') . $this->folder . '/'; $this->defaultsettings = array( 'youtube' => array( 'button' => 'on', 'width' => '425', 'height' => '335', ), 'googlevideo' => array( 'button' => 'on', 'width' => '400', 'height' => '326', ), 'ifilm' => array( 'button' => NULL, 'width' => '448', 'height' => '365', ), 'metacafe' => array( 'button' => NULL, 'width' => '400', 'height' => '345', ), 'myspace' => array( 'button' => NULL, 'width' => '430', 'height' => '346', ), 'quicktime' => array( 'button' => NULL, ), 'videofile' => array( 'button' => NULL, ), 'flv' => array( 'button' => NULL, ), 'tinymce_linenumber' => 1, 'usewmp' => 'on', 'alignment' => 'center', ); $this->settings = get_option('vvq_options'); if ( !is_array($this->settings['youtube']) ) { $this->settings = $this->defaultsettings; } // Set required options added in later versions for people upgrading if ( empty($this->settings['alignment']) ) $this->settings['alignment'] = $this->defaultsettings['alignment']; // Load up the localization file if we're using WordPress in a different language // Place it in the "localization" folder and name it "vvq-[value in wp-config].mo" // A link to some localization files is location at the homepage of this plugin load_plugin_textdomain('vvq', $this->folder . '/localization'); // No sense in running the addbuttons() function if no buttons are to be displayed if ( TRUE === $this->anybuttons() ) add_action('init', array(&$this, 'addbuttons')); // Are we running at least WordPress 2.1? $this->twopointoneplus = version_compare($wp_version, '2.1.0', '>='); // Loads the needed Javascript file if ( !is_admin() && TRUE == $this->twopointoneplus ) wp_enqueue_script('vvq', $this->folder . '/vipers_videoquicktags.js', FALSE, $this->version); // And lastly, register our hooks and filter add_action('admin_menu', array(&$this, 'admin_menu')); add_action('admin_head', array(&$this, 'admin_head')); add_action('edit_form_advanced', array(&$this, 'edit_form')); add_action('edit_page_form', array(&$this, 'edit_form')); add_action('wp_head', array(&$this, 'wp_head')); add_filter('the_content', array(&$this, 'replacebbcode'), 1); } // Checks to see if any buttons at all are to be displayed function anybuttons() { if ('on' == $this->settings['youtube']['button'] || 'on' == $this->settings['googlevideo']['button'] || 'on' == $this->settings['ifilm']['button'] || 'on' == $this->settings['metacafe']['button'] || 'on' == $this->settings['myspace']['button'] || 'on' == $this->settings['quicktime']['button'] || 'on' == $this->settings['videofile']['button'] || 'on' == $this->settings['flv']['button'] ) return TRUE; else return FALSE; } // Make our buttons on the write screens function addbuttons() { // Don't bother doing this stuff if the current user lacks permissions as they'll never see the pages if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) return; // If WordPress 2.1+ and using TinyMCE, we need to insert the buttons differently if ( TRUE == $this->twopointoneplus && 'true' == get_user_option('rich_editing') ) { // Load and append our TinyMCE external plugin add_filter('mce_plugins', array(&$this, 'mce_plugins')); if ( 1 != $this->settings['tinymce_linenumber'] ) { add_filter('mce_buttons_' . $this->settings['tinymce_linenumber'], array(&$this, 'mce_buttons')); } else { add_filter('mce_buttons', array(&$this, 'mce_buttons')); } add_action('tinymce_before_init', array(&$this, 'tinymce_before_init')); add_action('admin_head', array(&$this, 'buttonhider')); } else { buttonsnap_separator(); if ( 'on' == $this->settings['youtube']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/youtube.png', __('YouTube', 'vvq'), "VVQInsertVideoSite('" . __('YouTube', 'vvq') . "', 'http://www.youtube.com/watch?v=JzqumbhfxRo', 'youtube');"); if ( 'on' == $this->settings['googlevideo']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/googlevideo.png', __('GVideo', 'vvq'), "VVQInsertVideoSite('" . __('Google Video', 'vvq') . "', 'http://video.google.com/videoplay?docid=3688185030664621355', 'googlevideo');"); if ( 'on' == $this->settings['ifilm']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/ifilm.png', __('IFILM', 'vvq'), "VVQInsertVideoSite('" . __('IFILM', 'vvq') . "', 'http://www.ifilm.com/video/2710582', 'ifilm');"); if ( 'on' == $this->settings['metacafe']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/metacafe.png', __('Metacafe', 'vvq'), "VVQInsertVideoSite('" . __('Metacafe', 'vvq') . "', 'http://www.metacafe.com/watch/299980/italian_police_lamborghini/', 'metacafe');"); if ( 'on' == $this->settings['myspace']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/myspace.png', __('MySpace', 'vvq'), "VVQInsertVideoSite('" . __('MySpace', 'vvq') . "', 'http://vids.myspace.com/index.cfm?fuseaction=vids.individual&videoid=1387215221', 'myspace');"); if ( 'on' == $this->settings['quicktime']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/quicktime.png', __('QT', 'vvq'), "VVQInsertVideoFile('" . __('Quicktime', 'vvq') . "', 'mov', 'quicktime');"); if ( 'on' == $this->settings['videofile']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/videofile.png', __('Video File', 'vvq'), 'VVQInsertVideoFile();'); if ( 'on' == $this->settings['flv']['button'] ) buttonsnap_jsbutton($this->fullfolderurl . 'images/flv.png', __('FLV', 'vvq'), "VVQInsertVideoFile('" . __('FLV', 'vvq') . "', 'flv', 'flv');"); } } // Add buttons in WordPress v2.1+, thanks to An-archos function mce_plugins($plugins) { array_push($plugins, '-vipersvideoquicktags'); return $plugins; } function mce_buttons($buttons) { if ( 1 == $this->settings['tinymce_linenumber'] ) array_push($buttons, 'separator'); array_push($buttons, 'vipersvideoquicktags'); return $buttons; } function tinymce_before_init() { echo "tinyMCE.loadPlugin('vipersvideoquicktags', '" . $this->fullfolderurl . "resources/tinymce/');\n"; } // Hide buttons the user doesn't want to see in WP v2.1+ function buttonhider() { echo "\n"; } function admin_menu() { // The spaces to   is done so that the menu tab doesn't ever end up on two lines add_options_page(__("Viper's Video Quicktags Configuration", 'vvq'), str_replace(' ', ' ', __('Video Quicktags', 'vvq')), 'manage_options', basename(__FILE__), array(&$this, 'optionspage')); } // Handle form submit and add the Javascript file function admin_head() { // Handle options page submits if ( $_POST && basename(__FILE__) == $_GET['page'] ) { if ( $_POST['defaults'] ) { $this->settings = $this->defaultsettings; } else { $this->settings = array( 'youtube' => array( 'button' => $_POST['youtube']['button'], 'width' => (int) $_POST['youtube']['width'], 'height' => (int) $_POST['youtube']['height'], ), 'googlevideo' => array( 'button' => $_POST['googlevideo']['button'], 'width' => (int) $_POST['googlevideo']['width'], 'height' => (int) $_POST['googlevideo']['height'], ), 'ifilm' => array( 'button' => $_POST['ifilm']['button'], 'width' => (int) $_POST['ifilm']['width'], 'height' => (int) $_POST['ifilm']['height'], ), 'metacafe' => array( 'button' => $_POST['metacafe']['button'], 'width' => (int) $_POST['metacafe']['width'], 'height' => (int) $_POST['metacafe']['height'], ), 'myspace' => array( 'button' => $_POST['myspace']['button'], 'width' => (int) $_POST['myspace']['width'], 'height' => (int) $_POST['myspace']['height'], ), 'quicktime' => array( 'button' => $_POST['quicktime']['button'], ), 'videofile' => array( 'button' => $_POST['videofile']['button'], ), 'flv' => array( 'button' => $_POST['flv']['button'], ), 'tinymce_linenumber' => (int) $_POST['tinymce_linenumber'], 'usewmp' => $_POST['usewmp'], 'alignment' => $_POST['alignment'], ); } update_option('vvq_options', $this->settings); } } // Outputs the needed Javascript (not in a .js file as it's dynamic and just easier this way) function edit_form() { ?>

' . __('Options reset to defaults.', 'vvq') . '

' . "\n"; elseif ($_POST) echo "\n" . '

' . __('Options saved.') . '

' . "\n"; ?>

twopointoneplus ) ? 'class="widefat"' : 'width="100%" cellpadding="3" cellspacing="3"'; ?> style="text-align: center">
settings['youtube']['button'], 'on'); ?> />
settings['googlevideo']['button'], 'on'); ?> />
settings['ifilm']['button'], 'on'); ?> />
settings['metacafe']['button'], 'on'); ?> />
settings['myspace']['button'], 'on'); ?> />
settings['quicktime']['button'], 'on'); ?> />

Implementation of this feature isn't
perfect and may need some work
settings['videofile']['button'], 'on'); ?> />
settings['flv']['button'], 'on'); ?> />


for normal users function wp_head() { // Align the video based on the user's preference $margins = '5px '; if ( 'center' == $this->settings['alignment'] ) $margins .= 'auto 0 auto'; elseif ( 'right' == $this->settings['alignment'] ) $margins .= '0 0 auto'; else $margins .= 'auto 0 0'; ?> twopointoneplus ) { echo ' ' . "\n"; } } // Do the actual regex and replace all BBCode for Flash powered video with HTML function replacebbcode($content) { # First we handle all Flash based videos $jsoutput = ''; // Clear it out and set it // This is the regex we use to search and then what order the data will come out in // Format is: 'match regex' => array('type' => 'videotype', results => array( ... )) // The type is used internally and the results array is the order in which the data will be returned (width, height, url, + anything else you want) $searchpatterns = array ( '#\[youtube\]http://(www.youtube|youtube)\.com/watch\?v=([\w-]+)(.*?)\[/youtube\]#i' => array('type' => 'youtube', 'results' => array('', 'videoid')), '#\[youtube\]([\w-]+)\[/youtube\]#i' => array('type' => 'youtube', 'results' => array('videoid')), '#\[youtube width="(\d+)" height="(\d+)"]http://(www.youtube|youtube)\.com/watch\?v=([\w-]+)(.*?)\[\/youtube]#i' => array('type' => 'youtube', 'results' => array('width', 'height', '', 'videoid')), '#\[youtube width="(\d+)" height="(\d+)"]([\w-]+)\[\/youtube]#i' => array('type' => 'youtube', 'results' => array('width', 'height', 'videoid')), '#\[googlevideo]http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)\[\/googlevideo]#i' => array('type' => 'googlevideo', 'results' => array('', 'videoid')), '#\[googlevideo]([\d-]+)\[\/googlevideo]#i' => array('type' => 'googlevideo', 'results' => array('videoid')), '#\[googlevideo width="(\d+)" height="(\d+)"]http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)\[\/googlevideo]#i' => array('type' => 'googlevideo', 'results' => array('width', 'height', '', 'videoid')), '#\[googlevideo width="(\d+)" height="(\d+)"]([\d-]+)\[\/googlevideo]#i' => array('type' => 'googlevideo', 'results' => array('width', 'height', 'videoid')), '#\[ifilm]http://(www.ifilm|ifilm)\.com/video/([\d-]+)(.*?)\[\/ifilm]#i' => array('type' => 'ifilm', 'results' => array('', 'videoid')), '#\[ifilm]([\d-]+)\[\/ifilm]#i' => array('type' => 'ifilm', 'results' => array('videoid')), '#\[ifilm width="(\d+)" height="(\d+)"]http://(www.ifilm|ifilm)\.com/video/([\d-]+)(.*?)\[\/ifilm]#i' => array('type' => 'ifilm', 'results' => array('width', 'height', '', 'videoid')), '#\[ifilm width="(\d+)" height="(\d+)"]([\d-]+)\[\/ifilm]#i' => array('type' => 'ifilm', 'results' => array('width', 'height', 'videoid')), '#\[metacafe]http://(www.metacafe|metacafe)\.com/watch/([\d-]+)/([a-z_]+)/\[\/metacafe]#i' => array('type' => 'metacafe', 'results' => array('', 'videoid', 'videoname')), '#\[metacafe width="(\d+)" height="(\d+)"]http://(www.metacafe|metacafe)\.com/watch/([\d-]+)/([a-z_]+)/\[\/metacafe]#i' => array('type' => 'metacafe', 'results' => array('width', 'height', '', 'videoid', 'videoname')), '#\[myspace]http://vids\.myspace\.com/index\.cfm\?fuseaction=vids\.individual(&|&)videoid=(\d+)\[\/myspace]#i' => array('type' => 'myspace', 'results' => array('', 'videoid')), '#\[myspace width="(\d+)" height="(\d+)"]http://vids\.myspace\.com/index\.cfm\?fuseaction=vids\.individual(&|&)videoid=(\d+)\[\/myspace]#i' => array('type' => 'ifilm', 'results' => array('width', 'height', '', 'videoid')), ); // Now we loop through each search item and look for matches. If we find a match, we replace it using the replacement pattern. foreach ( $searchpatterns as $regex => $params ) { preg_match_all($regex, $content, $matches, PREG_SET_ORDER); if ( $matches ) { // Loop through each result for this regex pattern foreach ( $matches as $match) { // Save the string that matched the regex $matchstring = $match[0]; // Loop through each of the output data $count = 0; unset($data); foreach ( $params['results'] as $name ) { $count++; $data[$name] = addslashes($match[$count]); } unset($data['']); // Remove any blank data // If the BBCode didn't have a width or height in it, fill it in with the default value if ( !$data['width'] ) $data['width'] = $this->defaultsettings[$params['type']]['width']; if ( !$data['height'] ) $data['height'] = $this->defaultsettings[$params['type']]['height']; // Create a unique ID for use as the div ID $objectid = uniqid('vvq'); // Do some stuff for each video type if ( 'youtube' == $params['type'] ) { $url = 'http://www.youtube.com/watch?v=' . $data['videoid']; $jsoutput .= ' vvq_youtube("' . $objectid . '", "' . $data['width'] . '", "' . $data['height'] . '", "' . $data['videoid'] . '");' . "\n"; } elseif ( 'googlevideo' == $params['type'] ) { $url = 'http://video.google.com/videoplay?docid=' . $data['videoid']; $jsoutput .= ' vvq_googlevideo("' . $objectid . '", "' . $data['width'] . '", "' . $data['height'] . '", "' . $data['videoid'] . '");' . "\n"; } elseif ( 'ifilm' == $params['type'] ) { $url = 'http://www.ifilm.com/video/' . $data['videoid']; $jsoutput .= ' vvq_ifilm("' . $objectid . '", "' . $data['width'] . '", "' . $data['height'] . '", "' . $data['videoid'] . '");' . "\n"; } elseif ( 'metacafe' == $params['type'] ) { $url = 'http://www.metacafe.com/watch/' . $data['videoid'] . '/' . $data['videoname'] . '/'; $jsoutput .= ' vvq_metacafe("' . $objectid . '", "' . $data['width'] . '", "' . $data['height'] . '", "' . $data['videoid'] . '", "' . $data['videoname'] . '");' . "\n"; } elseif ( 'myspace' == $params['type'] ) { $url = 'http://vids.myspace.com/index.cfm?fuseaction=vids.individual&videoid=' . $data['videoid']; $jsoutput .= ' vvq_myspace("' . $objectid . '", "' . $data['width'] . '", "' . $data['height'] . '", "' . $data['videoid'] . '");' . "\n"; } // Replace the first occurance of the $matchstring with some HTML $content = preg_replace('/' . preg_quote($matchstring, '/') . '/', '

' . $url . '

', $content, 1); } } } # Process all Quicktime videos preg_match_all('#\[quicktime width="(\d+)" height="(\d+)"](.*?)\[\/quicktime]#i', $content, $matches, PREG_SET_ORDER); if ( $matches ) { foreach ( $matches as $match ) { list($matchstring, $width, $height, $url) = $match; $content = str_replace($matchstring, '

' . $url . '

', $content); } } # Process generic video types preg_match_all('#\[(video|avi|mpeg|wmv) width="(\d+)" height="(\d+)"](.*?)\[\/(video|avi|mpeg|wmv)]#i', $content, $matches, PREG_SET_ORDER); if ( $matches ) { // MPEG is not listed here as we'll use it as the default $mimetypes = array( 'wmv' => 'video/x-ms-wmv', 'avi' => 'video/x-msvideo', 'asf' => 'video/x-ms-asf', 'asx' => 'video/x-ms-asf', ); foreach ( $matches as $match ) { list($matchstring, , $width, $height, $url) = $match; // Compensate for the player controls $height = $height + 64; // Find out what type of video this is, based on the extension $mimetype = $mimetypes[array_pop(explode('.', $url))]; if ( empty($mimetype) ) $mimetype = 'video/mpeg'; // If we don't know the MIME type, just pick something (MPEG) // This part needs work, please feel free to suggest any changes if ( 'on' == $this->settings['usewmp'] && FALSE !== strpos($_SERVER['HTTP_USER_AGENT'], 'Windows') && FALSE === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) { $content = str_replace($matchstring, '
', $content); } else { $content = str_replace($matchstring, '
', $content); } } } # Lastly, we add in the Javascript for the Flash based videos (adding it in last saves a little CPU power on preg_match_all() runs) if ( !empty($jsoutput) && !is_feed() ) { $content .= "\n\n"; } return $content; } } $VipersVideoQuicktags = new VipersVideoQuicktags(); // ButtonSnap needs to be loaded outside the class in order to work right require(ABSPATH . $VipersVideoQuicktags->folder . '/resources/buttonsnap.php'); ?>