Changeset 1684637
- Timestamp:
- 06/24/2017 08:05:07 AM (9 years ago)
- Location:
- gcal-events-list/trunk
- Files:
-
- 2 edited
-
gcal_events_list.php (modified) (4 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
gcal-events-list/trunk/gcal_events_list.php
r310915 r1684637 6 6 Version: 0.1 7 7 Author: Carlo Daniele 8 Author URI: http:// digitaladoptive.wordpress.com/8 Author URI: http://carlodaniele.it/ 9 9 */ 10 10 11 /* Copyright 201 0 Carlo Daniele (email : carloxdaniele@yahoo.it)11 /* Copyright 2017 Carlo Daniele (email : carlo.daniele@gmail.com) 12 12 13 13 This program is free software; you can redistribute it and/or modify … … 26 26 27 27 class gcalEventsList extends WP_Widget { 28 function gcalEventsList(){ 29 add_action('wp_print_styles', 'add_styles'); 30 $widget_ops = array('description' => 'A widget that generates a list of events from a public Google Calendar'); 31 $this->WP_Widget('gcal-events-list', 'GCal Events List', $widget_ops); 32 } 33 function widget($args, $instance){ 34 extract($args, EXTR_SKIP); 35 //open div 36 echo $before_widget; 28 29 /** 30 * Set up the widget name and options 31 */ 32 public function __construct(){ 33 34 $widget_ops = array( 'description' => 'A widget that generates a list of events from a public Google Calendar' ); 35 36 parent::__construct( 'gcal-events-list', 'GCal Events List', $widget_ops ); 37 38 } 39 40 /** 41 * Print the content of the widget on front-end 42 * 43 * @param array $args Widget arguments 44 * @param array $instance 45 * 46 */ 47 public function widget( $args, $instance ){ 48 49 echo $args['before_widget']; 37 50 38 51 //widget title 39 $title = apply_filters('widget_title', $instance['title']); 40 52 if ( ! empty( $instance['title'] ) ) { 53 echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; 54 } 55 41 56 //google calendar parameters 42 57 $params = array( 43 'id' => $instance['calendar'], //calendar ID 44 'orderby' => $instance['orderby'], 45 'sortorder' => $instance['sortorder'], 46 'max-results' => $instance['maxresults'], //ricordati che il parametro del gcal è max-results 47 'start-min' => $instance['startmin'], //ricordati che il parametro del gcal è start-min 48 'start-max' => $instance['startmax'] //ricordati che il parametro del gcal è start-max 58 'calendar' => ! empty( $instance['calendar'] ) ? $instance['calendar'] : '', //calendar ID 59 'key' => ! empty( $instance['key'] ) ? $instance['key'] : '', 60 'timeMin' => ( ! empty( $instance['timeMin'] ) && isDate( $instance['timeMin'] ) ) ? $instance['timeMin'] : date( 'Y-m-d' ), 61 'maxResults' => ! empty( $instance['maxResults'] ) ? $instance['maxResults'] : '5', 62 //'sortorder' => $instance['sortorder'], 63 //'max-results' => $instance['maxresults'], //ricordati che il parametro del gcal è max-results 64 //'start-min' => $instance['startmin'], //ricordati che il parametro del gcal è start-min 65 //'start-max' => $instance['startmax'] //ricordati che il parametro del gcal è start-max 49 66 ); 50 67 51 if(!empty($title)){ 52 echo $before_title . $title . $after_title; 53 } 54 55 if(!empty($params['id'])){ 56 getData($params); 68 if( ! empty( $params['calendar'] ) ){ 69 getData( $params ); 57 70 }else{ 58 echo __( 'You shoud set the calendar ID to make this widget work');71 echo __( 'You shoud set the calendar ID to make this widget work' ); 59 72 } 60 73 //close div 61 echo $after_widget; 62 } 63 function update($new_instance, $old_instance){ 64 $instance = $old_instance; 65 $instance['title'] = strip_tags($new_instance['title']); 66 $instance['calendar'] = strip_tags($new_instance['calendar']); 67 $instance['orderby'] = $new_instance['orderby']; 68 $instance['sortorder'] = $new_instance['sortorder']; 69 $instance['maxresults'] = (int)$new_instance['maxresults']; 70 $instance['startmin'] = $new_instance['startmin']; 71 $instance['startmax'] = $new_instance['startmax']; 74 echo $args['after_widget']; 75 } 76 77 /** 78 * Processing widget options on save 79 * 80 * @param array $new_instance The new options 81 * @param array $old_instance The previous options 82 * 83 * @return array $instance 84 */ 85 public function update( $new_instance, $old_instance ){ 86 87 $instance = array(); 88 89 $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; 90 $instance['calendar'] = ( ! empty( $new_instance['calendar'] ) ) ? strip_tags( $new_instance['calendar'] ) : ''; 91 $instance['key'] = ( ! empty( $new_instance['key'] ) ) ? strip_tags( $new_instance['key'] ) : ''; 92 $instance['timeMin'] = ( ! empty( $new_instance['timeMin'] ) && isDate( $new_instance['timeMin'] ) ) ? strip_tags( $new_instance['timeMin'] ) : date( 'Y-m-d' ); 93 $instance['maxResults'] = ( ! empty( $new_instance['maxResults'] ) ) ? strip_tags( $new_instance['maxResults'] ) : '5'; 94 72 95 return $instance; 73 96 } 74 function form($instance){ 75 //http://codex.wordpress.org/Function_Reference/esc_attr 76 $title = esc_attr($instance['title']); 77 $calendar = esc_attr($instance['calendar']); 78 $orderby = esc_attr($instance['orderby']); 79 $sortorder = esc_attr($instance['sortorder']); 80 $maxresults = esc_attr($instance['maxresults']); 81 $startmin = esc_attr($instance['startmin']); 82 $startmax = esc_attr($instance['startmax']); 97 98 /** 99 * Outputs the options form on admin 100 * 101 * @param array $instance The widget options 102 * 103 */ 104 public function form( $instance ){ 105 106 $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Title' ); 107 $calendar = ! empty( $instance['calendar'] ) ? $instance['calendar'] : ''; 108 $key = ! empty( $instance['key'] ) ? $instance['key'] : ''; 109 $timeMin = ( ! empty( $instance['timeMin'] ) && isDate( $instance['timeMin'] ) ) ? $instance['timeMin'] : date( 'Y-m-d' ); 110 $maxResults = ! empty( $instance['maxResults'] ) ? $instance['maxResults'] : '5'; 83 111 84 112 ?> 85 113 <p> 86 <label for="<?php echo $this->get_field_id( 'title'); ?>">Title:87 <input class="widefat" 88 id="<?php echo $this->get_field_id( 'title'); ?>"89 name="<?php echo $this->get_field_name( 'title'); ?>"114 <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title: 115 <input class="widefat" 116 id="<?php echo $this->get_field_id( 'title' ); ?>" 117 name="<?php echo $this->get_field_name( 'title' ); ?>" 90 118 type="text" 91 119 value="<?php echo $title; ?>" /> … … 93 121 </p> 94 122 <p> 95 <label for="<?php echo $this->get_field_id( 'calendar'); ?>">Calendar ID:96 <input class="widefat" 97 id="<?php echo $this->get_field_id( 'calendar'); ?>"98 name="<?php echo $this->get_field_name( 'calendar'); ?>"123 <label for="<?php echo $this->get_field_id( 'calendar' ); ?>">Calendar ID: 124 <input class="widefat" 125 id="<?php echo $this->get_field_id( 'calendar' ); ?>" 126 name="<?php echo $this->get_field_name( 'calendar' ); ?>" 99 127 type="text" 100 128 value="<?php echo $calendar; ?>" /> … … 102 130 </p> 103 131 <p> 104 <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php echo __('Order by'); ?>: </label> 105 <select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>" class="widefat"> 106 <option <?php if ( $instance['orderby'] == 'lastmodified' ) echo 'selected="selected"'; ?>>lastmodified</option> 107 <option <?php if ( $instance['orderby'] != 'lastmodified' ) echo 'selected="selected"'; ?>>starttime</option> 132 <label for="<?php echo $this->get_field_id( 'key' ); ?>">Your API key: 133 <input class="widefat" 134 id="<?php echo $this->get_field_id( 'key' ); ?>" 135 name="<?php echo $this->get_field_name( 'key' ); ?>" 136 type="text" 137 value="<?php echo $key; ?>" /> 138 </label> 139 </p> 140 <p> 141 <label for="<?php echo $this->get_field_id('timeMin'); ?>">Start from (YYYY-MM-DD): 142 <input class="widefat" 143 id="<?php echo $this->get_field_id('timeMin'); ?>" 144 name="<?php echo $this->get_field_name('timeMin'); ?>" 145 type="text" 146 value="<?php echo $timeMin; ?>" /> 147 </label> 148 </p> 149 <p> 150 <label for="<?php echo $this->get_field_id('maxResults'); ?>"><?php echo __('Max results'); ?>: </label> 151 <select id="<?php echo $this->get_field_id( 'maxResults' ); ?>" name="<?php echo $this->get_field_name( 'maxResults' ); ?>" class="widefat"> 152 <option <?php if ( $instance['maxResults'] == '1' ) echo 'selected="selected"'; ?>>1</option> 153 <option <?php if ( $instance['maxResults'] == '3' ) echo 'selected="selected"'; ?>>3</option> 154 <option <?php if ( $instance['maxResults'] == '5' ) echo 'selected="selected"'; ?>>5</option> 155 <option <?php if ( $instance['maxResults'] == '10' ) echo 'selected="selected"'; ?>>10</option> 156 <option <?php if ( $instance['maxResults'] == '15' ) echo 'selected="selected"'; ?>>15</option> 157 <option <?php if ( $instance['maxResults'] == '20' ) echo 'selected="selected"'; ?>>20</option> 108 158 </select> 109 159 </p> 110 <p>111 <label for="<?php echo $this->get_field_id('sortorder'); ?>"><?php echo __('Sort order'); ?>: </label>112 <select id="<?php echo $this->get_field_id( 'sortorder' ); ?>" name="<?php echo $this->get_field_name( 'sortorder' ); ?>" class="widefat">113 <option <?php if ( $instance['sortorder'] == 'ascending' ) echo 'selected="selected"'; ?>>ascending</option>114 <option <?php if ( $instance['sortorder'] != 'ascending' ) echo 'selected="selected"'; ?>>descending</option>115 </select>116 </p>117 <p>118 <label for="<?php echo $this->get_field_id('maxresults'); ?>"><?php echo __('Max results'); ?>: </label>119 <select id="<?php echo $this->get_field_id( 'maxresults' ); ?>" name="<?php echo $this->get_field_name( 'maxresults' ); ?>" class="widefat">120 <option <?php if ( $instance['maxresults'] == 1 ) echo 'selected="selected"'; ?>>1</option>121 <option <?php if ( $instance['maxresults'] == 3 ) echo 'selected="selected"'; ?>>3</option>122 <option <?php if ( $instance['maxresults'] == 5 ) echo 'selected="selected"'; ?>>5</option>123 <option <?php if ( $instance['maxresults'] == 10 ) echo 'selected="selected"'; ?>>10</option>124 <option <?php if ( $instance['maxresults'] == 15 ) echo 'selected="selected"'; ?>>15</option>125 <option <?php if ( $instance['maxresults'] == 20 ) echo 'selected="selected"'; ?>>20</option>126 </select>127 </p>128 <p>129 <label for="<?php echo $this->get_field_id('startmin'); ?>">Start min (YYYY-MM-DD):130 <input class="widefat"131 id="<?php echo $this->get_field_id('startmin'); ?>"132 name="<?php echo $this->get_field_name('startmin'); ?>"133 type="text"134 value="<?php echo $startmin; ?>" />135 </label>136 </p>137 <p>138 <label for="<?php echo $this->get_field_id('startmax'); ?>">Start max (YYYY-MM-DD):139 <input class="widefat"140 id="<?php echo $this->get_field_id('startmax'); ?>"141 name="<?php echo $this->get_field_name('startmax'); ?>"142 type="text"143 value="<?php echo $startmax; ?>" />144 </label>145 </p>146 160 <?php 147 161 } 148 162 } 149 add_action('widgets_init', create_function('', 'return register_widget("gcalEventsList");')); 150 151 152 function getData($params){ 153 extract($params, EXTR_SKIP); 154 155 $calID = $params['id']; 156 $feed = "http://www.google.com/calendar/feeds/" . $calID . "/public/full?"; 157 $params = "orderby=". $params['orderby'] 158 . "&sortorder=" . $params['sortorder'] 159 . "&max-results=" . $params['max-results'] 160 . "&start-min=" . $params['start-min'] 161 . "&start-max=" . $params['start-max']; 162 163 $contents = @file_get_contents($feed . $params) or die(__('Bad request')); 164 $xml = new SimpleXmlElement($contents); 165 166 echo '<div id="eventslist">'; 167 foreach($xml->entry as $entry){ 168 $gd = $entry->children('http://schemas.google.com/g/2005'); 169 $start = strtotime($gd->when->attributes()->startTime); 170 $end = strtotime($gd->when->attributes()->endTime); 171 $dayName = __(date('l', $start)); 172 $dayNum = date('j', $start); 173 $month = __(date('F', $start)); 174 $year = date('Y', $start); 175 $date = $dayName . ', ' . $dayNum . ' ' . $month . ' ' . $year; 176 $startTime = date('G:i', $start); 177 $endTime = date('G:i', $end); 178 179 echo '<p>'; 180 echo '<span class="gcelist_date">' . $date . '</span><br />'; 181 echo '<span class="gcelist_title">' . (string)$entry->title . '</span><br />'; 182 if($startTime != $endTime){ 183 echo '<span class="gcelist_hour">' . $startTime . " - " . $endTime . '</span>'; 184 } 185 echo '</p>'; 186 } 187 echo '</div>'; 163 add_action( 'widgets_init', function(){ 164 register_widget( 'gcalEventsList' ); 165 }); 166 167 168 /** 169 * Retrieve data from a public calendar 170 * 171 * @param array $params An array of arguments 172 * 173 */ 174 function getData( $params ){ 175 176 $calendarID = urlencode( $params['calendar'] ); 177 178 $key = $params['key']; 179 $maxResults = ! empty( $params['maxResults'] ) ? ( "&maxResults=" . $params['maxResults'] ) : '5'; 180 181 if( ! empty( $params['timeMin'] ) && isDate( $params['timeMin'] ) ){ 182 183 $RFC3339 = $params['timeMin'] . 'T00:00:00Z'; 184 185 $timeMin = '&timeMin=' . $RFC3339; 186 187 }else{ 188 189 $timeMin = '&timeMin=' . date( 'Y-m-d' ) . 'T00:00:00Z'; 190 191 } 192 193 $resource = "https://www.googleapis.com/calendar/v3/calendars/" . $calendarID . "/events?key=" . $key . 194 $maxResults . "&orderBy=startTime" . "&singleEvents=true" . $timeMin; 195 196 $request = wp_remote_get( $resource ); 197 198 if( is_wp_error( $request ) ) { 199 return false; 200 } 201 202 $body = wp_remote_retrieve_body( $request ); 203 $data = json_decode( $body ); 204 205 if( ! empty( $data->items ) ) { 206 ?> 207 <ul> 208 <?php foreach( $data->items as $item ) { ?> 209 <li><?php echo $item->summary; ?><br /> 210 <?php echo $item->start->date; ?> 211 </li> 212 <?php } ?> 213 </ul> 214 <?php 215 }else{ 216 echo __( 'No items found!' ); 217 } 188 218 } 189 219 190 function add_styles() { 191 $myStyleUrl = WP_PLUGIN_URL . '/gcal_events_list/css/gcel-style.css'; 192 $myStyleFile = WP_PLUGIN_DIR . '/gcal_events_list/css/gcel-style.css'; 193 if ( file_exists($myStyleFile) ) { 194 wp_register_style('gcel_styles', $myStyleUrl); 195 wp_enqueue_style( 'gcel_styles'); 196 } 197 } 220 /** 221 * Verify if is a valide date in Y-m-d format 222 * 223 * @param string $date 224 * 225 * @return bool 226 */ 227 function isDate( $date ){ 228 229 $dateTime = new DateTime(); 230 231 $d = $dateTime->createFromFormat( 'Y-m-d', $date ); 232 233 return $d && $d->format( 'Y-m-d' ) === $date; 234 } -
gcal-events-list/trunk/readme.txt
r1684633 r1684637 4 4 Requires at least: 4.8 5 5 Tested up to: 4.8 6 Stable tag: 1. 06 Stable tag: 1.1 7 7 8 8 GCal Events List retrieves future events from a public Google Calendar and shows data in a widget.
Note: See TracChangeset
for help on using the changeset viewer.