Пример #1
0
function lyte_parse($the_content, $doExcerpt = false)
{
    /** main function to parse the content, searching and replacing httpv-links */
    global $lyteSettings, $toCache_index, $postID, $cachekey;
    $lyteSettings['path'] = plugins_url() . "/" . dirname(plugin_basename(__FILE__)) . '/lyte/';
    $urlArr = parse_url($lyteSettings['path']);
    $origin = $urlArr['scheme'] . "://" . $urlArr['host'] . "/";
    /** API: filter hook to preparse the_content, e.g. to force normal youtube links to be parsed */
    $the_content = apply_filters('lyte_content_preparse', $the_content);
    if (get_option('lyte_greedy', '1') === "1") {
        $the_content = preg_replace('/^https?:\\/\\/(www.)?youtu(be.com|.be)\\/playlist\\?list=/m', 'httpv://www.youtube.com/playlist?list=', $the_content);
        $the_content = preg_replace('/^https?:\\/\\/(www.)?youtu(be.com|.be)\\/(watch\\?v=)?/m', 'httpv://www.youtube.com/watch?v=', $the_content);
    }
    if (strpos($the_content, "httpv") !== FALSE || strpos($the_content, "httpa") !== FALSE) {
        $char_codes = array('×', '–');
        $replacements = array("x", "--");
        $the_content = str_replace($char_codes, $replacements, $the_content);
        $lyte_feed = is_feed();
        $hidefClass = $lyteSettings['hidef'] === "1" ? " hidef" : "";
        $postID = get_the_ID();
        $toCache_index = array();
        $lytes_regexp = "/(?:<p>)?http(v|a):\\/\\/([a-zA-Z0-9\\-\\_]+\\.|)(youtube|youtu)(\\.com|\\.be)\\/(((watch(\\?v\\=|\\/v\\/)|.+?v\\=|)([a-zA-Z0-9\\-\\_]{11}))|(playlist\\?list\\=([a-zA-Z0-9\\-\\_]*)))([^\\s<]*)(<?:\\/p>)?/";
        preg_match_all($lytes_regexp, $the_content, $matches, PREG_SET_ORDER);
        foreach ($matches as $match) {
            /** API: filter hook to preparse fragment in a httpv-url, e.g. to force hqThumb=1 or showinfo=0 */
            $match[12] = apply_filters('lyte_match_preparse_fragment', $match[12]);
            preg_match("/stepSize\\=([\\+\\-0-9]{2})/", $match[12], $sMatch);
            preg_match("/showinfo\\=([0-1]{1})/", $match[12], $showinfo);
            preg_match("/start\\=([0-9]*)/", $match[12], $start);
            preg_match("/enablejsapi\\=([0-1]{1})/", $match[12], $jsapi);
            preg_match("/hqThumb\\=([0-1]{1})/", $match[12], $hqThumb);
            preg_match("/noMicroData\\=([0-1]{1})/", $match[12], $microData);
            $thumb = "normal";
            if ($lyteSettings['hidef'] === "1") {
                $thumb = "highres";
            } else {
                if (!empty($hqThumb)) {
                    if ($hqThumb[0] === "hqThumb=1") {
                        $thumb = "highres";
                    }
                }
            }
            $noMicroData = "0";
            if (!empty($microData)) {
                if ($microData[0] === "noMicroData=1") {
                    $noMicroData = "1";
                }
            }
            $qsa = "";
            if (!empty($showinfo[0])) {
                $qsa = "&amp;" . $showinfo[0];
                $titleClass = " hidden";
            } else {
                $titleClass = "";
            }
            if (!empty($start[0])) {
                $qsa .= "&amp;" . $start[0];
            }
            if (!empty($jsapi[0])) {
                $qsa .= "&amp;" . $jsapi[0] . "&amp;origin=" . $origin;
            }
            if (!empty($qsa)) {
                $esc_arr = array("&" => "\\&", "?" => "\\?", "=" => "\\=");
                $qsaClass = " qsa_" . strtr($qsa, $esc_arr);
            } else {
                $qsaClass = "";
            }
            if (!empty($sMatch)) {
                $newSize = (int) $sMatch[1];
                $newPos = (int) $lyteSettings['selSize'] + $newSize;
                if ($newPos < 0) {
                    $newPos = 0;
                } else {
                    if ($newPos > count($lyteSettings['sizeArray']) - 1) {
                        $newPos = count($lyteSettings['sizeArray']) - 1;
                    }
                }
                $lyteSettings[2] = $lyteSettings['sizeArray'][$newPos]['w'];
                $lyteSettings[3] = $lyteSettings['sizeArray'][$newPos]['h'];
            } else {
                $lyteSettings[2] = $lyteSettings['sizeArray'][$lyteSettings['selSize']]['w'];
                $lyteSettings[3] = $lyteSettings['sizeArray'][$lyteSettings['selSize']]['h'];
            }
            if ($match[1] !== "a") {
                $divHeight = $lyteSettings[3];
                $audioClass = "";
                $audio = false;
            } else {
                $audio = true;
                $audioClass = " lyte-audio";
                $divHeight = 38;
            }
            $NSimgHeight = $divHeight - 20;
            if ($match[11] != "") {
                $plClass = " playlist";
                $vid = $match[11];
                switch ($lyteSettings['links']) {
                    case "0":
                        $noscript_post = "<br />" . __("Watch this playlist on YouTube", "wp-youtube-lyte");
                        $noscript = "<noscript><a href=\"" . $lyteSettings['scheme'] . "://youtube.com/playlist?list=" . $vid . "\">" . $noscript_post . "</a></noscript>";
                        $lytelinks_txt = "";
                        break;
                    default:
                        $noscript = "";
                        $lytelinks_txt = "<div class=\"lL\" style=\"max-width:100%;width:" . $lyteSettings[2] . "px;" . $lyteSettings['pos'] . "\">" . __("Watch this playlist", "wp-youtube-lyte") . " <a href=\"" . $lyteSettings['scheme'] . "://www.youtube.com/playlist?list=" . $vid . "\">" . __("on YouTube", "wp-youtube-lyte") . "</a></div>";
                }
            } else {
                if ($match[9] != "") {
                    $plClass = "";
                    $vid = $match[9];
                    switch ($lyteSettings['links']) {
                        case "0":
                            $noscript_post = "<br />" . __("Watch this video on YouTube", "wp-youtube-lyte");
                            $lytelinks_txt = "<div class=\"lL\" style=\"max-width:100%;width:" . $lyteSettings[2] . "px;" . $lyteSettings['pos'] . "\"></div>";
                            break;
                        case "2":
                            $noscript_post = "";
                            $lytelinks_txt = "<div class=\"lL\" style=\"max-width:100%;width:" . $lyteSettings[2] . "px;" . $lyteSettings['pos'] . "\">" . __("Watch this video", "wp-youtube-lyte") . " <a href=\"" . $lyteSettings['scheme'] . "://youtu.be/" . $vid . "\">" . __("on YouTube", "wp-youtube-lyte") . "</a> " . __("or on", "wp-youtube-lyte") . " <a href=\"http://icant.co.uk/easy-youtube/?http://www.youtube.com/watch?v=" . $vid . "\">Easy Youtube</a>.</div>";
                            break;
                        default:
                            $noscript_post = "";
                            $lytelinks_txt = "<div class=\"lL\" style=\"max-width:100%;width:" . $lyteSettings[2] . "px;" . $lyteSettings['pos'] . "\">" . __("Watch this video", "wp-youtube-lyte") . " <a href=\"" . $lyteSettings['scheme'] . "://youtu.be/" . $vid . "\">" . __("on YouTube", "wp-youtube-lyte") . "</a>.</div>";
                    }
                    $noscript = "<noscript><a href=\"" . $lyteSettings['scheme'] . "://youtu.be/" . $vid . "\"><img src=\"" . $lyteSettings['scheme'] . "://i.ytimg.com/vi/" . $vid . "/0.jpg\" alt=\"\" width=\"" . $lyteSettings[2] . "\" height=\"" . $NSimgHeight . "\" />" . $noscript_post . "</a></noscript>";
                }
            }
            // fetch data from YT api (v2 or v3)
            $isPlaylist = false;
            if ($plClass === " playlist") {
                $isPlaylist = true;
            }
            $cachekey = '_lyte_' . $vid;
            $yt_resp_array = lyte_get_YT_resp($vid, $isPlaylist, $cachekey);
            // If there was a result from youtube or from cache, use it
            if ($yt_resp_array) {
                if (is_array($yt_resp_array)) {
                    if ($isPlaylist !== true) {
                        // captions, thanks to Benetech
                        $captionsMeta = "";
                        $doCaptions = true;
                        /** API: filter hook to disable captions */
                        $doCaptions = apply_filters('lyte_docaptions', $doCaptions);
                        if ($lyteSettings['microdata'] === "1" && $noMicroData !== "1" && $doCaptions === true) {
                            if (array_key_exists('captions_data', $yt_resp_array)) {
                                if ($yt_resp_array["captions_data"] == "true") {
                                    $captionsMeta = "<meta itemprop=\"accessibilityFeature\" content=\"captions\" />";
                                    $forceCaptionsUpdate = false;
                                } else {
                                    $forceCaptionsUpdate = true;
                                }
                            } else {
                                $forceCaptionsUpdate = true;
                                $yt_resp_array["captions_data"] = false;
                            }
                            if ($forceCaptionsUpdate === true) {
                                $captionsMeta = "";
                                $threshold = 30;
                                if (array_key_exists('captions_timestamp', $yt_resp_array)) {
                                    $cache_timestamp = $yt_resp_array["captions_timestamp"];
                                    $interval = (strtotime("now") - $cache_timestamp) / 60 / 60 / 24;
                                } else {
                                    $cache_timestamp = false;
                                    $interval = $threshold + 1;
                                }
                                if (!is_int($cache_timestamp) || $interval > $threshold && !is_null($yt_resp_array["captions_data"])) {
                                    $yt_resp_array['captions_timestamp'] = strtotime("now");
                                    wp_schedule_single_event(strtotime("now") + 60 * 60, 'schedule_captions_lookup', array($postID, $cachekey, $vid));
                                    $yt_resp_precache = json_encode($yt_resp_array);
                                    $toCache = base64_encode(gzcompress($yt_resp_precache));
                                    update_post_meta($postID, $cachekey, $toCache);
                                }
                            }
                        }
                    }
                    $thumbUrl = "";
                    if ($thumb === "highres" && !empty($yt_resp_array["HQthumbUrl"])) {
                        $thumbUrl = $yt_resp_array["HQthumbUrl"];
                    } else {
                        if (!empty($yt_resp_array["thumbUrl"])) {
                            $thumbUrl = $yt_resp_array["thumbUrl"];
                        } else {
                            $thumbUrl = "//i.ytimg.com/vi/" . $vid . "/hqdefault.jpg";
                        }
                    }
                } else {
                    // no useable result from youtube, fallback on video thumbnail (doesn't work on playlist)
                    $thumbUrl = "//i.ytimg.com/vi/" . $vid . "/hqdefault.jpg";
                }
            } else {
                // same fallback
                $thumbUrl = "//i.ytimg.com/vi/" . $vid . "/hqdefault.jpg";
            }
            /** API: filter hook to override thumbnail URL */
            $thumbUrl = apply_filters('lyte_match_thumburl', $thumbUrl);
            if ($audio === true) {
                $wrapper = "<div class=\"lyte-wrapper-audio\" style=\"width:" . $lyteSettings[2] . "px;max-width:100%;overflow:hidden;height:38px;" . $lyteSettings['pos'] . "\">";
            } else {
                $wrapper = "<div class=\"lyte-wrapper" . $lyteSettings['ratioClass'] . "\" style=\"width:" . $lyteSettings[2] . "px;max-width: 100%;" . $lyteSettings['pos'] . "\">";
            }
            if ($doExcerpt) {
                $lytetemplate = "";
                $templateType = "excerpt";
            } elseif ($lyte_feed) {
                $postURL = get_permalink($postID);
                $textLink = $lyteSettings['links'] === 0 ? "" : "<br />" . strip_tags($lytelinks_txt, '<a>') . "<br />";
                $lytetemplate = "<a href=\"" . $postURL . "\"><img src=\"" . $thumbUrl . "\" alt=\"YouTube Video\"></a>" . $textLink;
                $templateType = "feed";
            } elseif ($audio !== true && $plClass !== " playlist" && ($lyteSettings['microdata'] === "1" && $noMicroData !== "1")) {
                $lytetemplate = $wrapper . "<div class=\"lyMe" . $audioClass . $hidefClass . $plClass . $qsaClass . "\" id=\"WYL_" . $vid . "\" itemprop=\"video\" itemscope itemtype=\"http://schema.org/VideoObject\"><meta itemprop=\"thumbnailUrl\" content=\"" . $thumbUrl . "\" /><meta itemprop=\"embedURL\" content=\"http://www.youtube.com/embed/" . $vid . "\" /><meta itemprop=\"uploadDate\" content=\"" . $yt_resp_array["dateField"] . "\" />" . $captionsMeta . "<div id=\"lyte_" . $vid . "\" data-src=\"" . $thumbUrl . "\" class=\"pL\"><div class=\"tC" . $titleClass . "\"><div class=\"tT\" itemprop=\"name\">" . $yt_resp_array["title"] . "</div></div><div class=\"play\"></div><div class=\"ctrl\"><div class=\"Lctrl\"></div><div class=\"Rctrl\"></div></div></div>" . $noscript . "<meta itemprop=\"description\" content=\"" . $yt_resp_array["description"] . "\"></div></div>" . $lytelinks_txt;
                $templateType = "postMicrodata";
            } else {
                $lytetemplate = $wrapper . "<div class=\"lyMe" . $audioClass . $hidefClass . $plClass . $qsaClass . "\" id=\"WYL_" . $vid . "\"><div id=\"lyte_" . $vid . "\" data-src=\"" . $thumbUrl . "\" class=\"pL\">";
                if (isset($yt_resp_array) && !empty($yt_resp_array) && !empty($yt_resp_array["title"])) {
                    $lytetemplate .= "<div class=\"tC" . $titleClass . "\"><div class=\"tT\">" . $yt_resp_array['title'] . "</div></div>";
                }
                $lytetemplate .= "<div class=\"play\"></div><div class=\"ctrl\"><div class=\"Lctrl\"></div><div class=\"Rctrl\"></div></div></div>" . $noscript . "</div></div>" . $lytelinks_txt;
                $templateType = "post";
            }
            /** API: filter hook to parse template before being applied */
            $lytetemplate = apply_filters('lyte_match_postparse_template', $lytetemplate, $templateType);
            $the_content = preg_replace($lytes_regexp, $lytetemplate, $the_content, 1);
        }
        // update lyte_cache_index
        if (is_array($toCache_index) && !empty($toCache_index)) {
            $lyte_cache = json_decode(get_option('lyte_cache_index'), true);
            $lyte_cache[$postID] = $toCache_index;
            update_option('lyte_cache_index', json_encode($lyte_cache));
        }
        if (!$lyte_feed) {
            lyte_initer();
        }
    }
    /** API: filter hook to postparse the_content before returning */
    $the_content = apply_filters('lyte_content_postparse', $the_content);
    return $the_content;
}
Пример #2
0
function lyte_check_yt_api_key_callback()
{
    check_ajax_referer("lyte_check_api_key", 'lyte_nonce');
    $api_key = strip_tags($_POST['lyte_yt_api_key']);
    // use random video to make sure a cache is not spoiling things
    $vidToCheck = array("jLOnUWJTCG0", "ZmnZHudtzXg", "2_7oQcAkyl8", "nOvv80wkSgI", "pBCt5nfsZ30", "KHw7gdJ14uQ", "qJ_PMvjmC6M", "DVwHCGAr_OE", "LtOGa5M8AuU", "VHO9uZX9FNU");
    $randVidIndex = array_rand($vidToCheck);
    $api_response = lyte_get_YT_resp($vidToCheck[$randVidIndex], false, "", $api_key);
    if (is_array($api_response)) {
        if (!empty($api_response["title"])) {
            _e("API seems OK, you can Save Changes below now.");
        } else {
            if (!empty($api_response["reason"])) {
                _e("API key not OK, your key seems to ");
                switch ($api_response["reason"]) {
                    case "keyInvalid":
                        _e("be invalid.", 'wp-youtube-lyte');
                        break;
                    case "ipRefererBlocked":
                        _e("be valid, but restricted to an IP-address which is not your server's.", 'wp-youtube-lyte');
                        _e("Try changing the allowed IP for your API key to include this one: ", 'wp-youtube-lyte');
                        echo $_SERVER["SERVER_ADDR"];
                        break;
                    case "keyExpired":
                        _e("have expired, please check in the Google Developer Console.", 'wp-youtube-lyte');
                        break;
                    case "limitExceeded":
                    case "quotaExceeded":
                    case "rateLimitExceeded":
                    case "userRateLimitExceeded":
                        _e("be correct, but seems to have exceeded the number of requests that can be made with it.", 'wp-youtube-lyte');
                        break;
                    case "videoNotFound":
                        _e("probably work, but as the video with id ", 'wp-youtube-lyte');
                        echo $vidToCheck[$randVidIndex];
                        _e(" was not found we cannot be sure, please try again.", 'wp-youtube-lyte');
                        break;
                    default:
                        _e("be faulty, with YouTube API returning reason: ", 'wp-youtube-lyte');
                        echo $api_response["reason"];
                }
            }
        }
    } else {
        _e("Something went wrong, WP YouTube Lyte might not have been able to retrieve information from the YouTube API, got error: ", 'wp-youtube-lyte');
        print_r($api_response);
    }
    wp_die();
}
Пример #3
0
    function widget($args, $instance)
    {
        extract($args);
        global $wSize, $wyl_version, $wp_lyte_plugin_url, $lyteSettings;
        $lyteSettings['path'] = plugins_url() . "/" . dirname(plugin_basename(__FILE__)) . '/lyte/';
        $qsa = "";
        $WYLtitle = apply_filters('widget_title', $instance['WYLtitle']);
        $WYLtext = apply_filters('widget_text', $instance['WYLtext'], $instance);
        $WYLsize = apply_filters('widget_text', $instance['WYLsize'], $instance);
        if ($WYLsize == "") {
            $WYLsize = $wDefault;
        }
        $WYLaudio = apply_filters('widget_text', $instance['WYLaudio'], $instance);
        if ($WYLaudio !== "audio") {
            $wrapperClass = " lidget";
            $audioClass = "";
            $wHeight = $wSize[$WYLsize]['h'];
        } else {
            $wrapperClass = "-audio lidget";
            $audioClass = " lyte-audio";
            $wHeight = "38";
        }
        $WYLurl = str_replace("httpv://", "http://", trim($instance['WYLurl']));
        $WYLqs = substr(strstr($WYLurl, '?'), 1);
        parse_str($WYLqs, $WYLarr);
        if (strpos($WYLurl, 'youtu.be')) {
            $WYLid = substr(parse_url($WYLurl, PHP_URL_PATH), 1, 11);
            $PLClass = "";
            $WYLthumb = "http://img.youtube.com/vi/" . $WYLid . "/mqdefault.jpg";
        } else {
            if (isset($WYLarr['v'])) {
                $WYLid = $WYLarr['v'];
                $PLClass = "";
                $WYLthumb = "http://img.youtube.com/vi/" . $WYLid . "/mqdefault.jpg";
            } else {
                if (isset($WYLarr['list'])) {
                    $WYLid = $WYLarr['list'];
                    $yt_resp = lyte_get_YT_resp($WYLid, true, "", "", true);
                    $WYLthumb = $yt_resp["thumbUrl"];
                    $PLClass = " playlist";
                }
            }
        }
        $WYLthumb = apply_filters("lyte_filter_widget_thumb", $WYLthumb, $WYLid);
        if (isset($WYLarr['start'])) {
            $qsa = "&amp;start=" . $WYLarr['start'];
        }
        if (isset($WYLarr['enablejsapi'])) {
            $urlArr = parse_url($lyteSettings['path']);
            $origin = $urlArr[scheme] . "://" . $urlArr[host] . "/";
            $qsa .= "&amp;enablejsapi=" . $WYLarr['enablejsapi'] . "&amp;origin=" . $origin;
        }
        if (!empty($qsa)) {
            $esc_arr = array("&" => "\\&", "?" => "\\?", "=" => "\\=");
            $qsaClass = " qsa_" . strtr($qsa, $esc_arr);
        } else {
            $qsaClass = "";
        }
        $WYL_dom_id = "YLW_" . $WYLid;
        ?>
	<?php 
        echo $before_widget;
        ?>
        <?php 
        if ($WYLtitle) {
            echo $before_title . $WYLtitle . $after_title;
        }
        ?>
	<div class="lyte-wrapper<?php 
        echo $wrapperClass;
        ?>
" style="width:<?php 
        echo $wSize[$WYLsize]['w'];
        ?>
px; height:<?php 
        echo $wHeight;
        ?>
px; min-width:200px; max-width:100%;"><div class="lyMe<?php 
        echo $PLClass;
        echo $audioClass;
        echo $qsaClass;
        ?>
" id="<?php 
        echo $WYL_dom_id;
        ?>
"><div id="lyte_<?php 
        echo $WYLid;
        ?>
" data-src="<?php 
        echo $WYLthumb;
        ?>
" class="pL"><div class="play"></div><div class="ctrl"><div class="Lctrl"></div></div></div></div><noscript><a href="http://youtu.be/<?php 
        echo $WYLid;
        ?>
"><img src="<?php 
        echo $WYLthumb;
        ?>
" alt="" /></a></noscript></div>
	<div><?php 
        echo $WYLtext;
        ?>
</div>
        <?php 
        echo $after_widget;
        ?>
        <?php 
        lyte_initer();
    }