| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Timed Content |
| 4 |
Text Domain: timed-content |
| 5 |
Plugin URI: http://www.notabenemarketing.com/resources/wordpress-plugins/timed-content-plugin |
| 6 |
Description: Plugin to show or hide portions of a Page or Post at a specified time after loading. These actions can either be processed either server-side or client-side, depending on the desired effect. |
| 7 |
Author: K. Tough |
| 8 |
Version: 1.1 |
| 9 |
Author URI: http://www.notabenemarketing.com/resources/wordpress-plugins/timed-content-plugin |
| 10 |
*/ |
| 11 |
|
| 12 |
define( "TIMED_CONTENT_PLUGIN_URL", WP_PLUGIN_URL . '/timed-content' ); |
| 13 |
define( "TIMED_CONTENT_CLIENT_TAG", "timed-content-client" ); |
| 14 |
define( "TIMED_CONTENT_SERVER_TAG", "timed-content-server" ); |
| 15 |
define( "TIMED_CONTENT_ZERO_TIME", "1970-Jan-01 00:00:00 +000" ); // Start of Unix Epoch |
| 16 |
define( "TIMED_CONTENT_END_TIME", "2038-Jan-19 03:14:07 +000" ); // End of Unix Epoch |
| 17 |
/* translators: date/time format for debugging messages. */ |
| 18 |
define( "TIMED_CONTENT_DT_FORMAT", __( "l, F jS, Y, g:i:s A T (e)" , 'timed-content') ); |
| 19 |
|
| 20 |
|
| 21 |
if ( !class_exists( "timedContentPlugin" ) ) { |
| 22 |
class timedContentPlugin { |
| 23 |
|
| 24 |
function timedContentPlugin() { |
| 25 |
//constructor |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
function _date_diff( $date_1 = 0, $date_2 = 0 ) { |
| 30 |
// |
| 31 |
// Compares two timestamps and returns array with period differences (year, month, day, hour, minute, second) |
| 32 |
// http://ca2.php.net/manual/en/function.mktime.php#93914 |
| 33 |
// |
| 34 |
|
| 35 |
//check higher timestamp and switch if neccessary |
| 36 |
if ($date_1 < $date_2){ |
| 37 |
$temp = $date_2; |
| 38 |
$date_2 = $date_1; |
| 39 |
$date_1 = $temp; |
| 40 |
$diff['before'] = true; |
| 41 |
} |
| 42 |
else { |
| 43 |
$temp = $date_1; //temp can be used for day count if required |
| 44 |
$diff['before'] = false; |
| 45 |
} |
| 46 |
$date_1 = date_parse( date( "Y-m-d H:i:s", $date_1 ) ); |
| 47 |
$date_2 = date_parse( date( "Y-m-d H:i:s", $date_2 ) ); |
| 48 |
//seconds |
| 49 |
if ( $date_1['second'] >= $date_2['second'] ) { |
| 50 |
$diff['second'] = $date_1['second'] - $date_2['second']; |
| 51 |
} |
| 52 |
else { |
| 53 |
$date_1['minute']--; |
| 54 |
$diff['second'] = 60 - $date_2['second'] + $date_1['second']; |
| 55 |
} |
| 56 |
//minutes |
| 57 |
if ( $date_1['minute'] >= $date_2['minute'] ) { |
| 58 |
$diff['minute'] = $date_1['minute'] - $date_2['minute']; |
| 59 |
} |
| 60 |
else { |
| 61 |
$date_1['hour']--; |
| 62 |
$diff['minute'] = 60 - $date_2['minute'] + $date_1['minute']; |
| 63 |
} |
| 64 |
//hours |
| 65 |
if ( $date_1['hour'] >= $date_2['hour'] ) { |
| 66 |
$diff['hour'] = $date_1['hour'] - $date_2['hour']; |
| 67 |
} |
| 68 |
else { |
| 69 |
$date_1['day']--; |
| 70 |
$diff['hour'] = 24 - $date_2['hour'] + $date_1['hour']; |
| 71 |
} |
| 72 |
//days |
| 73 |
if ( $date_1['day'] >= $date_2['day'] ) { |
| 74 |
$diff['day'] = $date_1['day'] - $date_2['day']; |
| 75 |
} |
| 76 |
else { |
| 77 |
$date_1['month']--; |
| 78 |
$diff['day'] = date( "t", $temp ) - $date_2['day'] + $date_1['day']; |
| 79 |
} |
| 80 |
//months |
| 81 |
if ( $date_1['month'] >= $date_2['month'] ) { |
| 82 |
$diff['month'] = $date_1['month'] - $date_2['month']; |
| 83 |
} |
| 84 |
else { |
| 85 |
$date_1['year']--; |
| 86 |
$diff['month'] = 12 - $date_2['month'] + $date_1['month']; |
| 87 |
} |
| 88 |
//years |
| 89 |
$diff['year'] = $date_1['year'] - $date_2['year']; |
| 90 |
return $diff; |
| 91 |
} |
| 92 |
|
| 93 |
function clientShowHTML( $atts, $content = null ) { |
| 94 |
$show_attr = ""; |
| 95 |
$hide_attr = ""; |
| 96 |
extract( shortcode_atts( array( 'show' => '0:00:000' , 'hide' => '0:00:000' ), $atts ) ); |
| 97 |
|
| 98 |
// Initialize show/hide arguments |
| 99 |
$s_min = 0; $s_sec = 0; $s_fade = 0; |
| 100 |
$h_min = 0; $h_sec = 0; $h_fade = 0; |
| 101 |
@list( $s_min, $s_sec, $s_fade ) = explode( ":", $show ); |
| 102 |
@list( $h_min, $h_sec, $h_fade ) = explode( ":", $hide ); |
| 103 |
|
| 104 |
if ( ( (int)$s_min + (int)$s_sec ) > 0 ) |
| 105 |
$show_attr = "_show_" . $s_min . "_" . $s_sec . "_" . $s_fade; |
| 106 |
if ( ( (int)$h_min + (int)$h_sec ) > 0 ) |
| 107 |
$hide_attr = "_hide_" . $h_min . "_" . $h_sec . "_" . $h_fade; |
| 108 |
|
| 109 |
$the_class = TIMED_CONTENT_CLIENT_TAG . $show_attr . $hide_attr ; |
| 110 |
//die($the_class); |
| 111 |
$the_HTML = "<div class='" . $the_class . "'" . ( ( $show_attr != "" ) ? " style='display: none;'" : "" ) .">" . do_shortcode($content) . "</div>"; |
| 112 |
|
| 113 |
return $the_HTML; |
| 114 |
} |
| 115 |
|
| 116 |
function serverShowHTML( $atts, $content = null ) { |
| 117 |
extract( shortcode_atts( array( 'show' => TIMED_CONTENT_ZERO_TIME , 'hide' => TIMED_CONTENT_END_TIME, 'debug' => 'false' ), $atts ) ); |
| 118 |
$show_t = strtotime( $show ); |
| 119 |
$hide_t = strtotime( $hide ); |
| 120 |
$right_now_t = time(); |
| 121 |
$debug_header = ""; |
| 122 |
|
| 123 |
if ( $debug == "true" ) { |
| 124 |
$temp_tz = date_default_timezone_get(); |
| 125 |
date_default_timezone_set( get_option( 'timezone_string' ) ); |
| 126 |
|
| 127 |
$right_now = date_i18n( TIMED_CONTENT_DT_FORMAT, $right_now_t ); |
| 128 |
|
| 129 |
$show_diff = $this->_date_diff( $show_t, $right_now_t ); |
| 130 |
$show_diff_str = sprintf( _n( "%d year", "%d years", $show_diff['year'] , 'timed-content'), $show_diff['year'] ) . ", "; |
| 131 |
$show_diff_str .= sprintf( _n( "%d month", "%d months", $show_diff['month'] , 'timed-content'), $show_diff['month'] ) . ", "; |
| 132 |
$show_diff_str .= sprintf( _n( "%d day", "%d days", $show_diff['day'] , 'timed-content'), $show_diff['day'] ) . ", "; |
| 133 |
$show_diff_str .= sprintf( _n( "%d hour", "%d hours", $show_diff['hour'] , 'timed-content'), $show_diff['hour'] ) . ", "; |
| 134 |
$show_diff_str .= sprintf( _n( "%d minute", "%d minutes", $show_diff['minute'] , 'timed-content'), $show_diff['minute'] ) . ", "; |
| 135 |
$show_diff_str .= sprintf( _n( "%d second", "%d seconds", $show_diff['second'] , 'timed-content'), $show_diff['second'] ) . " "; |
| 136 |
$show_diff_str .= ( $show_diff['before'] == true ? __( "ago" , 'timed-content') : __( "from now" , 'timed-content') ); |
| 137 |
|
| 138 |
$hide_diff = $this->_date_diff( $hide_t, $right_now_t ); |
| 139 |
$hide_diff_str = sprintf( _n( "%d year", "%d years", $hide_diff['year'] , 'timed-content'), $hide_diff['year'] ) . ", " ; |
| 140 |
$hide_diff_str .= sprintf( _n( "%d month", "%d months", $hide_diff['month'] , 'timed-content'), $hide_diff['month'] ) . ", "; |
| 141 |
$hide_diff_str .= sprintf( _n( "%d day", "%d days", $hide_diff['day'] , 'timed-content'), $hide_diff['day'] ) . ", "; |
| 142 |
$hide_diff_str .= sprintf( _n( "%d hour", "%d hours", $hide_diff['hour'] , 'timed-content'), $hide_diff['hour'] ) . ", "; |
| 143 |
$hide_diff_str .= sprintf( _n( "%d minute", "%d minutes", $hide_diff['minute'] , 'timed-content'), $hide_diff['minute'] ) . ", "; |
| 144 |
$hide_diff_str .= sprintf( _n( "%d second", "%d seconds", $hide_diff['second'] , 'timed-content'), $hide_diff['second'] ) . " "; |
| 145 |
$hide_diff_str .= ( $hide_diff['before'] == true ? __( "ago" , 'timed-content') : __( "from now" , 'timed-content') ); |
| 146 |
|
| 147 |
$debug_header = "<!-- " . __( "START TIMED-CONTENT-SERVER DEBUGGING" , 'timed-content') . " -->\n<!--\n\n"; |
| 148 |
|
| 149 |
if ( $show == TIMED_CONTENT_ZERO_TIME ) |
| 150 |
$debug_header .= " " . __( "'Show' attribute not set." , 'timed-content') . "\n"; |
| 151 |
else |
| 152 |
$debug_header .= " " . __( "'Show' attribute" , 'timed-content') . " : " . $show . ",\n " . __( "Timestamp derived from 'Show' attribute" , 'timed-content') . " = " . $show_t . ",\n " . __( "Date/time derived from timestamp" , 'timed-content') . " : " . date_i18n(TIMED_CONTENT_DT_FORMAT, $show_t) . ". " . $show_diff_str . ". \n\n"; |
| 153 |
|
| 154 |
if ( $hide == TIMED_CONTENT_END_TIME ) |
| 155 |
$debug_header .= " " . __( "'Hide' attribute not set." , 'timed-content') . "\n"; |
| 156 |
else |
| 157 |
$debug_header .= " " . __( "'Hide' attribute" , 'timed-content') . " : " . $hide . ",\n " . __( "Timestamp derived from 'Hide' attribute" , 'timed-content') . " = " . $hide_t . ",\n " . __( "Date/time derived from timestamp" , 'timed-content') . " : " . date_i18n(TIMED_CONTENT_DT_FORMAT, $hide_t) . ". " . $hide_diff_str . ". \n\n"; |
| 158 |
|
| 159 |
$debug_header .= " " . __( "Current time" , 'timed-content') . " : " . $right_now . ", " . __( "Timestamp derived from current time" , 'timed-content') . " = " . $right_now_t . "\n"; |
| 160 |
|
| 161 |
$debug_header .= " " . __( "Wordpress option" , 'timed-content') . " 'gmt_offset' : " . get_option( 'gmt_offset' ) . "\n"; |
| 162 |
$debug_header .= " " . __( "Wordpress option" , 'timed-content') . " 'timezone_string' : " . get_option( 'timezone_string' ) . "\n\n"; |
| 163 |
|
| 164 |
|
| 165 |
$debug_header .= "-->\n<!-- " . __( "END TIMED-CONTENT-SERVER DEBUGGING" , 'timed-content') . " -->\n"; |
| 166 |
date_default_timezone_set( $temp_tz ); |
| 167 |
|
| 168 |
} |
| 169 |
$content_header = "<!-- " . __( "START TIMED-CONTENT-SERVER CONTENT" , 'timed-content') . " -->\n"; |
| 170 |
$content_footer = "<!-- " . __( "END TIMED-CONTENT-SERVER CONTENT" , 'timed-content') . " -->\n"; |
| 171 |
|
| 172 |
if ( ( $show_t <= $right_now_t ) && ( $right_now_t <= $hide_t ) ) |
| 173 |
$the_HTML = $debug_header . do_shortcode($content) . "\n"; |
| 174 |
else |
| 175 |
$the_HTML = $debug_header; |
| 176 |
|
| 177 |
return $content_header . $the_HTML . $content_footer; |
| 178 |
} |
| 179 |
|
| 180 |
function addHeaderCode() { |
| 181 |
global $wp_query; |
| 182 |
if ( ! is_admin() ) |
| 183 |
{ |
| 184 |
foreach ( $wp_query->posts as $post ) { |
| 185 |
$pattern = get_shortcode_regex(); |
| 186 |
preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches, PREG_SET_ORDER ); |
| 187 |
if ( is_array( $matches ) ) { |
| 188 |
foreach ( $matches as $m ) { |
| 189 |
if ( $m[2] == TIMED_CONTENT_CLIENT_TAG ) { |
| 190 |
//shortcode is being used; enqueue script and get out (script can handle multiple uses of the shortcode already) |
| 191 |
wp_enqueue_script( 'timed-content_js', TIMED_CONTENT_PLUGIN_URL . '/js/timed-content.js', array( 'jquery' ), '1.0' ); |
| 192 |
break 2; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
function initTinyMCEPlugin() { |
| 201 |
if ( ( ! current_user_can( 'edit_posts' ) ) && ( ! current_user_can( 'edit_pages' ) ) ) |
| 202 |
return; |
| 203 |
|
| 204 |
// Add only in Rich Editor mode |
| 205 |
if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 206 |
add_filter( "mce_external_plugins", array( &$this, "addTimedContentTinyMCEPlugin" ) ); |
| 207 |
add_filter( "mce_buttons", array( &$this, "registerTinyMCEButton" ) ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
function registerTinyMCEButton( $buttons ) { |
| 214 |
array_push( $buttons, "|", "timed_content" ); |
| 215 |
return $buttons; |
| 216 |
} |
| 217 |
|
| 218 |
// Load the TinyMCE plugin : editor_plugin.js (wp2.5) |
| 219 |
function addTimedContentTinyMCEPlugin( $plugin_array ) { |
| 220 |
$plugin_array['timed_content'] = TIMED_CONTENT_PLUGIN_URL . "/tinymce_plugin/editor_plugin_src.js"; |
| 221 |
return $plugin_array; |
| 222 |
} |
| 223 |
|
| 224 |
function i18nInit() { |
| 225 |
$plugin_dir = basename( dirname( __FILE__ ) ) . "/lang/"; |
| 226 |
load_plugin_textdomain( 'timed-content', null, $plugin_dir ); |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
} //End Class timedContentPlugin |
| 232 |
|
| 233 |
// Initialize plugin |
| 234 |
if ( class_exists( "timedContentPlugin" ) ) { |
| 235 |
$timedContentPluginInstance = new timedContentPlugin(); |
| 236 |
} |
| 237 |
|
| 238 |
// Actions and Filters |
| 239 |
if ( isset( $timedContentPluginInstance ) ) { |
| 240 |
add_action( "init", array( &$timedContentPluginInstance, "i18nInit" ), 1 ); |
| 241 |
add_action( "wp_head", array( &$timedContentPluginInstance, "addHeaderCode" ), 1 ); |
| 242 |
add_action( "admin_init", array( &$timedContentPluginInstance, "initTinyMCEPlugin" ), 1 ); |
| 243 |
add_shortcode( TIMED_CONTENT_CLIENT_TAG, array( &$timedContentPluginInstance, "clientShowHTML" ), 1 ); |
| 244 |
add_shortcode( TIMED_CONTENT_SERVER_TAG, array( &$timedContentPluginInstance, "serverShowHTML" ), 1 ); |
| 245 |
} |
| 246 |
?> |