'li', 'p'=>'p', 'dd'=>'dd', 'lip'=>'lip');
var $default_html_tag = 'li';
var $google_calendar = array(
'api-key' => '',
'id' => '',
'api-url' => 'https://www.googleapis.com/calendar/v3/calendars/',
'start-date' => '', // Default events are from today to the future.
'end-date' => '',
'orderby' => 'startTime', // startTime, updated (only ascending).
'orderbysort' => 'descending', // ascending or descending.
'maxResults' => '', // Get items '',
);
var $lang_dir = 'lang'; // Language folder name
var $settings;
function __construct(){
$this->settings = get_option($this->set_op);
$this->init_settings();
register_activation_hook(__FILE__, array(&$this, 'installer'));
register_deactivation_hook(__FILE__, array(&$this, 'uninstaller'));
// Add Setting to WordPress 'Settings' menu.
add_action('admin_menu', array(&$this, 'add_to_settings_menu'));
add_action('plugins_loaded', array(&$this,'enable_language_translation'));
add_shortcode($this->plugin_shortcode, array(&$this, 'shortcodes'));
}
public function enable_language_translation(){
load_plugin_textdomain($this->plugin_name)
or load_plugin_textdomain($this->plugin_name, false, dirname( plugin_basename( __FILE__ ) ) . '/' . $this->lang_dir . '/');
}
public function init_settings(){
$this->settings = $this->google_calendar; // Save to default settings.
$this->settings['version'] = 590;
$this->settings['db_version'] = 100;
}
public function installer(){
update_option($this->set_op , $this->settings);
}
public function uninstaller(){
// Remove Save data.
delete_option($this->set_op);
}
/* WordPress Timezone are 2 types (Strings (ex. "Asia/Tokyo") or Offset (ex. +9,-1, or etc.) ).
* "get_date_from_gmt" function requires "String" timezone set (ex. "Asia/Tokyo"), so if WordPress timezone is set by Offset, the date gotten by this function is incorrect. Therefore, if a plugin/theme needs to handle a date with timezone on WordPress, the special handling for not only the timezone set (String) but also the timezone (Offset) is required.
* The library can handle the date with both of 2 types Timezone on WordPress!
*/
public function wp_datetime_converter_init(){
$timezone_set = '';
// WordPress Timezone are 2 types (Strings or Offset).
$timezone_set = get_option('timezone_string');
if (! $timezone_set ):
$gmt_offset = get_option( 'gmt_offset' );
$gmt_hours = (int) $gmt_offset;
$gmt_minutes = ( $gmt_offset - floor( $gmt_offset ) ) * 60;
$timezone_set = sprintf( '%+03d:%02d', $gmt_hours, $gmt_minutes );
endif;
return $timezone_set;
}
// Get current time with Timezone.
public function wp_datetime_converter_current_time($format="c"){
$timezone_set = $this->wp_datetime_converter_init();
$date_obj = new DateTime('', new DateTimeZone($timezone_set)); // get UTC time.
return $date_obj->format($format);
}
// Get the date time with WordPress timezone.
public function wp_datetime_converter_get_date_from_gmt($format="c", $dateTime, $timezone_set=""){
$timezone_set = $this->wp_datetime_converter_init();
if(empty($dateTime)) return $date;
if(empty($timezone_set)) return $timezone_set;
$date_obj = new DateTime(date('Y-m-d H:i:s', strtotime($dateTime))); // UTC timezone
$date_obj->setTimezone(new DateTimeZone($timezone_set)); // Set timezone.
return $date_obj->format($format);
}
// Set the date time with WordPress timezone.
public function wp_datetime_converter_setTimeZone($format="c", $dateTime, $timezone_set=""){
$timezone_set = $this->wp_datetime_converter_init();
if(empty($dateTime)) return $date;
if(empty($timezone_set)) return $timezone_set;
$date_obj = new DateTime(date('Y-m-d H:i:s', strtotime($dateTime)), new DateTimeZone($timezone_set)); // Set timezone.
return $date_obj->format($format);
}
// Convert time to the beginning of the day or the end of the day with WordPress timezone.
// Default: convert to the beginning of the day.
public function wp_datetime_converter_setDayTime($format="c", $dateTime, $flag="start", $timezone_set=""){
$timezone_set = $this->wp_datetime_converter_init();
if(empty($dateTime)) return $dateTime;
if(empty($timezone_set)) return $timezone_set;
if($dateTime == strtolower("today")):
$date_obj = new DateTime('', new DateTimeZone($timezone_set)); // Set timezone.
else:
$date_obj = new DateTime(date('Y-m-d H:i:s', strtotime($dateTime)), new DateTimeZone($timezone_set)); // Set timezone.
endif;
if($flag == strtolower("start")):
$date_num = mktime(0,0,0,$date_obj->format("m"), $date_obj->format("d"), $date_obj->format("Y"));
else:
$date_num = mktime(23,59,59,$date_obj->format("m"), $date_obj->format("d"), $date_obj->format("Y"));
endif;
$date_obj = new DateTime(date('Y-m-d H:i:s', $date_num), new DateTimeZone($timezone_set));
return $date_obj->format($format);
}
public function shortcodes($atts){
$atts = $this->security_check_array($atts);
// If there are not any options, $atts will be initialized by array().
if(empty($atts) || !is_array($atts)):
$atts = array();
endif;
// Allow g_id_*** and g_api_key_*** version 4.0
$atts_special_allow_options = array();
foreach($atts as $key=>$value):
if(preg_match('/^g_id_/', $key) || preg_match('/^g_api_key_/', $key)):
$atts_special_allow_options[$key] = $value;
endif;
endforeach;
$atts_options = array(
'id' => '',
'start_date' => '',
'end_date' => '',
'date_format' => 'Y.m.d',
'orderbysort' => '', // ascending or descending.
'g_api_key' => '', // Google Calendar API KEY
'g_id' => '', // Google Calendar ID
'max_view' => '', // Maximum number of view
'max_display' => '', // Maximum number of display
'html_tag' => '', // Allow $this->html_tags value.
'html_tag_class' => '', // adding a class to html tag (default: $this->plugin_name)
'hook_secret_key' => '', // If you use a hook, please set the secret key because of preventing an overwrite from any other plugins.
'lang' => '', // List only specific languages. #lang [value] on the comment of Google Calendar. version 2.1
'enable_view_category' => '', // If you want to display the category (#type and #organizer), please set this value to "true" or not empty value. version 3.0
'view_location' => '', // If the value is not empty, the location data is displayed with title.
);
if(!empty($atts_special_allow_options)):
$atts_options = array_merge($atts_options, $atts_special_allow_options); // Overwrite the same options.
endif;
extract($atts = shortcode_atts($atts_options, $atts));
$html_tag_class = $html_tag_class ?: $this->plugin_name;
$settings = get_option($this->set_op);
$gc_data = $this->get_google_calendar_contents($atts);
// get lang data.
$gc_data = $this->get_select_lang_data($gc_data, $atts);
// Security check for the hook (clean up ALL html tag except description).
$gc_data = $this->security_check_array($gc_data);
if(!isset($settings['google_calendar'])):
$settings['google_calendar'] = array();
endif;
if(!isset($settings['google_calendar']['html_tag'])):
$settings['google_calendar']['html_tag'] = "";
endif;
if(isset($html_tag) && !empty($html_tag)):
$settings['google_calendar']['html_tag'] = wp_strip_all_tags($html_tag);
if(!isset($this->html_tags[$settings['google_calendar']['html_tag']])) $settings['google_calendar']['html_tag'] = $this->html_tags[$default_html_tag];
endif;
$atts['html_tag'] = $settings['google_calendar']['html_tag'] ? $settings['google_calendar']['html_tag'] : $this->default_html_tag;
$html_tag = $atts['html_tag'];
$out = '';
$match = array();
if( isset($gc_data['items']) ):
foreach($gc_data['items'] as $gc_key=>$gc_value):
if(isset($gc_value['start']['dateTime'])):
$dateTime = $gc_value['start']['dateTime'];
else:
$dateTime = $gc_value['start']['date'];
endif;
if(isset($gc_value['end']['dateTime'])):
$end_dateTime = $gc_value['end']['dateTime'];
else:
$end_dateTime = $gc_value['end']['date'];
endif;
$today_date_num = $this->wp_datetime_converter_current_time("Ymd");
$start_date_num = $this->wp_datetime_converter_get_date_from_gmt("Ymd", $dateTime);
$start_date_value = $this->wp_datetime_converter_get_date_from_gmt($date_format, $dateTime);
$end_date_num = $this->wp_datetime_converter_get_date_from_gmt("Ymd", $end_dateTime);
$end_date_value = $this->wp_datetime_converter_get_date_from_gmt($date_format, $end_dateTime);
$holding_flag = false;
if($today_date_num >= $start_date_num && $today_date_num plugin_name;
$html_tag_class_c = $holding_flag ? $html_tag_class . '_holding' : $html_tag_class;
// for a hook.
$hash_tags = $this->get_hash_tags($gc_value, $atts);
$hash_tags_type_title = "";
if(isset($hash_tags['type']['title'])) $hash_tags_type_title = $hash_tags['type']['title'];
$hash_tags_organizer_value = "";
if(isset($hash_tags['organizer']['value'])) $hash_tags_organizer_value = $hash_tags['organizer']['value'];
if(isset($hash_tags['organizer']['title'])) $hash_tags_organizer_value .= ' ' . $hash_tags['organizer']['title'];
$output_category_temp = '';
if(!empty($enable_view_category)):
if(!empty($hash_tags_type_title)):
$output_category_temp .= " $hash_tags_type_title ";
endif;
if(!empty($hash_tags_organizer_value)):
$output_category_temp .= " $hash_tags_organizer_value ";
endif;
endif;
$out_atts = array(
'start_date_num' => $start_date_num,
'start_date_value' => $start_date_value,
'end_date_num' => $end_date_num,
'end_date_value' => $end_date_value,
'today_date_num' => $today_date_num,
'holding_flag' => $holding_flag,
'gc_link' => $gc_link,
'gc_title' => $gc_title,
'gc_description' => $gc_description,
'gc_location' => $gc_location,
'plugin_name' => $plugin_name,
'html_tag_class' => $html_tag_class,
'html_tag_class_c' => $html_tag_class_c,
'id' => $id,
'lang' => $lang,
'hash_tags' => $hash_tags,
'hash_tags_type_title' => $hash_tags_type_title,
'hash_tags_organizer_value' => $hash_tags_organizer_value,
'output_category_temp' => $output_category_temp,
);
$out_temp = '';
if(!empty($html_tag) && file_exists (dirname( __FILE__ ) . '/library/tags/' . $html_tag . '.php')):
include(dirname( __FILE__ ) . '/library/tags/' . $html_tag . '.php');
endif;
if(!empty($hook_secret_key)):
// $gc_value components is referred in https://developers.google.com/calendar/v3/reference/events#resource.
$out_t = apply_filters( 'lvgc_each_output_data', $out_temp, $out_atts, $gc_value);
if(isset($out_t['hook_secret_key']) && $hook_secret_key === $out_t['hook_secret_key']):
$out .= wp_kses_post($out_t['data']);
else:
$out .= $out_temp;
endif;
else:
$out .= $out_temp;
endif;
endforeach;
endif;
return $out;
}
// Remove all tag except "description" on Google Calendar
public function security_check_array($array){
static $exception = "";
if (empty($array)) return $array;
if(is_array($array)):
foreach($array as $k => $v):
if($k === "description") $exception = "description";
else $exception = "";
$array[$k] = $this->security_check_array($v);
endforeach;
else:
if($exception === "description")
$array = wp_kses_post($array);
else
$array = esc_html(wp_strip_all_tags($array));
endif;
return $array;
}
public function get_google_calendar_contents($atts){
if($atts) extract($atts = $this->security_check_array($atts));
// Getting the settings from the setting menu.
$settings = get_option($this->set_op);
$gc = array();
$gc['api-url'] = $this->google_calendar['api-url'];
if(isset($settings['google_calendar']))
$gc = $settings['google_calendar'];
// Priority of the attribution value in the shortcode.
if(isset($start_date) && !empty($start_date)):
$gc['start-date'] = wp_strip_all_tags($start_date);
else:
$gc['start-date'] = '';
endif;
if(isset($end_date) && !empty($end_date)):
$gc['end-date'] = wp_strip_all_tags($end_date);
else:
$gc['end-date'] = '';
endif;
if(isset($orderbysort) && !empty($orderbysort)):
$gc['orderbysort'] = wp_strip_all_tags($orderbysort);
else:
$gc['orderbysort'] = $this->google_calendar['orderbysort'];
endif;
if(isset($g_api_key) && !empty($g_api_key)) $gc['api-key'] = wp_strip_all_tags($g_api_key);
if(isset($g_id) && !empty($g_id)) $gc['id'] = wp_strip_all_tags($g_id);
if(isset($max_view) && !empty($max_view)):
if((int)$max_view > 0 && (int)($max_view) default_maxResults;
endif;
endif;
if(isset($lang) && !empty($lang)) $gc['lang'] = wp_strip_all_tags($lang);
// Additional Calendars (g_id_*** and g_api_key_*)
$g_urls = array();
foreach($atts as $key=>$value):
$matches = array();
if(preg_match('/^(g_id_)(.+)$/', $key, $matches)):
if(isset($matches[2]) && !empty($matches[2])):
if(isset($atts['g_api_key_' . $matches[2]]) && !empty($atts['g_api_key_' . $matches[2]])):
$g_urls[$key] = esc_url($gc['api-url']) . wp_strip_all_tags($value) . '/events?key=' . wp_strip_all_tags($atts['g_api_key_' . $matches[2]]) . '&singleEvents=true';
else:
$g_urls[$key] = esc_url($gc['api-url']) . wp_strip_all_tags($value) . '/events?key=' . wp_strip_all_tags($gc['api-key']) . '&singleEvents=true';
endif;
endif;
endif;
endforeach;
$g_url = esc_url($gc['api-url']) . wp_strip_all_tags($gc['id']) . '/events?key=' . wp_strip_all_tags($gc['api-key']) . '&singleEvents=true';
$today_date = $this->wp_datetime_converter_current_time("c");
$today_start_date = $this->wp_datetime_converter_setDayTime("c", "today", "start");
$today_end_date = $this->wp_datetime_converter_setDayTime("c", "today", "end");
$params = array();
$params[] = 'orderBy=' . wp_strip_all_tags($this->google_calendar['orderby']);
$params[] = 'maxResults=' . (int)(isset($gc['maxResults']) ? wp_strip_all_tags($gc['maxResults']) : $this->default_maxResults);
/* No limitation : Start Date = all, End Date = empty/all
* Start Date = now : Start Date = empty
* Start Date = value : Start Date != empty/all
* End Date = value : End Date != empty/all
*/
if(!empty($gc['start-date'])):
if(strtolower($gc['start-date']) != "all"):
if(strtolower($gc['start-date']) === "now"):
$params[] = 'timeMin='.urlencode($today_start_date);
else:
$params[] = 'timeMin='.urlencode($this->wp_datetime_converter_setDayTime("c", $gc['start-date']));
endif;
endif;
else:
$params[] = 'timeMin='.urlencode($today_start_date);
endif;
if(!empty($gc['end-date'])):
if(strtolower($gc['end-date']) != "all"):
if(strtolower($gc['end-date']) == "now"):
$params[] = 'timeMax='.urlencode($today_end_date);
else:
$params[] = 'timeMax='.urlencode($this->wp_datetime_converter_setDayTime("c", $gc['end-date']));
endif;
endif;
endif;
$urls = array();
if(!empty($g_urls)):
foreach($g_urls as $key=>$value):
$urls[$key] = $value .'&'.implode('&', $params);
endforeach;
endif;
$url = $g_url .'&'.implode('&', $params);
// Fixed the warning : Ref. https://qiita.com/kawaguchi_011/items/29cc3811b2bc2ce2d85e
$fgc_context = stream_context_create(array(
'http' => array('ignore_errors' => true),
));
$urls_json = array();
$urls_results = array();
foreach($urls as $key=>$value):
$urls_results = file_get_contents($value, false, $fgc_context);
$urls_json[$key] = $urls_results ? json_decode($urls_results, true) : '';
endforeach;
$results = array();
if(isset($gc['id']) && isset($gc['api-key'])):
$results = file_get_contents($url, false, $fgc_context);
endif;
$json = $results ? json_decode($results, true) : '';
// Merge Events of Multi Galendar
if($urls_json && isset($json['items'])):
foreach($urls_json as $key=>$value):
if(isset($value['items'])):
foreach((array)$value['items'] as $s_key=>$s_value):
array_push($json['items'], $s_value);
endforeach;
endif;
endforeach;
endif;
// Instead of ordersort (like Google Calendar API v2)
// Sorting at every time for Multi Calendar
if(isset($json['items']) && !empty($json['items'])):
$s_date = array();
foreach($json['items'] as $key=>$item):
if($this->google_calendar['orderby'] === strtolower('updated')):
$s_date[] = $item['updated'];
$json['items'][$key][$this->google_calendar['orderby']] = $item['updated'];
else:
if(isset($item['start']['dateTime'])):
$s_date[] = $item['start']['dateTime'];
$json['items'][$key][$this->google_calendar['orderby']] = $item['start']['dateTime'];
else:
$s_date[] = $item['start']['date'];
$json['items'][$key][$this->google_calendar['orderby']] = $item['start']['date'];
endif;
endif;
endforeach;
if(strtolower($gc['orderbysort']) !== "descending"):
array_multisort($s_date, SORT_ASC,$json['items']);
else:
array_multisort($s_date, SORT_DESC,$json['items']);
endif;
endif;
/* Pick up $max_display array from the head of $json (data).
*/
if(isset($json['items'])):
if(!empty($max_display) && $max_display > 0):
$json['items'] = array_slice($json['items'], 0, (int)$max_display);
elseif(isset($gc['maxResults']) && !empty($gc['maxResults'])):
$json['items'] = array_slice($json['items'], 0, (int)$gc['maxResults']);
endif;
endif;
return $json;
}
public function add_to_settings_menu(){
add_options_page(sprintf(__('%s Settings', $this->plugin_title), $this->plugin_title), sprintf(__('%s Settings', $this->plugin_title), $this->plugin_title), 'manage_options', __FILE__,array(&$this,'admin_settings_page'));
}
// Processing Setting menu for the plugin.
public function admin_settings_page(){
$settings = get_option($this->set_op);
if(isset($settings['google_calendar']) && is_array($settings['google_calendar'])):
$this->google_calendar = $settings['google_calendar'];
endif;
$google_calendar_flag = false;
if(isset($_POST["gclv-form"]) && $_POST["gclv-form"]):
if(check_admin_referer("gclv-nonce-key", "gclv-form")):
// GET setting data in Settings.
if(isset($_POST['google-calendar-api-key'])):
$this->google_calendar['api-key'] = wp_strip_all_tags($_POST['google-calendar-api-key']);
$google_calendar_flag = true;
endif;
if(isset($_POST['google-calendar-id'])):
$this->google_calendar['id'] = wp_strip_all_tags($_POST['google-calendar-id']);
$google_calendar_flag = true;
endif;
if(isset($_POST['google-calendar-start-date'])):
$this->google_calendar['start-date'] = wp_strip_all_tags($_POST['google-calendar-start-date']);
$google_calendar_flag = true;
endif;
if(isset($_POST['google-calendar-end-date'])):
$this->google_calendar['end-date'] = wp_strip_all_tags($_POST['google-calendar-end-date']);
$google_calendar_flag = true;
endif;
if(isset($_POST['google-calendar-maxResults'])):
// maxResults
if((int)$_POST['google-calendar-maxResults'] > 0 && (int)($_POST['google-calendar-maxResults'] google_calendar['maxResults'] = (int) wp_strip_all_tags($_POST['google-calendar-maxResults']);
else:
$this->google_calendar['maxResults'] = $this->default_maxResults;
endif;
$google_calendar_flag = true;
endif;
if(isset($_POST['google-calendar-orderbysort'])):
$this->google_calendar['orderbysort'] = wp_strip_all_tags($_POST['google-calendar-orderbysort']);
$google_calendar_flag = true;
endif;
if(isset($_POST['google-calendar-html_tag'])):
$this->google_calendar['html_tag'] = wp_strip_all_tags($_POST['google-calendar-html_tag'] ? $_POST['google-calendar-html_tag'] : $this->default_html_tag);
$google_calendar_flag = true;
endif;
endif;
endif;
$settings['google_calendar'] = $this->google_calendar;
if($google_calendar_flag):
update_option($this->set_op , $settings);
endif;
?>
<div class="plugin_name;?>_updated">
<?php
endif;
} // close admin_settings_page function
} // close class
$wm = new gclv();
plugin_title . ' Settings', $this->plugin_name); ?>
<div class="plugin_name;?>_updated">
plugin_name); ?>