ko_calendar_load

Plugin Name: Google Calendar Widget Plugin URI: http://notions.okuda.ca/wordpress-plugins/google-calendar-widget/ Description: This plugin adds a sidebar widget containing an agenda from a Google Calendar.

Description

ko_calendar_load(); 

It is based on the Google Calendar samples and inspired by wpng-calendar. It is smaller and simpler than wpng-calendar and allows for multiple widgets to each show their own agenda. Version: 1.4.6 Author: Kaz Okuda Author URI: http://notions.okuda.ca


Usage

  1. if ( !function_exists( 'ko_calendar_load' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-calendar-widget/ko-calendar.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = ko_calendar_load(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/ko-calendar.php  
  1. function ko_calendar_load() 
  2. class WP_Widget_KO_Calendar extends WP_Widget 
  3. function WP_Widget_KO_Calendar() 
  4. $widget_ops = array('classname' => 'ko_calendar', 'description' => __('Google Calendar Widget', 'ko-calendar')); 
  5. $control_ops = array('width' => 400, 'height' => 200); 
  6. $this->WP_Widget('ko_calendar', __('Google Calendar', 'ko-calendar'), $widget_ops, $control_ops); 
  7.  
  8. function widget($args, $instance) 
  9. extract($args); 
  10. $title = empty($instance['title']) ? 'Calendar' : $instance['title']; 
  11. $url = empty($instance['url']) ? 'developer-calendar@google.com' : $instance['url']; 
  12. $url2 = empty($instance['url2']) ? '' : $instance['url2']; 
  13. $url3 = empty($instance['url3']) ? '' : $instance['url3']; 
  14. $maxresults = empty($instance['maxresults']) ? '5' : $instance['maxresults']; 
  15. $autoexpand = empty($instance['autoexpand']) ? FALSE : $instance['autoexpand']; 
  16. $titleformat = empty($instance['titleformat']) ? '[STARTTIME - ][TITLE]' : $instance['titleformat']; 
  17.  
  18. $title_id = $this->get_field_id('widget_title'); 
  19. $event_id = $this->get_field_id('widget_events'); 
  20.  
  21. echo $before_widget; 
  22. echo $before_title . '<div class="ko-calendar-widget-title" id="' . $title_id . '">' . $title . '</div>' . $after_title; 
  23. echo '<div class="ko-calendar-widget-events" id="' . $event_id . '">'; 
  24. echo '<div class="ko-calendar-widget-loading"><img class="ko-calendar-widget-image" src="' . plugins_url('/loading.gif', __FILE__) . '" alt="Loading..."/></div>'; 
  25. echo '</div>'; 
  26. echo $after_widget; 
  27.  
  28. $settings = (array)get_option('ko_calendar_settings'); 
  29. $apikey = esc_attr($settings['apikey']); 
  30.  
  31. ?> 
  32. <script type="text/javascript" defer="defer"> 
  33. ko_calendar.loadCalendarDefered('<?php echo $apikey ?>', '<?php echo $title_id ?>', '<?php echo $event_id ?>', <?php echo $maxresults ?>, <?php echo empty($autoexpand) ? 'false' : 'true' ?>, '<?php echo $url ?>', '<?php echo $url2 ?>', '<?php echo $url3 ?>', '<?php echo $titleformat ?>'); 
  34. </script> 
  35. <?php 
  36.  
  37. function update($new_instance, $old_instance) 
  38. if (!isset($new_instance['submit'])) { 
  39. return false; 
  40. $instance = $old_instance; 
  41. $instance['title'] = trim(strip_tags($new_instance['title'])); 
  42. $instance['url'] = trim(strip_tags($new_instance['url'])); 
  43. $instance['url2'] = trim(strip_tags($new_instance['url2'])); 
  44. $instance['url3'] = trim(strip_tags($new_instance['url3'])); 
  45. $instance['maxresults'] = intval($new_instance['maxresults']); 
  46. $instance['autoexpand'] = empty($new_instance['autoexpand']) ? FALSE : $new_instance['autoexpand']; 
  47. $instance['titleformat'] = strip_tags($new_instance['titleformat'], '<br><p>'); 
  48. return $instance; 
  49.  
  50. function form($instance) 
  51. $defaults = array( 'title' => '', 'url' => '', 'url2' => '', 'url3' => '', 'maxresults' => 5, 'autoexpand' => FALSE, 'titleformat' => '[STARTTIME - ][TITLE]'); 
  52. $instance = wp_parse_args( (array) $instance, $defaults ); 
  53. $title = esc_attr($instance['title']); 
  54. $url = esc_attr($instance['url']); 
  55. $url2 = esc_attr($instance['url2']); 
  56. $url3 = esc_attr($instance['url3']); 
  57. $maxresults = intval($instance['maxresults']); 
  58. $autoexpand = empty($instance['autoexpand']) ? FALSE : $instance['autoexpand']; 
  59. $titleformat = esc_attr($instance['titleformat']); 
  60.  
  61. $settings = (array)get_option('ko_calendar_settings'); 
  62. $apiKey = esc_attr($settings['apikey']); 
  63. if ($apiKey == null || $apiKey == "") 
  64. // Missing the API key, remind the user. 
  65. $apiKeyMissing = true; 
  66.  
  67. ?> 
  68. <div> 
  69. <table width="100%"><tr><td> 
  70. <label for="<?php echo $this->get_field_id('title'); ?>" style="line-height:35px;display:block;"> 
  71. <?php _e('Calendar Title');?>: 
  72. </label></td><td width="100%" style="width:100%"> 
  73. <input type="text" style="width:100%" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" /> 
  74. </td></tr></table><table width="100%"><tr><td> 
  75. <label for="<?php echo $this->get_field_id('maxresults'); ?>" style="line-height:35px;display:block;"> 
  76. <?php _e('Maximum Results');?>: 
  77. </label></td><td width="100%" style="width:100%"> 
  78. <input type="text" id="<?php echo $this->get_field_id('maxresults'); ?>" name="<?php echo $this->get_field_name('maxresults'); ?>" value="<?php echo $maxresults; ?>" /> 
  79. </td></tr></table><table width="100%"><tr><td> 
  80. <label for="<?php echo $this->get_field_id('autoexpand'); ?>" style="line-height:35px;display:block;"> 
  81. <?php _e('Expand Entries by Default');?>: 
  82. </label></td><td width="100%" style="width:100%"> 
  83. <input type="checkbox" id="<?php echo $this->get_field_id('autoexpand'); ?>" name="<?php echo $this->get_field_name('autoexpand'); ?>" <?php echo empty($autoexpand) ? '' : 'checked'; ?> value="true" /> 
  84. </td></tr></table><table width="100%"><tr><td> 
  85. <label for="<?php echo $this->get_field_id('url'); ?>" style="line-height:35px;display:block;"> 
  86. <?php _e('Calendar ID 1');?>: 
  87. </label></td><td width="100%" style="width:100%"> 
  88. <input type="text" style="width:100%" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" value="<?php echo $url; ?>" /> 
  89. </td></tr></table><table width="100%"><tr><td> 
  90. <label for="<?php echo $this->get_field_id('url2'); ?>" style="line-height:35px;display:block;"> 
  91. <?php _e('Calendar ID 2 (Optional)');?>: 
  92. </label></td><td width="100%" style="width:100%"> 
  93. <input type="text" style="width:100%" id="<?php echo $this->get_field_id('url2'); ?>" name="<?php echo $this->get_field_name('url2'); ?>" value="<?php echo $url2; ?>" /> 
  94. </td></tr></table><table width="100%"><tr><td> 
  95. <label for="<?php echo $this->get_field_id('url3'); ?>" style="line-height:35px;display:block;"> 
  96. <?php _e('Calendar ID 3 (Optional)');?>: 
  97. </label></td><td width="100%" style="width:100%"> 
  98. <input type="text" style="width:100%" id="<?php echo $this->get_field_id('url3'); ?>" name="<?php echo $this->get_field_name('url3'); ?>" value="<?php echo $url3; ?>" /> 
  99. </td></tr></table><table width="100%"><tr><td> 
  100. <label for="<?php echo $this->get_field_id('titleformat'); ?>" style="line-height:35px;display:block;"> 
  101. <?php _e('Event Title Format');?>: 
  102. </label></td><td width="100%" style="width:100%"> 
  103. <input type="text" style="width:100%" id="<?php echo $this->get_field_id('titleformat'); ?>" name="<?php echo $this->get_field_name('titleformat'); ?>" value="<?php echo $titleformat; ?>" /> 
  104. </td></tr></table> 
  105. <?php if ($apiKeyMissing) { ?> 
  106. <p style="color:red"><?php _e('WARNING: You must set a Google API Key before the widget will work.');?> 
  107. <a href="options-general.php?page=ko_calendar_admin.php"><?php _e('Add your API Key here.');?></a></p> 
  108. <?php } ?> 
  109. <input type="hidden" name="<?php echo $this->get_field_name('submit'); ?>" id="<?php echo $this->get_field_id('submit'); ?>" value="1" /> 
  110. </div> 
  111. <?php 
  112.  
  113. function ko_calendar_plugins_loaded() 
  114. load_plugin_textdomain('ko-calendar', false, basename( dirname( __FILE__ ) ) . '/languages' ); 
  115.  
  116. function ko_calendar_head() 
  117. echo '<link type="text/css" rel="stylesheet" href="' . plugins_url('ko-calendar.css', __FILE__) . '" />'; 
  118.  
  119. function ko_calendar_init() 
  120. {  
  121. if ( !is_admin() ) 
  122. // Register our script first, then localize, then enqueue. 
  123. wp_register_script('ko-calendar', plugins_url('/ko-calendar.js', __FILE__), array('date-js')); 
  124.  
  125. $translation_array = array( 
  126. 'all_day' => __( 'All Day', 'ko-calendar'),  
  127. 'all_day_event' => __( 'All Day Event', 'ko-calendar') 
  128. ); 
  129. wp_localize_script( 'ko-calendar', 'ko_calendar_loc', $translation_array); 
  130.  
  131. // I believe that the google apikey is no longer needed 
  132. wp_enqueue_script('wiky-js', plugins_url('wiky.js', __FILE__), null, '1.0'); 
  133. wp_enqueue_script('date-js', plugins_url('/date.js', __FILE__), null, 'alpha-1'); 
  134. //wp_enqueue_script('ko-calendar-test', plugins_url('/ko-calendar-test.js', __FILE__), array('date-js', 'google')); 
  135. wp_enqueue_script('ko-calendar'); 
  136. wp_enqueue_script('googleclient', '//apis.google.com/js/client.js?onload=ko_calendar_google_init', array('ko-calendar')); 
  137.  
  138. function ko_calendar_register_widget() 
  139. register_widget('WP_Widget_KO_Calendar'); 
  140.  
  141. add_action('admin_menu', 'ko_calendar_admin_menu'); 
  142. function ko_calendar_admin_menu() 
  143. // See http://kovshenin.com/2012/the-wordpress-settings-api/ for a good tutorial on adding settings 
  144. add_options_page(__('Google Calendar Widget', 'ko-calendar'), __('Google Calendar Widget', 'ko-calendar'), 'manage_options', 'ko_calendar_admin', 'ko_calendar_admin_action'); 
  145.  
  146. function ko_calendar_admin_action() 
  147. ?> 
  148. <div class="wrap"> 
  149. <h2><?php _e("Google Calendar Widget")?></h2> 
  150. <form action="options.php" method="POST"> 
  151. <?php settings_fields( 'ko_calendar_settings_group' ); ?> 
  152. <?php do_settings_sections( 'ko_calendar_admin' ); ?> 
  153. <?php submit_button(); ?> 
  154. </form> 
  155. </div> 
  156. <?php 
  157.  
  158. function ko_calendar_setting_section_function() 
  159. ?> 
  160. You need a unique Google API key for users of your web site to access Google services. 
  161. <ol> 
  162. <li>Go to <a href='https://console.developers.google.com'>https://console.developers.google.com</a>.</li> 
  163. <li>Create or select a project for your web site</li> 
  164. <li>In the left sidebar, expand <b>APIs & auth</b> then select <b>APIs</b></li> 
  165. <li>Change the status of the <b>Calendar API</b> to <b>ON</b></li> 
  166. <li>In the left sidebar, select <b>Credentials</b></li> 
  167. <li>Click on <b>Create new Key</b> and choose <b>Browser key</b></li> 
  168. <li>For testing purposes you can leave the referrers empty, but to prevent your key from being used on unauthorized sites, only allow referrals from domains you administer.</li> 
  169. <li>Enter the key below</li> 
  170. </ol> 
  171. <?php 
  172.  
  173. function ko_calendar_setting_api_key_function() 
  174. $settings = (array)get_option('ko_calendar_settings'); 
  175. $apikey = esc_attr($settings['apikey']); 
  176. echo "<input name='ko_calendar_settings[apikey]' size='40' type='text' value='$apikey' />"; 
  177.  
  178. add_action('admin_init', 'ko_calendar_admin_init' ); 
  179. function ko_calendar_admin_init() 
  180. register_setting( 'ko_calendar_settings_group', 'ko_calendar_settings' ); 
  181.  
  182. add_settings_section( 
  183. 'ko_calendar_setting_section',  
  184. __('Settings', 'ko-calendar'),  
  185. 'ko_calendar_setting_section_function',  
  186. 'ko_calendar_admin' 
  187. ); 
  188.  
  189. add_settings_field( 
  190. 'ko_calendar_setting_api_key',  
  191. __('Google API Key', 'ko-calendar'),  
  192. 'ko_calendar_setting_api_key_function',  
  193. 'ko_calendar_admin',  
  194. 'ko_calendar_setting_section' 
  195. ); 
  196.  
  197. add_action('plugins_loaded', 'ko_calendar_plugins_loaded'); 
  198. add_action('wp_head', 'ko_calendar_head'); 
  199. add_action('init', 'ko_calendar_init'); 
  200. add_action('widgets_init', 'ko_calendar_register_widget');