Skip to main content

Get Google Calendar Event List in WordPress

<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, ...

Google Calender Event Plugin

<?php
/*
  Plugin Name: Google Calender Event Plugin
  Plugin URI:
  Description: This plugin provides add event on google calender
  Version: 1.0
  Author: Samrat
  Author URI: http://www.edreamztech.com
 */
 global $samrat_db_version;
$samrat_db_version = '1.0';


function samrat_install() {
 global $wpdb;
 global $samrat_db_version;
 $charset_collate = $wpdb->get_charset_collate();
 $sql = "CREATE TABLE IF NOT EXISTS `wp_google_calender_event_list` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `google_event_id` varchar(255) NOT NULL,
  `booking_id` int(11) NOT NULL,
  `booking_type` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) $charset_collate;";
 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
 dbDelta( $sql );
 add_option( 'samrat_db_version', $samrat_db_version );
}
register_activation_hook( __FILE__, 'samrat_install' );

//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

 


function ez_custom_settings() {

  $client_id = get_option('client_id');
    $email_address = get_option('email_address');
    $applicationname = get_option('applicationname');
 $pfile = get_option('pfile');


    add_menu_page( 'Google Event', 'Google Setting', 'manage_options','google_details', 'google_details_options');
   add_submenu_page(
            'google_details', 'Add client_application', 'Add client_application', 'manage_options', 'add_client', 'nme_banner_menu'
    );
 if($client_id!="" && $email_address!="" && $applicationname!="" && $pfile!=""){
 add_submenu_page(
            'google_details', 'Export Houses', 'Export Houses', 'manage_options', 'export_houses', 'export_houses_here'
    );
 }

}
add_action('admin_menu', 'ez_custom_settings');


//view client_application link
function google_details_options()
{
 $submit = isset($_POST['submit']) ? $_POST['submit'] : '';
    if ($submit) {
        update_option('client_id', $_POST['client_id']);
        update_option('email_address', $_POST['email_address']);
   update_option('calid', $_POST['calid']);
        update_option('applicationname', $_POST['applicationname']);
   
   
// post data strts

 $allowedExts = array("jpg", "jpeg", "gif", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ($_FILES["file"]["name"]!="")
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
         
            if (file_exists( ABSPATH ."wp-content/plugins/google-add-event/upload/" . $_FILES["file"]["name"]))
            {
               // echo $_FILES["file"]["name"] . " file already exists. ";
     echo '<div class="updated settings-error" id="setting-error-settings_updated">
                                <p><strong>file already exists.</strong></p></div>';
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                                   ABSPATH ."wp-content/plugins/google-add-event/upload/" . $_FILES["file"]["name"]);
           update_option('pfile', $_FILES["file"]["name"]);
               // echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
   
   
            }
        }
    }
  
// post data ends

   
   
   
        echo '<div class="updated settings-error" id="setting-error-settings_updated">
                                <p><strong>Settings saved.</strong></p></div>';
    }
   // $ez_home_logo = get_option('ez_home_logo');
    $client_id = get_option('client_id');
    $email_address = get_option('email_address');
    $applicationname = get_option('applicationname');
  $calid = get_option('calid');
 $pfile = get_option('pfile');



    ?>

<script>
function add_alldata()
{
   // alert(x);
    //jQuery('#response').html("<img src='<?php echo  get_site_url(); ?>/wp-content/themes/7m/images/loading.gif' />");
      jQuery.ajax({
          type:'POST',
          data:{action:'delete_boot'},
            url: '<?php echo  get_site_url(); ?>/wp-content/plugins/google-add-event/action.php',
             cache: false,
          success: function(value) {
            jQuery('#response').html("");
            alert("Export to google calendar successfully..");
      //location.reload();
           //jQuery("#showcontent").html(value);
          },
        }); 
}

</script>
    <div id="settings"><br><br>

 <h2> Google Calender Setting</h2>
        <form action="" method="POST" enctype="multipart/form-data">
            <div>
                <table>
                    <tr>
                        <th><label>Client Id:</label></th>
                        <td><input size="70px" type="text" value="<?php echo $client_id; ?>" name="client_id" id="client_id" required/></td>
                    </tr>
                    <tr>
                        <th><label>Email Address :</label></th>
                        <td><input size="70px" type="text" value="<?php echo $email_address; ?>" name="email_address" id="email_address" required/></td>
                    </tr>
                    <tr>
                        <th><label>Application Name :</label></th>
                        <td>
                        <input size="70px" type="text" value="<?php echo $applicationname; ?>" name="applicationname" id="applicationname" required/>
                        </td>
                    </tr>
     <tr>
                        <th><label>Calendar Id :</label></th>
                        <td>
                        <input size="70px" type="text" value="<?php echo $calid; ?>" name="calid" id="calid" required/>
                        </td>
                    </tr>
    
      <tr>
                        <th><label>P2 File:</label></th>
     
                        <td><input size="70px" type="text" value="<?php echo $pfile; ?>" name="pfile" id="pfile" readonly/><br>
      <input name="file" type="file" /></td>
                    </tr>
                </table>
            </div>
            <br><br>
            <input type="submit" name="submit" value="Save Changes" class="button button-primary"/>
        </form>
    </div>
    <?php
}

function export_houses_here()
{global $wpdb;
$sql = "SELECT * FROM wp_bookingtypes;";
$pageposts = $wpdb->get_results($sql);?>
<script>

function add_data(x,y)
{
   // alert(x);
    jQuery('#response').html("Progress...");
      jQuery.ajax({
          type:'POST',
          data:{action:'delete_boot',id:x,title:y},
            url: '<?php echo  get_site_url(); ?>/wp-content/plugins/google-add-event/action.php',
             cache: false,
          success: function(value) {
            jQuery('#response').html("");
            alert("Export to google calendar successfully..");
      //location.reload();
           //jQuery("#showcontent").html(value);
          },
        }); 
}
function add_alldata()
{
   // alert(x);
    jQuery('#response').html("Progress...");
      jQuery.ajax({
          type:'POST',
          data:{action:'delete_boot'},
           url: '<?php echo  get_site_url(); ?>/wp-content/plugins/google-add-event/action.php',
             cache: false,
          success: function(value) {
            jQuery('#response').html("");
            alert("Export to google calendar successfully..");
      //location.reload();
           //jQuery("#showcontent").html(value);
          },
        }); 
}
 
</script>
<style>
.title_text{font-size:12px;}
table td{ font-size:11px;}
.event_table{ padding:0 10px 0 0;}
.red{color:#dc1522; line-height:30px; font-weight:700;}
.green{color:#00c42e; line-height:30px; font-weight:700;}
.pagination{ float:right; margin:10px 10px 0 0; border:#CCCCCC solid 1px; background:#FFFFFF;}
.pagination a{ border-right:#CCCCCC solid 1px; padding:0 10px; line-height:20px; text-decoration:none;}
.pagination a:last-child{ border-right:none;}
</style>
<div class="wrap">
<h2>Resources Details</h2>
<div id="response" style="
    float: right;
    margin-right: 93px;
"></div>

<div class="event_table"> 
<table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#e5e5e5">
  <tr>
  <!-- <td align="center" valign="middle" bgcolor="#dfdfdf" class="title_text"><strong>client_name</strong></td>-->
  <td align="center" valign="middle" bgcolor="#dfdfdf" class="title_text"><strong>ID</strong></td>
  <td align="center" valign="middle" bgcolor="#dfdfdf" class="title_text"><strong>Resource name</strong></td>
     <td align="center" valign="middle" bgcolor="#dfdfdf" class="title_text"><strong>Cost </strong></td>  
  <td align="center" valign="middle" bgcolor="#dfdfdf" class="title_text"><strong>Action</strong><br />
                              <a href="javascript:;" onclick="add_alldata()">Export All</a></td>
  </tr>
<?php
foreach ($pageposts as $singlepost) {
?>
  <tr>
  <td align="left" valign="top" bgcolor="#FFFFFF"><?php echo $singlepost->booking_type_id;?></td>
  <td align="left" valign="top" bgcolor="#FFFFFF"><?php echo $singlepost->title;?></td>
  <td align="left" valign="top" bgcolor="#FFFFFF"><?php echo $singlepost->cost;?></td>
  <td align="left" valign="top" bgcolor="#FFFFFF">
    <a href="javascript:;" onclick="add_data(<?php echo $singlepost->booking_type_id; ?>,'<?php echo $singlepost->title; ?>')"> Export</a>
 
  </td>
  </tr>
<?php
}?>
</table>
</div>
<?php die;
}
function nme_banner_menu() {
            $action = isset($_POST['submit']) ? $_POST['submit'] : '';
            $saved = false;
            $options = '_banner';
            if ($action) {
                update_option($options, $_POST[$options]);
                $saved = true;
            }
            $$options = get_option($options);
            if (!is_array($$options)) {
                $$options = array();
            }
            ?>
        <form method="post" action="">
            <div id="tabs" style="width:95%" >
                <ul>
                    <li><a href="#tab-1">Banner 1</a></li>
                    <li><a href="#tab-2">Banner 2</a></li>
                    <li><a href="#tab-3">Banner 3</a></li>
                    <li><a href="#tab-4">Banner 4</a></li>
                </ul>
    <?php
    for ($i = 1; $i <= 4; $i++) {
        if ($i == 1) {
            $span_text = '(image size should not exceed 873x457 px)';
        } else {
            $span_text = '(image size should not exceed 234x149 px)';
        }
        echo '<div id="tab-' . $i . '" >
                                 <table class="nme-banner-content" >
                                     <tr class="nme_image_src">
                                        <th><label for="nme-image-src' . $i . '">  Image SRC :</label></th>
                                         <td><input type="text" name="_banner[nme-image-src' . $i . ']"  id="image_src" value="' . $_banner['nme-image-src' . $i . ''] . '" class="nme-image-src' . $i . '" />
                                    <input type="submit" class="button upload_image" name="upload_button" id="uploader_button" value="Select an Image" />
                                         <span class="banner_text">' . $span_text . '</span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td>
                                                 <img src="' . $_banner['nme-image-src' . $i . ''] . '" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="nme-image-src' . $i . '-alt">  Alt Text :</label></th>
                                        <td><input type="text" value="' . $_banner['nme-image-src' . $i . '-alt'] . '" name="_banner[nme-image-src' . $i . '-alt]" id="nme-image-src' . $i . '-alt"/></td>
                                    </tr>
                                    <tr>
                                        <th><label for="nme-image-src' . $i . '-url"> Text URL :</label></th>
                                        <td><input type="text" value="' . $_banner['nme-image-src' . $i . '-url'] . '" name="_banner[nme-image-src' . $i . '-url]" id="nme-image-src' . $i . '-url"/></td>
                                    </tr>
                                    <tr>
                                        <th><label for="nme-image-src' . $i . '-title-link"> Link Title :</label></th>
                                        <td><input type="text" value="' . $_banner['nme-image-src' . $i . '-title-link'] . '" name="_banner[nme-image-src' . $i . '-title-link]" id="nme-image-src' . $i . '-title-link"/></td>
                                    </tr>
                                     <tr>
                                       <th><label for=""nme-image-src' . $i . '-link-target""> Link Target:</label></th>
                                       <td>
                                       <select name="_banner[nme-image-src' . $i . '-link-target]"  id="nme-image-src' . $i . '-link-target">
                                            <option value="_new_window" ' . selected($_banner['nme-image-src' . $i . '-link-target'], '_new_window', false) . '>Open In New Window</option>
                                            <option value="_same_window" ' . selected($_banner['nme-image-src' . $i . '-link-target'], '_same_window', false) . '>Stay In Same Window</option>
                                        </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><label for="nme-image-src' . $i . '-headline"> Headline :</label></th>
                                        <td><input type="text" value="' . $_banner['nme-image-src' . $i . '-headline'] . '" name="_banner[nme-image-src' . $i . '-headline]" id="nme-image-src' . $i . '-headline"/></td>
                                    </tr>
                                     <tr class="nme_video_image">
                                       <th><label for="nme-video-image-src' . $i . '"> Select Type :</label></th>
                                       <td>
                                       <select name="_banner[nme-video-image-src' . $i . ']"  id="nme-video-image-src' . $i . '">
                                            <option value="image" ' . selected($_banner['nme-video-image-src' . $i . ''], 'image', false) . '>Image</option>
                                            <option value="video" ' . selected($_banner['nme-video-image-src' . $i . ''], 'video', false) . '>Video</option>
                                        </select>
                                        </td>
                                    </tr>
                                     </table>
                                    </div>
                                    ';
    }
    ?>
            </div>
            <input type="submit" name="submit" value="Save Changes" class="button button-primary"/>  
        </form>
                <?php
            }
  
   ?>
   ***********************************
  
In action.php
<?php
include_once("../../../wp-load.php");
global $wpdb;
$id=$_POST['id'];
global $wpdb;
$client_id = get_option('client_id');
$email_address = get_option('email_address');
$applicationname = get_option('applicationname');
$pfile = get_option('pfile');
$calid = get_option('calid');
     require 'google-api-php-client-master/src/Google/autoload.php';
        require_once 'google-api-php-client-master/src/Google/Client.php';
        require_once 'google-api-php-client-master/src/Google/Service/Calendar.php';
        //$client_id = '417715593465-jq77uqm8a94obaoe1m0imfj7l9k94t2q.apps.googleusercontent.com'; //change this
        //$Email_address = '417715593465-jq77uqm8a94obaoe1m0imfj7l9k94t2q@developer.gserviceaccount.com'; //change this
        $key_file_location = 'upload/'. $pfile; //change this
        $client = new Google_Client();
        $client->setApplicationName($applicationname);
        $key = file_get_contents($key_file_location);
 
 

        $scopes = "https://www.googleapis.com/auth/calendar";
        $cred = new Google_Auth_AssertionCredentials(
                $Email_address, array($scopes), $key
        );
        $client->setAssertionCredentials($cred);
        if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion($cred);
        }
        $service = new Google_Service_Calendar($client);
        $calendarList = $service->calendarList->listCalendarList();
        while (true) {
            foreach ($calendarList->getItems() as $calendarListEntry) {
                //echo "<a href='Oauth2.php?type=event&id=" . $calendarListEntry->id . " '>" . $calendarListEntry->getSummary() . "</a><br>\n";
            }
            $pageToken = $calendarList->getNextPageToken();
            if ($pageToken) {
                $optParams = array('pageToken' => $pageToken);
                $calendarList = $service->calendarList->listCalendarList($optParams);
            } else {
                break;
            }
        }
       
function DataIT2DB($datapega) {
            if ($datapega) {
                $data = explode('/', $datapega);
                if (count($data) > 1) {
                    $datacerta = $data[2] . '-' . $data[1] . '-' . $data[0];
                } else {
                    $datacerta = $datapega;
                }
            } else {
                $datacerta = $datapega;
            }
            return $datacerta;
        }
        function geratime($DateStart, $TimeStart) {
            $dataHora = $DateStart . 'T' . $TimeStart . ':00.000+02:00'; //Fuso Rome
            return $dataHora;
        }


if($id!="")
$where= "booking_type_id = $id";
//$action=$_POST['action'];
$sqlhouse='SELECT * FROM wp_bookingtypes';
        if($where != null)
            $sqlhouse .= ' WHERE '.$where;
     
    echo  $sqlhouse;
$houses = $wpdb->get_results($sqlhouse);
foreach($houses as $house)
{
    echo $id = $house->booking_type_id;
     echo "-----";
    echo $booking_title = $house->title;
    echo ", ";

$sql="SELECT * FROM wp_booking where booking_type=$id";
$events = $wpdb->get_results($sql);
foreach($events as $event)
{
$booking_id=$event->booking_id;
{
   
   
    //  $calid="0qv3ue79acbod88nvsg0889jt0@group.calendar.google.com";
   
}
$sql55="SELECT * FROM wp_google_calender_event_list WHERE booking_id =$booking_id";
$events55 = $wpdb->get_row($sql55);
if($events55->booking_id=='')
{
 
  $sql1="SELECT MIN(booking_date) AS startdata, MAX(booking_date) AS enddate
   FROM wp_bookingdates where booking_id=$booking_id";
$events1 = $wpdb->get_row($sql1);
 $startdata=$events1->startdata;
 $enddate=$events1->enddate;
 $title=$bt->title;
 $startdata = date("d/m/Y", strtotime($startdata));
 $enddate = date("d/m/Y", strtotime($enddate));

$test=$event->form;
$test=str_replace("~text","",$test);
$test=str_replace("~email","",$test);
$test=str_replace("~checkbox","",$test);
$test=str_replace("~select-one","",$test);
$test=explode("^",$test);
if($test[16]!="")
{
    $TimeStart=$test[16];
}
else{
       $TimeStart="12:00";
    
}
 
  if($test[18]!="")
{
    $TimeEnd=$test[18];
}
else{
       $TimeEnd="12:00";
}
 
 
            $Summary = "Resource name :".$booking_title."(".$booking_id.":".$test[2].")";
   $Description = "First Name:".$test[2]." Last Name:".$test[4]." Email:".$test[6]." Phone:".$test[8]." Number of visitors:".$test[10]." Start Time:".$test[16]. " End Time:".$test[18];
            $Location = "";
            $DateStart = $startdata;
            $DateEnd = $enddate;
           
           
            //$DateStart = "25/10/2015";
         
           // $DateEnd = "26/10/2015";
           
           
           
       $event = new Google_Service_Calendar_Event();
        $event->setSummary($Summary);
        $event->setLocation($Location);
        $event->setDescription($Description);
        $start = new Google_Service_Calendar_EventDateTime();
        $datatimeI = geratime(DataIT2DB($DateStart), $TimeStart);
       
        $start->setDateTime($datatimeI);
        $event->setStart($start);
        $end = new Google_Service_Calendar_EventDateTime();
        $datatimeF = geratime(DataIT2DB($DateEnd), $TimeEnd);
       
        $end->setDateTime($datatimeF);
        $event->setEnd($end);
        $attendee1 = new Google_Service_Calendar_EventAttendee();
        $attendee1->setEmail($calid);
        $attendees = array($attendee1);
        $event->attendees = $attendees;
       
       
        $createdEvent = $service->events->insert('primary', $event);
         $google_event_id= $_SESSION['eventID'] = $createdEvent->getId();
       
         $sql4="INSERT INTO `wp_google_calender_event_list` (`id`, `google_event_id`, `booking_id`, `booking_type`) VALUES (NULL, '$google_event_id', '$booking_id', '$id');";
       
        // echo $sql;
        $wpdb->query($sql4);

  }
else
{
 echo $events->booking_id;
    echo "<br>";
}

         
}
}


?>
   

Comments

Popular posts from this blog

Create Signature pad with save on database in php

Create Signature pad with save on database in php 1.create a folder images index.php ============   <!DOCTYPE >     <head>     <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />     <title>Signature Pad</title>     <script type="text/javascript" src="jquery-1.8.0.min.js"></script>     <script type="text/javascript"> $(document).ready(function () {     /** Set Canvas Size **/     var canvasWidth = 400;     var canvasHeight = 100;     /** IE SUPPORT **/     var canvasDiv = document.getElementById('signaturePad');     canvas = document.createElement('canvas');     canvas.setAttribute('width', canvasWidth);     canvas.setAttribute('height', canvasHeight);     canvas.setAttribute('id', 'canvas');     canvasDiv.appendChild(canvas);    ...

Get Google Calendar Event List in WordPress

<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, ...

WooCommerce Mini cart With Ajax

WooCommerce Mini cart //MINI CART SECTION   <div class="productdiv rightcart">                                                         <?php if ( ! WC()->cart->is_empty() ) : ?>     <ul class="woocommerce-mini-cart cart_list product_list_widget <?php echo esc_attr( $args['list_class'] ); ?>">         <?php             do_action( 'woocommerce_before_mini_cart_contents' );             foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {                 $_product     = apply_filters( 'woocommerce_cart_item_pro...