Created
September 13, 2013 15:50
-
-
Save Otto42/6552471 to your computer and use it in GitHub Desktop.
Time shortcode used for make p2's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin: Time Shortcode | |
* Author: Otto | |
* | |
* Usage: [time]any-valid-time-string-here[/time] | |
* Will attempt to parse the time string and create a format that shows it in the viewers local time zone | |
* Note that times should be complete with year, month, day, and hour and minute.. something strtotime can parse meaningfully | |
* Conversion happens via Javascript and will depend on the users browser. | |
**/ | |
add_action('init','time_shortcode_init'); | |
function time_shortcode_init() { | |
if ( ! class_exists( 'P2' ) ) | |
return; | |
add_shortcode('time','time_shortcode'); | |
} | |
function time_shortcode( $attr, $content = null ) { | |
$post = get_post(); | |
// try to parse the time, relative to the post time | |
$time = strtotime($content, get_the_date( 'U' ) ); | |
// if strtotime doesn't work, try stronger measures | |
if ( $time === false || $time === -1 ) { | |
$timearray = date_parse($content); | |
foreach ( $timearray as $key => $value ) { | |
if ( $value === false ) unset ( $timearray[$key] ); | |
} | |
//merge the info from the post date with this one | |
$posttime = date_parse( $post->post_date ); | |
$timearray = array_merge( $posttime, $timearray ); | |
// use the blog's timezone if none was specified | |
if ( !isset( $timearray['tz_id'] ) ) { | |
$timearray['tz_id'] = get_option('timezone_string'); | |
} | |
// build a normalized time string, then parse it to an integer time using strtotime | |
$time = strtotime("{$timearray['year']}-{$timearray['month']}-{$timearray['day']}T{$timearray['hour']}:{$timearray['minute']}:{$timearray['second']} {$timearray['tz_id']}"); | |
} | |
// if that didn't work, give up | |
if ( $time === false || $time === -1 ) { | |
return $content; | |
} | |
// build the link and abbr microformat | |
$out = '<a href="http://www.timeanddate.com/worldclock/fixedtime.html?iso='.gmdate('Ymd\THi', $time).'"><abbr class="date" title="'.gmdate('c', $time).'">'.$content.'</abbr></a>'; | |
// add the time converter JS code | |
add_action('wp_footer','time_converter_script'); | |
// return the new link | |
return $out; | |
} | |
function time_converter_script() { | |
$timestrings = array( | |
'months' => array(__('January'), __('February'), __('March'),__('April'),__('May'),__('June'),__('July'),__('August'),__('September'),__('October'),__('November'),__('December')), | |
'days' => array( __('Sunday'),__('Monday'),__('Tuesday'),__('Wednesday'),__('Thursday'),__('Friday'),__('Saturday')), | |
); | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready( function ($) { | |
var timestrings = <?php echo json_encode($timestrings); ?> | |
var parse_date = function (text) { | |
var m = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})\+00:00$/.exec(text); | |
var d = new Date(); | |
d.setUTCFullYear(+m[1]); | |
d.setUTCDate(+m[3]); | |
d.setUTCMonth(+m[2]-1); | |
d.setUTCHours(+m[4]); | |
d.setUTCMinutes(+m[5]); | |
d.setUTCSeconds(+m[6]); | |
return d; | |
} | |
var format_date = function (d) { | |
var p = function(n) {return ('00'+n).slice(-2); }; | |
var tz = -d.getTimezoneOffset()/60; | |
if (tz>=0) { tz = "+"+tz; } | |
return ""+timestrings['days'][d.getDay()]+", "+timestrings['months'][d.getMonth()]+" "+p(d.getDate())+", "+d.getFullYear()+" "+p(d.getHours())+":"+p(d.getMinutes())+" UTC"+tz; | |
} | |
var nodes = document.getElementsByTagName('abbr'); | |
for (var i=0; i<nodes.length; ++i) { | |
var node = nodes[i]; | |
if (node.className === 'date') { | |
var d = parse_date(node.getAttribute('title')); | |
if (d) { | |
node.textContent = format_date(d); | |
} | |
} | |
} | |
}); | |
</script> | |
<?php | |
} | |
add_filter('comment_text', 'time_shortcode_in_comments'); | |
function time_shortcode_in_comments($comment) { | |
global $shortcode_tags; | |
// save the shortcodes | |
$saved_tags = $shortcode_tags; | |
// only process the time shortcode | |
unset($shortcode_tags); | |
$shortcode_tags['time']='time_shortcode'; | |
// do the time shortcode on the comment | |
$comment = do_shortcode($comment); | |
// restore the normal shortcodes | |
$shortcode_tags = $saved_tags; | |
return $comment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment