SimpleCalendarFeedsAdminGoogle_Admin

Google Calendar feed admin.

Defined (1)

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

/includes/feeds/admin/google-admin.php  
  1. class Google_Admin { 
  2.  
  3. /** 
  4. * Google calendar feed object. 
  5. * @access private 
  6. * @var Google 
  7. */ 
  8. private $feed = null; 
  9.  
  10. /** 
  11. * Google Api Key. 
  12. * @access private 
  13. * @var string 
  14. */ 
  15. private $google_api_key = ''; 
  16.  
  17. /** 
  18. * Google Calendar id. 
  19. * @access private 
  20. * @var string 
  21. */ 
  22. private $google_calendar_id = ''; 
  23.  
  24. /** 
  25. * Hook in tabs. 
  26. * @since 3.0.0 
  27. * @param Google $feed 
  28. * @param string $google_api_key 
  29. * @param string $google_calendar_id 
  30. */ 
  31. public function __construct( Google $feed, $google_api_key, $google_calendar_id ) { 
  32.  
  33. $this->feed = $feed; 
  34. $this->google_api_key = $google_api_key; 
  35. $this->google_calendar_id = $google_calendar_id; 
  36.  
  37. $screen = simcal_is_admin_screen(); 
  38.  
  39. if ( 'calendar' == $screen ) { 
  40. $this->test_api_key_connection( $this->google_calendar_id ); 
  41. add_filter( 'simcal_settings_meta_tabs_li', array( $this, 'add_settings_meta_tab_li' ), 10, 1 ); 
  42. add_action( 'simcal_settings_meta_panels', array( $this, 'add_settings_meta_panel' ), 10, 1 ); 
  43.  
  44. add_action( 'simcal_process_settings_meta', array( $this, 'process_meta' ), 10, 1 ); 
  45.  
  46. /** 
  47. * Feed settings page fields. 
  48. * @since 3.0.0 
  49. * @return array 
  50. */ 
  51. public function settings_fields() { 
  52. return array( 
  53. 'name' => $this->feed->name,  
  54. 'description' => __( "To read events from your public Google Calendars you'll need create a Google API key and save it here.", 'google-calendar-events' ) . 
  55. '<br/><br/>' . 
  56. '<em style="font-size: 14px;">' . 
  57. sprintf( __( '<strong>Note:</strong> Calendars configured to use the <strong><a href="%s" target="_blank">Google Calendar Pro add-on</a></strong> use a different method of authorization.', 'google-calendar-events' ),  
  58. simcal_ga_campaign_url( simcal_get_url( 'addons' ), 'core-plugin', 'settings-link' ) 
  59. ) . 
  60. '</em>',  
  61. 'fields' => array( 
  62. 'api_key' => array( 
  63. 'type' => 'standard',  
  64. 'subtype' => 'text',  
  65. 'class' => array( 'simcal-wide-text regular-text', 'ltr' ),  
  66. 'title' => __( 'Google API Key', 'google-calendar-events' ),  
  67. 'validation' => array( $this, 'check_google_api_key' ),  
  68. ),  
  69. ),  
  70. ); 
  71.  
  72. /** 
  73. * Check if there's a Google API Key or a legacy key is being used. 
  74. * This method only checks if the api key setting is not empty. 
  75. * It is not currently possible to check or validate an API Key without performing a full request. 
  76. * On the settings page there are no known calendars to use for this so we can only check if there is a string. 
  77. * @since 3.0.0 
  78. * @param string $api_key Google API key. 
  79. * @return true|string 
  80. */ 
  81. public function check_google_api_key( $api_key = '' ) { 
  82.  
  83. $message = ''; 
  84. $has_errors = false; 
  85.  
  86. if ( empty( $api_key ) ) { 
  87. $api_key = $this->google_api_key; 
  88. if ( empty( $api_key ) ) { 
  89. $settings = get_option( 'simple-calendar_settings_feeds' ); 
  90. $api_key = isset( $settings['google']['api_key'] ) ? esc_attr( $settings['google']['api_key'] ) : ''; 
  91.  
  92. $message = '<p class="description">' . 
  93. sprintf( __( '<a href="%s" target="_blank">Step-by-step instructions</a> ', 'google-calendar-events' ),  
  94. simcal_ga_campaign_url( simcal_get_url( 'docs' ) . '/google-api-key/', 'core-plugin', 'settings-link' ) 
  95. ) . 
  96. '<br/>' . 
  97. sprintf( __( '<a href="%s" target="_blank">Google Developers Console</a> ', 'google-calendar-events' ),  
  98. simcal_get_url( 'gdev-console' ) 
  99. ) . 
  100. '</p>'; 
  101.  
  102. return $message; 
  103.  
  104. /** 
  105. * Add a tab to the settings meta box. 
  106. * @since 3.0.0 
  107. * @param array $tabs 
  108. * @return array 
  109. */ 
  110. public function add_settings_meta_tab_li( $tabs ) { 
  111. return array_merge( $tabs, array( 
  112. 'google' => array( 
  113. 'label' => $this->feed->name,  
  114. 'target' => 'google-settings-panel',  
  115. 'class' => array( 'simcal-feed-type', 'simcal-feed-type-google' ),  
  116. 'icon' => 'simcal-icon-google',  
  117. ),  
  118. ) ); 
  119.  
  120. /** 
  121. * Add a panel to the settings meta box. 
  122. * @since 3.0.0 
  123. * @param int $post_id 
  124. */ 
  125. public function add_settings_meta_panel( $post_id ) { 
  126.  
  127. $inputs = array( 
  128. $this->feed->type => array( 
  129. '_google_calendar_id' => array( 
  130. 'type' => 'standard',  
  131. 'subtype' => 'text',  
  132. 'name' => '_google_calendar_id',  
  133. 'id' => '_google_calendar_id',  
  134. 'title' => __( 'Calendar ID', 'google-calendar-events' ),  
  135. 'tooltip' => __( 'Visit your Google Calendar account, copy your public calendar ID, then paste it here.', 'google-calendar-events' ),  
  136. 'placeholder' => __( 'Enter a valid Google Calendar ID from a public calendar', 'google-calendar-events' ),  
  137. 'escaping' => array( $this->feed, 'esc_google_calendar_id' ),  
  138. 'validation' => array( $this, 'test_api_key_connection' ),  
  139. ),  
  140. '_google_events_search_query' => array( 
  141. 'type' => 'standard',  
  142. 'subtype' => 'text',  
  143. 'name' => '_google_events_search_query',  
  144. 'id' => '_google_events_search_query',  
  145. 'title' => __( 'Search Query', 'google-calendar-events' ),  
  146. 'tooltip' => __( 'Type in keywords if you only want display events that match these terms. You can use basic boolean search operators too.', 'google-calendar-events' ),  
  147. 'placeholder' => __( 'Filter events to display by search terms...', 'google-calendar-events' ),  
  148. ),  
  149. '_google_events_recurring' => array( 
  150. 'type' => 'select',  
  151. 'name' => '_google_events_recurring',  
  152. 'id' => '_google_events_recurring',  
  153. 'title' => __( 'Recurring Events', 'google-calendar-events' ),  
  154. 'tooltip' => __( 'Events that are programmed to repeat themselves periodically.', 'google-calendar-events' ),  
  155. 'options' => array( 
  156. 'show' => __( 'Show all', 'google-calendar-events' ),  
  157. 'first-only' => __( 'Only show first occurrence', 'google-calendar-events' ),  
  158. ),  
  159. ),  
  160. '_google_events_max_results' => array( 
  161. 'type' => 'standard',  
  162. 'subtype' => 'number',  
  163. 'name' => '_google_events_max_results',  
  164. 'id' => '_google_events_max_results',  
  165. 'title' => __( 'Maximum Events', 'google-calendar-events' ),  
  166. 'tooltip' => __( 'Google Calendar only allows to query for a maximum amount of 2500 events from a calendar each time.', 'google-calendar-events' ),  
  167. 'class' => array( 
  168. 'simcal-field-small',  
  169. ),  
  170. 'default' => '2500',  
  171. 'attributes' => array( 
  172. 'min' => '0',  
  173. 'max' => '2500',  
  174. ),  
  175. ),  
  176. ),  
  177. ); 
  178.  
  179. ?> 
  180. <div id="google-settings-panel" class="simcal-panel"> 
  181. <table> 
  182. <thead> 
  183. <tr><th colspan="2"><?php _e( 'Google Calendar Settings', 'google-calendar-events' ); ?></th></tr> 
  184. </thead> 
  185. <?php Settings::print_panel_fields( $inputs, $post_id ); ?> 
  186. </table> 
  187. </div> 
  188. <?php 
  189.  
  190.  
  191. /** 
  192. * Test a connection to Google Calendar API. 
  193. * @since 3.0.0 
  194. * @param string $google_calendar_id 
  195. * @return true|string 
  196. */ 
  197. public function test_api_key_connection( $google_calendar_id ) { 
  198.  
  199. global $post; 
  200.  
  201. $post_id = isset( $post->ID ) ? $post->ID : 0; 
  202. $feed = null; 
  203. if ( $feed_type = wp_get_object_terms( $post_id, 'calendar_feed' ) ) { 
  204. $feed = sanitize_title( current( $feed_type )->name ); 
  205.  
  206. $message = ''; 
  207. $error = ''; 
  208. $has_errors = false; 
  209.  
  210. $message .= '<p class="description">' . 
  211. sprintf( 
  212. __( 'Step 1: Set the Google Calendar you want to use as <strong>"public."</strong> <a href="%1s" target="_blank">Detailed instructions</a>', 'google-calendar-events' ) . '<br />' . 
  213. __( 'Step 2: Copy and paste your Google Calendar ID here. <a href="%2s" target="_blank">Detailed instructions</a>', 'google-calendar-events' ),  
  214. simcal_ga_campaign_url( simcal_get_url( 'docs' ) . '/make-google-calendar-public/', 'core-plugin', 'settings-link' ),  
  215. simcal_ga_campaign_url( simcal_get_url( 'docs' ) . '/find-google-calendar-id/', 'core-plugin', 'settings-link' ) 
  216. ) . '</p>'; 
  217.  
  218. if ( $post_id > 0 && ! is_null( $feed ) && ! empty( $this->feed->type ) ) { 
  219.  
  220. $no_key_notice = new Notice( array( 
  221. 'id' => array( 'calendar_' . $post_id => 'google-no-api-key' ),  
  222. 'type' => 'error',  
  223. 'screen' => 'calendar',  
  224. 'post' => $post_id,  
  225. 'dismissable' => false,  
  226. 'content' => '<p>' . 
  227. '<i class="simcal-icon-warning"></i> ' . 
  228. sprintf( 
  229. __( 'Your Google Calendar events will not show up until you <a href="%s">create and save a Google API key</a>.', 'google-calendar-events' ),  
  230. admin_url( 'edit.php?post_type=calendar&page=simple-calendar_settings&tab=feeds' ) 
  231. ) . 
  232. '</p>',  
  233. ); 
  234.  
  235. if ( empty( $this->google_api_key ) && ( $feed == $this->feed->type ) ) { 
  236.  
  237. $has_errors = true; 
  238. $no_key_notice->add(); 
  239.  
  240. } else { 
  241.  
  242. $no_key_notice->remove(); 
  243.  
  244. try { 
  245. $this->feed->make_request( $google_calendar_id ); 
  246. } catch ( \Exception $e ) { 
  247. $error = $e->getMessage(); 
  248. $message = ! empty( $error ) ? '<blockquote>' . $error . '</blockquote>' : ''; 
  249.  
  250. $error_notice = new Notice( array( 
  251. 'id' => array( 'calendar_' . $post_id => 'google-error-response' ),  
  252. 'type' => 'error',  
  253. 'screen' => 'calendar',  
  254. 'post' => $post_id,  
  255. 'dismissable' => false,  
  256. 'content' => '<p>' . 
  257. '<i class="simcal-icon-warning"></i> ' . 
  258. __( 'While trying to retrieve events, Google returned an error:', 'google-calendar-events' ) . 
  259. '<br>' . $message . '<br>' . 
  260. __( 'Please ensure that both your Google Calendar ID and API Key are valid and that the Google Calendar you want to display is public.', 'google-calendar-events' ) . 
  261. '</p>',  
  262. ); 
  263.  
  264. if ( ! empty( $error ) && ( $feed == $this->feed->type ) ) { 
  265. $error_notice->add(); 
  266. $has_errors = true; 
  267. } else { 
  268. $error_notice->remove(); 
  269. $has_errors = false; 
  270.  
  271.  
  272.  
  273. return $message; 
  274.  
  275. /** 
  276. * Process meta fields. 
  277. * @since 3.0.0 
  278. * @param int $post_id 
  279. */ 
  280. public function process_meta( $post_id ) { 
  281.  
  282. $calendar_id = isset( $_POST['_google_calendar_id'] ) ? base64_encode( trim( $_POST['_google_calendar_id'] ) ): ''; 
  283. update_post_meta( $post_id, '_google_calendar_id', $calendar_id ); 
  284.  
  285. $search_query = isset( $_POST['_google_events_search_query'] ) ? sanitize_text_field( $_POST['_google_events_search_query'] ) : ''; 
  286. update_post_meta( $post_id, '_google_events_search_query', $search_query ); 
  287.  
  288. $recurring = isset( $_POST['_google_events_recurring'] ) ? sanitize_key( $_POST['_google_events_recurring'] ) : 'show'; 
  289. update_post_meta( $post_id, '_google_events_recurring', $recurring ); 
  290.  
  291. $max_results = isset( $_POST['_google_events_max_results'] ) ? absint( $_POST['_google_events_max_results'] ) : '2500'; 
  292. update_post_meta( $post_id, '_google_events_max_results', $max_results ); 
  293.  
  294. $this->test_api_key_connection( $calendar_id ); 
  295.