<a href="https://console.cloud.google.com/apis/credentials">Get Your Api Credentials Here</a>
<a href="https://calendar.google.com/calendar">Get your Calender ID</a>
<?php
include_once("wp-load.php");
function get_calender_events()
{
$params = array();
/*Get current date*/
$current_date = date('Y-m-d H:i:s');
/*Convert it to google calendar's rfc_format */
$rfc_format = date("c", strtotime($current_date));
$params[] = 'orderBy=startTime';
$params[] ='maxResults=100';
$params[] = 'timeMin='.urlencode($rfc_format);
$url_param = '';
foreach($params as $param)
{
$url_param.= '&'.$param;
}
$calender_id = "calender_id";
$client_key = "client_key";
$url = "https://www.googleapis.com/calendar/v3/calendars/".$calender_id."/events?key=".$client_key."&singleEvents=true".$url_param;
$list_events = wp_remote_post($url, array(
'method' => 'GET',
'timeout' => 45,
)
);
$list_events = json_decode($list_events['body']);
$cal_events = $list_events->items;
$full_calender = array();
foreach($cal_events as $single_event)
{
if(isset($single_event->start->date) && strpos(json_encode($single_event), 'date') > 0 )
{
$all_day = 'yes';
$startdate = $single_event->start->date;
$enddate = $single_event->end->date;
$event_date = date('Y-m-d', strtotime($startdate));
}
if(isset($single_event->start->dateTime))
{
$all_day = 'no';
$startdate = $single_event->start->dateTime;
$enddate = $single_event->end->dateTime;
$event_date = date('Y-m-d', strtotime($startdate));
}
if($single_event->status == 'confirmed')
{
if(isset($single_event->start->timeZone))
{
$event_timezone = $single_event->start->timeZone;
}
else
{
$event_timezone = '';
}
if(isset($single_event->description) && $single_event->description != '')
{
$description = $single_event->description;
}
else
{
$description = '';
}
if(isset($single_event->location) && $single_event->location != '')
{
$location = $single_event->location;
}
else
{
$location = '';
}
$full_calender[] = array(
'name'=> $single_event->summary,
'description'=>wp_kses_post($description),
'all_day'=>$all_day,
'start_date'=>$startdate,
'end_date'=>$enddate,
'timezone'=>$event_timezone,
'location'=>wp_kses_post($location),
'link'=> esc_url_raw( $single_event->htmlLink ),
);
}
}/**end of foreach condition*/
usort($full_calender,function($a,$b)
{
$t1 = strtotime($a['start_date']);
$t2 = strtotime($b['start_date']);
return $t1 - $t2;
});
return $full_calender;
}
$events = get_calender_events();
$total_events = count($events);
$output = '';
if($total_events == 0)
{
$output.='<div class="gc_errors"><p>No events Found. Please add Some events to your calendar.</p></div>';
}
else
{
$events_to_show = $total_events;
$output.='<div id="ecfg_events_wrap" class="the_gc_event">';
for($i = 0; $i < $events_to_show; $i++)
{
$start_date = $events[$i]['start_date'];
$end_date = $events[$i]['end_date'];
$event_timezone = $events[$i]['timezone'];
$event_title = sanitize_text_field($events[$i]['name']);
$event_content = wp_kses_post($events[$i]['description']);
$event_location = wp_kses_post($events[$i]['location']);
$event_link = esc_url_raw($events[$i]['link']);
$alldayevent = $events[$i]['all_day'];
$output.= $event_title."<br>";
$output.= $event_content."<br>";
$output.= date('Y-m-d', strtotime($start_date))."<br>";
$output.= date('Y-m-d', strtotime($end_date))."<br>";
}
$output.='</div>';
}/*end for layout condition*/
$output.='</div>';
echo $output;
?>
Comments
Post a Comment