/**
* Add option to Admin Bar (WP 3.3+)
*
* Add link to YouTube Embed profile options to Admin Bar.
* With help from http://technerdia.com/1140_wordpress-admin-bar.html
*
* @uses     aye_set_general_default     Set default options
*
* @since	2.5
*/
function aye_admin_bar_render_3_3($meta = TRUE)
{
    global $wp_version;
    if ((double) $wp_version >= 3.3) {
        $options = aye_set_general_defaults();
        $profile_access = $options['profile_access'];
        $list_access = $options['list_access'];
        if ($options['admin_bar'] != '') {
            global $wp_admin_bar;
            if (!is_user_logged_in()) {
                return;
            }
            if (!is_admin_bar_showing()) {
                return;
            }
            $wp_admin_bar->add_menu(array('id' => 'aye-menu', 'title' => __('YouTube Embed', 'youtube-embed')));
            if (function_exists('wp_readme_parser')) {
                $wp_admin_bar->add_menu(array('parent' => 'aye-menu', 'id' => 'aye-readme', 'title' => __('Instructions', 'youtube-embed'), 'href' => admin_url('admin.php?page=aye-support-instructions'), 'meta' => array('target' => '_blank')));
            }
            if (current_user_can('delete_plugins')) {
                $wp_admin_bar->add_menu(array('parent' => 'aye-menu', 'id' => 'aye-options', 'title' => __('Options', 'youtube-embed'), 'href' => admin_url('admin.php?page=aye-general-options'), 'meta' => array('target' => '_blank')));
            }
            if (current_user_can($profile_access)) {
                $wp_admin_bar->add_menu(array('parent' => 'aye-menu', 'id' => 'aye-profile', 'title' => __('Profiles', 'youtube-embed'), 'href' => admin_url('admin.php?page=aye-profile-options'), 'meta' => array('target' => '_blank')));
            }
            if (current_user_can($list_access)) {
                $wp_admin_bar->add_menu(array('parent' => 'aye-menu', 'id' => 'aye-lists', 'title' => __('Lists', 'youtube-embed'), 'href' => admin_url('admin.php?page=aye-list-options'), 'meta' => array('target' => '_blank')));
            }
        }
    }
}
/**
* Admin Screen Initialisation
*
* Set up admin menu and submenu options
*
* @since	2.0
*
* @uses     aye_contextual_help_type    Work out help type
*/
function aye_menu_initialise()
{
    // Get level access for profile and list screens.
    $options = aye_set_general_defaults();
    $profile_access = $options['profile_access'];
    $list_access = $options['list_access'];
    // Depending on WordPress version and available functions decide which (if any) contextual help system to use
    $contextual_help = aye_contextual_help_type();
    // Add main admin option
    add_menu_page(__('About Artiss YouTube Embeds', 'youtube-embed'), __('YouTube', 'youtube-embed'), 'edit_posts', 'aye-support-about', 'aye_support_about', plugins_url() . '/youtube-embed/images/menu_icon.png');
    // Add about sub-menu
    if ($contextual_help == 'new') {
        global $aye_about_hook;
    }
    $aye_about_hook = add_submenu_page('aye-support-about', __('About Artiss YouTube Embed', 'youtube-embed'), __('About', 'youtube-embed'), 'edit_posts', 'aye-support-about', 'aye_support_about');
    if ($contextual_help == 'new') {
        add_action('load-' . $aye_about_hook, 'aye_add_about_help');
    }
    if ($contextual_help == 'old') {
        add_contextual_help($aye_about_hook, aye_about_help());
    }
    // Add instructions sub-menu
    if (function_exists('wp_readme_parser')) {
        add_submenu_page('aye-support-about', __('Artiss YouTube Embed Instructions', 'youtube-embed'), __('Instructions', 'youtube-embed'), 'edit_posts', 'aye-support-instructions', 'aye_support_instructions');
    }
    // Add options sub-menu
    if ($contextual_help == 'new') {
        global $aye_options_hook;
    }
    $aye_options_hook = add_submenu_page('aye-support-about', __('Artiss YouTube Embed Options', 'youtube-embed'), __('Options', 'youtube-embed'), 'delete_plugins', 'aye-general-options', 'aye_general_options');
    if ($contextual_help == 'new') {
        add_action('load-' . $aye_options_hook, 'aye_add_options_help');
    }
    if ($contextual_help == 'old') {
        add_contextual_help($aye_options_hook, aye_options_help());
    }
    // Add profiles sub-menu
    if ($contextual_help == 'new') {
        global $aye_profiles_hook;
    }
    $aye_profiles_hook = add_submenu_page('aye-support-about', __('Artiss YouTube Embed Profiles', 'youtube-embed'), __('Profiles', 'youtube-embed'), $profile_access, 'aye-profile-options', 'aye_profile_options');
    if ($contextual_help == 'new') {
        add_action('load-' . $aye_profiles_hook, 'aye_add_profiles_help');
    }
    if ($contextual_help == 'old') {
        add_contextual_help($aye_profiles_hook, aye_profiles_help());
    }
    // Add lists sub-menu
    if ($contextual_help == 'new') {
        global $aye_lists_hook;
    }
    $aye_lists_hook = add_submenu_page('aye-support-about', __('Artiss YouTube Embed Lists', 'youtube-embed'), __('Lists', 'youtube-embed'), $list_access, 'aye-list-options', 'aye_list_options');
    if ($contextual_help == 'new') {
        add_action('load-' . $aye_lists_hook, 'aye_add_lists_help');
    }
    if ($contextual_help == 'old') {
        add_contextual_help($aye_lists_hook, aye_lists_help());
    }
}
/**
* Set up TinyMCE button
*
* Add filters (assuming user is editing) for TinyMCE
*
* @uses     aye_set_general_defaults    Set default options
*
* @since 	2.0
*/
function youtube_embed_button()
{
    // Ensure user is in rich editor and button option is switched on
    if (get_user_option('rich_editing') == 'true') {
        $options = aye_set_general_defaults();
        if ($options['editor_button'] != '') {
            // Ensure shortcode cookie is set
            $editor_sc = aye_set_editor_sc();
            // Add filters
            add_filter('mce_external_plugins', 'add_youtube_embed_mce_plugin');
            add_filter('mce_buttons', 'register_youtube_embed_button');
        }
    }
}
<?php

// Set default options
$default = array('titles' => 'YouTube', 'id' => '', 'profile' => '', 'type' => '', 'template' => '', 'style' => '', 'start' => '', 'autoplay' => '', 'width' => '', 'height' => '', 'dynamic' => '', 'list' => '', 'loop' => '', 'stop' => '', 'id_type' => 'v');
$instance = wp_parse_args((array) $instance, $default);
$general = aye_set_general_defaults();
// Widget Title field
$field_id = $this->get_field_id('titles');
$field_name = $this->get_field_name('titles');
echo "\r\n" . '<p><label for="' . $field_id . '">' . __('Widget Title', 'youtube-embed') . ': </label><input type="text" class="widefat" id="' . $field_id . '" name="' . $field_name . '" value="' . attribute_escape($instance['titles']) . '" /></p>';
// Video ID field
$field_id = $this->get_field_id('id');
$field_name = $this->get_field_name('id');
echo "\r\n" . '<p><label for="' . $field_id . '">' . __('Video ID', 'youtube-embed') . ': </label><input type="text" class="widefat" id="' . $field_id . '" name="' . $field_name . '" value="' . attribute_escape($instance['id']) . '" /></p>';
echo "<table>\n";
// ID Type
$field_id = $this->get_field_id('id_type');
$field_name = $this->get_field_name('id_type');
echo "\r\n" . '<tr><td width="100%">' . __('ID Type', 'youtube-embed') . '</td><td><select name="' . $field_name . '" id="' . $field_id . '"><option value="v"';
if (attribute_escape($instance['id_type']) == 'v') {
    echo " selected='selected'";
}
echo '>' . __('Video or Playlist', 'youtube-embed') . '</option><option value="s"';
if (attribute_escape($instance['id_type']) == 's') {
    echo " selected='selected'";
}
echo '>' . __('Search', 'youtube-embed') . '</option><option value="u"';
if (attribute_escape($instance['id_type']) == 'u') {
    echo " selected='selected'";
}
echo '>' . __('User', 'youtube-embed') . '</option></select></td></tr>';
*/
?>
<div class="wrap" style="width: 1010px;">
<div class="icon32"><img src="<?php 
echo plugins_url();
?>
/youtube-embed/images/screen_icon.png" alt="" title="" height="32px" width="32px"/><br /></div>

<h2><?php 
_e('About Artiss YouTube Embed', 'youtube-embed');
?>
</h2>

<?php 
// Display ads
$options = aye_set_general_defaults();
if ($options['donated'] != 1) {
    artiss_plugin_ads('youtube-embed', 990);
}
?>

<div class="updated fade"><p>Have you voted for which features of YouTube Embed you'd like to retain in version 3? <a href="http://www.artiss.co.uk/youtube-embed/youtube-embed-features-vote" target="_new">Click here</a> to vote!</p></div>

<p><?php 
echo sprintf(__('You are using Artiss YouTube Embed version %s. It was written by David Artiss.', 'youtube-embed'), youtube_embed_version);
?>
</p>

<a href="http://www.youtube.com/"><img src="<?php 
echo plugins_url();
?>
/**
* Validate video type
*
* Function to work out what type of video has been requested and
* whether it is valid. Also fetches the video title.
*
* @since	2.0
*
* @uses     aye_set_general_defaults Get the default settings
*
* @param	string	$id				Video ID
* @param	string	$title_needed	Whether the title requires extracting
* @return	string					Array containing file details
*/
function aye_validate_id($id, $title_needed = false)
{
    $type = false;
    $title = false;
    $options = false;
    $options = aye_set_general_defaults();
    // Attempt to get the video type and title from cache
    if ($options['info_cache'] != 0) {
        $type = get_transient('aye_type_' . $id);
        $title = get_transient('aye_title_' . $id);
    }
    // Check if items are cached
    $cache = true;
    if (!$type or !$title) {
        $cache = false;
    }
    // Get video information if not cached
    if (!$cache) {
        $type = '-';
        if ($options['api'] != 0) {
            if ($options['api'] == 2 or $options['api'] == 4) {
                $https = 's';
            } else {
                $https = '';
            }
            // Check with YouTube API as to whether the ID is a video
            $id_check = aye_get_file('http' . $https . '://gdata.youtube.com/feeds/api/videos/' . $id . '?v=2', false);
            if ($id_check['rc'] == 0) {
                if ($id_check['response'] != 200) {
                    // Check with YouTube API as to whether the ID is a playlist
                    if (strtolower(substr($id, 0, 2)) == 'pl') {
                        $id = substr($id, 2);
                    }
                    $id_check = aye_get_file('http' . $https . '://gdata.youtube.com/feeds/api/playlists/' . $id . '?v=2', false);
                    if ($id_check['rc'] == 0) {
                        if ($id_check['response'] != 200) {
                            $type = '';
                        } else {
                            $type = 'p';
                        }
                    }
                } else {
                    $type = 'v';
                }
            }
        }
        // If no type has been assigned then an API error must have occured
        if ($type == '-') {
            if ($options['api'] == 1 or $options['api'] == 2) {
                // If reporting API errors, output it
                $type = sprintf(__('An error occurred accessing the YouTube API for video ID %s - %s', 'youtube-embed'), $id, $id_check['error']);
            } else {
                // If not reporting errors or API switched off, work out whether a video or playlist
                $type = '';
                if (strlen($id) == 11) {
                    $type = 'v';
                } else {
                    if (strlen($id) == 16) {
                        $type = 'p';
                    }
                }
            }
        }
        // Update the cache
        set_transient('aye_type_' . $id, $type, $options['info_cache'] * 3600);
        // If the video is valid extract the title from the XML
        if ($title_needed && $id_check['file'] != '') {
            // Find title in XML
            $tag_start = strpos($id_check['file'], '<title>', 1) + 7;
            if ($tag_start !== false) {
                $tag_end = strpos($id_check['file'], '</title>', $tag_start);
                if ($tag_end !== false) {
                    $title = substr($id_check['file'], $tag_start, $tag_end - $tag_start);
                }
            }
            // Update the cache
            set_transient('aye_title_' . $id, $title, $options['info_cache'] * 3600);
        }
    }
    if (!$title_needed) {
        return $type;
    }
    $return['type'] = $type;
    $return['title'] = $title;
    return $return;
}
/**
* Get YouTube Transcript
*
* Generate re-encoded YouTube transcript
*
* @since	2.0
*
* @uses     aye_extract_id              Extract the ID
* @uses		aye_get_file			    Get a file
* @uses     aye_set_general_defaults    Set default options
* @uses     aye_validate_id             Validate the ID
*
* @param	string	$id			        Video ID
* @return	string	$output		        Transcript output
*/
function aye_generate_transcript($id)
{
    // Extract the ID if a full URL has been specified
    $id = aye_extract_id($id);
    // Check what type of video it is and whether it's valid
    $embed_type = aye_validate_id($id);
    if ($embed_type != 'v') {
        if (strlen($embed_type) > 1) {
            return aye_error($embed_type);
        } else {
            return aye_error(sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id));
        }
    }
    // Get general options
    $general = aye_set_general_defaults();
    // Check to see if cache is available
    if ($general['transcript_cache'] != 0) {
        $cache_key = 'aye_transcript_' . $id;
        $output = get_transient($cache_key);
        if ($output !== false) {
            return $output;
        }
    }
    // Get transcript file
    $return = aye_get_file('http://video.google.com/timedtext?lang=en&v=' . $id);
    $xml = $return['file'];
    $output = '';
    // If transcript file exists, strip and output
    if ($return['rc'] == 0) {
        $output = "<!-- YouTube Embed v" . youtube_embed_version . " | http://www.artiss.co.uk/youtube-embed -->\n";
        $pos = 0;
        $eof = false;
        while (!$eof) {
            $text_start = strpos($xml, '<text ', $pos);
            if ($text_start !== false) {
                // Extract the start time
                $start_start = strpos($xml, 'start="', $text_start) + 7;
                $start_end = strpos($xml, '"', $start_start) - 1;
                $start = substr($xml, $start_start, $start_end - $start_start + 1);
                // Convert time format
                $start = str_pad(floor($start), 3, '0', STR_PAD_LEFT);
                $start = substr($start, 0, -2) . ':' . substr($start, -2, 2);
                // Now extract the text
                $text_start = strpos($xml, '>', $text_start) + 1;
                $text_end = strpos($xml, '</text>', $text_start) - 1;
                $text = substr($xml, $text_start, $text_end - $text_start + 1);
                // Now return the output
                $output .= "<div class=\"Transcript\"><span class=\"TranscriptTime\">" . $start . "</span> <span class=\"TranscriptText\">" . htmlspecialchars_decode($text) . "</span></div>\n";
                $pos = $text_end + 7;
            } else {
                $eof = true;
            }
        }
        $output .= "<!-- End of YouTube Embed code -->\n";
    }
    // Save the cache
    if ($general['transcript_cache'] != 0) {
        set_transient($cache_key, $output, 3600 * $general['transcript_cache']);
    }
    return $output;
}
/**
* Generate embed code
*
* Generate XHTML compatible YouTube embed code
*
* @since	2.0
*
* @uses		aye_error				    Display an error
* @uses		aye_extract_id			    Get the video ID
* @uses		aye_validate_list		    Get the requested listr
* @uses		aye_validate_id			    Validate the video ID
* @uses		aye_validate_profile		Get the requested profile
* @uses		aye_set_general_defaults	Get general options
* @uses		aye_set_profile_defaults	Set default profile options
*
* @param	string		$id				Video ID
* @param	string		$type			Embed type
* @param	string		$width			Video width
* @param	string		$height			Video height
* @param	string		$fullscreen		Fullscreen button
* @param	string		$related		Show related info.
* @param	string		$autoplay		Start video automatically
* @param	string		$loop			Loop video to start
* @param	string		$start			Start in seconds
* @param	string		$info			Show video info.
* @param	string		$annotation		Annotations
* @param	string		$cc				Closed captions
* @param	string		$style			Stylesheet information
* @param	string		$link			Link back to YouTube
* @param	string		$react			Show EmbedPlus reactions
* @param	string		$stop			Stop in seconds
* @param	string		$sweetspot		Show EmbedPlus sweetspots
* @param	string		$disablekb		Disable keyboard controls
* @param	string		$ratio			Video size ratio
* @param	string		$autohide		Autohide controls
* @param	string		$controls		Display controls
* @param	string		$profile		Which profile to use
* @param	string		$list_style		How to use a list, if used
* @param	string		$audio			Only show controls, for audio playback
* @param	string		$template		Display template
* @param	string		$hd				Use HD, if available
* @param	string		$color		 	Progress bar colour
* @param	string		$theme			Use dark or light theme
* @param	string		$https			Use HTTPS for links
* @param    string      $dynamic        Show dynamic output
* @param    string      $search         Perform a search
* @param    string      $user           Look up user videos
* @return	string						Code output
*/
function aye_generate_youtube_code($id = '', $type = '', $width = '', $height = '', $fullscreen = '', $related = '', $autoplay = '', $loop = '', $start = '', $info = '', $annotation = '', $cc = '', $style = '', $link = '', $react = '', $stop = '', $sweetspot = '', $disablekb = '', $ratio = '', $autohide = '', $controls = '', $profile = '', $list_style = '', $audio = '', $template = '', $hd = '', $color = '', $theme = '', $https = '', $dynamic = '', $search = '', $user = '')
{
    // Ensure an ID is passed
    if ($id == '') {
        return aye_error(__('No video/playlist ID has been supplied', 'youtube-embed'));
    }
    // Get general options
    $general = aye_set_general_defaults();
    // Find the profile, if one is specified
    $profile = aye_validate_profile($profile, $general['profile_no']);
    // Get default values if no values are supplied
    $options = aye_set_profile_defaults($profile);
    // If a user look-up or search has been requested, mis-out looking up list details and
    // simple assign it as an IFRAME video
    if ($user == 0 && $search == 0) {
        // Check it's not a list
        $playlist_ids = '';
        $list = aye_validate_list($id, $general['list_no']);
        if (!is_array($list)) {
            // Check if certain parameters are included in the URL
            $width = aye_get_url_para($id, 'w', $width);
            $height = aye_get_url_para($id, 'h', $height);
            // Extract the ID if a full URL has been specified
            $id = aye_extract_id($id);
            // Is it being previewed? In which case remove any cache
            if (preg_match('/p=([0-9]*)&preview=true/', $_SERVER['QUERY_STRING']) == 1 && $general['preview'] == 1) {
                delete_transient('aye_type_' . $id);
                delete_transient('aye_title_' . $id);
            }
            // Check what type of video it is and whether it's valid
            $return = aye_validate_id($id, true);
            $embed_type = $return['type'];
            // If the video is invalid, output an appropriate error
            if ($embed_type == '' or strlen($embed_type) != 1) {
                if ($embed_type == '') {
                    $error = sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id);
                } else {
                    $error = $embed_type;
                }
                $result = "\n<!-- YouTube Embed v" . youtube_embed_version . " | http://www.artiss.co.uk/youtube-embed -->\n";
                $result .= "<!-- " . $error . " -->\n" . aye_decode($general['error_message']) . "\n<!-- End of YouTube Embed code -->\n";
                return $result;
            }
        } else {
            $return = '';
            $embed_type = 'v';
            // Randomize the video
            if ($list_style == 'random') {
                shuffle($list);
            }
            // Extract one video randomly
            if ($list_style == 'single') {
                $id = $list[array_rand($list, 1)];
                // Build the playlist
            } else {
                $id = $list[0];
                // Build the playlist
                if (count($list) > 1) {
                    $loop = 1;
                    while ($loop < count($list)) {
                        if ($playlist_ids != '') {
                            $playlist_ids .= ',';
                        }
                        $list_id = aye_extract_id($list[$loop]);
                        $playlist_ids .= $list_id;
                        $loop++;
                    }
                }
            }
        }
    }
    // Generate a cache key for the above passed parameters
    $cache_key = 'aye_video_' . md5($id . $type . $width . $height . $fullscreen . $related . $autoplay . $loop . $start . $info . $annotation . $cc . $style . $link . $react . $stop . $sweetspot . $disablekb . $ratio . $autohide . $controls . $profile . $list_style . $audio . $template . $hd . $color . $theme . $https . $dynamic . $search . $user . serialize($general) . serialize($options) . serialize($list) . serialize($return));
    // Try and get the output from cache. If it exists, return the code
    if ($general['embed_cache'] != 0 && !is_feed() && $list_style != 'random') {
        $result = get_transient($cache_key);
        if ($result !== false) {
            return $result;
        }
    }
    $metadata = $general['metadata'];
    // Work out correct protocol to use - HTTP or HTTPS
    if ($https == '') {
        $https = $options['https'];
    }
    if ($https == 1) {
        $https = 's';
    } else {
        $https = '';
    }
    // If this is a feed then display a thumbnail and/or text link to the original video
    if (is_feed()) {
        $result = '';
        if ($playlist_ids != '') {
            $result .= '<p>' . __('A video list cannot be viewed within this feed - please view the original content', 'youtube-embed') . ".</p>\n";
        } else {
            $youtube_url = 'http' . $https . '://www.youtube.com/watch?' . $embed_type . '=' . $id;
            if ($embed_type == 'v' && $general['feed'] != 't') {
                $result .= '<p><a href="' . $youtube_url . '"><img src="http://img.youtube.com/vi/' . $id . '/' . $general['thumbnail'] . ".jpg\"></a></p>\n";
            }
            if ($general['feed'] != 'v' or $embed_type != 'v') {
                $result .= '<p><a href="' . $youtube_url . '">' . __('Click here to view the video on YouTube', 'youtube-embed') . "</a>.</p>\n";
            }
        }
        return $result;
    }
    // If a dynamic size has been requested, check whether the width should be fixed
    $fixed = 0;
    if ($dynamic == '') {
        $dynamic = $options['dynamic'];
        $fixed = $options['fixed'];
    } else {
        if ($width != '') {
            $fixed = 1;
        }
    }
    // Only set width and height from defaults if both are missing
    if ($width == '' && $height == '') {
        $width = $options['width'];
        $height = $options['height'];
    }
    // If controls parameter is not numeric then convert to 0 or 1
    // This is to maintain backwards compatibility after version 2.6
    if (!is_numeric($controls) && $controls != '') {
        $controls = aye_convert($controls);
    }
    // If values have not been pressed, use the default values
    if ($fullscreen == '') {
        $fullscreen = $options['fullscreen'];
    }
    if ($related == '') {
        $related = $options['related'];
    }
    if ($autoplay == '') {
        $autoplay = $options['autoplay'];
    }
    if ($loop == '') {
        $loop = $options['loop'];
    }
    if ($info == '') {
        $info = $options['info'];
    }
    if ($annotation == '') {
        $annotation = $options['annotation'];
    }
    if ($cc == '') {
        $cc = $options['cc'];
    }
    if ($link == '') {
        $link = $options['link'];
    }
    if ($react == '') {
        $react = $options['react'];
    }
    if ($sweetspot == '') {
        $sweetspot = $options['sweetspot'];
    }
    if ($disablekb == '') {
        $disablekb = $options['disablekb'];
    }
    if ($autohide == '') {
        $autohide = $options['autohide'];
    }
    if ($controls == '') {
        $controls = $options['controls'];
    }
    if ($audio == '') {
        $audio = $options['audio'];
    }
    if ($hd == '') {
        $hd = $options['hd'];
    }
    if ($style == '') {
        $style = $options['style'];
    }
    if ($color == '') {
        $color = $options['color'];
    }
    if ($theme == '') {
        $theme = $options['theme'];
    }
    $wmode = $options['wmode'];
    if ($theme == '') {
        $theme = $options['theme'];
    }
    // Build the required template
    if ($template == '') {
        $template = $options['template'];
    } else {
        $template = aye_decode($template);
    }
    if (strpos($template, '%video%') === false) {
        $template = '%video%';
    }
    // If a multi-play list has been specified and EmbedPlus selected, use fallback embedding method instead
    if ($playlist_ids != '' && $type == 'm' && $list_style != 'single') {
        $type = $options['fallback'];
    }
    // If looping and no playlist has been generated, add the current ID
    // This is a workaround for the AS3 player which won't otherwise loop
    if ($loop == 1 && $embed_type != 'p' && $playlist_ids == '') {
        $playlist_ids = $id;
    }
    // If no type was specified, depending on whether this is a video or playlist, set the specific default
    if ($type == '') {
        if ($embed_type == 'v') {
            $type = $options['type'];
        } else {
            $type = $options['playlist'];
        }
    }
    // If a playlist, user or search was specified and this is is Chromeless, switch back to IFRAME to allow
    if (($embed_type == 'p' or $user != 0 or $search != 0) && $type == 'c') {
        $type = 'v';
    }
    // Set parameters without default values
    if ($start == '') {
        $start = '0';
    }
    if ($stop == '') {
        $stop = '0';
    }
    // If height or width is missing, calculate missing parameter using ratio
    if (($width == '' or $height == '') && ($width != '' or $height != '')) {
        $ratio_to_use = '';
        if ($ratio != '') {
            // Extract the ratio from the provided string
            $pos = strpos($ratio, ':', 0);
            if ($pos !== false) {
                $ratio_l = substr($ratio, 0, $pos);
                $ratio_r = substr($ratio, $pos + 1);
                if (is_numeric($ratio_l) && is_numeric($ratio_r)) {
                    $ratio_to_use = $ratio_l / $ratio_r;
                }
            }
        }
        // If no, or invalid, ratio supplied, calculate from the default video dimensions
        if ($ratio_to_use == '') {
            $ratio_to_use = $options['width'] / $options['height'];
        }
        // Complete the missing width or height using the ratio
        if ($width == '') {
            $width = round($height * $ratio_to_use, 0);
        }
        if ($height == '') {
            $height = round($width / $ratio_to_use, 0);
        }
    }
    // Set Frameborder output
    $frameborder = '';
    if (isset($general['frameborder'])) {
        if ($general['frameborder'] == 1) {
            $frameborder = 'frameborder="0" ';
        }
    }
    // If audio playback option is set, restrict the height to just show the player toolbar
    if ($audio == '1') {
        $height = 27;
    }
    // Set up embed types
    $tab = '';
    $class = 'youtube-player';
    $paras = '';
    $embedplus = false;
    $swf = false;
    $iframe = false;
    $chromeless = false;
    if ($type == 'm' && ($user != 0 or $search != 0)) {
        $type = $options['fallback'];
    }
    if ($type != 'v') {
        if ($type == 'm') {
            $embedplus = true;
            $tab = "\t";
            $embedheight = $height + 32;
            $class = 'cantembedplus';
            $fallback = $options['fallback'];
        } else {
            if ($type == "c") {
                $chromeless = true;
            } else {
                $swf = true;
            }
        }
        $paras .= '&amp;version=3';
    } else {
        $iframe = true;
    }
    // Generate parameters to add to URL
    if ($options['modest'] == 1) {
        $paras .= '&amp;modestbranding=1';
    }
    if ($fullscreen == 1) {
        $paras .= '&amp;fs=1';
    } else {
        $paras .= '&amp;fs=0';
    }
    if ($related != 1) {
        $paras .= '&amp;rel=0';
    }
    if ($autoplay == 1) {
        $paras .= '&amp;autoplay=1';
        $paras_ep .= '&amp;autoplay=1';
    }
    if ($loop == 1) {
        $paras .= '&amp;loop=1';
    }
    if ($info != 1) {
        $paras .= '&amp;showinfo=0';
    }
    if ($annotation != 1) {
        $paras .= '&amp;iv_load_policy=3';
    }
    if ($cc == 1) {
        $paras .= '&amp;cc_load_policy=1';
    }
    if ($disablekb == 1) {
        $paras .= '&amp;disablekb=1';
    }
    if ($autohide != 2) {
        $paras .= '&amp;autohide=' . $autohide;
    }
    if ($controls != 1) {
        $paras .= '&amp;controls=' . $controls;
    }
    if (strtolower($color) != 'red') {
        $paras .= '&amp;color=' . strtolower($color);
    }
    if (strtolower($theme) != 'dark') {
        $paras .= '&amp;theme=' . strtolower($theme);
    }
    // If not a playlist, add the playlist parameter
    if ($playlist_ids != '') {
        $paras .= '&amp;playlist=' . $playlist_ids;
    }
    // Generate EmbedPlus parameters
    $paras_ep = '&amp;width=' . $width . '&amp;height=' . $height;
    if ($react != 1) {
        $paras_ep .= '&amp;react=0';
    }
    if ($sweetspot != 1) {
        $paras_ep .= '&amp;sweetspot=0';
    }
    if ($hd == 1) {
        $paras_ep .= '&amp;hd=1';
    }
    // Add start & stop parameters
    if ($start != 0) {
        $paras .= '&amp;start=' . $start;
        $paras_ep .= '&amp;start=' . $start;
    }
    if ($stop != 0) {
        $paras_ep .= '&amp;stop=' . $stop;
        $paras .= '&amp;end=' . $stop;
    }
    // Generate DIVs to wrap around video
    if ($dynamic == 1) {
        $result = "<div class=\"ye-container\">\n";
        if ($fixed == 1) {
            $result = '<div style="width: ' . $width . 'px; max-width: 100%">' . "\n" . $result;
        }
    }
    // Add EmbedPlus code
    if ($embedplus) {
        $result .= "<object type=\"application/x-shockwave-flash\" width=\"" . $width . "\" height=\"" . $embedheight . "\" data=\"http://getembedplus.com/embedplus.swf\" style=\"" . $style . "\" id=\"" . uniqid('ep_', true) . "\" >\n";
        $result .= "\t<param value=\"http://getembedplus.com/embedplus.swf\" name=\"movie\" />\n";
        $result .= "\t<param value=\"high\" name=\"quality\" />\n";
        $result .= "\t<param value=\"" . $wmode . "\" name=\"wmode\" />\n";
        $result .= "\t<param value=\"always\" name=\"allowscriptaccess\" />\n";
        if ($fullscreen == 1) {
            $result .= "\t<param name=\"allowFullScreen\" value=\"true\" />\n";
        }
        $result .= "\t<param name=\"flashvars\" value=\"ytid=" . $id . $paras_ep . "\" />\n";
    }
    // Work out, depending on privacy settings, the main address to use
    $privacy = $general['privacy'];
    if ($privacy == 2) {
        $do_not_track = aye_do_not_track();
        if ($do_not_track) {
            $privacy = 1;
        } else {
            $privacy = 0;
        }
    }
    if ($privacy == 1) {
        $url_privacy = 'youtube-nocookie.com';
    } else {
        $url_privacy = 'youtube.com';
    }
    // Generate the first part of the embed URL along with the ID section
    if ($chromeless) {
        $embed_url = 'http' . $https . '://www.youtube.com/apiplayer';
        $id_paras = '?video_id=' . $id;
    } else {
        $embed_url = 'http' . $https . '://www.' . $url_privacy . '/';
        if ($type == 'v') {
            $embed_url .= 'embed';
        } else {
            $embed_url .= 'v/';
        }
        $id_paras = $id;
        if ($type == 'v') {
            $id_paras = '/' . $id_paras;
        }
    }
    // If a playlist, user or download build the ID appropriately
    if ($embed_type == 'p' or $user != 0 or $search != 0) {
        $list_type = '';
        if ($embed_type == 'p') {
            $list_type = 'playlist';
        }
        if ($user != 0) {
            $list_type = 'user_uploads';
        }
        if ($search != 0) {
            $list_type = 'search';
            $id = urlencode($id);
        }
        $id_paras = '';
        if ($type == 'p') {
            $id_paras .= 'videoseries';
        }
        $id_paras .= '?listType=' . $list_type . '&amp;list=';
        if ($embed_type == 'p' && strtolower(substr($id, 0, 2)) != 'pl') {
            $id_paras .= 'PL';
        }
        $id_paras .= $id;
    }
    // Combine URL parts together
    $embed_url .= $id_paras;
    if (!strpos($embed_url, '?') && substr($paras, 0, 5) == '&amp;') {
        $paras = '?' . substr($paras, 5);
    }
    $embed_url .= $paras;
    // Add AS3 YouTube embed code
    if ($swf or $chromeless or $embedplus && ($fallback == 'o' or $fallback == 'p')) {
        $result .= $tab . "<object class=\"" . $class . "\" type=\"application/x-shockwave-flash\" data=\"" . $embed_url . "\" width=\"" . $width . "\" height=\"" . $height . "\" style=\"" . $style . "\"";
        if ($metadata != 0) {
            $result .= " rel=\"media:video\" resource=\"http" . $https . "://www.youtube.com/" . $embed_type . "/" . $id . "\" xmlns:media=\"http://search.yahoo.com/searchmonkey/media/\"";
        }
        $result .= " >\n";
        if ($metadata != 0) {
            $result .= $tab . "\t<a rel=\"media:thumbnail\" href=\"http://img.youtube.com/vi/" . $id . "/default.jpg\" /></a>\n";
        }
        $result .= $tab . "\t<param name=\"movie\" value=\"" . $embed_url . "\" />\n";
        $result .= $tab . "\t<param name=\"wmode\" value=\"" . $wmode . "\" />\n";
        if ($fullscreen == 1) {
            $result .= $tab . "\t<param name=\"allowFullScreen\" value=\"true\" />\n";
        }
        if ($link != 1 && $link != '') {
            $result .= $tab . "\t<param name=\"allowNetworking\" value=\"internal\" />\n";
        }
        if ($metadata != 0 && $return['title'] != '') {
            $result .= $tab . "\t<span property=\"media:title\" content=\"" . htmlentities($return['title']) . "\" />\n";
        }
        $result .= $tab . "</object>\n";
    }
    // Add IFRAME embed code
    if ($iframe or $embedplus && $fallback == "v") {
        if ($embed_type == "p") {
            $playlist_para = "p/";
        } else {
            $playlist_para = '';
        }
        $result .= $tab . '<iframe ' . $frameborder . 'style="border: 0;' . $style . '" class="' . $class . '" width="' . $width . '" height="' . $height . '" src="' . $embed_url . '&amp;wmode=' . $wmode . '"';
        if ($fullscreen == 1) {
            $result .= ' allowfullscreen="allowfullscreen"';
        }
        $result .= " ></iframe>\n";
    }
    // If using EmbedPlus, add the OBJECT closure tag
    if ($embedplus) {
        $result .= "</object>\n<!--[if lte IE 6]> <style type=\"text/css\">.cantembedplus{display:none;}</style><![endif]-->\n";
    }
    // Now apply the template to the result
    $end_tag = '';
    if ($dynamic == 1) {
        $end_tag .= "</div>\n";
        if ($fixed == 1) {
            $end_tag .= "</div>\n";
        }
    }
    $result = str_replace('%video%', $result . $end_tag, $template);
    // Now add a commented header and trailer
    $result = "\n<!-- YouTube Embed v" . youtube_embed_version . " | http://www.artiss.co.uk/youtube-embed -->\n" . $result;
    $result .= "<!-- End of YouTube Embed code -->\n";
    // Cache the output
    if ($general['embed_cache'] != 0) {
        set_transient($cache_key, $result, 3600 * $general['embed_cache']);
    }
    return $result;
}
/**
* Video shortcode
*
* Use shortcode parameters to embed a YouTube video or playlist
*
* @since	2.0
*
* @uses		aye_get_embed_type			Get the embed type
* @uses		aye_set_autohide			Get the autohide parameter
* @uses     aye_set_general_defaults    Set default options
* @uses		aye_generate_youtube_code	Generate the embed code
*
* @param    string		$paras			Shortcode parameters
* @param	string		$content		Shortcode content
* @param	string		$alt_shortcode	The number of the alternative shortcode used
* @return   string						YouTube embed code
*/
function aye_video_shortcode($paras = '', $content = '', $callback = '', $alt_shortcode = '')
{
    extract(shortcode_atts(array('type' => '', 'width' => '', 'height' => '', 'fullscreen' => '', 'related' => '', 'autoplay' => '', 'loop' => '', 'start' => '', 'info' => '', 'annotation' => '', 'cc' => '', 'style' => '', 'link' => '', 'react' => '', 'stop' => '', 'sweetspot' => '', 'disablekb' => '', 'ratio' => '', 'autohide' => '', 'controls' => '', 'profile' => '', 'embedplus' => '', 'audio' => '', 'id' => '', 'url' => '', 'rel' => '', 'fs' => '', 'cc_load_policy' => '', 'iv_load_policy' => '', 'showinfo' => '', 'youtubeurl' => '', 'template' => '', 'list' => '', 'hd' => '', 'color' => '', 'theme' => '', 'ssl' => '', 'height' => '', 'width' => '', 'dynamic' => '', 'h' => '', 'w' => '', 'search' => '', 'user' => ''), $paras));
    // If no profile specified and an alternative shortcode used, get that shortcodes default profile
    if ($profile == '' && $alt_shortcode != '') {
        // Profile is now blank or 2
        if ($alt_shortcode == '1') {
            $alt_shortcode = '';
        }
        // Get general options
        $options = aye_set_general_defaults();
        $profile = $options['alt_profile' . $alt_shortcode];
    }
    // If an alternative field is set, use it
    if ($id != '' && $content == '') {
        $content = $id;
    }
    if ($url != '' && $content == '') {
        $content = $url;
    }
    if ($youtubeurl != '' && $content == '') {
        $content = $youtubeurl;
    }
    if ($h != '' && $height == '') {
        $height = $h;
    }
    if ($w != '' && $width == '') {
        $width = $w;
    }
    if ($rel != '' && $related == '') {
        $related = $rel;
    }
    if ($fs != '' && $fullscreen == '') {
        $fullscreen = $fs;
    }
    if ($cc_load_policy != '' && $cc == '') {
        $cc = $cc_load_policy;
    }
    if ($iv_load_policy != '' && $annotation == '') {
        $annotation = $iv_load_policy;
    }
    if ($showinfo != '' && $info == '') {
        $info = $showinfo;
    }
    // If ID was not passed in the content and the first parameter is set, assume that to be the ID
    if ($content == '' && $paras[0] != '') {
        $content = $paras[0];
        if (substr($content, 0, 1) == ":" or substr($content, 0, 1) == "=") {
            $content = substr($content, 1);
        }
        if ($paras[1] != '') {
            $width = $paras[1];
        }
        if ($paras[2] != '') {
            $height = $paras[2];
        }
    }
    // Get Embed type
    $type = aye_get_embed_type($type, $embedplus);
    // Set up Autohide parameter
    $autohide = aye_set_autohide($autohide);
    // Create YouTube code
    $youtube_code = aye_generate_youtube_code($content, $type, $width, $height, aye_convert($fullscreen), aye_convert($related), aye_convert($autoplay), aye_convert($loop), $start, aye_convert($info), aye_convert_3($annotation), aye_convert($cc), $style, aye_convert($link), aye_convert($react), $stop, aye_convert($sweetspot), aye_convert($disablekb), $ratio, $autohide, $controls, $profile, $list, aye_convert($audio), $template, aye_convert($hd), $color, $theme, aye_convert($ssl), aye_convert($dynamic), aye_convert($search), aye_convert($user));
    return do_shortcode($youtube_code);
}
/**
* Filter comments
*
* Filter comments and add YouTube embeds
*
* @since	2.0
*
* @uses		aye_set_general_defaults		Get the general defaults
* @uses		aye_generate_youtube_code	Generate the YouTube embed code
*
* @param    string		$content		The comments content
* @return   string						Content to return
*/
function aye_filter_comments($content)
{
    $options = aye_set_general_defaults();
    if ($options['comments'] == 1) {
        $identifier = 'http://www.youtube.com/watch?v=';
        $search_pos = 0;
        $search = strpos($content, $identifier, $search_pos);
        while ($search !== false) {
            $prefix = substr($content, $search - 1, 1);
            if ($prefix != ']' && $prefix != '"' && $prefix != "'") {
                // Check if link around video. If so, work out how much more needs stripping out
                $anchor_start = 0;
                $anchor_end = 0;
                if ($prefix == ">") {
                    $anchor_pos = strrpos(substr($content, 0, $search), '<a');
                    if ($anchor_pos !== false) {
                        $anchor_start = $search - $anchor_pos;
                        $anchor_end = strpos(substr($content, $search), '</a>') - strlen($identifier) - 11;
                    }
                }
                // Work out video ID and generate code
                $id = substr($content, $search + strlen($identifier), 11);
                $code = aye_generate_youtube_code($id, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', $options['comments_profile'], '', '');
                // Now replace the URL with the resultant code
                $first_section = $search - 1 - $anchor_start;
                if ($first_section < 1) {
                    $content = $code . substr($content, $search + strlen($identifier) + 11 + $anchor_end);
                } else {
                    $content = substr($content, 0, $first_section) . $code . substr($content, $search + strlen($identifier) + 11 + $anchor_end);
                }
                $search_pos = $search + strlen($code) - $anchor_start;
            } else {
                $search_pos = $search + strlen($identifier) + 11;
            }
            $search = strpos($content, $identifier, $search_pos - 1);
        }
    }
    return $content;
}