Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.54 KB | None | 0 0
  1. <?php
  2. /**
  3. * Class td_video_support - tagDiv video support V 2.0 @since 4 nov 2015
  4. * downloads the video thumbnail and puts it asa a featured image to the post
  5. */
  6. class td_video_support{
  7.  
  8. private static $on_save_post_post_id; // here we keep the post_id when the save_post hook runs. We need the post_id to pass it to the other hook @see on_add_attachment_set_featured_image
  9. private static $fb_access_token = 'EAAC0twN8wjQBAPOUhZAWJohvqwr4iEeGooiNEKoRkkJ0KMik9nSX6xiiMZCZBSgRRai8ZAHjZCzniq36dZBgbJw93Vsom56qBi24CqesirT2sNZBvN6yTylhjDED9ri4iShPON3grZAF0fpUijQTSmzxOO71h70fN7lFpN0YLhV3Ugs2ZCaZAdvfZAd';
  10.  
  11. private static $caching_time = 10800; //seconds -> 3 hours
  12.  
  13. // flag to make sure we run the 'on_save_post_get_video_thumb' save_post hook only once..
  14. // ..this is mainly bacause on gutenberg editor this hook runs twice and triggers a duplicate on video thumb generation
  15. private static $on_save_post_did_action = false;
  16.  
  17. /**
  18. * Render a video on the fornt end from URL
  19. * @param $videoUrl - the video url that we want to render
  20. *
  21. * @return string - the player HTML
  22. */
  23. static function render_video($videoUrl) {
  24. $buffy = '';
  25. switch (self::detect_video_service($videoUrl)) {
  26. case 'youtube':
  27. $buffy .= '
  28. <div class="wpb_video_wrapper">
  29. <iframe id="td_youtube_player" width="600" height="560" src="' . 'https://www.youtube.com/embed/' . self::get_youtube_id($videoUrl) . '?enablejsapi=1&feature=oembed&wmode=opaque&vq=hd720' . self::get_youtube_time_param($videoUrl) . '" frameborder="0" allowfullscreen=""></iframe>
  30. <script type="text/javascript">
  31. var tag = document.createElement("script");
  32. tag.src = "https://www.youtube.com/iframe_api";
  33.  
  34. var firstScriptTag = document.getElementsByTagName("script")[0];
  35. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  36.  
  37. var player;
  38.  
  39. function onYouTubeIframeAPIReady() {
  40. player = new YT.Player("td_youtube_player", {
  41. height: "720",
  42. width: "960",
  43. events: {
  44. "onReady": onPlayerReady
  45. }
  46. });
  47. }
  48.  
  49. function onPlayerReady(event) {
  50. player.setPlaybackQuality("hd720");
  51. }
  52. </script>
  53.  
  54. </div>
  55.  
  56. ';
  57.  
  58. break;
  59. case 'dailymotion':
  60. $buffy .= '
  61.  
  62. <iframe frameborder="0" width="600" height="560" src="' . td_global::$http_or_https . '://www.dailymotion.com/embed/video/' . self::get_dailymotion_id($videoUrl) . '"></iframe>
  63. </div>
  64. ';
  65. break;
  66.  
  67. case 'jwplayer':
  68. $buffy .= '
  69. <div class="wpb_video_wrapper">
  70. <iframe src="https://cdn.jwplayer.com/players/' . self::get_jwplayer_video_id($videoUrl) . '-' . self::get_jwplayer_player_ID() . '.html" width="100%" height="100%" scrolling="auto" title="' . self::get_jwplayer_title($videoUrl) . '" allowfullscreen></iframe>
  71. </div>
  72. ';
  73. break;
  74.  
  75. case 'vimeo':
  76. $buffy = '
  77. <div class="wpb_video_wrapper">
  78. <iframe src="' . td_global::$http_or_https . '://player.vimeo.com/video/' . self::get_vimeo_id($videoUrl) . '" width="500" height="212" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
  79. </div>
  80. ';
  81. break;
  82. case 'facebook':
  83. /* $buffy = '
  84. <div class="wpb_video_wrapper td-facebook-video">
  85. <iframe src="' . td_global::$http_or_https . '://www.facebook.com/plugins/video.php?href=' . urlencode($videoUrl) . '&show_text=0" width="' . $width . '" height="' . $height . '" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true" ></iframe>
  86. </div>
  87. ';
  88. */
  89.  
  90. /**
  91. * cache & oembed implementation
  92. */
  93. $cache_key = self::get_facebook_id($videoUrl);
  94. $group = 'td_facebook_video';
  95.  
  96. if (td_remote_cache::is_expired($group, $cache_key) === true) {
  97.  
  98. // cache is expired - do a request
  99. $facebook_api_json = td_remote_http::get_page('https://www.facebook.com/plugins/video/oembed.json/?url=' . urlencode($videoUrl) , __CLASS__);
  100.  
  101. if ($facebook_api_json !== false) {
  102. $facebook_api = @json_decode($facebook_api_json);
  103.  
  104. //json data decode
  105. if ($facebook_api === null and json_last_error() !== JSON_ERROR_NONE) {
  106. td_log::log(__FILE__, __FUNCTION__, 'json decode failed for facebook video embed api', $videoUrl);
  107. }
  108.  
  109. if (is_object($facebook_api) and !empty($facebook_api->html)) {
  110.  
  111. //add the html to the buffer
  112. $buffy = '<div class="wpb_video_wrapper">' . $facebook_api->html . '</div>';
  113.  
  114. //set the cache
  115. td_remote_cache::set($group, $cache_key, $facebook_api->html, self::$caching_time);
  116. }
  117.  
  118. } else {
  119. td_log::log(__FILE__, __FUNCTION__, 'facebook api html data cannot be retrieved/json request failed', $videoUrl);
  120. }
  121.  
  122. } else {
  123. // cache is valid
  124. $api_html_embed_data = td_remote_cache::get($group, $cache_key);
  125. $buffy = '<div class="wpb_video_wrapper">' . $api_html_embed_data . '</div>';
  126. }
  127. break;
  128. case 'twitter':
  129.  
  130. /**
  131. * cache & oembed implementation
  132. */
  133.  
  134. $cache_key = self::get_twitter_id($videoUrl);
  135. $group = 'td_twitter_video';
  136.  
  137.  
  138. if (td_remote_cache::is_expired($group, $cache_key) === true) {
  139.  
  140. // cache is expired - do a request
  141. $twitter_json = td_remote_http::get_page('https://publish.twitter.com/oembed?url=' . urlencode($videoUrl) . '&widget_type=video&align=center' , __CLASS__);
  142.  
  143. if ($twitter_json !== false) {
  144. $twitter_api = @json_decode($twitter_json);
  145.  
  146. //json data decode
  147. if ($twitter_api === null and json_last_error() !== JSON_ERROR_NONE) {
  148. td_log::log(__FILE__, __FUNCTION__, 'json decode failed for twitter video embed api', $videoUrl);
  149. }
  150.  
  151. if (is_object($twitter_api) and !empty($twitter_api->html)) {
  152.  
  153. //add the html to the buffer
  154. $buffy = '<div class="wpb_video_wrapper">' . $twitter_api->html . '</div>';
  155. //embed twitter featured video on AMP
  156. if(td_util::is_amp()) {
  157. $buffy = '<amp-twitter width="375"
  158. height="472"
  159. layout="responsive"
  160. data-tweetid="' . $cache_key . '">
  161. </amp-twitter>';
  162. }
  163.  
  164. //set the cache
  165. td_remote_cache::set($group, $cache_key, $twitter_api->html, self::$caching_time);
  166. }
  167.  
  168. } else {
  169. td_log::log(__FILE__, __FUNCTION__, 'twitter api html data cannot be retrieved/json request failed', $videoUrl);
  170. }
  171.  
  172. } else {
  173. // cache is valid
  174. $api_html_embed_data = td_remote_cache::get($group, $cache_key);
  175. $buffy = '<div class="wpb_video_wrapper">' . $api_html_embed_data . '</div>';
  176. //embed twitter featured video on AMP
  177. if(td_util::is_amp()) {
  178. $buffy = '<amp-twitter width="375"
  179. height="472"
  180. layout="responsive"
  181. data-tweetid="' . $cache_key . '">
  182. </amp-twitter>';
  183. }
  184. }
  185.  
  186. break;
  187. }
  188. return $buffy;
  189. }
  190.  
  191.  
  192. /**
  193. * Downloads the video thumb on the save_post hook
  194. * @param $post_id
  195. *
  196. * @updated 06.02.2019 - changed implementation to fix gutenberg compatibility issues
  197. */
  198. static function on_save_post_get_video_thumb($post_id) {
  199.  
  200. // bail if this hook has already run
  201. if( !td_global::get_demo_installing() && self::$on_save_post_did_action ){
  202. return;
  203. }
  204.  
  205. // if this is the first time this hook runs update the flag to avoid running this again
  206. self::$on_save_post_did_action = true;
  207.  
  208. //verify post is not a revision
  209. if ( !wp_is_post_revision( $post_id ) ) {
  210.  
  211. if (
  212. ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ||
  213. td_util::tdc_is_live_editor_ajax() ||
  214. td_util::tdc_is_live_editor_iframe() ||
  215. 'post' !== get_post_type($post_id)
  216. ) {
  217. return;
  218. }
  219.  
  220. // get the current submitted video url and the last saved post video url
  221. $td_post_video = td_util::get_post_meta_array($post_id, 'td_post_video');
  222. $td_last_set_video = get_post_meta($post_id, 'td_last_set_video', true);
  223.  
  224. // check to see if we have a video url submitted
  225. if ( empty( $td_post_video['td_video'] ) ) {
  226.  
  227. // if we have a last video saved
  228. if ( !empty( $td_last_set_video ) ) {
  229. // if we have an empty video url field and we have a last video saved, empty the last video saved meta..
  230. // ..to avoid cases in which we don't have an featured image set and we use the same video url the img does not set
  231. update_post_meta( $post_id, 'td_last_set_video', $td_post_video['td_video'] );
  232. }
  233.  
  234. return;
  235. }
  236.  
  237. // check to see if the url is valid
  238. // if ( self::validate_video_url( $td_post_video['td_video'] ) === false ) {
  239. // // we stop here if we do not have a valid video url
  240. // return;
  241. // }
  242.  
  243. // check to see if we have a last saved post video url and bail if its the same as the submitted video url
  244. if ( !empty( $td_last_set_video ) and $td_last_set_video == $td_post_video['td_video'] ) {
  245. return;
  246. }
  247.  
  248. $videoThumbUrl = self::get_thumb_url( $td_post_video['td_video'] );
  249.  
  250. // its time to setup the thumb
  251. if ( !empty( $videoThumbUrl ) ) {
  252.  
  253. // save the post id
  254. self::$on_save_post_post_id = $post_id;
  255.  
  256. // update the last saved post video url post meta
  257. update_post_meta( $post_id, 'td_last_set_video', $td_post_video['td_video'] );
  258.  
  259. // add the function above to catch the attachments creation
  260. add_action('add_attachment', array(__CLASS__, 'on_add_attachment_set_featured_image'));
  261.  
  262. // load the attachment from the URL
  263. media_sideload_image($videoThumbUrl, $post_id, $post_id);
  264.  
  265. // we have the Image now, and the function above will have fired too setting the thumbnail ID in the process, so lets remove the hook so we don't cause any more trouble
  266. remove_action('add_attachment', array(__CLASS__, 'on_add_attachment_set_featured_image'));
  267. }
  268. }
  269. }
  270.  
  271.  
  272. /**
  273. * set the last uploaded image as a featured image. We 'upload' the video thumb via the media_sideload_image call from above
  274. * @internal
  275. */
  276. static function on_add_attachment_set_featured_image($att_id){
  277. update_post_meta(self::$on_save_post_post_id, '_thumbnail_id', $att_id);
  278. }
  279.  
  280.  
  281. /**
  282. * detects if we have a recognized video service and makes sure that it's a valid url
  283. * @param $videoUrl
  284. * @return bool
  285. */
  286. private static function validate_video_url($videoUrl) {
  287. if (self::detect_video_service($videoUrl) === false) {
  288. return false;
  289. }
  290. if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $videoUrl)) {
  291. return false;
  292. }
  293. return true;
  294. }
  295.  
  296.  
  297. /**
  298. * Returns the video thumb url from the video URL
  299. * @param $videoUrl
  300. * @return string
  301. */
  302. protected static function get_thumb_url($videoUrl) {
  303.  
  304. switch (self::detect_video_service($videoUrl)) {
  305. case 'youtube':
  306. $yt_1920_url = td_global::$http_or_https . '://img.youtube.com/vi/' . self::get_youtube_id($videoUrl) . '/maxresdefault.jpg';
  307. $yt_640_url = td_global::$http_or_https . '://img.youtube.com/vi/' . self::get_youtube_id($videoUrl) . '/sddefault.jpg';
  308. $yt_480_url = td_global::$http_or_https . '://img.youtube.com/vi/' . self::get_youtube_id($videoUrl) . '/hqdefault.jpg';
  309.  
  310. if (!self::is_404($yt_1920_url)) {
  311. return $yt_1920_url;
  312. }
  313.  
  314. elseif (!self::is_404($yt_640_url)) {
  315. return $yt_640_url;
  316. }
  317.  
  318. elseif (!self::is_404($yt_480_url)) {
  319. return $yt_480_url;
  320. }
  321.  
  322. else {
  323. td_log::log(__FILE__, __FUNCTION__, 'No suitable thumb found for youtube.', $videoUrl);
  324. }
  325. break;
  326.  
  327. case 'dailymotion':
  328. $dailymotion_api_json = td_remote_http::get_page('https://api.dailymotion.com/video/' . self::get_dailymotion_id($videoUrl) . '?fields=thumbnail_url', __CLASS__);
  329. if ($dailymotion_api_json !== false) {
  330. $dailymotion_api = @json_decode($dailymotion_api_json);
  331. if ($dailymotion_api === null and json_last_error() !== JSON_ERROR_NONE) {
  332. td_log::log(__FILE__, __FUNCTION__, 'json decaode failed for daily motion api', $videoUrl);
  333. return '';
  334. }
  335.  
  336. if (!empty($dailymotion_api) and !empty($dailymotion_api->thumbnail_url)) {
  337. return $dailymotion_api->thumbnail_url;
  338. }
  339. }
  340. break;
  341.  
  342. case 'jwplayer':
  343.  
  344. // $jwthumb_url = 'https://cdn.jwplayer.com/v2/media/' . self::get_jwplayer_id($videoUrl) . '/poster.jpg';
  345.  
  346.  
  347. $jw_1920_url = td_global::$http_or_https . '://cdn.jwplayer.com/thumbs/' . self::get_jwplayer_id($videoUrl) . '-1920.jpg';
  348. $jw_720_url = td_global::$http_or_https . '://cdn.jwplayer.com/thumbs/' . self::get_jwplayer_id($videoUrl) . '-720.jpg';
  349. $jw_640_url = td_global::$http_or_https . '://cdn.jwplayer.com/thumbs/' . self::get_jwplayer_id($videoUrl) .'-640.jpg';
  350.  
  351. if (!self::is_404($jw_1920_url)) {
  352. return $jw_1920_url;
  353. }
  354.  
  355. elseif (!self::is_404($jw_720_url)) {
  356. return $jw_720_url;
  357. }
  358.  
  359. elseif (!self::is_404($jw_640_url)) {
  360. return $jw_640_url;
  361. }
  362.  
  363. else {
  364. td_log::log(__FILE__, __FUNCTION__, 'No suitable thumb found for youtube.', $videoUrl);
  365. }
  366. break;
  367.  
  368.  
  369.  
  370.  
  371.  
  372. case 'vimeo':
  373. $url = 'http://vimeo.com/api/oembed.json?url=https://vimeo.com/' . self::get_vimeo_id($videoUrl);
  374.  
  375. $response = wp_remote_get($url, array(
  376. 'timeout' => 10,
  377. 'sslverify' => false,
  378. 'user-agent' => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0'
  379. ));
  380.  
  381. if (!is_wp_error($response)) {
  382. $td_result = @json_decode(wp_remote_retrieve_body($response));
  383. $result = $td_result->thumbnail_url;
  384. $result = preg_replace("#_[0-9]+(x)?[0-9]+\.jpg#", '.jpg', $result);
  385.  
  386. return $result;
  387. }
  388. break;
  389.  
  390. case 'facebook':
  391. $facebook_api_json = td_remote_http::get_page('https://graph.facebook.com/v2.7/' . self::get_facebook_id($videoUrl) . '/thumbnails?access_token=' . self::$fb_access_token , __CLASS__);
  392.  
  393. if ( $facebook_api_json !== false ) {
  394. $facebook_api = @json_decode($facebook_api_json);
  395. if ($facebook_api === null and json_last_error() !== JSON_ERROR_NONE) {
  396. td_log::log(__FILE__, __FUNCTION__, 'json decode failed for facebook api', $videoUrl);
  397. return '';
  398. }
  399.  
  400. if (is_object($facebook_api) and !empty($facebook_api)) {
  401. foreach ($facebook_api->data as $result) {
  402. if ($result->is_preferred !== false) {
  403. return ($result->uri);
  404. }
  405. }
  406.  
  407. }
  408. }
  409. break;
  410.  
  411. case 'twitter':
  412. if (!class_exists('TwitterApiClient')) {
  413. require_once 'wp-admin/external/twitter-client.php';
  414. $Client = new TwitterApiClient;
  415. $Client->set_oauth (YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, SOME_ACCESS_KEY, SOME_ACCESS_SECRET);
  416. try {
  417. $path = 'statuses/show';
  418. $args = array (
  419. 'id' => self::get_twitter_id($videoUrl),
  420. 'include_entities' => true
  421. );
  422. $data = $Client->call( $path, $args, 'GET' );
  423.  
  424.  
  425. //json data decode
  426. if ($data === null) {
  427. td_log::log(__FILE__, __FUNCTION__, 'api call failed for twitter video thumbnail request api', $videoUrl);
  428. }
  429.  
  430. if (empty($data['entities']['media'])){
  431. add_filter( 'redirect_post_location', array( __CLASS__, 'td_twitter_notice_on_redirect_post_location' ), 99 );
  432.  
  433. } else {
  434. return $data['entities']['media'][0]['media_url'] . ':large';
  435. }
  436. }
  437. catch( Exception $Ex ){
  438. //twitter rate limit will show here
  439. //echo 'Caught exception: ', $Ex->getMessage();
  440. //print_r($Ex);
  441. }
  442. } else {
  443. add_filter( 'redirect_post_location', array( __CLASS__, 'td_twitter_class_notice_on_redirect_post_location' ), 99 );
  444. }
  445. }
  446. return '';
  447. }
  448.  
  449.  
  450. /*
  451. * youtube
  452. */
  453. private static function get_youtube_id($videoUrl) {
  454. $query_string = array();
  455. parse_str(parse_url($videoUrl, PHP_URL_QUERY), $query_string);
  456.  
  457. if (empty($query_string["v"])) {
  458. //explode at ? mark
  459. $yt_short_link_parts_explode1 = explode('?', $videoUrl);
  460.  
  461. //short link: http://youtu.be/AgFeZr5ptV8
  462. $yt_short_link_parts = explode('/', $yt_short_link_parts_explode1[0]);
  463. if (!empty($yt_short_link_parts[3])) {
  464. return $yt_short_link_parts[3];
  465. }
  466.  
  467. return $yt_short_link_parts[0];
  468. } else {
  469. return $query_string["v"];
  470. }
  471. }
  472.  
  473. /*
  474. * youtube t param from url (ex: http://youtu.be/AgFeZr5ptV8?t=5s)
  475. */
  476. private static function get_youtube_time_param($videoUrl) {
  477. $query_string = array();
  478. parse_str(parse_url($videoUrl, PHP_URL_QUERY), $query_string);
  479. if (!empty($query_string["t"])) {
  480.  
  481. if (strpos($query_string["t"], 'm')) {
  482. //take minutes
  483. $explode_for_minutes = explode('m', $query_string["t"]);
  484. $minutes = trim($explode_for_minutes[0]);
  485.  
  486. //take seconds
  487. $explode_for_seconds = explode('s', $explode_for_minutes[1]);
  488. $seconds = trim($explode_for_seconds[0]);
  489.  
  490. $startTime = ($minutes * 60) + $seconds;
  491. } else {
  492. //take seconds
  493. $explode_for_seconds = explode('s', $query_string["t"]);
  494. $seconds = trim($explode_for_seconds[0]);
  495.  
  496. $startTime = $seconds;
  497. }
  498.  
  499. return '&start=' . $startTime;
  500. } else {
  501. return '';
  502. }
  503. }
  504.  
  505. /*
  506. * Vimeo id
  507. */
  508. private static function get_vimeo_id($videoUrl) {
  509. sscanf(parse_url($videoUrl, PHP_URL_PATH), '/%d', $video_id);
  510. return $video_id;
  511. }
  512.  
  513. /*
  514. * JWPlayer
  515. */
  516. private static function get_jwplayer_video_id($videoUrl) {
  517. sscanf(parse_url($videoUrl, PHP_URL_PATH), '/%d', $video_id);
  518. // var_dump($videoUrl);
  519. return $videoUrl;
  520. }
  521.  
  522. private static function get_jwplayer_title($videoUrl)
  523. {
  524. $jw_id = self::get_jwplayer_video_id($videoUrl);
  525. $jw_json = file_get_contents('https://cdn.jwplayer.com/v2/media/' . $jw_id);
  526. $jw_array = json_decode($jw_json);
  527. $jwtitle = $jw_array->title;
  528. return $jwtitle;
  529. }
  530.  
  531. private static function get_jwplayer_player_ID()
  532. {
  533. $player_id = td_util::get_option('jw_player_id', 'TADMMIT9');
  534. return $player_id;
  535. }
  536.  
  537. /*
  538. * Dailymotion
  539. */
  540. private static function get_dailymotion_id($videoUrl) {
  541. $id = strtok(basename($videoUrl), '_');
  542. if (strpos($id,'#video=') !== false) {
  543. $videoParts = explode('#video=', $id);
  544. if (!empty($videoParts[1])) {
  545. return $videoParts[1];
  546. }
  547. } else {
  548. return $id;
  549. }
  550. return '';
  551. }
  552.  
  553. /**
  554. * Facebook
  555. * @param $videoUrl
  556. * @return string - the fb video id
  557. */
  558. private static function get_facebook_id($videoUrl) {
  559.  
  560. /**
  561. * https://www.facebook.com/{page-name}/videos/{video-id}/
  562. * https://www.facebook.com/{username}/videos/{video-id}/ - user's video must be public
  563. * https://www.facebook.com/video.php?v={video-id}
  564. * https://www.facebook.com/video.php?id={video-id} - this video url does not work in this format
  565. */
  566.  
  567. if (strpos($videoUrl, '//www.facebook.com') !== false) {
  568.  
  569. $id = basename($videoUrl);
  570. if (strpos($id,'video.php?v=') !== false) {
  571. $query = parse_url($videoUrl, PHP_URL_QUERY);
  572. parse_str($query, $vars);
  573. return $vars['v'];
  574. } else {
  575. return $id;
  576. }
  577. }
  578. return '';
  579. }
  580.  
  581. /**
  582. * Twitter
  583. * @param $videoUrl
  584. * @return string - the tweet id
  585. */
  586. private static function get_twitter_id($videoUrl) {
  587.  
  588. /**
  589. * https://twitter.com/video/status/760619209114071040
  590. */
  591.  
  592. if (strpos($videoUrl, 'twitter.com') !== false) {
  593. $id = basename($videoUrl);
  594. return $id;
  595. }
  596. return '';
  597. }
  598.  
  599. /**
  600. * appends a query variable to the URL query, to show the 'non supported embeddable twitter videos' notice, on the redirect_post_location hook
  601. * @param $location - the destination URL
  602. * @return mixed
  603. */
  604. static function td_twitter_notice_on_redirect_post_location( $location ) {
  605. remove_filter( 'redirect_post_location', array( __CLASS__, 'td_twitter_notice_on_redirect_post_location' ), 99 );
  606. return add_query_arg( 'td_twitter_video', 'error_notice', $location );
  607. }
  608.  
  609. /**
  610. * the twitter video notice for non supported embeddable twitter videos
  611. */
  612. static function td_twitter_on_admin_notices() {
  613. if ( ! isset( $_GET['td_twitter_video'] ) ) {
  614. return;
  615. }
  616.  
  617. ?>
  618. <div class="notice notice-error is-dismissible">
  619. <p>Sorry, but the twitter video you have used is not supported by twitter api, so the video thumb image cannot be retrieved!<br>
  620. Some twitter videos, like Vine and Amplify or other content videos are not available through the twitter API therefore resources, like video thumb images, are not available.</p>
  621. </div>
  622. <?php
  623. }
  624.  
  625. /**
  626. * appends a query variable to the URL query, to show the 'class already defined' notice, on the redirect_post_location hook
  627. * @param $location - the destination URL
  628. * @return mixed
  629. */
  630. static function td_twitter_class_notice_on_redirect_post_location( $location ) {
  631. remove_filter( 'redirect_post_location', array( __CLASS__, 'td_twitter_class_notice_on_redirect_post_location' ), 99 );
  632. return add_query_arg( 'td_twitter_video_class', 'class_notice', $location );
  633. }
  634.  
  635. /**
  636. * the twitter video notice for class already defined
  637. */
  638. static function td_twitter_class_on_admin_notices() {
  639. if ( ! isset( $_GET['td_twitter_video_class'] ) ) {
  640. return;
  641. }
  642.  
  643. ?>
  644. <div class="notice notice-error">
  645. <p>The twitter api class is already defined! It might have been already defined by one of your plugins so please try without having any plugins active!</p>
  646. </div>
  647. <?php
  648. }
  649.  
  650. /*
  651. * Detect the video service from url
  652. */
  653. private static function detect_video_service($videoUrl) {
  654. $videoUrl = strtolower($videoUrl);
  655. if (strpos($videoUrl,'youtube.com') !== false or strpos($videoUrl,'youtu.be') !== false) {
  656. return 'youtube';
  657. }
  658. if (strpos($videoUrl,'dailymotion.com') !== false) {
  659. return 'dailymotion';
  660. }
  661. if (strpos($videoUrl,'vimeo.com') !== false) {
  662. return 'vimeo';
  663. }
  664. if (strpos($videoUrl,'facebook.com') !== false) {
  665. return 'facebook';
  666. }
  667.  
  668. if (strpos($videoUrl,'twitter.com') !== false) {
  669. return 'twitter';
  670. }
  671. if (strpos($videoUrl,'.com') == false && strlen($videoUrl)== 8) {
  672. return 'jwplayer';
  673. }
  674. return false;
  675. }
  676.  
  677. /**
  678. * detect a 404 page
  679. * @param $url
  680. * @return bool
  681. */
  682. private static function is_404($url) {
  683. $headers = @get_headers($url);
  684. if (!empty($headers[0]) and strpos($headers[0],'404') !== false) {
  685. return true;
  686. }
  687. return false;
  688. }
  689.  
  690.  
  691. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement