array( 'UTC;;;;;1;1' ), 'prefer_user_timezones' => false, 'query_time' => '', 'query_timezone' => '', 'before_list' => '', 'format_timezone' => '
  • %abbreviation: %datetime
  • ', 'format_datetime' => 'Y-m-d H:i', 'use_container' => true, 'display' => true ); /* current defaults (merged database and fallback_defaults) */ private $defaults=array(); /* fallback options */ private $fallback_options=array( 'use_ajax_refresh' => true, 'ajax_refresh_time' => 30, 'renew_nonce' => false, 'dashboard_widget' => false, 'dashboard_widget_capability' => 'read', 'dashboard_right_now' => false, 'dashboard_right_now_capability' => 'read', 'calculator_capability' => 'read', 'world_clock_tools_page' => false, 'world_clock_tools_page_capability' => 'read', 'include_world_clock_user_profile' => false, 'include_wordpress_clock_admin_bar' => true, 'all_users_can_view_timezones' => true, 'view_timezones_capability' => 'read', 'view_other_users_timezones_capability' => 'edit_users', 'debug_mode' => false, 'section' => 'selection_gui' ); /* current options (merged database and fallback_options) */ private $options=array(); /* block_count holds the current number of elements which have been processed */ private $block_count=0; /* options-page sections/option-groups */ private $options_page_sections=array( 'selection_gui' => array( 'nicename' => 'Selection GUI', 'callback' => 'selection_gui' ), 'manual_selection' => array( 'nicename' => 'Manual Selection', 'callback' => 'manual_selection', 'fields' => array( 'timezones' => 'TimeZones' ) ), 'format' => array( 'nicename' => 'Format', 'callback' => 'format', 'fields' => array( 'before_list' => 'before List', 'after_list' => 'after List', 'format_timezone' => 'Format of TimeZone-Entry', 'format_datetime' => 'Format of Date/Time', 'use_container' => 'Wrap output in div-container', 'display' => 'Display Results' ) ), 'ajax_refresh' => array( 'nicename' => 'Ajax Refresh', 'callback' => 'ajax_refresh', 'fields' => array( 'use_ajax_refresh' => 'Use Ajax Refresh', 'ajax_refresh_time' => 'Ajax Refresh Time', 'renew_nonce' => 'Renew nonce to assure continous updates' ) ), 'dashboard' => array( 'nicename' => 'Dashboard', 'callback' => 'dashboard', 'fields' => array( 'dashboard_widget' => 'Enable Dashboard Widget', 'dashboard_widget_capability' => 'Capability to view Dashboard Widget', 'dashboard_right_now' => 'Integrate in "Right Now" Box', 'dashboard_right_now_capability' => 'Capability to integrate in "Right Now" Box' ) ), 'calculator' => array( 'nicename' => 'Calculator', 'callback' => 'calculator', 'fields' => array( 'calculator_capability' => 'Capability to work with TimeZone-Calculator' ) ), 'world_clock' => array( 'nicename' => 'World Clock', 'callback' => 'world_clock', 'fields' => array( 'world_clock_tools_page' => 'Enable World Clock in Tools Menu', 'world_clock_tools_page_capability' => 'Capability to view World Clock in Tools Menu', 'include_world_clock_user_profile' => 'Display user selected timezones (world clock) on user profile page' ) ), 'administrative_options' => array( 'nicename' => 'Administrative Options', 'callback' => 'administrative_options', 'fields' => array( 'prefer_user_timezones' => 'Prefer User TimeZones', 'include_wordpress_clock_admin_bar' => 'Display WordPress Clock in Admin Bar', 'all_users_can_view_timezones' => 'All users can view timezones', 'view_timezones_capability' => 'Capability to view timezones', 'view_other_users_timezones_capability' => 'Capability to view timezones-selection of other users', 'debug_mode' => 'Enable Debug-Mode' ) ), 'preview' => array( 'nicename' => 'Preview', 'callback' => 'preview' ) ); /* calculator-page sections/calculator-groups */ private $calculator_page_sections=array( 'calculator' => array( 'nicename' => 'Calculator', 'callback' => 'calculator' ), 'selected_timezones' => array( 'nicename' => 'Selected TimeZones', 'callback' => 'selected_timezones' ) ); /* Constructor */ function __construct() { /* store current timestamp in constant */ DEFINE ('TIMEZONECALCULATOR_CURRENTGMDATE', gmdate('U')); /* initialize object */ $this->set_plugin_url(); $this->retrieve_settings(); $this->register_hooks(); } /* register js libraries */ function register_scripts() { /* DatePicker v5.4 by frequency-decoder.com http://www.frequency-decoder.com/2009/09/09/unobtrusive-date-picker-widget-v5 */ $unobtrusive_date_picker_widget_pre_conditions=array(); $lang=substr(get_locale(), 0, 2); if ($lang!='en') { $maybe_lang_file='vendor/datepicker/js/lang/'.$lang.'.js'; if (file_exists(plugin_dir_path(__FILE__).$maybe_lang_file)) { $unobtrusive_date_picker_widget_pre_conditions[]='unobtrusive_date_picker_widget_lang'; wp_register_script('unobtrusive_date_picker_widget_lang', $this->get_plugin_url().$maybe_lang_file, array(), '5.4'); } } wp_register_script('unobtrusive_date_picker_widget', $this->get_plugin_url().'vendor/datepicker/js/datepicker.js', $unobtrusive_date_picker_widget_pre_conditions, '5.4'); /* jshashtable v3.0 by Tim Down http://www.timdown.co.uk/jshashtable/ */ wp_register_script('jshashtable', $this->get_plugin_url().'vendor/jshashtable/hashtable.js', array(), '3.0'); /* TimeZoneCalculator JS */ wp_register_script($this->get_prefix().'refresh', $this->get_plugin_url().'js/refresh.js', array('jquery', 'jshashtable'), '3.20'); wp_register_script($this->get_prefix().'utils', $this->get_plugin_url().'js/utils.js', array('jquery'), '3.00'); wp_register_script($this->get_prefix().'timezones', $this->get_plugin_url().'js/timezones.js', array('jquery'), '3.10'); wp_register_script($this->get_prefix().'selection_gui', $this->get_plugin_url().'js/selection_gui.js', array('jquery', 'jquery-ui-sortable', 'jquery-effects-highlight', $this->get_prefix().'utils', $this->get_prefix().'timezones'), '3.21'); wp_register_script($this->get_prefix().'settings_page', $this->get_plugin_url().'js/settings_page.js', array('jquery', $this->get_prefix().'selection_gui', $this->get_prefix().'utils'), '3.20'); wp_register_script($this->get_prefix().'calculator', $this->get_plugin_url().'js/calculator.js', array('jquery', 'jshashtable', 'unobtrusive_date_picker_widget', $this->get_prefix().'selection_gui', $this->get_prefix().'refresh', $this->get_prefix().'timezones', $this->get_prefix().'utils'), '3.34'); } /* register css styles */ function register_styles() { wp_register_style($this->get_prefix().'admin', $this->get_plugin_url().'css/admin.css', array('dashicons'), '3.20'); wp_register_style('unobtrusive_date_picker_widget', $this->get_plugin_url().'vendor/datepicker/css/datepicker.css', array('dashicons'), '3.20'); } /* register WordPress hooks */ private function register_hooks() { /* register externals */ add_action('init', array($this, 'register_scripts')); add_action('init', array($this, 'register_styles')); /* general */ add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2); add_action('admin_menu', array($this, 'admin_menu')); /* ajax refresh calls */ /* always allowed ajax actions */ add_action('wp_ajax_'.$this->get_prefix().'calculator', array($this, 'wp_ajax_calculator')); if ($this->get_option('use_ajax_refresh')) { /* include ajax refresh scripts */ add_action('wp_print_scripts', array($this, 'refresh_print_scripts')); /* allowed ajax actions */ add_action('wp_ajax_'.$this->get_prefix().'output', array($this, 'wp_ajax_refresh')); /* the calculator_ajax_nonce action should only be allowed if renew_nonce has been set */ if ($this->get_option('renew_nonce')) add_action('wp_ajax_'.$this->get_prefix().'calculator_ajax_nonce', array($this, 'wp_ajax_calculator_ajax_nonce')); /* anonymous ajax refresh requests can be restricted */ if ($this->get_option('all_users_can_view_timezones')) add_action('wp_ajax_nopriv_'.$this->get_prefix().'output', array($this, 'wp_ajax_refresh')); } /* meta-data */ add_action('wp_head', array($this, 'head_meta')); add_action('admin_head', array($this, 'head_meta')); /* widgets */ add_action('widgets_init', array($this, 'widgets_init')); add_action('wp_dashboard_setup', array($this, 'add_dashboard_widget')); add_action('activity_box_end', array($this, 'add_right_now_box')); /* i18n for timezones */ add_action('init', array($this, 'init')); /* shortcode */ add_shortcode($this->get_prefix().'output', array($this, 'shortcode_output')); /* profile_page */ add_action('show_user_profile', array($this, 'show_user_profile')); add_action('edit_user_profile', array($this, 'show_user_profile')); /* add admin-clock as we need to check for the WordPress Admin Bar and this function subsequently checks if a user is logged-in we can do this only after plugins_loaded has fired */ if ($this->get_option('include_wordpress_clock_admin_bar')) add_action('plugins_loaded', array($this, 'admin_bar_clock')); /* whitelist options */ add_action('admin_init', array($this, 'admin_init')); } /* GETTERS AND SETTERS */ /* getter for prefix true with trailing _ false without trailing _ */ function get_prefix($trailing_=true) { if ($trailing_) return $this->prefix.'_'; else return $this->prefix; } /* getter for nicename */ function get_nicename() { return $this->nicename; } /* setter for plugin_url */ private function set_plugin_url() { $this->plugin_url=plugins_url('', __FILE__).'/'; } /* getter for plugin_url */ private function get_plugin_url() { return $this->plugin_url; } /* increment for block_count */ private function increment_block_count() { $this->block_count++; } /* getter for block-count */ private function get_block_count() { return $this->block_count; } /* getter for calculator tools_page */ private function get_calculator_tools_page() { return $this->get_prefix().'calculator'; } /* getter for world_clock tools_page */ private function get_world_clock_tools_page() { return $this->get_prefix().'world_clock'; } /* getter for default parameter */ private function get_default($param) { if (isset($this->defaults[$param])) return $this->defaults[$param]; else return false; } /* getter for default parameter */ private function get_option($param) { if (isset($this->options[$param])) return $this->options[$param]; else return false; } /* retrieve settings from database and merge with fallback-settings */ private function retrieve_settings() { $settings=get_option($this->get_prefix(false)); /* did we retrieve an non-empty settings-array which we can merge with the default settings? */ if (!empty($settings) && is_array($settings)) { /* process options-array */ if (array_key_exists('options', $settings) && is_array($settings['options'])) { $this->options = array_merge($this->fallback_options, $settings['options']); $this->log('merging fallback-options '.var_export($this->fallback_options, true).' with database options '.var_export($settings['options'], true)); } /* process defaults-array */ if (array_key_exists('defaults', $settings) && is_array($settings['defaults'])) { $this->defaults = array_merge($this->fallback_defaults, $settings['defaults']); $this->log('merging fallback-defaults '.var_export($this->fallback_defaults, true).' with database defaults '.var_export($settings['defaults'], true)); } } /* settings-array does not exist */ else { /* are we handling an update? check for trigger field if this field exist, we handle an update */ $maybe_timezones=get_option('TimeZones'); if (!empty($maybe_timezones)) { /* conduct upgrade */ $this->upgrade_v2(); /* restart retrieval process */ $this->retrieve_settings(); /* avoid further processing of first retrieve_settings function call */ return; } /* we intentionally write the fallbacks in the database to activate built-in caching and repair a broken settings-array */ else { $defaults=$this->fallback_defaults; unset($defaults['query_time']); unset($defaults['query_timezone']); update_option( $this->get_prefix(false), array( 'defaults' => $defaults, 'options' => $this->fallback_options ) ); } } /* if the settings have not been set we use the fallback-options array instead */ if (empty($this->options)) { $this->options = $this->fallback_options; $this->log('using fallback-options '.var_export($this->fallback_options, true)); } /* if the settings have not been set we use the fallback-defaults array instead */ if (empty($this->defaults)) { $this->defaults = $this->fallback_defaults; $this->log('using fallback-defaults '.var_export($this->fallback_defaults, true)); } /* maybe upgrade to v2.40? */ if (array_key_exists('anonymous_ajax_refresh', $this->options)) $this->upgrade_v24(); /* maybe upgrade to v3.00? */ if (array_key_exists('include_wordpress_clock_admin_head', $this->options) || array_key_exists('ajax_refresh_lib', $this->options)) $this->upgrade_v30(); /* maybe upgrade to v3.34? */ if (array_key_exists('display_sponsored_link', $this->defaults) || array_key_exists('display_sponsored_link', $this->options)) $this->upgrade_v334(); $this->log('setting options to '.var_export($this->options, true)); $this->log('setting defaults to '.var_export($this->defaults, true)); } /* Sanitize and validate input Accepts an array, return a sanitized array */ function settings_validate($input) { /* we handle a reset call */ if (isset($input['reset'])) { $defaults=$this->fallback_defaults; unset($defaults['query_time']); unset($defaults['query_timezone']); return array( 'defaults' => $defaults, 'options' => $this->fallback_options ); } /* check-fields will be converted to true/false */ $check_fields=array( 'use_container', 'display', 'use_ajax_refresh', 'renew_nonce', 'dashboard_widget', 'dashboard_right_now', 'world_clock_tools_page', 'include_world_clock_user_profile', 'prefer_user_timezones', 'include_wordpress_clock_admin_bar', 'all_users_can_view_timezones', 'debug_mode' ); foreach ($check_fields as $check_field) { $input[$check_field] = (isset($input[$check_field]) && $input[$check_field] == 1) ? true : false; } /* these text-fields should not be empty */ $text_fields=array( 'timezones', 'format_timezone', 'format_datetime', 'ajax_refresh_time' ); foreach ($text_fields as $text_field) { if (isset($input[$text_field]) && strlen($input[$text_field])<1) unset($input[$text_field]); } /* selected capabilities have to be within available capabilities */ $capability_fields=array( 'dashboard_widget', 'dashboard_right_now', 'calculator', 'world_clock_tools_page', 'view_timezones', 'view_other_users_timezones' ); $capabilities=$this->get_all_capabilities(); foreach ($capability_fields as $capability_field) { if (isset($input[$capability_field.'_capability']) && !in_array($input[$capability_field.'_capability'], $capabilities)) unset($input[$capability_field.'_capability']); } /* 1 <= ajax_refresh_time <= 3600 (seconds) */ if (array_key_exists('ajax_refresh_time', $input)) if (!$this->is_integer($input['ajax_refresh_time']) || $input['ajax_refresh_time']<1 || $input['ajax_refresh_time']>3600) $input['ajax_refresh_time']=$this->fallback_options['ajax_refresh_time']; /* split timezones-string by newline */ if (array_key_exists('timezones', $input)) { $temp_timezones=explode("\n", $input['timezones']); /* did we receive a results array? */ if (is_array($temp_timezones)) { $input['timezones']=array(); /* we don't need empty lines */ foreach ($temp_timezones as $timezone) { if (strlen($timezone)>1) array_push($input['timezones'], trim($timezone)); } /* nothing parsed */ if (empty($input['timezones'])) unset($input['timezones']); } /* couldn't convert */ else unset($input['timezones']); } /* include options */ $options=$this->fallback_options; foreach($options as $option => $value) { if (array_key_exists($option, $input)) $options[$option]=$input[$option]; } /* include defaults */ $defaults=$this->fallback_defaults; foreach($defaults as $default => $value) { if (array_key_exists($default, $input)) $defaults[$default]=$input[$default]; } /* we don't need to store the query_time or query_timezone as they are only necessary for non-default function calls */ if (array_key_exists('query_time', $defaults)) unset($defaults['query_time']); if (array_key_exists('query_timezone', $defaults)) unset($defaults['query_timezone']); $ret_val=array(); $ret_val['defaults']=$defaults; $ret_val['options']=$options; return $ret_val; } /* upgrade options to TimeZoneCalculator v2 */ private function upgrade_v2() { $this->log('upgrade options to '.$this->get_nicename().' v2'); $fieldsPre='timezones_'; $sectionPost='_Section'; /* this array will hold all old settings as if we would handle a "Save Changes" call of the options-page */ $settings=array(); $timezones=get_option('TimeZones'); $settings['timezones']=$timezones; $this->log('timezones set to '.$timezones); delete_option('TimeZones'); /* key field holds the old option name value the new option name */ $upgrade_options=array( 'before_List' => 'before_list', 'after_List' => 'after_list', 'before_Tag' => 'before_tag', 'after_Tag' => 'after_tag', 'Time_Format' => 'format_datetime', 'Use_Ajax_Refresh' => 'use_ajax_refresh', 'Refresh_Time' => 'ajax_refresh_time' ); /* loop through all available old options */ foreach ($upgrade_options as $old_option => $new_option) { $old_option_value=get_option($fieldsPre.$old_option); $settings[$new_option]=$old_option_value; $this->log('option '.$new_option.' set to '.$old_option_value); /* remove old option */ delete_option($fieldsPre.$old_option); } /* old before_Tag and after_Tag, will be merged to format_timezone */ $settings['format_timezone']=$settings['before_tag'].'%abbreviation: %datetime'.$settings['after_tag']; unset($settings['before_tag']); unset($settings['after_tag']); /* delete section settings */ $sections=array( 'Instructions', 'Content', 'CSS_Tags', 'Administrative_Options', 'Calculation' ); foreach ($sections as $section) { delete_option($fieldsPre.$section.$sectionPost); } /* include check_fields which need to be set true */ $settings['use_container']='1'; $settings['display']='1'; $settings['all_users_can_view_timezones']='1'; /* validate retrieved settings */ $settings=$this->settings_validate($settings); /* store new settings */ update_option($this->get_prefix(false), $settings); $this->log('upgrade finished. - retrieved options are: '.var_export($settings, true)); } /* upgrade options to TimeZoneCalculator v2.40 */ private function upgrade_v24() { $this->log('upgrade options to '.$this->get_nicename().' v2.40'); /* rename setting */ $this->options['all_users_can_view_timezones']=$this->options['anonymous_ajax_refresh']; unset($this->options['anonymous_ajax_refresh']); /* we don't need to store the query_time or query_timezone as they are only necessary for non-default function calls */ if (array_key_exists('query_time', $this->defaults)) unset($this->defaults['query_time']); if (array_key_exists('query_timezone', $this->defaults)) unset($this->defaults['query_timezone']); /* combine settings-array */ $settings=array(); $settings['defaults']=$this->defaults; $settings['options']=$this->options; /* store new settings */ update_option($this->get_prefix(false), $settings); $this->log('upgrade finished. - retrieved options are: '.var_export($settings, true)); } /* upgrade options to TimeZoneCalculator v3.00 */ private function upgrade_v30() { $this->log('upgrade options to '.$this->get_nicename().' v3.00'); /* rename setting */ $this->options['include_wordpress_clock_admin_bar']=$this->options['include_wordpress_clock_admin_head']; unset($this->options['include_wordpress_clock_admin_head']); /* remove setting */ unset($this->options['ajax_refresh_lib']); /* we don't need to store the query_time or query_timezone as they are only necessary for non-default function calls */ if (array_key_exists('query_time', $this->defaults)) unset($this->defaults['query_time']); if (array_key_exists('query_timezone', $this->defaults)) unset($this->defaults['query_timezone']); /* combine settings-array */ $settings=array(); $settings['defaults']=$this->defaults; $settings['options']=$this->options; /* store new settings */ update_option($this->get_prefix(false), $settings); $this->log('upgrade finished. - retrieved options are: '.var_export($settings, true)); } /* upgrade options to TimeZoneCalculator v3.34 */ private function upgrade_v334() { $this->log('upgrade options to '.$this->get_nicename().' v3.34'); /* remove setting */ if (array_key_exists('display_sponsored_link', $this->defaults)) unset($this->defaults['display_sponsored_link']); if (array_key_exists('display_sponsored_link', $this->options)) unset($this->options['display_sponsored_link']); /* we don't need to store the query_time or query_timezone as they are only necessary for non-default function calls */ if (array_key_exists('query_time', $this->defaults)) unset($this->defaults['query_time']); if (array_key_exists('query_timezone', $this->defaults)) unset($this->defaults['query_timezone']); /* combine settings-array */ $settings=array(); $settings['defaults']=$this->defaults; $settings['options']=$this->options; /* store new settings */ update_option($this->get_prefix(false), $settings); $this->log('upgrade finished. - retrieved options are: '.var_export($settings, true)); } /* merges parameter array with defaults array defaults-array can be changed with filter 'timezonecalculator_defaults' */ private function fill_default_parameters($params) { /* apply filter timezonecalculator_defaults */ $filtered_defaults=apply_filters($this->get_prefix().'defaults', $this->defaults); /* merge filtered defaults with params params overwrite merged defaults */ return wp_parse_args($params, $filtered_defaults); } /* UTILITY FUNCTIONS */ /* checks if a value is an integer regex taken from php.net by mark at codedesigner dot nl */ function is_integer($value) { return preg_match('@^[-]?[0-9]+$@', $value); } /* rounds to the nearest multiple by Mark Blaszczyk taken from http://www.markblah.com/2008/05/11/php-round-number-to-nearest-x/ */ function round_to_nearest($num, $nearest) { $ret = 0; $mod = $num % $nearest; if ($mod >= 0) $ret = ( $mod > ( $nearest / 2)) ? $num + ( $nearest - $mod) : $num - $mod; else $ret = ( $mod > (-$nearest / 2)) ? $num - $mod : $num + ( -$nearest - $mod); return $ret; } /* shows log messages on screen if debug_mode is set to true optionally executes trigger_error if we're handling an error */ private function log($message, $status=0) { $date_format = get_option('date_format'); $time_format = get_option('time_format'); $log_line=gmdate($date_format.' '.$time_format, current_time('timestamp')).' '; /* determine the log line's prefix */ if ($status==0) $log_line.='INFO'; else if ($status==-1) $log_line.='ERROR'; else if ($status==-2) $log_line.='WARNING'; else if ($status==1) $log_line.='SQL'; /* append message */ $log_line.=' '.$message.'
    '; /* output message to screen */ if ($this->get_option('debug_mode')) echo($log_line); /* output message to file */ if ($status<0) trigger_error($message); } /* called by wp_ajax_* and wp_ajax_nopriv_* hooks */ private function do_ajax_refresh($_ajax_nonce=true) { $action=$_REQUEST['action']; $query_string=''; if (isset($_REQUEST['query_string'])) $query_string=$_REQUEST['query_string']; /* security check */ if (!$this->get_option('all_users_can_view_timezones') && !current_user_can($this->get_option('view_timezones_capability'))) wp_die(-1, '', array('response' => 403)); $security_string=$action.str_replace(array('\n', "\n"), '', $query_string); check_ajax_referer($security_string); /* convert query_string to params-array */ $params=array(); $ajax_musts=array( 'use_container' => true, 'display' => false, 'no_refresh' => true ); /* parse retrieved query_string */ if (!empty($query_string)) $params=wp_parse_args($query_string); $params=array_merge($params, $ajax_musts); /* remove leading prefix from action */ $method=str_replace($this->get_prefix(), '', $action); /* prepare json object */ $json_params=array(); /* we only provide a renewed nonce if the admin has chosen to */ if ($this->get_option('renew_nonce')) { /* return updated (2nd tick) _ajax_nonce */ if ($_ajax_nonce===true) $json_params['_ajax_nonce']=wp_create_nonce($security_string); /* use provided _ajax_nonce */ else if (!empty($_ajax_nonce)) $json_params['_ajax_nonce']=$_ajax_nonce; } /* call function output */ $json_params['result']=call_user_func(array($this, $method), $params); $this->output_json($json_params); } /* outputs a json-object */ private function output_json($params) { if (!is_array($params)) { echo("-1"); return -1; } $ret_val=''; /* use built-in function if available */ if (function_exists('json_encode')) $ret_val=json_encode($params); /* or do our own json-encoding */ else { /* prepare json string */ $ret_val='{'; foreach ($params as $key => $param) $ret_val.='"'.$key.'":"'.str_replace(array('\\', '"'), array('\\\\', '\"'), $param).'",'; $ret_val=substr($ret_val, 0, -1); $ret_val.='}'; } header('Content-type: application/json'); echo($ret_val); } /* returns all capabilities without 'level_' */ private function get_all_capabilities() { $wp_roles=new WP_Roles(); $names=$wp_roles->get_names(); $all_caps=array(); foreach($names as $name_key => $name) { $wp_role=$wp_roles->get_role($name_key); $role_caps=$wp_role->capabilities; foreach($role_caps as $cap_key => $role_cap) { if (!in_array($cap_key, $all_caps) && strpos($cap_key, 'level_')===false) $all_caps[]=$cap_key; } } asort($all_caps); return $all_caps; } /* get block id from output block */ private function get_block_id_from_block($block) { $prefix='block_'; $pos_block=strpos($block, $prefix); $class=strpos($block, '"', $pos_block+1); return substr($block, $pos_block+strlen($prefix), $class-$pos_block-strlen($prefix)); } /* sort timezones-array */ function sort_timezones_array($a, $b) { if ($a['t_continent']==$b['t_continent']) { if ($a['t_city']==$b['t_city']) return strnatcasecmp($a['t_subcity'], $b['t_subcity']); return strnatcasecmp($a['t_city'], $b['t_city']); } else return strnatcasecmp($a['t_continent'], $b['t_continent']); } /* returns or echoes js-timezones-array */ function timezone_js_arrays($etc_group=true, $echo=true) { global $wp_version; $continents=''; $timezones=''; $transient_name=$this->get_prefix().'js_array_'.get_locale().(($etc_group) ? '_etc' : ''); $maybe_cache=get_transient($transient_name); $cache=null; if (!empty($maybe_cache)) $cache=@unserialize(base64_decode($maybe_cache)); /* check contents of cache - compare php-version number - wordpress version number */ if (!empty($cache) && is_array($cache) && array_key_exists('php_version', $cache) && $cache['php_version']==PHP_VERSION && array_key_exists('wp_version', $cache) && $cache['wp_version']==$wp_version && array_key_exists('continents', $cache) && array_key_exists('timezones', $cache) && !empty($cache['continents']) && !empty($cache['timezones'])) { $continents=$cache['continents']; $timezones=$cache['timezones']; $this->log('use cached results for timezone-arrays'); } else { $timezone_array=$this->retrieve_timezone_js_arrays($etc_group); $continents=$timezone_array['continents']; $timezones=$timezone_array['timezones']; $cache=array_merge( array( 'php_version' => PHP_VERSION, 'wp_version' => $wp_version ), $timezone_array ); set_transient($transient_name, base64_encode(serialize($cache)), 86400); $this->log('refresh cache for timezone-arrays'); } if ($echo) { ?> get_prefix().'continents_array=\''; /* add etc group */ if ($etc_group) $continents.=''; $timezones='var '.$this->get_prefix().'timezones_array=new Array('; /* add etc/UTC as first entry */ if ($etc_group) { $timezones.='\''; /* if the user has already selected a timezone in the admin menu, we provide the Local_WordPress_Time option */ $wordpress_timezone=get_option('timezone_string'); if (!empty($wordpress_timezone)) $timezones.=''; $timezones.='\', '; } $selectcontinent=''; $firstcontinent=true; /* loop through the timezones */ foreach ($zonen as $zone) { /* create continent optgroup and close an open one */ if (($selectcontinent!=$zone['continent']) && !empty($zone['city'])) { $selectcontinent=$zone['continent']; if ($firstcontinent) $firstcontinent=false; else $timezones.="', "; $continents .= ''; $timezones.="'"; } /* if a city name exists, add entry to list */ if (!empty($zone['city'])) { if (!empty($zone['subcity'])) { $zone['city'] = $zone['city'].'/'.$zone['subcity']; $zone['t_city'] = $zone['t_city'].'/'.$zone['t_subcity']; } $timezones .= ''; } } $continents .= "';"; $timezones.= "');"; return array( 'continents' => $continents, 'timezones' => $timezones ); } /* checks and transforms a datetime and a timezone into a UTC unix timestamp */ function calculate_date($query_time, $query_timezone=null, $current_utc=TIMEZONECALCULATOR_CURRENTGMDATE) { $ret_val=false; /* process unix timestamp */ if ($this->is_integer($query_time)) $ret_val=$query_time; /* parse date/time */ elseif (strlen($query_time)>2) { $adapted_for_strtotime=$current_utc; /* adapt timestamp for strtotime so +2 hours and tomorrow 3pm will be interpreted correctly */ if (!empty($query_timezone)) { $offset=$this->calculate_utc_offset($current_utc, $query_timezone); $adapted_for_strtotime+=$offset; } /* set default_timezone to UTC because strtotime uses local_time remark: actually WordPress uses UTC as default timezone in wp-settings.php, so the following code shouldn't get executed; though I leave it for compatibility purposes */ $zero_offsets=array( 'UTC', 'UCT', 'GMT', 'GMT0', 'GMT+0', 'GMT-0', 'Greenwich', 'Universal', 'Zulu' ); $server_timezone=@date_default_timezone_get(); $server_timezone_changed=false; /* check for UTC-strings */ if (!in_array(str_ireplace('etc/', '', $server_timezone), $zero_offsets)) { @date_default_timezone_set('UTC'); $server_timezone_changed=true; } /* convert query_time, based on adapted datetime */ $parsed_date=strtotime($query_time, $adapted_for_strtotime); /* set back to previous timezone */ if ($server_timezone_changed) @date_default_timezone_set($server_timezone); /* could we parse the date/time? -1 because of https://secure.php.net/manual/en/function.strtotime.php */ if ($parsed_date===false || $parsed_date==-1) throw new Exception('could not parse date!'); $ret_val=$parsed_date; } /* all timestamps between 1930-01-01 and 2038-01-01 are accepted */ if ($ret_val!==false && (int)$ret_val>=-1262304000 && (int)$ret_val<=2145916800) { /* if no timezone has been given, we're finished */ if (empty($query_timezone)) return $ret_val; /* otherwise we calculate the offset to utc */ $offset=$this->calculate_utc_offset($ret_val, $query_timezone); return ($ret_val-$offset); } throw new Exception('unkown datetime'); } /* CALLED BY HOOKS (and therefore public) */ /* called by wp_ajax_* and wp_ajax_nopriv_* hooks */ function wp_ajax_refresh() { $this->do_ajax_refresh(); exit; } /* ajax refresh for calculator */ function wp_ajax_calculator() { /* get current user id */ $user_ID=get_current_user_id(); /* security check */ $pre_security_string=$this->get_prefix().'calculator'.$user_ID; check_ajax_referer($pre_security_string); $security_string=$this->get_prefix().'output'.str_replace(array('\n', "\n"), '', $_REQUEST['query_string']); $_ajax_nonce=wp_create_nonce($security_string); $_REQUEST['_ajax_nonce']=$_ajax_nonce; $_REQUEST['action']=$this->get_prefix().'output'; $this->do_ajax_refresh(false); if (!empty($_REQUEST['query_string'])) { $params=wp_parse_args($_REQUEST['query_string']); if (!empty($params) && array_key_exists('query_timezone', $params)) { try { new DateTimeZone($params['query_timezone']); update_user_option($user_ID, $this->get_prefix().'calculator_timezone', $params['query_timezone']); } catch(Exception $e) {} } } exit; } /* ajax refresh for calculator's ajax nonce */ function wp_ajax_calculator_ajax_nonce() { /* get current user id */ $user_ID=get_current_user_id(); /* security check */ $security_string=$this->get_prefix().'calculator'.$user_ID; check_ajax_referer($security_string); /* prepare json string */ $json_params=array(); /* return updated (2nd tick) _ajax_nonce */ $json_params['_ajax_nonce']=wp_create_nonce($security_string); $this->output_json($json_params); exit; } /* Calculator Tools-Page */ function calculator_page_timezones_update() { /* get current user id */ $user_ID=get_current_user_id(); /* security check */ if (!current_user_can($this->get_option('calculator_capability'))) wp_die(__('You do not have sufficient permissions to access this page.'), '', array('response' => 403)); /* we handle a save call */ if (isset($_REQUEST[$this->get_prefix(false)]) && is_array($_REQUEST[$this->get_prefix(false)])) { /* security check */ $result = (isset($_REQUEST['_wpnonce'])) ? wp_verify_nonce($_REQUEST['_wpnonce'], $this->get_prefix().'set_user_timezones'.$user_ID) : false; if (!$result) { echo('

    '.__('Please try again.').'

    '); return; } $input=$_REQUEST[$this->get_prefix(false)]; /* reset -> use global timezones */ if (array_key_exists('reset', $input)) { update_user_option($user_ID, $this->get_prefix().'timezones', array()); } /* handle a save call */ else if (array_key_exists('timezones', $input)) { $temp_timezones=explode("\n", $input['timezones']); /* did we receive a results array? */ if (is_array($temp_timezones)) { $input['timezones']=array(); /* we don't need empty lines */ foreach ($temp_timezones as $timezone) { if (strlen($timezone)>1) array_push($input['timezones'], trim($timezone)); } update_user_option($user_ID, $this->get_prefix().'timezones', $input['timezones']); } } /* dirty patch, because WordPress does not update the user's cached options, so if we reload the page once, we 'force the cache to be refreshed' */ $redirect_args=array( 'page' => $this->get_calculator_tools_page(), 'updated' => 'true' ); wp_safe_redirect(esc_url(add_query_arg($redirect_args, admin_url('tools.php')), null, 'redirect')); exit; } } /* Calculator Tools-Page */ function calculator_page() { /* security check */ if (!current_user_can($this->get_option('calculator_capability'))) wp_die(__('You do not have sufficient permissions to access this page.'), '', array('response' => 403)); /* handle a save call */ if (isset($_GET['updated']) && $_GET['updated']) { ?>

    Timezones updated!

    settings_page($this->calculator_page_sections, $this->get_option('calculator_capability'), 'calculator', false); } /* Calculator World-Clock-Page */ function world_clock_page() { ?>
    get_option('world_clock_tools_page') && current_user_can($this->get_option('world_clock_tools_page_capability'))) { global $wp_version; $h_level=(version_compare($wp_version, '4.3', '>=')) ? '1' : '2'; echo('World Clock
    '); $this->display_world_clock('world_clock_tools_page'); } ?>
    settings_page($this->options_page_sections, 'manage_options', 'settings', true); } /* Options Page Help Tab */ function options_page_help_tab() { $this->add_help_tab($this->options_page_help()); } /* white list options */ function admin_init() { /* if on calculator-page handle user's timezones-update */ if (isset($_REQUEST['page']) && !empty($_REQUEST['page']) && $_REQUEST['page']==$this->get_calculator_tools_page()) $this->calculator_page_timezones_update(); /* settings */ register_setting($this->get_prefix(false), $this->get_prefix(false), array($this, 'settings_validate')); $this->add_settings_sections($this->options_page_sections, 'settings'); $this->add_settings_sections($this->calculator_page_sections, 'calculator'); } /* add TimeZoneCalculator to WordPress Settings Menu and create submenus in tools.php */ function admin_menu() { /* options page */ $options_page=add_options_page($this->get_nicename(), $this->get_nicename(), 'manage_options', $this->get_prefix(false), array($this, 'options_page')); add_action('admin_print_scripts-'.$options_page, array($this, 'settings_print_scripts')); add_action('admin_print_styles-'.$options_page, array($this, 'admin_print_styles')); add_action('load-'.$options_page, array($this, 'options_page_help_tab')); /* calculator tools page */ $calculator_page=add_management_page($this->get_nicename(), $this->get_nicename(), $this->get_option('calculator_capability'), $this->get_calculator_tools_page(), array($this, 'calculator_page')); add_action('admin_print_scripts-'.$calculator_page, array($this, 'calculator_print_scripts')); add_action('admin_print_scripts-'.$calculator_page, array($this, 'refresh_print_scripts')); add_action('admin_print_styles-'.$calculator_page, array($this, 'calculator_print_styles')); add_action('admin_print_styles-'.$calculator_page, array($this, 'admin_print_styles')); /* world clock tools page */ if ($this->get_option('world_clock_tools_page')) { $world_clock_page=add_management_page('World Clock', 'World Clock', $this->get_option('world_clock_tools_page_capability'), $this->get_world_clock_tools_page(), array($this, 'world_clock_page')); add_action('admin_print_styles-'.$world_clock_page, array($this, 'admin_print_styles')); } } /* adds meta-information to HTML header */ function head_meta() { echo("get_nicename()."\" content=\"3.37\"/>\n"); } /* add dashboard widget */ function add_dashboard_widget() { if ($this->get_option('dashboard_widget') && current_user_can($this->get_option('dashboard_widget_capability'))) wp_add_dashboard_widget($this->get_prefix().'dashboard_widget', $this->get_nicename(), array($this, 'dashboard_widget_output')); } /* dashboard widget */ function dashboard_widget_output() { /* security check */ if (!current_user_can($this->get_option('dashboard_widget_capability'))) return; $this->current_timezones_block('dashboard_widget'); } /* add timezones to dashboard's right now box */ function add_right_now_box() { if ($this->get_option('dashboard_right_now') && current_user_can($this->get_option('dashboard_right_now_capability'))) { echo('

    '); $this->current_timezones_block('dashboard_right_now'); } } /* embed world clock in user profile */ function show_user_profile($profileuser) { if ($this->get_option('include_world_clock_user_profile')) { global $wp_version; $h_sub_level=(version_compare($wp_version, '4.4', '>=')) ? '2' : '3'; echo('World Clock'); $this->display_world_clock('user_profile', $profileuser->ID); } } /* embed local WordPress clock in WordPress Admin Bar */ function admin_bar_clock() { add_action('admin_bar_menu', array($this, 'admin_bar_wordpress_clock'), apply_filters($this->get_prefix().'admin_bar_clock_position', 1000)); } /* attach clock to admin bar */ function admin_bar_wordpress_clock() { $format_container='display:inline'; /* decide where the link of the clock should point to */ $clock_href=''; if (current_user_can($this->get_option('calculator_capability'))) $clock_href=add_query_arg(array('page' => $this->get_calculator_tools_page()), admin_url('tools.php')); else $clock_href=admin_url('profile.php'); /* don't display the timezone-name as title because the wrapped span-elements have problems on Ajax-updates in some browsers -> keep it simple */ $format_timezone='%datetime %abbreviation'; /* generate WordPress-clock */ $wordpress_clock=$this->wordpress_clock('admin_bar_clock', $format_container, $format_timezone, false); /* to be html-valid we replace the div-elements with span-elements as we cannot have a div inside of a link, and links are necessary in the admin-bar API https://core.trac.wordpress.org/ticket/14772 https://core.trac.wordpress.org/ticket/15519 */ $wordpress_clock_span=str_replace(array(' $this->get_prefix(false), 'title' => $wordpress_clock_span, 'href' => esc_url($clock_href) ); $wp_admin_bar->add_node($admin_bar_params); } /* called from widget_init hook */ function widgets_init() { register_widget('WP_Widget_'.$this->get_nicename()); } /* called from init hook */ function init() { load_textdomain('continents-cities', WP_LANG_DIR . '/continents-cities-' . get_locale() . '.mo'); } /* adds the javascript code for re-occuring timezone-updates */ function refresh_print_scripts() { /* security check */ if (!$this->get_option('all_users_can_view_timezones') && !current_user_can($this->get_option('view_timezones_capability'))) return; wp_enqueue_script($this->get_prefix().'refresh'); $security_string=$this->get_prefix().'output'; $_ajax_nonce=wp_create_nonce($security_string); /* make sure that Ajax-queries use the same protocol as the site */ $ajax_url=admin_url('admin-ajax.php', is_ssl() ? 'https' : 'http'); wp_localize_script( $this->get_prefix().'refresh', $this->get_prefix().'refresh_settings', array( 'ajax_url' => $ajax_url, '_ajax_nonce' => $_ajax_nonce, 'refresh_time' => $this->get_option('ajax_refresh_time') ) ); } /* loads the necessary java-scripts for the options-page */ function settings_print_scripts() { wp_enqueue_script($this->get_prefix().'settings_page'); $this->timezone_js_arrays(); } /* loads the necessary java-scripts for the calculator-page */ function calculator_print_scripts() { /* we also need the libraries of options-page */ $this->settings_print_scripts(); wp_enqueue_script($this->get_prefix().'calculator'); /* get current user id */ $user_ID=get_current_user_id(); /* datepicker fallback language is en */ if (!wp_script_is('unobtrusive_date_picker_widget_lang', 'registered')) { echo(''); } $security_string=$this->get_prefix().'calculator'.$user_ID; $_ajax_nonce=wp_create_nonce($security_string); $unfiltered_params=array( 'format_timezone' => urlencode('
  • %datetime %name (%abbreviation)
  • '), 'format_datetime' => urlencode($this->get_default('format_datetime')), 'before_list' => urlencode($this->fallback_defaults['before_list']), 'after_list' => urlencode($this->fallback_defaults['after_list']) ); $filtered_params=apply_filters($this->get_prefix().'calculator', $unfiltered_params); $params=array( '_ajax_nonce' => $_ajax_nonce, 'refresh_nonce' => $this->get_option('renew_nonce') ); $params=array_merge($filtered_params, $params); wp_localize_script($this->get_prefix().'calculator', $this->get_prefix().'calculator_settings', $params); } /* loads the necessary css-styles for the calculator-page */ function calculator_print_styles() { wp_enqueue_style('unobtrusive_date_picker_widget'); } /* adds a settings and a calculator link in the plugin-tab */ function plugin_action_links($links, $file) { if ($file == plugin_basename(__FILE__)) { $links[] = '' . __('Settings') . ''; $links[] = '' . __('Calculator') . ''; } return $links; } /* includes the necessary CSS-styles for the admin-page */ function admin_print_styles() { wp_enqueue_style($this->get_prefix().'admin'); } /* LOGIC FUNCTIONS */ /* internal function of output */ private function _output($params=array()) { /* log call */ $this->log('function _output, $params='.var_export($params, true)); /* security check */ if (!$this->get_option('all_users_can_view_timezones') && !current_user_can($this->get_option('view_timezones_capability'))) throw new Exception('You are not authorized to view timezones!'); /* fill params with default-values */ $params=$this->fill_default_parameters($params); /* get current user id */ $user_ID=get_current_user_id(); /* allow to query for a user's timezones-selection */ if (is_user_logged_in() && $params['prefer_user_timezones']) { /* if user_id has not been set, we use the user who is currently logged in */ if (!isset($params['user_id']) || empty($params['user_id'])) $params['user_id']=$user_ID; /* if a user tries to view the timezones of another user, we conduct a security check */ if ($params['user_id']!=$user_ID && !current_user_can($this->get_option('view_other_users_timezones_capability'))) throw new Exception('You are not authorized to view another user\'s timezones!'); /* validate user_id */ if (!$this->is_integer($params['user_id']) || $params['user_id']<1 || !get_userdata($params['user_id'])) throw new Exception('user_id '.$params['user_id'].' does not exist'); $maybe_timezones=get_user_option($this->get_prefix().'timezones', $params['user_id']); if (!empty($maybe_timezones) && is_array($maybe_timezones)) { $params['timezones']=$maybe_timezones; $this->log('use timezones-selection of user '.$params['user_id']); } } /* convert query type string to array */ if (!array_key_exists('timezones', $params) || empty($params['timezones']) || !is_array($params['timezones'])) { $maybe_timezones=str_replace("\r", '', $params['timezones']); $maybe_timezones=explode("\n", $maybe_timezones); if (!empty($maybe_timezones) && is_array($maybe_timezones)) $params['timezones']=$maybe_timezones; else $params['timezones']=$this->get_default('timezones'); } /* log call */ $this->log('function _output, merged with defaults, $params='.var_export($params, true)); $ret_val=''; $refreshable=$this->is_output_refreshable($params); /* set block-id */ if (!isset($params['id'])) $params['id']=$this->get_block_count(); /* shall we wrap the output in a container? */ if ($params['use_container']) { $ret_val.='
    get_prefix().'block_'.$params['id'].'" '; $ret_val.='class="'.$this->get_prefix(false).'-'; if ($refreshable) $ret_val.='refreshable-'; $ret_val.='output"'; if (!empty($params['format_container'])) $ret_val.=' style="'.$params['format_container'].'"'; $ret_val.='>'; } /* if query_time has not been set we use the current unix timestamp in utc instead */ if (!isset($params['query_time']) || empty($params['query_time'])) $params['query_time']=TIMEZONECALCULATOR_CURRENTGMDATE; /* use a handed over timestamp or datetime-string */ else { /* calculate unix timestamp with parameters */ $params['query_time']=$this->calculate_date($params['query_time'], $params['query_timezone']); if ($params['query_time']===false) throw new Exception('Either the query_time or the query_timezone are not correct!'); /* we don't need the query_timezone anymore */ if (isset($params['query_timezone'])) unset($params['query_timezone']); } $ret_val.=$params['before_list']; /* process timezone-entries */ foreach ($params['timezones'] as $timezone) { /* is there anything to parse in the particular line? */ if (strlen($timezone)>1) { /* calculate and format timezone-entry */ try { $datetimezone=new TimeZoneCalculator_DateTimeZone($timezone, $params['query_time']); $ret_val.=$datetimezone->format_timezone($params); } catch(Exception $e) { $this->log($e->getMessage()); $ret_val.=str_replace(array('%name', '%abbreviation', '%datetime'), array('', '', $e->getMessage()), $params['format_timezone']); } } } $ret_val.=$params['after_list']; if ($params['use_container']) $ret_val.='
    '; /* produce js refresh code */ if ($this->get_option('use_ajax_refresh') && $params['use_container'] && !$refreshable && !isset($params['no_refresh']) && $params['query_time']==TIMEZONECALCULATOR_CURRENTGMDATE) { /* allowed query params for ajax refresh call */ $refresh_query_params=array( 'id', 'prefer_user_timezones', 'user_id', 'before_list', 'after_list', 'format_timezone', 'format_datetime', 'format_container' ); $query_string=''; /* build ajax query-string */ foreach ($params as $key => $param) if (in_array($key, $refresh_query_params)) $query_string.=$key.'='.urlencode($param).'&'; $query_string.='timezones='; foreach ($params['timezones'] as $timezone) $query_string.=urlencode($timezone).'\\n'; $query_string=substr($query_string, 0, -2); ?> increment_block_count(); /* echo results */ if ($params['display']) echo($ret_val); /* return results */ else return $ret_val; } /* display clock with WordPress time */ private function wordpress_clock($filter, $format_container, $format_timezone, $display) { $filtered_style=apply_filters($this->get_prefix().$filter.'_format_container', $format_container); $unfiltered_params=array( 'timezones' => array( 'Local_WordPress_Time' ), 'prefer_user_timezones' => false, 'before_list' => '', 'after_list' => '', 'format_timezone' => $format_timezone ); $filtered_params=apply_filters($this->get_prefix().$filter, $unfiltered_params); $params=array( 'use_container' => true, 'format_container' => $filtered_style, 'display' => $display ); return $this->output(array_merge($filtered_params, $params)); } /* display world-clock */ private function display_world_clock($filter, $user_id=null) { $unfiltered_params=array( 'user_id' => $user_id, 'before_list' => '
    ', 'format_timezone' => '
  • %datetime %name (%abbreviation)
  • ' ); $filtered_params=apply_filters($this->get_prefix().$filter, $unfiltered_params); $params=array( 'use_container' => true, 'display' => true, 'prefer_user_timezones' => true ); $this->output(array_merge($filtered_params, $params)); if (current_user_can($this->get_option('calculator_capability'))) echo('
    You can customize your timezones in the '.$this->get_nicename().'.'); } /* checks if params lead to refreshable div check if timezones and defaults are same as selected in option page */ private function is_output_refreshable($params) { /* if a timestamp has been set the output is not automatically refreshable */ if (isset($params['query_time']) && !empty($params['query_time'])) return false; /* if a id has been set the output is not automatically refreshable */ if (isset($params['id']) && strlen($params['id'])>0) return false; /* check for different timezones setting */ if (array_key_exists('timezones', $params)) { $default_timezones=$this->get_default('timezones'); $params_timezones=$params['timezones']; /* do the arrays differ in size? */ if (sizeof($default_timezones)!=sizeof($params_timezones)) { $this->log('found different timezones setting - disabling refreshable'); return false; } /* compare order of timezones of global defaults and function call timezones */ for($i=0; $ilog('found different timezones setting or timezones order - disabling refreshable'); return false; } } } /* every set fallback-default which is not 'display' or 'use_container' will trigger a new creation of the timezones block */ $trigger_params=$params; unset($trigger_params['display']); unset($trigger_params['use_container']); foreach($trigger_params as $key => $value) { if (array_key_exists($key, $this->defaults) && $value!=$this->get_default($key)) { $this->log('found trigger option '.$key. ' - disabling refreshable'); return false; } } return true; } /* calculates offset to UTC for given unix-timestamp and query_timezone */ private function calculate_utc_offset($local_time, $query_timezone) { $offset=0; /* WordPress TimeZones Support */ if ($query_timezone=='Local_WordPress_Time') { $query_timezone=get_option('timezone_string'); } $timezone=@timezone_open($query_timezone); /* if timezone is available, lookup offset */ if (!$timezone) throw new Exception('timezone unknown'); /* inspired from Derick's talk https://talks.php.net/show/time-ffm2006/28 lookup array until current transition has been found */ foreach (timezone_transitions_get($timezone) as $tr) { if ($tr['ts'] > ($local_time-$offset)) { return $offset; } $offset=$tr['offset']; } return $offset; } /* output current timezones-block */ private function current_timezones_block($filter) { $filtered_params=apply_filters($this->get_prefix().$filter, array()); $params=array( 'use_container' => true, 'display' => true ); $this->output(array_merge($filtered_params, $params)); } /* ADMIN MENU - UTILITY */ /* register settings sections and fields */ private function add_settings_sections($settings_sections, $section_prefix) { /* settings-sections */ foreach($settings_sections as $section_key => $section) { $this->add_settings_section($section_key, $section['nicename'], $section_prefix, $section['callback']); /* fields for each section */ if (array_key_exists('fields', $section)) { foreach ($section['fields'] as $field_key => $field) { $this->add_settings_field($field_key, $field, $section_key, $section_prefix); } } } } /* adds a settings section */ private function add_settings_section($section_key, $section_name, $section_prefix, $callback) { add_settings_section('default', $section_name, array($this, 'callback_'.$section_prefix.'_'.$callback), $this->get_prefix().$section_prefix.'_'.$section_key); } /* adds a settings field */ private function add_settings_field($field_key, $field_name, $section_key, $section_prefix, $label_for='') { if (empty($label_for)) $label_for=$this->get_prefix().$field_key; add_settings_field($this->get_prefix().$field_key, $field_name, array($this, 'setting_'.$field_key), $this->get_prefix().$section_prefix.'_'.$section_key, 'default', array('label_for' => $label_for)); } /* creates section link */ private function get_section_link($sections, $section, $section_nicename='', $create_id=false) { if (strlen($section_nicename)<1) $section_nicename=$sections[$section]['nicename']; $id=''; $class=''; $section_span=''; if ($create_id) $id=' id="'.$this->get_prefix().$section.'_link"'; else { $class=' class="'.$this->get_prefix().'section_link"'; $section_span=''.$section_nicename.''; } $menuitem_onclick=" onclick=\"".$this->get_prefix()."open_section('".$section."');\""; $section_link=''.$section_nicename.''; return $section_span.$section_link; } /* returns name="timezonecalculator[setting]" id="timezonecalculator_setting" */ private function get_setting_name_and_id($setting) { return 'name="'.$this->get_prefix(false).'['.$setting.']" id="'.$this->get_prefix().$setting.'"'; } /* returns default value for option-field */ private function get_setting_default_value($field, $type) { $default_value=null; if ($type=='options') $default_value=htmlentities($this->get_option($field), ENT_QUOTES, get_option('blog_charset'), false); else if ($type=='defaults') $default_value=htmlentities($this->get_default($field), ENT_QUOTES, get_option('blog_charset'), false); else throw new Exception('type '.$type.' does not exist for field '.$field.'!'); return $default_value; } /* outputs a settings section */ private function do_settings_sections($section_key, $section_prefix) { do_settings_sections($this->get_prefix().$section_prefix.'_'.$section_key); } /* handles adding a help-tab */ private function add_help_tab($help_text) { $current_screen=get_current_screen(); $help_options=array( 'id' => $this->get_prefix(), 'title' => $this->get_nicename(), 'content' => $help_text ); $current_screen->add_help_tab($help_options); } /* Settings Page */ private function settings_page($settings_sections, $permissions, $section_prefix, $is_wp_options) { /* security check */ if (!current_user_can($permissions)) wp_die(__('You do not have sufficient permissions to access this page.'), '', array('response' => 403)); /* option-page html */ global $wp_version; $h_level=(version_compare($wp_version, '4.3', '>=')) ? '1' : '2'; ?>
    >get_nicename()); ?>>
    get_prefix(false)); } foreach ($settings_sections as $key => $section) { ?>
    do_settings_sections($key, $section_prefix); echo('
    '); } ?>

    'Save Changes', 'reset' => 'Default' ); foreach ($submit_buttons as $key => $submit_button) $this->setting_submit_button($key, $submit_button); ?>

    support(); ?>
    settings_page_js($settings_sections, $is_wp_options); ?> get_prefix().'toggle_related_fields('; $javascript_fields=', ['.implode(', ', $related_fields).']'; /* check for disabled fields on document ready */ ?> get_setting_default_value($name, $type); ?> get_setting_name_and_id($name)); ?> type="checkbox" value="1" /> get_setting_default_value($name, $type); $size_attribute=($size>40) ? 'class="widefat"' : 'size="'.$size.'"'; ?> get_setting_name_and_id($name).' '.$javascript_validate); ?> maxlength="" value="" /> is_integer($selected_minute) && $selected_minute>-1) $selected_minute=$this->round_to_nearest($selected_minute, 5); /* to avoid to much binding we cheat a little bit here because 60 minutes would actually mean that we would have to also change the hour and maybe the date */ if ($selected_minute==60) $selected_minute=55; $format_12_hours=false; /* look for 'a' in format a=am/pm A=AM/PM */ if (stripos($format, 'a')!==false) $format_12_hours=true; /* make hour select */ echo(' : '); /* make minute select */ echo(''); } /* outputs support paragraph */ private function support() { global $user_identity; ?>

    Support

    , if you would like to support the development of get_nicename()); ?>, you can invite me for a virtual pizza for my work.

    Donate

    Maybe you also want to add a link to https://www.bernhard-riedl.com/projects/.

    Plan your travels with the free JourneyCalculator which is based on TimeZoneCalculator.

    Welcome to the Settings-Page of get_nicename()); ?>. This plugin calculates, displays and automatically updates times and dates in different timezones with respect to daylight saving. get_prefix()."wrap\">
    • You can insert new or edit your existing timezone-entries in the ".$this->get_section_link($this->options_page_sections, 'selection_gui', 'Selection GUI Section')." or in the ".$this->get_section_link($this->options_page_sections, 'manual_selection', 'Manual Selection Section').". Latter section also works without the usage of JavaScript. In any way, new entries are only saved after clicking on Save Changes.
    • Style-customizations can be made in the ".$this->get_section_link($this->options_page_sections, 'format', 'Format Section').".
    • You can activate an optional ".$this->get_section_link($this->options_page_sections, 'ajax_refresh', 'Ajax refresh for automatical updates ')." of your timezones-output.
    • Before you publish the results you can use the ".$this->get_section_link($this->options_page_sections, 'preview', 'Preview Section').".
    • Finally, you can publish the previously selected and saved timezones either by adding a Sidebar Widget, ".$this->get_section_link($this->options_page_sections, 'dashboard', 'Dashboard Widget')." or by enabling the ".$this->get_section_link($this->options_page_sections, 'world_clock', 'World Clock').". Moreover you can display a clock with your WordPress timezone in the ".$this->get_section_link($this->options_page_sections, 'administrative_options', 'Admin Bar').".
    • Geek stuff: You can output your timezones-selection by calling the PHP function $".$this->get_prefix(false)."->output(\$params) wherever you like (don't forget global $".$this->get_prefix(false)."). This function can also be invoked by the usage of a shortcode.
    • You can enable the get_calculator_tools_page()."\">TimeZone-Calculator for your users or just for yourself in the ".$this->get_section_link($this->options_page_sections, 'calculator', 'Calculator Section').", where you can calculate a certain timestamp in your selected timezones.
    • If you decide to uninstall ".$this->get_nicename().", firstly remove the optionally added Sidebar Widget, integrated PHP function or WordPress shortcode call(s). Afterwards, disable and delete ".$this->get_nicename()." in the Plugins Tab.
    • For more information:
      get_prefix(false))."/\">".$this->get_nicename()." in the WordPress Plugin Directory
    "; } /* section selection_gui */ function callback_settings_selection_gui() { $this->do_settings_selection_gui(); } /* function selection_gui */ function do_settings_selection_gui($timezones_array=array()) { ?>
    • You can insert new timezones by filling out the TimeZone form and clicking Insert.
    • To customize existing timezones click on the entry you want to change in the TimeZone Entries form and edit the parameters in the TimeZone form. If you want to adjust the details of a timezone entry, you have to click on the arrow
      in the TimeZone form. This will open the advanced menu where you can either select to use the abbreviations and names of the timezone database, or manually insert your own descriptions. After clicking Edit the selected timezone's parameters will be adapted.
    • To re-order the timezones within a list either use drag and drop or click on the arrows
      on the left side of the particular timezone.
    • To remove timezones from the list just drag and drop them into the Garbage Bin or click on the double-arrow
      .
    • Don't forget to save all your adjustments by clicking on Save Changes.
    • To include the Local WordPress TimeZone in the timezones-select, set your timezone in the options'); ?>
    get_default('timezones'); $list_selected=''; $before_tag='
  • '; /* build list */ $before_key=$this->get_prefix().'timezone_'; $counter=0; $list_selected_listeners=''; /* loop through all timezone-entries */ foreach ($timezones_array as $timezone) { if (!empty($timezone)) { $timezone_attributes=explode(';', $timezone); $tag=''; $otherOptions=''; if (sizeof($timezone_attributes)>0) { $tag=$timezone_attributes[0]; if (strlen($tag)<1) $tag='UTC'; $otherOptions=''; /* arrows */ $up_arrow='
    '; $down_arrow='
    '; $move_arrow='
    '; /* add listener for edit panel */ $list_selected_listeners.="jQuery('#".$before_key.$counter."').click(function(){ ".$this->get_prefix()."populate_list_edit('".$counter."') });"; /* add timezone to list-selected */ $list_selected.= $before_tag. '"'.$before_key.$counter.'">'.$up_arrow.$down_arrow.$move_arrow.''.$tag.''.$otherOptions.$after_tag. "\n"; $counter++; } } } /* format list */ $element_height=32; $sizelist_selected=$element_height; if ($counter>1) $sizelist_selected=$counter*$element_height; $sizelist_available=$element_height; /* lists-container */ echo('
    '); /* output selected timezones */ echo('

    TimeZone Entries

    • '.$list_selected.'
    '); /* provide garbage bin */ echo('

    Garbage Bin

    '); echo('
    '); /* output edit form */ $edit_abbr_fields=array( 'edit_abbr_standard' => 'Abbreviation Standard', 'edit_abbr_daylightsaving' => 'Abbreviation Daylightsaving' ); $edit_name_fields=array( 'edit_name_standard' => 'Name Standard', 'edit_name_daylightsaving' => 'Name Daylightsaving' ); $oninput_check_semicolons='oninput="'.$this->get_prefix().'inline_error(jQuery(this), \'Semicolon is not supported!\', jQuery(this).val().indexOf(\';\')>-1);"'; ?>
    setting_timezone($this->get_prefix().'edit_'); ?> $edit_abbr_field) { echo(''); echo(''); } ?> $edit_name_field) { echo(''); echo(''); } ?>

    callback_settings_selection_gui_js($list_selected_listeners, $edit_abbr_fields, $edit_name_fields); } private function callback_settings_selection_gui_js($list_selected_listeners, $edit_abbr_fields, $edit_name_fields) { ?> In this section you can adapt your timezones-selection 'by hand'. Changes you make here are only reflected in the get_section_link($this->options_page_sections, 'selection_gui', 'Selection GUI Section')); ?> after clicking on Save Changes.

    All parameters need to be separated by a semicolon. Please note, that only the timezone_id is mandatory.

    Syntax

    • timezone_id;
    • abbreviation_standard;
    • abbreviation_daylightsaving;
    • name_standard;
    • name_daylightsaving;
    • use_db_abbreviations;
      • 0 → use user-input as abbreviations
      • 1 → use abbreviations from PHP timezones database
    • use_db_names
      • 0 → use user-input as names
      • 1 → use names from PHP timezones database (the timezone_id)

    Examples

    • Asia/Bangkok
    • America/New_York;EST;EDT;New York, NY, US;New York, NY, US;0;0
    • Europe/Vienna;;;sleep longer in winter;get up earlier to enjoy the sun;1;0

    Infos about TimeZones

    In this section you can customize the layout of get_section_link($this->options_page_sections, 'preview', $this->get_nicename().'\'s output')); ?> after saving your changes by clicking on Save Changes. Tutorials, references and examples about HTML and CSS can be found on W3Schools.
    • The timezones-list will be wrapped within before List and after List. Each timezone-entry is based on Format of TimeZone-Entry. The following fields will be replaced by the attributes of each timezone-entry:
      • %abbreviation
      • %name
      • %datetime
    • You can customize the Format of Date/Time by using standard PHP syntax. The default format is yyyy-mm-dd hh:mm which looks like Y-m-d H:i in PHP. - For details please refer to the WordPress Documentation on date and time formatting.
    • In case you do not need a container, you can disable the option Wrap output in div-container.
    • The last option, Display Results, only refers to PHP function calls with $get_prefix(false)); ?>->output($params).
    • Moreover, you can add CSS style attributes for the following div elements in your Theme, e.g. with the WordPress Theme-Editor.

    ContainerTypeFunction/Shortcode callsused if
    get_prefix(false)); ?>-refreshable-outputdiv$get_prefix(false)); ?>->output($params)same timezones and format as set in Admin Menu
    get_prefix(false)); ?>-outputdiv$get_prefix(false)); ?>->output($params)different timezones or format as in Admin Menu

    setting_textfield('before_list', 'defaults'); } function setting_after_list($params=array()) { $this->setting_textfield('after_list', 'defaults'); } function setting_format_timezone($params=array()) { $this->setting_textfield('format_timezone', 'defaults', 500); } function setting_format_datetime($params=array()) { $this->setting_textfield('format_datetime', 'defaults'); } function setting_use_container($params=array()) { $this->setting_checkfield('use_container', 'defaults'); } function setting_display($params=array()) { $this->setting_checkfield('display', 'defaults'); } /* section ajax refresh */ function callback_settings_ajax_refresh() { ?> In this section you can enable and customize the Ajax-Refresh of get_nicename()); ?>.
    • After activating Use Ajax Refresh you can specify the seconds for the update interval (Ajax Refresh Time).
    • Remember that on every refresh all timezone-information has to be retrieved from the server. Thus, an Ajax Refresh Time of one second is not practicable for the average server out there. In addition, every update causes bandwith usage for your readers and your server.
    • Due to security reasons, the time for Ajax updates will be limited by default. In your installation, the nonce-life-time is defined as . If you activate Renew nonce to assure continous updates you override this security feature (only for get_nicename()); ?>) but provide unlimited time for Ajax updates of your timezones.
    setting_checkfield('use_ajax_refresh', 'options', array('ajax_refresh_time', 'renew_nonce', 'ajax_refresh_lib')); } function setting_ajax_refresh_time($params=array()) { $this->setting_textfield('ajax_refresh_time', 'options', 4, 'oninput="'.$this->get_prefix().'check_integer(jQuery(this), 1, 3600);"'); } function setting_renew_nonce($params=array()) { $this->setting_checkfield('renew_nonce', 'options'); } /* section dashboard */ function callback_settings_dashboard() { ?> If you enable one of the next options, get_nicename()); ?> will show your timezones either as a Dashboard Widget or in the Right-Now-Box on the Dashboard. You can also choose the necessary capability. setting_checkfield('dashboard_widget', 'options', array('dashboard_widget_capability')); } function setting_dashboard_widget_capability($params=array()) { $this->setting_capability('dashboard_widget', 'options'); } function setting_dashboard_right_now($params=array()) { $this->setting_checkfield('dashboard_right_now', 'options', array('dashboard_right_now_capability')); } function setting_dashboard_right_now_capability($params=array()) { $this->setting_capability('dashboard_right_now', 'options'); } /* section calculator */ function callback_settings_calculator() { ?> In this section you can select which capability is necessary to enable the timezone-calculator in the tools-menu. setting_capability('calculator', 'options'); } /* section world_clock */ function callback_settings_world_clock() { ?>
    • The user's timezones-selection (or as fallback get_section_link($this->options_page_sections, 'selection_gui', 'the global timezones-selection')); ?> can be added as submenu of Tools. You can select which capability is necessary for your users to be able to access the world clock.
    • Furthermore, it can also be included on the user's profile page, if you enable Display user selected timezones (world clock) on user profile page.
    setting_checkfield('world_clock_tools_page', 'options', array('world_clock_tools_page_capability')); } function setting_world_clock_tools_page_capability($params=array()) { $this->setting_capability('world_clock_tools_page', 'options'); } function setting_include_world_clock_user_profile($params=array()) { $this->setting_checkfield('include_world_clock_user_profile', 'options'); } /* section administrative options (also holds hidden section id) */ function callback_settings_administrative_options() { ?>
    • You can choose that user selected timezones should be preferred to global and function call ones with the option Prefer User TimeZones.
    • Your local WordPress Date/Time can be displayed in the Admin Bar if you enable Display WordPress Clock in Admin Bar.
    • If you want to keep the timezones as a secret, you can deactivate All users can view timezones. In that case, only users with the Capability to view timezones can access this information.
    • As it may be a privacy invasion to provide someone with access to a certain user's timezones-selection, you can define in addition the Capability to view timezones-selection of other users. In others words, if Alice wants to access Bob's timezones-selection, she needs to have both of the mentioned capabilities.
    • The Debug Mode can be used to have a look on the actions undertaken by get_nicename()); ?> and to investigate unexpected behaviour.
    get_setting_name_and_id('section')); ?> value="get_option('section')); ?>" /> setting_checkfield('prefer_user_timezones', 'defaults'); } function setting_include_wordpress_clock_admin_bar($params=array()) { $this->setting_checkfield('include_wordpress_clock_admin_bar', 'options'); } function setting_all_users_can_view_timezones($params=array()) { $this->setting_checkfield('all_users_can_view_timezones', 'options', array('view_timezones_capability'), false); } function setting_view_timezones_capability($params=array()) { $this->setting_capability('view_timezones', 'options'); } function setting_view_other_users_timezones_capability($params=array()) { $this->setting_capability('view_other_users_timezones', 'options'); } function setting_debug_mode($params=array()) { $this->setting_checkfield('debug_mode', 'options'); } /* section preview */ function callback_settings_preview() { ?> You can publish this output either by adding a Sidebar Widget, get_section_link($this->options_page_sections, 'dashboard', 'Dashboard Widget')); ?> or by calling the PHP function $get_prefix(false)); ?>->output($params) (optionally with several parameters as described in the Other Notes) after calling global $get_prefix(false)); ?> wherever you like.

    true, 'display' => true ); $this->output($params); } /* CALCULATOR PAGE - SECTIONS */ /* intro callback */ function callback_calculator_intro() { ?> Welcome to your personal timezone calculator. Schedule a conference call or just keep track of your friends around the globe. Please specify your date/time of interest and let it be displayed in the get_section_link($this->options_page_sections, 'selected_timezones', 'pre-selected timezones')); ?>.
    Sorry, you need to have JavaScript enabled in order to use this section.
    get_option('world_clock_tools_page') && current_user_can($this->get_option('world_clock_tools_page_capability'))) { ?>

    You can also view the current time for the pre-selected timezonesget_option('use_ajax_refresh')) { ?>, which automatically updates, on the world clock. get_prefix().'calculator_timezone', get_user_option($this->get_prefix().'calculator_timezone', $user_ID)); $date_format=apply_filters($this->get_prefix().'calculator_format_date', array($this->get_default('format_datetime'))); $time_format=apply_filters($this->get_prefix().'calculator_format_time', $this->get_default('format_datetime')); ?> Choose your current timezone, then pick your date/time in the calendar/drop-down or enter it manually in the date field by using one of these formats: ISO (yyyy-mm-dd), US (mm/dd/yyyy) or any English textual datetime description (tomorrow 3 pm or 2009-04-23 16:30). Let's start!

    TimeZone
    setting_timezone($this->get_prefix(), $timezone); ?>
    Date
    setting_date($this->get_prefix(), false, $date_format); ?>
    Time
    setting_time($this->get_prefix(), '', '', $time_format); ?>
    get_block_id_from_block($initial_block); $this->callback_calculator_calculator_js($block_id); } private function callback_calculator_calculator_js($block_id) { ?> get_prefix().'timezones', $user_ID); if (!is_array($timezones_array) || empty($timezones_array)) $timezones_array=$this->get_default('timezones'); $this->do_settings_selection_gui($timezones_array); ?>
    get_prefix().'set_user_timezones'.$user_ID); ?> get_setting_name_and_id('section')); ?> />

    'Save Changes', 'reset' => 'Default' ); foreach ($submit_buttons as $key => $submit_button) $this->setting_submit_button($key, $submit_button); ?>

    ` - `after_list`: default `` - `format_timezone`: default `
  • %abbreviation: %datetime
  • ` - `format_datetime`: default `Y-m-d H:i` - `timezones`: alternative timezones-array - each array entry has to be a string as described in the Manual Selection Section of the Admin Menu; default is the timezones-entries array which can be modified in the Admin Menu - `prefer_user_timezones`: prefer user set timezones - if they exist - to global or function call timezones; default is `false` - `use_container`: if set to `true` (default value), the current UTC is used as `query_time` and the same selected timezones and format is used as set in the admin menu, TimeZoneCalculator wraps the output in a html div with the class `timezonecalculator-refreshable-output` - the class `timezonecalculator-output` will be used for all other output; if you set `use_container` to `false`, no container div will be generated - `display`: if you want to return the timezone-information (e.g. for storing in a variable) instead of echoing it with this function-call, set this to `false`; default setting is `true` - `format_container`: This option can be used to format the `div` container with css. Please note, that it should only be used to provide individual formats in case the class-style itself cannot be changed. - `no_refresh`: If set to true, TimeZoneCalculator will not produce any Ajax-Refresh-code, even if you have enabled the Ajax refresh in the admin menu. */ function output($params=array()) { try { return $this->_output($params); } catch(Exception $e) { $this->log($e->getMessage(), -1); return false; } } /* SHORTCODE */ /* shortcode for function output */ function shortcode_output($params, $content=null) { $params['display']=false; return $this->output($params); } } /* DATETIMEZONE CLASS this class is an extended version of DateTimeZone and is capable of calculating daylightsaving and offset for a certain timestamp */ class TimeZoneCalculator_DateTimeZone extends DateTimeZone { private $timezone_id='UTC'; private $timestamp=null; private $abbreviation_std=null; private $abbreviation_dst=null; private $db_abbreviation=null; private $name_std=null; private $name_dst=null; private $use_db_abbreviation=false; private $use_db_name=false; private $is_dst=false; private $offset; /* Constructor */ function __construct($timezone, $timestamp) { /* parse data */ $this->parse_data($timezone); /* set timestamp */ $this->set_timestamp($timestamp); /* call parent's constructor */ parent::__construct($this->get_timezone_id()); $this->is_dst(); } /* GETTERS AND SETTERS */ private function set_timezone_id($timezone_id) { $this->timezone_id=$timezone_id; } private function get_timezone_id() { return $this->timezone_id; } private function set_timestamp($timestamp) { $this->timestamp=$timestamp; } private function get_timestamp() { return $this->timestamp; } private function set_name_std($name_std) { $this->name_std=$name_std; } private function get_name_std() { return $this->name_std; } private function set_name_dst($name_dst) { $this->name_dst=$name_dst; } private function get_name_dst() { return $this->name_dst; } /* return std/dst db or handed over name */ function get_name() { /* use db name */ if ($this->get_use_db_name()) { $tokens = explode('/', $this->getName()); $tokens[0]=__(str_replace('_', ' ', $tokens[0]), 'continents-cities'); if (isset($tokens[1])) $tokens[1]=__(str_replace('_', ' ', $tokens[1]), 'continents-cities'); if (isset($tokens[2])) $tokens[2]=__(str_replace('_', ' ', $tokens[2]), 'continents-cities'); $string=$tokens[0]; if (isset($tokens[1]) && !empty($tokens[1])) $string.='/'.$tokens[1]; if (isset($tokens[2]) && !empty($tokens[2])) $string.='/'.$tokens[2]; return $string; } /* decide between custom std and dst name */ else { if (!$this->is_dst) return $this->get_name_std(); else return $this->get_name_dst(); } } private function set_abbreviation_std($abbreviation_std) { $this->abbreviation_std=$abbreviation_std; } private function get_abbreviation_std() { return $this->abbreviation_std; } private function set_abbreviation_dst($abbreviation_dst) { $this->abbreviation_dst=$abbreviation_dst; } private function get_abbreviation_dst() { return $this->abbreviation_dst; } private function set_db_abbreviation($db_abbreviation) { $this->db_abbreviation=$db_abbreviation; } private function get_db_abbreviation() { return $this->db_abbreviation; } /* return std/dst db or handed over abbreviation */ function get_abbreviation() { /* use db abbreviation */ if ($this->get_use_db_abbreviation()) return $this->get_db_abbreviation(); /* decide between custom std and dst abbreviation */ else { if (!$this->is_dst) return $this->get_abbreviation_std(); else return $this->get_abbreviation_dst(); } } private function set_use_db_name($use_db_name) { $this->use_db_name=$use_db_name; } private function get_use_db_name() { return $this->use_db_name; } private function set_use_db_abbreviation($use_db_abbreviation) { $this->use_db_abbreviation=$use_db_abbreviation; } private function get_use_db_abbreviation() { return $this->use_db_abbreviation; } private function set_is_dst($is_dst) { $this->is_dst=$is_dst; } private function get_is_dst() { return $this->is_dst; } private function set_offset($offset) { $this->offset=$offset; } function get_offset() { return $this->offset; } /* LOGIC FUNCTIONS */ /* parses a timezone-entry in format timezone_id; abbr_standard; abbr_daylightsaving; name_standard; name_daylightsaving; use_db_abbreviations; use_db_names and fills object's attributes */ private function parse_data($timezone) { $timezone_array=explode(';', $timezone); /* first check if the size of the timezones-array match */ if (sizeof($timezone_array)!=7 && sizeof($timezone_array)!=1) throw new Exception('wrong number of parameters'); /* WordPress TimeZones Support */ if ($timezone_array[0]=='Local_WordPress_Time') $timezone_array[0]=get_option('timezone_string'); /* the timezone_id should contain at least one character */ if (strlen($timezone_array[0])<1) throw new Exception('no timezone-id given'); /* if only the timezone-id has been given, we use default parameters for the rest */ if (sizeof($timezone_array)==1) { $this->set_timezone_id($timezone_array[0]); $this->set_abbreviation_std(''); $this->set_abbreviation_dst(''); $this->set_name_std(''); $this->set_name_dst(''); $this->set_use_db_abbreviation(true); $this->set_use_db_name(true); return true; } /* are the last two array-parameters 0 or 1? */ if (!$timezone_array[5]==1 && !$timezone_array[5]==0) throw new Exception('wrong parameter used for database abbreviations'); if (!$timezone_array[6]==1 && !$timezone_array[6]==0) throw new Exception('wrong parameter used for database names'); $this->set_timezone_id($timezone_array[0]); $this->set_abbreviation_std($timezone_array[1]); $this->set_abbreviation_dst($timezone_array[2]); $this->set_name_std($timezone_array[3]); $this->set_name_dst($timezone_array[4]); $this->set_use_db_abbreviation($timezone_array[5] == 1 ? true : false); $this->set_use_db_name($timezone_array[6] == 1 ? true : false); return true; } /* checks if timezone is within DST sets boolean isdst, current offset and current abbreviation in array */ private function is_dst() { /* defaults */ $isDst=0; $abbr=$this->getName(); $offset=0; /* inspired from Derick's talk https://talks.php.net/show/time-ffm2006/28 lookup array until current transition has been found */ foreach (timezone_transitions_get($this) as $tr) { if ($tr['ts'] > $this->get_timestamp()) break; if((bool)$tr['isdst']===true) $isDst=1; else $isDst=0; $abbr=$tr['abbr']; $offset=$tr['offset']; } $this->set_is_dst($isDst == 1 ? true : false); $this->set_db_abbreviation($abbr); $this->set_offset($offset); } /* this methods returns the actual timestamp including all relevant descriptive data for the chosen timezone, for example as list-entry */ function format_timezone($params) { $name=htmlentities($this->get_name(), ENT_QUOTES, get_option('blog_charset'), false); $abbreviation=htmlentities($this->get_abbreviation(), ENT_QUOTES, get_option('blog_charset'), false); $datetime=date_i18n($params['format_datetime'], ($params['query_time']+ $this->get_offset()), true); /* build tag for html output */ return str_replace(array('%name', '%abbreviation', '%datetime'), array($name, $abbreviation, $datetime), $params['format_timezone']); } } /* WIDGET CLASS */ class WP_Widget_TimeZoneCalculator extends WP_Widget { /* constructor */ function __construct() { global $timezonecalculator; $widget_ops = array( 'classname' => 'widget_'.$timezonecalculator->get_prefix(false), 'description' => 'Calculates, displays and automatically updates times and dates in different timezones with respect to daylight saving.' ); parent::__construct($timezonecalculator->get_prefix(false), $timezonecalculator->get_nicename(), $widget_ops); } /* produces the widget-output */ function widget($args, $instance) { global $timezonecalculator; $title = !isset($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); $prefer_user_timezones=false; if (isset($instance['prefer_user_timezones'])) $prefer_user_timezones=$instance['prefer_user_timezones']; $params=array( 'use_container' => true, 'display' => false, 'prefer_user_timezones' => $prefer_user_timezones ); $timezones=$timezonecalculator->output($params); if (empty($timezones)) return; echo $args['before_widget']; echo $args['before_title'] . $title . $args['after_title']; echo $timezones; echo $args['after_widget']; } /* the backend-form */ function form($instance) { global $timezonecalculator; $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; $prefer_user_timezones = isset($instance['prefer_user_timezones']) ? $instance['prefer_user_timezones'] : ''; ?>