Last active
January 5, 2022 12:57
-
-
Save johnnya23/f684406878bdffeebd20d46e32134c11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function jma_yt_kid_curl($url) | |
{ | |
$curl = curl_init($url); | |
//curl_setopt($curl, CURLOPT_SSLVERSION,3);//forMAMP | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
$return = json_decode($result, true); | |
if (!$return || array_key_exists('error', $return)) { | |
if (is_array($return) && array_key_exists('error', $return)) { | |
$return = $return['error']['errors'][0]['reason']; | |
}//keyInvalid, playlistNotFound, accessNotConfigured, ipRefererBlocked, keyExpired | |
else { | |
$return = 'unknown'; | |
} | |
} | |
return $return; | |
} | |
function jma_kid_video_snippet($id) | |
{ | |
global $jmayt_api_code; | |
$snippet = array(); | |
$youtube = 'https://www.googleapis.com/youtube/v3/videos?&part=snippet,contentDetails,statistics,status&id=' . $id . '&key=' . $jmayt_api_code; | |
$curl_array = jma_yt_kid_curl($youtube); | |
if (is_array($curl_array)) { | |
if (!count($curl_array['items'])) { | |
return 'videoNotFound'; | |
} | |
$snippet = $curl_array['items'][0]; | |
} elseif (is_string($curl_array)) { | |
$snippet = $curl_array; | |
} | |
return $snippet; | |
} | |
function jma_kid_build_ythtml($yt_array) | |
{ | |
$top = $bottom = ''; | |
foreach ($yt_array as $i => $item) { | |
$open_div = '<div style="background:#004400;position:relative;width:49%;">'; | |
$image = '<a href="' . $item['address'] . '">'; | |
$image .= '<img style="width: 100%;height:auto" src="' . $item['image_url'] . '" alt="' . $item['title'] . '">'; | |
$image .= '</a>'; | |
$caption = '<div style="padding:3px;color:#ffffff;text-align:center;font-size:0.8em;line-height: 110%">click for video:<br/>' . $item['title'] . '</div>'; | |
$close_div = '</div>'; | |
if (!$i) {//first item | |
$open_div = str_replace('style="', 'style="float:right;margin: 0 0 15px 15px;', $open_div); | |
$top .= $open_div . $image . $caption . $close_div; | |
} else {//bottom | |
$open_div = str_replace('style="', 'style="float:left;margin-bottom:3px;', $open_div); | |
if ($i % 2) {//odd | |
$open_div = str_replace('style="', 'style="clear:both;', $open_div); | |
} else {//even(left) | |
$open_div = str_replace('style="', 'style="margin-left:1%;', $open_div); | |
} | |
$bottom .= $open_div . $image . $caption . $close_div; | |
} | |
} | |
return array('top' => $top, 'bottom' => $bottom); | |
} | |
function jma_kid_the_content_feed($content) | |
{ | |
if (has_tag('share')) { | |
global $post; | |
$yt_array = array(); | |
$blocks = parse_blocks($post->post_content); | |
foreach ($blocks as $block) { | |
if ($block['blockName'] == 'jmayt-single/block') { | |
$snippet = jma_kid_video_snippet($block['attrs']['id']); | |
if (is_array($snippet)) { | |
$img_url = isset($snippet['snippet']['thumbnails']['maxres'])? $snippet['snippet']['thumbnails']['maxres']['url']:$snippet['snippet']['thumbnails']['high']['url']; | |
$yt_array[] = array( | |
'title' => $snippet['snippet']['title'], | |
'image_url' => $img_url, | |
'address' => 'https://www.youtube.com/watch?v=' . $block['attrs']['id'] | |
); | |
} | |
} | |
} | |
$thumb = get_the_post_thumbnail($post, 'large', array( 'style' => 'width:100%;margin-bottom:15px' )); | |
$title = '<div style"margin-bottom: 10px"><a href="' . get_the_permalink() . '" alt="' . get_the_title() . '"><h2>' . get_the_title() . '</h2></a>'; | |
$by_line = '<div style"margin-bottom: 10px">by ' . get_the_author() . ' on ' . get_the_date('F jS') . '</div>'; | |
$ythtmls = array('top' => '', 'bottom' => ''); | |
if (count($yt_array)) { | |
$ythtmls = jma_kid_build_ythtml($yt_array); | |
} | |
$content = $thumb . $title . $by_line. $ythtmls['top'] . $content. $ythtmls['bottom']; | |
$content = str_replace( | |
array( | |
'<button class="jmayt-btn jmayt-sm" ></button>', | |
'<sup style="margin-right: 0.5em;font-size: 1em;opacity:0.5"><i class="fas fa-quote-left"></i></sup>', | |
'<div id="jmaty' | |
), | |
array( | |
'', | |
'"', | |
'<div style="display:none" id="jmaty' | |
), | |
$content | |
); | |
} | |
return $content; | |
} | |
function jma_kid_template_redirect() | |
{ | |
add_filter('the_content_feed', 'jma_kid_the_content_feed'); | |
} | |
add_action('template_redirect', 'jma_kid_template_redirect'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment