PluginProbe
Timed Content / 2.1.1
Timed Content v2.1.1
2.99 trunk 1.0 1.1 1.2 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.10 2.11 2.12 2.15 2.2 2.3 2.3.1 2.4 2.5 2.5.1 2.50 2.51 2.52 All 67 releases
timed-content / timed-content.php

timed-content.php in Timed Content 2.1.1, at timed-content.php

1,129 lines 61.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Timed Content
4 Text Domain: timed-content
5 Plugin URI: http://wordpress.org/plugins/timed-content/
6 Description: Plugin to show or hide portions of a Page or Post based on specific date/time characteristics. These actions can either be processed either server-side or client-side, depending on the desired effect.
7 Author: K. Tough
8 Version: 2.1.1
9 Author URI: http://wordpress.org/plugins/timed-content/
10 */
11 if ( !class_exists( "timedContentPlugin" ) ) {
12
13 define( "TIMED_CONTENT_VERSION", "2.1.1" );
14 define( "TIMED_CONTENT_PLUGIN_URL", plugins_url() . '/timed-content' );
15 define( "TIMED_CONTENT_CLIENT_TAG", "timed-content-client" );
16 define( "TIMED_CONTENT_SERVER_TAG", "timed-content-server" );
17 define( "TIMED_CONTENT_RULE_TAG", "timed-content-rule" );
18 define( "TIMED_CONTENT_ZERO_TIME", "1970-Jan-01 00:00:00 +000" ); // Start of Unix Epoch
19 define( "TIMED_CONTENT_END_TIME", "2038-Jan-19 03:14:07 +000" ); // End of Unix Epoch
20 /* translators: date/time format for debugging messages. */
21 define( "TIMED_CONTENT_DT_FORMAT", __( "l, F jS, Y, g:i A T" , 'timed-content' ) );
22 define( "TIMED_CONTENT_RULE_TYPE", "timed_content_rule" );
23 define( "TIMED_CONTENT_RULE_POSTMETA_PREFIX", TIMED_CONTENT_RULE_TYPE . "_" );
24 define( "TIMED_CONTENT_CSS", TIMED_CONTENT_PLUGIN_URL . "/css/timed-content.css" );
25 // Required for styling the JQuery UI Datepicker and JQuery UI Timepicker
26 define( "TIMED_CONTENT_JQUERY_UI_CSS", TIMED_CONTENT_PLUGIN_URL . "/css/jqueryui/1.10.3/themes/smoothness/jquery-ui.css" );
27 define( "TIMED_CONTENT_JQUERY_UI_TIMEPICKER_JS", TIMED_CONTENT_PLUGIN_URL."/js/jquery-ui-timepicker-0.3.3/jquery.ui.timepicker.js" );
28 define( "TIMED_CONTENT_JQUERY_UI_TIMEPICKER_CSS", TIMED_CONTENT_PLUGIN_URL."/js/jquery-ui-timepicker-0.3.3/jquery.ui.timepicker.css" );
29 require_once( "lib/customFields-settings.php" );
30 require_once( "lib/customFieldsInterface.php" );
31
32
33 /**
34 * Class timedContentPlugin
35 *
36 * Class that contains all of the functions required to run the plugin. This cuts down on
37 * the possibility of name collisions with other plugins.
38 */
39 class timedContentPlugin {
40
41 function timedContentPlugin() {
42 //constructor
43
44 }
45
46 /**
47 * Creates the Timed Content Rule post type and registers it with Wordpress
48 *
49 */
50 function timedContentRuleTypeInit()
51 {
52 $labels = array(
53 'name' => _x( 'Timed Content Rules', 'post type general name', 'timed-content' ),
54 'singular_name' => _x( 'Timed Content Rule', 'post type singular name', 'timed-content' ),
55 'add_new' => _x( 'Add New', TIMED_CONTENT_RULE_TYPE, 'timed-content' ),
56 'add_new_item' => __( 'Add New Timed Content Rule', 'timed-content' ),
57 'edit_item' => __( 'Edit Timed Content Rule', 'timed-content' ),
58 'new_item' => __( 'New Timed Content Rule', 'timed-content' ),
59 'view_item' => __( 'View Timed Content Rule', 'timed-content' ),
60 'search_items' => __( 'Search Timed Content Rules', 'timed-content' ),
61 'not_found' => __( 'No Timed Content Rules found', 'timed-content' ),
62 'not_found_in_trash' => __( 'No Timed Content Rules found in Trash', 'timed-content' ),
63 'parent_item_colon' => '',
64 'menu_name' =>_x( 'Timed Content Rules', 'post type general name', 'timed-content' )
65 );
66 $args = array(
67 'labels' => $labels,
68 'description' => __( 'Create regular schedules to show or hide selected content in a Page or Post.', 'timed-content' ),
69 'public' => false,
70 'publicly_queryable' => false,
71 'exclude_from_search' => false,
72 'show_ui' => true,
73 'show_in_menu' => true,
74 'show_in_nav_menus' => true,
75 'show_in_admin_bar' => true,
76 'query_var' => false,
77 'rewrite' => false,
78 'capability_type' => 'post',
79 'has_archive' => false,
80 'hierarchical' => false,
81 'menu_position' => 5,
82 'supports' => array( 'title' )
83 );
84 register_post_type( TIMED_CONTENT_RULE_TYPE, $args );
85 }
86
87
88 /**
89 * Filter to customize CRUD messages for Timed Content Rules
90 *
91 * @param array $messages Array of currently defined messages for post types
92 * @return mixed Array of messages with appropriate messages for Timed Content Rules added in
93 */
94 function timedContentRuleUpdatedMessages( $messages ) {
95 global $post;
96 // global $post_ID;
97
98 $messages[TIMED_CONTENT_RULE_TYPE] = array(
99 0 => '', // Unused. Messages start at index 1.
100 1 => __( 'Timed Content Rule updated.', 'timed-content' ),
101 2 => __( 'Custom field updated.', 'timed-content' ),
102 3 => __( 'Custom field deleted.', 'timed-content' ),
103 4 => __( 'Timed Content Rule updated.', 'timed-content' ),
104 /* translators: %s: date and time of the revision */
105 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Timed Content Rule restored to revision from %s', 'timed-content' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
106 6 => __( 'Timed Content Rule published.', 'timed-content' ),
107 7 => __( 'Timed Content Rule saved.', 'timed-content' ),
108 8 => __( 'Timed Content Rule submitted.', 'timed-content' ),
109 /* translators: %s: date and time to activate rule */
110 9 => sprintf( __('Timed Content Rule scheduled for: <strong>%1$s</strong>.', 'timed-content' ), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ),
111 10 => __( 'Timed Content Rule draft updated.', 'timed-content' )
112 );
113
114 return $messages;
115 }
116
117 /**
118 * Advances a date/time by a set number of days
119 *
120 * @param int $current UNIX timestamp of the date/time before incrementing
121 * @param int $interval_multiplier Number of days to advance
122 * @return int Unix timestamp of the new date/time
123 */
124 function __getNextDay( $current, $interval_multiplier ) {
125 return strtotime( $interval_multiplier . " day", $current );
126 }
127
128 /**
129 * Advances a date/time by a set number of hours
130 *
131 * @param int $current UNIX timestamp of the date/time before incrementing
132 * @param int $interval_multiplier Number of hours to advance
133 * @return int Unix timestamp of the new date/time
134 */
135 function __getNextHour( $current, $interval_multiplier ) {
136 return strtotime( $interval_multiplier . " hour", $current );
137 }
138
139 /**
140 * Advances a date/time by a set number of weeks
141 *
142 * Advances a date/time by a set number of weeks. If given an array of days of the week, this function will
143 * advance the date/time to the next day in that array in the jumped-to week. Use this function if you're
144 * repeating an action on specific days of the week (i.e., on Weekdays, Tuesdays and Thursdays, etc.).
145 *
146 * @param int $current UNIX timestamp of the date/time before incrementing
147 * @param int $interval_multiplier Number of weeks to advance
148 * @param array $days Array of integers symbolizing the days of the week to
149 * repeat on (0 - Sunday, 1 - Monday, ..., 6 - Saturday).
150 * @return int Unix timestamp of the new date/time
151 */
152 function __getNextWeek( $current, $interval_multiplier, $days = array() ) {
153 // If $days is empty, advance $interval_multiplier weeks from $current and return the timestamp
154 if ( empty( $days ) ) return strtotime( $interval_multiplier . " week", $current );
155
156 // Otherwise, set up an array combining the days of the week to repeat on and the current day
157 // (keys and values of the array will be the same, and the array is sorted)
158 $currentDayOfWeekIndex = date( "w", $current );
159 sort( array_values( array_unique( array_merge( array( $currentDayOfWeekIndex ), $days ) ) ) );
160 $daysOfWeek = array_combine( $days, $days );
161
162 // If the current day is the last one of the days of the week to repeat on, jump ahead to
163 // the next week to be repeating on and get the earliest day in the array
164 if ( $currentDayOfWeekIndex == max( $daysOfWeek ) )
165 $pattern = ( ( 7 - $currentDayOfWeekIndex ) + ( 7 * ( $interval_multiplier - 1 ) ) + ( min( array_keys( $daysOfWeek ) ) ) ) . " day";
166 // Otherwise, cycle through the array until we find the next day to repeat on
167 else {
168 $nextDayOfWeekIndex = $currentDayOfWeekIndex;
169 do {} while ( !isset( $daysOfWeek[++$nextDayOfWeekIndex] ) );
170 $pattern = ( $nextDayOfWeekIndex - $currentDayOfWeekIndex ) . " day";
171 }
172 return strtotime( $pattern, $current );
173 }
174
175 /**
176 * Advances a date/time by a set number of months
177 *
178 * Advances a date/time by a set number of months. When the date/time of the first active period lies
179 * on the 29th, 30th, or 31st of the month, this function will return a date/time on the the last day
180 * of the month for those months not containing those days.
181 *
182 * @param int $current UNIX timestamp of the date/time before incrementing
183 * @param int $start UNIX timestamp of the first active period's date/time
184 * @param int $interval_multiplier Number of months to advance
185 * @return int Unix timestamp of the new date/time
186 */
187 function __getNextMonth( $current, $start, $interval_multiplier ) {
188
189 // For most days in the month, it's pretty easy. Get the day of month of the starting date.
190 $startDay = date( "j", $start );
191
192 // If it's before or on the 28th, just jump the number of months and be done with it.
193 if ( $startDay <= 28 )
194 return strtotime( $interval_multiplier . " month", $current );
195
196 // If it's on the 29th, 30th, or 31st, it gets tricky. Some months don't have those days - so on those
197 // months we need to repeat on the last day of the month instead, but we also need to jump back to the
198 // correct day the following month. Let's say we want to repeat something on the 31st every month: this
199 // is what we expect to see for a pattern:
200 //
201 // .
202 // .
203 // .
204 // December 31st
205 // January 31st
206 // February 28th
207 // March 31st
208 // April 30th
209 // .
210 // .
211 // .
212 //
213 // Unfortunately, PHP relative date handling isn't that smart (add "+1 month" to January 31st, and you
214 // end up in March), so we'll have to figure it out ourselves by figuring out how many days to jump instead.
215
216 // We'll need to calculate this for each interval and return the timestamp after the last jump.
217 $temp_current = $current;
218 for ( $i = 0; $i < $interval_multiplier; $i++ ) {
219 // The pattern for jumping will be different in each interval.
220 /** @noinspection PhpUnusedLocalVariableInspection */
221 $temp_pattern = "";
222
223 // Get the month number of the current date.
224 //$currentMonth = date( "n", $temp_current );
225
226 // Get the number of days in the month of the current date.
227 $lastDayThisMonth = date( "t", strtotime( "this month", $temp_current ) );
228
229 // Get the number of days for the next month relative to the current date .
230 // Subtract 3 days from the next month to counter known month skipping bugs in PHP's relative date
231 // handling, that being the difference between the shortest possible month (non-leap February - 28 days)
232 // and the longest (Jan., Mar., May, Jul., Aug., Oct., Dec. - 31 days). This may be fixed in PHP 5.3.x
233 // but this should be backwards-compatible anyway.
234 $lastDayNextMonth = date( "t", strtotime( "-3 day next month", $temp_current ) );
235
236 // If the current month is longer than next month, follow this block
237 if ( $lastDayThisMonth > $lastDayNextMonth ) {
238 // If we're repeating on the last day of this month, jump the number of days next month
239 if ( $startDay == $lastDayThisMonth )
240 $temp_pattern = $lastDayNextMonth . " days";
241 // If the start day doesn't exist in the next month (i.e., no "31st" in June), jump the
242 // number of days next month plus the difference between the start day and the number of days this month
243 elseif ( $startDay > $lastDayNextMonth )
244 $temp_pattern = ( $lastDayThisMonth + $lastDayNextMonth - $startDay ). " days";
245 // Otherwise, jump ahead the number of days in this month
246 else
247 $temp_pattern = $lastDayThisMonth . " days";
248 }
249 // Or, if the current month is shorter than next month
250 elseif ( $lastDayThisMonth < $lastDayNextMonth ) {
251 // If the start day doesn't exist in this month (i.e., no "31st" in June), jump the
252 // number of days next month plus the difference between the start day and the number of days this month
253 if ( $startDay >= $lastDayThisMonth )
254 $temp_pattern = $startDay . " days";
255 // Otherwise, jump ahead the number of days in this month
256 else
257 $temp_pattern = $lastDayThisMonth . " days";
258 }
259 // If the current month and next month are equally long, jumping by "1 month" is fine
260 else
261 $temp_pattern = "1 month";
262
263 $temp_current = strtotime( $temp_pattern, $temp_current );
264 }
265 return $temp_current;
266
267 }
268
269 /**
270 * Advances a date/time to the 'n'th weekday of the next month (eg., first Wednesday, third Monday, last Friday, etc.).
271 *
272 * NB: if $ordinal is set to '4' and $day is set to '7', it wil return the last day of the month.
273 *
274 * @param int $current UNIX timestamp of the date/time before incrementing
275 * @param int $ordinal Integer symbolizing the ordinal (0 - first, 1 - second, 2 - third, 3 - fourth, 4 - last)
276 * @param int $day integers symbolizing the days of the week to
277 * repeat on (0 - Sunday, 1 - Monday, ..., 6 - Saturday, 7 - day).
278 * @return int Unix timestamp of the new date/time
279 */
280 function __getNthWeekdayOfMonth( $current, $ordinal, $day ) {
281
282 // First, get the month/year we need to work with
283 $the_month = date( "F", $current );
284 $the_year = date( "Y", $current );
285 $lastDayThisMonth = date( "t", $current );
286
287 // Get the time for the $current timestamp
288 $current_time = date( "g:i A", $current );
289 $the_day = "";
290
291 if ( $day == 7 ) { // If $day is "day of the month", get the day of month based on the ordinal
292 switch ( $ordinal ) {
293 case 1 : $the_day = "1"; break; // First day of the month //
294 case 2 : $the_day = "2"; break; // Second day of the month //
295 case 3 : $the_day = "3"; break; // Third day of the month //
296 case 4 : $the_day = "4"; break; // Fourth day of the month //
297 case 5 : $the_day = $lastDayThisMonth; break; // Last day of the month //
298 default : $the_day = "1"; break;
299 }
300 } else { // If $day is one of the days of the week...
301 $day_range = array();
302 switch ( $ordinal ) { // ...get a 7-day range based on the ordinal...
303 case 1 : $day_range = range( 1, 7 ); break; // First 7 days of the month //
304 case 2 : $day_range = range( 8, 14 ); break; // Second 7 days of the month //
305 case 3 : $day_range = range( 15, 21 ); break; // Third 7 days of the month //
306 case 4 : $day_range = range( 22, 28 ); break; // Fourth 7 days of the month //
307 case 5 : $day_range = range( $lastDayThisMonth - 6, $lastDayThisMonth ); break; // Last 7 days of the month //
308 default : $day_range = range( 1, 7 ); break;
309 }
310 foreach ( $day_range as $a_day ) { // ...and find the matching weekday in that range.
311 if ( $day == date( "l", strtotime( $the_month . " " . $a_day . ", " . $the_year ) ) ) {
312 $the_day = $a_day;
313 break;
314 }
315 }
316 }
317
318 // Build the date/time string for the correct day and return its timestamp
319 $pattern = $the_month . " " . $the_day . ", " . $the_year . ", " . $current_time;
320 return strtotime( $pattern );
321
322 }
323
324 /**
325 * Advances a date/time by a set number of years
326 *
327 * @param int $current UNIX timestamp of the date/time before incrementing
328 * @param int $interval_multiplier Number of years to advance
329 * @return int Unix timestamp of the new date/time
330 */
331 function __getNextYear( $current, $interval_multiplier ) {
332 return strtotime( $interval_multiplier . " year", $current );
333 }
334
335 /**
336 * Validates the various Timed Content Rule parameters and returns a series of error messages.
337 *
338 * @param $args Array of Timed Content Rule parameters
339 * @return array Array of error messages
340 */
341 function __validate( $args ) {
342 $errors = array();
343
344 $instance_start = strtotime( $args['instance_start']['date'] . '' . $args['instance_start']['time'] . ' ' . $args['timezone'] );
345 $instance_end = strtotime( $args['instance_end']['date'] . '' . $args['instance_end']['time'] . ' ' . $args['timezone'] );
346 $end_date = strtotime( $args['end_date'] . '' . $args['instance_start']['time'] . ' ' . $args['timezone'] );
347
348 if ( $args['instance_start']['date'] == "" )
349 $errors[] = __( "Date in Starting Date/Time must not be empty.", 'timed-content' );
350 if ( $args['instance_start']['time'] == "" )
351 $errors[] = __( "Time in Starting Date/Time must not be empty.", 'timed-content' );
352 if ( $args['instance_end']['date'] == "" )
353 $errors[] = __( "Date in Ending Date/Time must not be empty.", 'timed-content' );
354 if ( $args['instance_end']['time'] == "" )
355 $errors[] = __( "Time in Ending Date/Time must not be empty.", 'timed-content' );
356 if ( $args['interval_multiplier'] == "" )
357 $errors[] = __( "Repeat How Often? must not be empty.", 'timed-content' );
358 if ( !is_numeric( $args['interval_multiplier'] ) )
359 $errors[] = __( "Repeat How Often? must be a number.", 'timed-content' );
360 if ( ( $args['num_repeat'] == "" ) && ( $args['recurr_type'] == "recurrence_duration_num_repeat" ) )
361 $errors[] = __( "Repeat How Many Times? must not be empty.", 'timed-content' );
362 if ( ( !is_numeric( $args['num_repeat'] ) ) && ( $args['recurr_type'] == "recurrence_duration_num_repeat" ) )
363 $errors[] = __( "Repeat How Many Times? must be a number.", 'timed-content' );
364 if ( ( $args['end_date'] == "" ) && ( $args['recurr_type'] == "recurrence_duration_end_date" ) )
365 $errors[] = __( "End Date must not be empty.", 'timed-content' );
366 if ( false === $instance_start )
367 $errors[] = __( "Starting Date/Time must be valid.", 'timed-content' );
368 if ( false === $instance_end )
369 $errors[] = __( "Ending Date/Time must be valid.", 'timed-content' );
370 if ( $instance_start > $instance_end )
371 $errors[] = __( "Starting Date/Time must be before Ending Date/Time.", 'timed-content' );
372 if ( ( $instance_end > $end_date ) && ( $args['recurr_type'] == "recurrence_duration_end_date" ) )
373 $errors[] = __( "End Date must be after Ending Date/Time.", 'timed-content' );
374
375 return $errors;
376 }
377
378 /**
379 * Calculates the active periods for a Timed Content Rule
380 *
381 * @param $args Array of Timed Content Rule parameters
382 * @return array Array of active periods. Each value in the array describes an active period as
383 * an array itself with "start" and "end" keys and values that are either UNIX
384 * timestamps or human-readable dates, based on whether $args['human_readable']
385 * is set to true or false.
386 */
387 function __getRulePeriods( $args ) {
388 $active_periods = array();
389 $period_count = 0;
390
391 $human_readable = $args['human_readable'];
392 $freq = $args['freq'];
393 $timezone = $args['timezone'];
394 $recurr_type = $args['recurr_type'];
395 $num_repeat = intval( $args['num_repeat'] );
396 $end_date = $args['end_date'];
397 $days_of_week = $args['days_of_week'];
398 $interval_multiplier = $args['interval_multiplier'];
399 $instance_start_date = $args['instance_start']['date'];
400 $instance_start_time = $args['instance_start']['time'];
401 $instance_end_date = $args['instance_end']['date'];
402 $instance_end_time = $args['instance_end']['time'];
403 $monthly_pattern = $args['monthly_pattern'];
404 $monthly_pattern_ord = $args['monthly_pattern_ord'];
405 $monthly_pattern_day = $args['monthly_pattern_day'];
406 //print_r($days_of_week);
407
408 $temp_tz = date_default_timezone_get();
409 date_default_timezone_set( $timezone );
410 $right_now_t = time();
411
412 $instance_start = strtotime( $instance_start_date . " " . $instance_start_time . " " . $timezone ); // Beginning of first occurrence
413 $instance_end = strtotime( $instance_end_date . " " . $instance_end_time . " " . $timezone ); // End of first occurrence
414 $current = $instance_start;
415 $end_current = $instance_end;
416
417 if ( $recurr_type == "recurrence_duration_num_repeat" )
418 $last_occurrence_start = strtotime( TIMED_CONTENT_END_TIME );
419 else
420 $last_occurrence_start = strtotime( $end_date . " " . $instance_start_time . " " . $timezone );
421
422 if ( $human_readable == true ) {
423 $active_periods[$period_count]["start"] = date( TIMED_CONTENT_DT_FORMAT, $current );
424 $active_periods[$period_count]["end"] = date( TIMED_CONTENT_DT_FORMAT, $end_current );
425 } else {
426 $active_periods[$period_count]["start"] = $current;
427 $active_periods[$period_count]["end"] = $end_current;
428 }
429 if ( $right_now_t < $current ) {
430 $active_periods[$period_count]["status"] = "upcoming";
431 $active_periods[$period_count]["time"] = sprintf( __( '%s from now.', 'timed-content' ), human_time_diff( $current, $right_now_t ) );
432 } elseif ( ( $current <= $right_now_t ) && ( $right_now_t <= $end_current ) ) {
433 $active_periods[$period_count]["status"] = "active";
434 $active_periods[$period_count]["time"] = __( "Right now!", 'timed-content' );
435 } else {
436 $active_periods[$period_count]["status"] = "expired";
437 $active_periods[$period_count]["time"] = sprintf( __( '%s ago.', 'timed-content' ), human_time_diff( $end_current, $right_now_t ) );
438 }
439 $period_count++;
440
441 if ( $recurr_type == "recurrence_duration_end_date" )
442 $loop_test = "return ( \$current < \$last_occurrence_start );";
443 else
444 $loop_test = "return ( \$period_count <= \$num_repeat );";
445
446 while ( eval ( $loop_test ) ) {
447 $temp_current = "";
448 if ( $freq == 0 )
449 $current = $this->__getNextHour( $current, $interval_multiplier );
450 elseif ( $freq == 1 )
451 $current = $this->__getNextDay( $current, $interval_multiplier );
452 elseif ( $freq == 2 )
453 $current = $this->__getNextWeek( $current, $interval_multiplier, $days_of_week );
454 elseif ( $freq == 3 ) {
455 $current = $this->__getNextMonth( $current, $instance_start, $interval_multiplier );
456 $temp_current = $current;
457 if ( $monthly_pattern == "yes" ) $current = $this->__getNthWeekdayOfMonth( $current, $monthly_pattern_ord, $monthly_pattern_day );
458 } elseif ( $freq == 4 )
459 $current = $this->__getNextYear( $current, $interval_multiplier );
460
461 if ( eval ( $loop_test ) ) {
462 $end_current = $current + ( $instance_end - $instance_start );
463 if ( $human_readable == true ) {
464 $active_periods[$period_count]["start"] = date( TIMED_CONTENT_DT_FORMAT, $current );
465 $active_periods[$period_count]["end"] = date( TIMED_CONTENT_DT_FORMAT, $end_current );
466 } else {
467 $active_periods[$period_count]["start"] = $current;
468 $active_periods[$period_count]["end"] = $end_current;
469 }
470 if ( $right_now_t < $current ) {
471 $active_periods[$period_count]["status"] = "upcoming";
472 $active_periods[$period_count]["time"] = sprintf( _x( '%s from now.', 'Human readable time difference', 'timed-content' ), human_time_diff( $current, $right_now_t ) );
473 } elseif ( ( $current <= $right_now_t ) && ( $right_now_t <= $end_current ) ) {
474 $active_periods[$period_count]["status"] = "active";
475 $active_periods[$period_count]["time"] = __( "Right now!", 'timed-content' );
476 } else {
477 $active_periods[$period_count]["status"] = "expired";
478 $active_periods[$period_count]["time"] = sprintf( _x( '%s ago.', 'Human readable time difference', 'timed-content' ), human_time_diff( $end_current, $right_now_t ) );
479 }
480 $period_count++;
481 }
482
483 if ( ( $freq == 3 ) && ( $monthly_pattern == "yes" ) ) $current = $temp_current;
484 }
485 date_default_timezone_set( $temp_tz );
486 return $active_periods;
487 }
488
489 /**
490 * Wrapper for calling timedContentPlugin::__getRulePeriods() by the ID of a Timed Content Rule
491 *
492 * @param int $ID ID of the Timed Content Rule
493 * @param bool $human_readable If true, the active periods are returned as a human-readable date/time
494 * as defined by the constant TIMED_CONTENT_DT_FORMAT; otherwise, they are
495 * returned as UNIX timestamps.
496 * @return array Array of active periods
497 */
498 function getRulePeriodsById( $ID, $human_readable = false ) {
499 if ( TIMED_CONTENT_RULE_TYPE != get_post_type( $ID ) )
500 return array();
501
502 $prefix = TIMED_CONTENT_RULE_POSTMETA_PREFIX;
503 $args = array();
504
505 $args['human_readable'] = (bool) $human_readable;
506 $args['freq'] = get_post_meta( $ID, $prefix . 'frequency', true );
507 $args['timezone'] = get_post_meta( $ID, $prefix . 'timezone', true );
508 $args['recurr_type'] = get_post_meta( $ID, $prefix . 'recurrence_duration', true );
509 $args['num_repeat'] = get_post_meta( $ID, $prefix . 'recurrence_duration_num_repeat', true );
510 $args['end_date'] = get_post_meta( $ID, $prefix . 'recurrence_duration_end_date', true );
511 $args['days_of_week'] = get_post_meta( $ID, $prefix . 'weekly_days_of_week_to_repeat', true );
512 if ( $args['freq'] == 0 ) $args['interval_multiplier'] = get_post_meta( $ID, $prefix . 'hourly_num_of_hours', true );
513 if ( $args['freq'] == 1 ) $args['interval_multiplier'] = get_post_meta( $ID, $prefix . 'daily_num_of_days', true );
514 if ( $args['freq'] == 2 ) $args['interval_multiplier'] = get_post_meta( $ID, $prefix . 'weekly_num_of_weeks', true );
515 if ( $args['freq'] == 3 ) $args['interval_multiplier'] = get_post_meta( $ID, $prefix . 'monthly_num_of_months', true );
516 if ( $args['freq'] == 4 ) $args['interval_multiplier'] = get_post_meta( $ID, $prefix . 'yearly_num_of_years', true );
517 $args['instance_start'] = get_post_meta( $ID, $prefix . 'instance_start', true );
518 $args['instance_end'] = get_post_meta( $ID, $prefix . 'instance_end', true );
519 $args['monthly_pattern'] = get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month', true );
520 $args['monthly_pattern_ord'] = get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month_nth', true );
521 $args['monthly_pattern_day'] = get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month_weekday', true );
522
523 return $this->__getRulePeriods( $args );
524
525 }
526
527 /**
528 * Wrapper for calling timedContentPlugin::__getRulePeriods() based on the contents of the form fields
529 * of the Add Timed Content Rule and Edit Timed Content Rule screens. Output is sent to output as JSON
530 */
531 function timedContentPluginGetRulePeriodsAjax() {
532 if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) {
533 $prefix = TIMED_CONTENT_RULE_POSTMETA_PREFIX;
534 $args = array();
535
536 $args['human_readable'] = ( ( ( isset( $_POST[$prefix . 'human_readable'] ) ) && ( $_POST[$prefix . 'human_readable'] == 'true' ) ) ? (bool)$_POST[$prefix . 'human_readable'] : false );
537 $args['freq'] = $_POST[$prefix . 'frequency'];
538 $args['timezone'] = $_POST[$prefix . 'timezone'];
539 $args['recurr_type'] = $_POST[$prefix . 'recurrence_duration'];
540 $args['num_repeat'] = $_POST[$prefix . 'recurrence_duration_num_repeat'];
541 $args['end_date'] = $_POST[$prefix . 'recurrence_duration_end_date'];
542 $args['days_of_week'] = ( isset( $_POST[$prefix . 'weekly_days_of_week_to_repeat'] ) ? $_POST[$prefix . 'weekly_days_of_week_to_repeat'] : array() );
543 $args['interval_multiplier'] = $_POST[$prefix . 'interval_multiplier'];
544 $args['instance_start'] = $_POST[$prefix . 'instance_start'];
545 $args['instance_end'] = $_POST[$prefix . 'instance_end'];
546 $args['monthly_pattern'] = $_POST[$prefix . 'monthly_nth_weekday_of_month'];
547 $args['monthly_pattern_ord'] = $_POST[$prefix . 'monthly_nth_weekday_of_month_nth'];
548 $args['monthly_pattern_day'] = $_POST[$prefix . 'monthly_nth_weekday_of_month_weekday'];
549
550 $response = json_encode( $this->__getRulePeriods( $args ) );
551
552 // response output
553 header( "Content-Type: application/json" );
554 echo $response;
555 }
556 die();
557
558 }
559
560 /**
561 * Returns a human-readable description of a Timed Content Rule
562 *
563 * @param $args Array of Timed Content Rule parameters
564 * @return string
565 */
566 function __getScheduleDescription( $args ) {
567 require("lib/Arrays_Definitions.php");
568
569 $interval_multiplier = 1;
570 $desc = "";
571
572 $errors = $this->__validate( $args );
573 if ( $errors ) {
574 $messages = "<div class=\"tcr-warning\">\n";
575 $messages .= "<p class=\"heading\">" . __( "Warning!", 'timed-content' ) . "</p>\n";
576 $messages .= "<p>" . __( "Some problems have been detected. Although you can still publish this rule, it may not work the way you expect.", 'timed-content' ) . "</p>\n";
577 $messages .= "<ul>\n";
578 foreach ( $errors as $error )
579 $messages .= " <li>" . $error . "</li>\n";
580 $messages .= "</ul>\n";
581 $messages .= "<p>" . __( "Check that all of the conditions for this rule are correct, and use Show Projected Dates/Times to ensure your rule is working properly.", 'timed-content' ) . "</p>\n";
582 $messages .= "</div>\n";
583 return $messages;
584 }
585
586 if ( $args['action'] )
587 $action = __( "Show the content", 'timed-content' );
588 else
589 $action = __( "Hide the content", 'timed-content' );
590 $freq = $args['freq'];
591 $timezone = $args['timezone'];
592 $recurr_type = $args['recurr_type'];
593 $num_repeat = intval( $args['num_repeat'] );
594 $end_date = $args['end_date'];
595 $days_of_week = $args['days_of_week'];
596 $interval_multiplier = $args['interval_multiplier'];
597 $instance_start_date = $args['instance_start']['date'];
598 $instance_start_time = $args['instance_start']['time'];
599 $instance_end_date = $args['instance_end']['date'];
600 $instance_end_time = $args['instance_end']['time'];
601 $monthly_pattern = $args['monthly_pattern'];
602 $monthly_pattern_ord = $args['monthly_pattern_ord'];
603 $monthly_pattern_day = $args['monthly_pattern_day'];
604
605 $desc = sprintf( _x( '%1$s on %2$s @ %3$s until %4$s @ %5$s.', 'Dates/times of first active period', 'timed-content' ), $action, $instance_start_date, $instance_start_time, $instance_end_date, $instance_end_time );
606
607 if ( $freq == 0 )
608 $desc .= "&nbsp;" . sprintf( _n( 'Repeat this action every hour.', 'Repeat this action every %d hours.', $interval_multiplier, 'timed-content' ), $interval_multiplier );
609 elseif ( $freq == 1 )
610 $desc .= "&nbsp;" . sprintf( _n( 'Repeat this action every day.', 'Repeat this action every %d days.', $interval_multiplier, 'timed-content' ), $interval_multiplier );
611 elseif ( $freq == 2 ) {
612 if ( ( $days_of_week ) && ( is_array( $days_of_week ) ) ) {
613 $days = array(); $days_list = "";
614 foreach ( $days_of_week as $v )
615 $days[] = $timed_content_rule_days_array[$v];
616 switch ( count( $days ) ) {
617 case 1: $days_list = sprintf( _x( '%1$s', 'List of one weekday', 'timed-content' ), $days[0] ); break;
618 case 2: $days_list = sprintf( _x( '%1$s and %2$s', 'List of two weekdays', 'timed-content' ), $days[0], $days[1] ); break;
619 case 3: $days_list = sprintf( _x( '%1$s, %2$s, and %3$s', 'List of three weekdays', 'timed-content' ), $days[0], $days[1], $days[2] ); break;
620 case 4: $days_list = sprintf( _x( '%1$s, %2$s, %3$s, and %4$s', 'List of four weekdays', 'timed-content' ), $days[0], $days[1], $days[2], $days[3] ); break;
621 case 5: $days_list = sprintf( _x( '%1$s, %2$s, %3$s, %4$s, and %5$s', 'List of five weekdays', 'timed-content' ), $days[0], $days[1], $days[2], $days[3], $days[4] ); break;
622 case 6: $days_list = sprintf( _x( '%1$s, %2$s, %3$s, %4$s, %5$s, and %6$s', 'List of six weekdays', 'timed-content' ), $days[0], $days[1], $days[2], $days[3], $days[4], $days[5] ); break;
623 case 7: $days_list = sprintf( _x( '%1$s, %2$s, %3$s, %4$s, %5$s, %6$s, and %7$s', 'List of all weekdays', 'timed-content' ), $days[0], $days[1], $days[2], $days[3], $days[4], $days[5], $days[6] ); break;
624 }
625 if ( $interval_multiplier == 1 )
626 $desc .= "&nbsp;" . sprintf( _x( 'Repeat this action every week on %s.', 'List the weekdays to repeat the rule when frequency is every week. %s is the list of weekdays.', 'timed-content' ), $days_list );
627 else
628 $desc .= "&nbsp;" . sprintf( _x( 'Repeat this action every %1$d weeks on %2$s.', 'List the weekdays to repeat the rule when frequency is every %1$d weeks. %2$s is the list of weekdays.', 'timed-content' ), $interval_multiplier, $days_list );
629 } else
630 $desc .= "&nbsp;" . sprintf( _n( 'Repeat this action every week.', 'Repeat this action every %d weeks.', $interval_multiplier, 'timed-content' ), $interval_multiplier );
631
632 } elseif ( $freq == 3 ) {
633 if ( $monthly_pattern == "yes" ) {
634 if ( $interval_multiplier == 1 )
635 $desc .= "&nbsp;" . sprintf( __( 'Repeat this action every month on the %1$s %2$s of the month.', 'timed-content' ), $timed_content_rule_ordinal_array[$monthly_pattern_ord], $timed_content_rule_ordinal_days_array[$monthly_pattern_day] );
636 else
637 $desc .= "&nbsp;" . sprintf( __( 'Repeat this action every %1$d months on the %2$s %3$s of the month.', 'timed-content' ), $interval_multiplier, $timed_content_rule_ordinal_array[$monthly_pattern_ord], $timed_content_rule_ordinal_days_array[$monthly_pattern_day] );
638 } else
639 $desc .= "&nbsp;" . sprintf( _n( 'Repeat this action every month.', 'Repeat this action every %d months.', $interval_multiplier, 'timed-content' ), $interval_multiplier );
640 } elseif ( $freq == 4 )
641 $desc .= "&nbsp;" . sprintf( _n( 'Repeat this action every year.', 'Repeat this action every %d years.', $interval_multiplier, 'timed-content' ), $interval_multiplier );
642
643 if ( $recurr_type == "recurrence_duration_num_repeat" )
644 $desc .= "&nbsp;" . sprintf( _n( 'This rule will be active for 1 repetition.', 'This rule will be active for %d repetitions.', $num_repeat, 'timed-content' ), $num_repeat );
645 elseif ( $recurr_type == "recurrence_duration_end_date" )
646 $desc .= "&nbsp;" . sprintf( __( 'This rule will be active until %s.', 'timed-content' ), $end_date );
647
648 $desc .= "&nbsp;" . sprintf( __( 'All times are in the %s timezone.', 'timed-content' ), $timezone );
649 return $desc;
650 }
651
652 /**
653 * Wrapper for calling timedContentPlugin::__getScheduleDescription() by the ID of a Timed Content Rule
654 *
655 * @param int $ID ID of the Timed Content Rule
656 * @return string
657 */
658 function getScheduleDescriptionById( $ID ) {
659 global $timed_content_rule_occurrence_custom_fields, $timed_content_rule_pattern_custom_fields, $timed_content_rule_recurrence_custom_fields;
660 $defaults = array();
661
662 foreach ( $timed_content_rule_occurrence_custom_fields as $field ) {
663 $defaults[$field['name']] = $field['default'];
664 }
665 foreach ( $timed_content_rule_pattern_custom_fields as $field ) {
666 $defaults[$field['name']] = $field['default'];
667 }
668 foreach ( $timed_content_rule_recurrence_custom_fields as $field ) {
669 $defaults[$field['name']] = $field['default'];
670 }
671
672 $prefix = TIMED_CONTENT_RULE_POSTMETA_PREFIX;
673 $args = array();
674
675 $args['action'] = ( false === get_post_meta( $ID, $prefix . 'action', true ) ? $defaults['action'] : get_post_meta( $ID, $prefix . 'action', true ) );
676 $args['freq'] = ( false === get_post_meta( $ID, $prefix . 'frequency', true ) ? $defaults['frequency'] : get_post_meta( $ID, $prefix . 'frequency', true ) );
677 $args['timezone'] = ( false === get_post_meta( $ID, $prefix . 'timezone', true ) ? $defaults['timezone'] : get_post_meta( $ID, $prefix . 'timezone', true ) );
678 $args['recurr_type'] = ( false === get_post_meta( $ID, $prefix . 'recurrence_duration', true ) ? $defaults['recurrence_duration'] : get_post_meta( $ID, $prefix . 'recurrence_duration', true ) );
679 $args['num_repeat'] = ( false === get_post_meta( $ID, $prefix . 'recurrence_duration_num_repeat', true ) ? $defaults['recurrence_duration_num_repeat'] : get_post_meta( $ID, $prefix . 'recurrence_duration_num_repeat', true ) );
680 $args['end_date'] = ( false === get_post_meta( $ID, $prefix . 'recurrence_duration_end_date', true ) ? $defaults['recurrence_duration_end_date'] : get_post_meta( $ID, $prefix . 'recurrence_duration_end_date', true ) );
681 $args['days_of_week'] = ( false === get_post_meta( $ID, $prefix . 'weekly_days_of_week_to_repeat', true ) ? $defaults['weekly_days_of_week_to_repeat'] : get_post_meta( $ID, $prefix . 'weekly_days_of_week_to_repeat', true ) );
682 if ( $args['freq'] == 0 ) $args['interval_multiplier'] = ( false === get_post_meta( $ID, $prefix . 'hourly_num_of_hours', true ) ? $defaults['hourly_num_of_hours'] : get_post_meta( $ID, $prefix . 'hourly_num_of_hours', true ) );
683 if ( $args['freq'] == 1 ) $args['interval_multiplier'] = ( false === get_post_meta( $ID, $prefix . 'daily_num_of_days', true ) ? $defaults['daily_num_of_days'] : get_post_meta( $ID, $prefix . 'daily_num_of_days', true ) );
684 if ( $args['freq'] == 2 ) $args['interval_multiplier'] = ( false === get_post_meta( $ID, $prefix . 'weekly_num_of_weeks', true ) ? $defaults['weekly_num_of_weeks'] : get_post_meta( $ID, $prefix . 'weekly_num_of_weeks', true ) );
685 if ( $args['freq'] == 3 ) $args['interval_multiplier'] = ( false === get_post_meta( $ID, $prefix . 'monthly_num_of_months', true ) ? $defaults['monthly_num_of_months'] : get_post_meta( $ID, $prefix . 'monthly_num_of_months', true ) );
686 if ( $args['freq'] == 4 ) $args['interval_multiplier'] = ( false === get_post_meta( $ID, $prefix . 'yearly_num_of_years', true ) ? $defaults['yearly_num_of_years'] : get_post_meta( $ID, $prefix . 'yearly_num_of_years', true ) );
687 $args['instance_start'] = ( false === get_post_meta( $ID, $prefix . 'instance_start', true ) ? $defaults['instance_start'] : get_post_meta( $ID, $prefix . 'instance_start', true ) );
688 $args['instance_end'] = ( false === get_post_meta( $ID, $prefix . 'instance_end', true ) ? $defaults['instance_end'] : get_post_meta( $ID, $prefix . 'instance_end', true ) );
689 $args['monthly_pattern'] = ( false === get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month', true ) ? $defaults['monthly_nth_weekday_of_month'] : get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month', true ) );
690 $args['monthly_pattern_ord'] = ( false === get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month_nth', true ) ? $defaults['monthly_nth_weekday_of_month_nth'] : get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month_nth', true ) );
691 $args['monthly_pattern_day'] = ( false === get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month_weekday', true ) ? $defaults['monthly_nth_weekday_of_month_weekday'] : get_post_meta( $ID, $prefix . 'monthly_nth_weekday_of_month_weekday', true ) );
692
693 return $this->__getScheduleDescription( $args );
694
695 }
696
697 /**
698 * Wrapper for calling timedContentPlugin::__getRulePeriods() based on the contents of the form fields
699 * of the Add Timed Content Rule and Edit Timed Content Rule screens. Output is sent to output as plain text
700 */
701 function timedContentPluginGetScheduleDescriptionAjax() {
702 if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) {
703 $prefix = TIMED_CONTENT_RULE_POSTMETA_PREFIX;
704 $args = array();
705
706 $args['action'] = $_POST[$prefix . 'action'];
707 $args['freq'] = $_POST[$prefix . 'frequency'];
708 $args['timezone'] = $_POST[$prefix . 'timezone'];
709 $args['recurr_type'] = $_POST[$prefix . 'recurrence_duration'];
710 $args['num_repeat'] = $_POST[$prefix . 'recurrence_duration_num_repeat'];
711 $args['end_date'] = $_POST[$prefix . 'recurrence_duration_end_date'];
712 $args['days_of_week'] = ( isset( $_POST[$prefix . 'weekly_days_of_week_to_repeat'] ) ? $_POST[$prefix . 'weekly_days_of_week_to_repeat'] : array() );
713 $args['interval_multiplier'] = $_POST[$prefix . 'interval_multiplier'];
714 $args['instance_start'] = $_POST[$prefix . 'instance_start'];
715 $args['instance_end'] = $_POST[$prefix . 'instance_end'];
716 $args['monthly_pattern'] = $_POST[$prefix . 'monthly_nth_weekday_of_month'];
717 $args['monthly_pattern_ord'] = $_POST[$prefix . 'monthly_nth_weekday_of_month_nth'];
718 $args['monthly_pattern_day'] = $_POST[$prefix . 'monthly_nth_weekday_of_month_weekday'];
719
720 $response = $this->__getScheduleDescription( $args );
721
722 // response output
723 header( "Content-Type: text/plain" );
724 echo $response;
725 }
726 die();
727 }
728
729 /**
730 * Processes the [timed-content-client] shortcode.
731 *
732 * @param array $atts
733 * @param null $content
734 * @return string
735 */
736 function clientShowHTML( $atts, $content = null ) {
737 $show_attr = "";
738 $hide_attr = "";
739 extract( shortcode_atts( array( 'show' => '0:00:000' , 'hide' => '0:00:000' , 'display' => 'div' ), $atts ) );
740
741 // Initialize show/hide arguments
742 $s_min = 0; $s_sec = 0; $s_fade = 0;
743 $h_min = 0; $h_sec = 0; $h_fade = 0;
744 @list( $s_min, $s_sec, $s_fade ) = explode( ":", $show );
745 @list( $h_min, $h_sec, $h_fade ) = explode( ":", $hide );
746
747 if ( ( (int)$s_min + (int)$s_sec ) > 0 )
748 $show_attr = "_show_" . $s_min . "_" . $s_sec . "_" . $s_fade;
749 if ( ( (int)$h_min + (int)$h_sec ) > 0 )
750 $hide_attr = "_hide_" . $h_min . "_" . $h_sec . "_" . $h_fade;
751
752 $the_class = TIMED_CONTENT_CLIENT_TAG . $show_attr . $hide_attr ;
753 $the_tag = ( $display == "div" ? "div" : "span" );
754
755 $the_HTML = "<" . $the_tag . " class='" . $the_class . "'" . ( ( $show_attr != "" ) ? " style='display: none;'" : "" ) .">" . do_shortcode( $content ) . "</" . $the_tag . ">";
756
757 return $the_HTML;
758 }
759
760 /**
761 * Processes the [timed-content-server] shortcode.
762 *
763 * @param array $atts
764 * @param null $content
765 * @return string
766 */
767 function serverShowHTML( $atts, $content = null ) {
768 global $post;
769 extract( shortcode_atts( array( 'show' => TIMED_CONTENT_ZERO_TIME , 'hide' => TIMED_CONTENT_END_TIME, 'debug' => 'false' ), $atts ) );
770 $show_t = strtotime( $show );
771 $hide_t = strtotime( $hide );
772 $right_now_t = time();
773 $debug_message = "";
774
775 if ( ( $debug == "true" ) && ( current_user_can( "edit_post", $post->post_id ) ) ) {
776 $temp_tz = date_default_timezone_get();
777 date_default_timezone_set( get_option( 'timezone_string' ) );
778
779 $right_now = date_i18n( TIMED_CONTENT_DT_FORMAT, $right_now_t );
780
781 if ( $show_t > $right_now_t )
782 $show_diff_str = sprintf( _x( '%s from now.', 'Human readable time difference', 'timed-content' ), human_time_diff( $show_t, $right_now_t ) );
783 else
784 $show_diff_str = sprintf( _x( '%s ago.', 'Human readable time difference', 'timed-content' ), human_time_diff( $show_t, $right_now_t ) );
785 if ( $hide_t > $right_now_t )
786 $hide_diff_str = sprintf( _x( '%s from now.', 'Human readable time difference', 'timed-content' ), human_time_diff( $hide_t, $right_now_t ) );
787 else
788 $hide_diff_str = sprintf( _x( '%s ago.', 'Human readable time difference', 'timed-content' ), human_time_diff( $hide_t, $right_now_t ) );
789
790 $debug_message = "<div class=\"tcr-warning\">\n";
791 $debug_message .= "<p class=\"heading\">" . _x( "Notice", "Noun", 'timed-content' ) . "</p>\n";
792 $debug_message .= "<p>" . __( "Debugging has been turned on for a <code>[timed-content-server]</code> shortcode on this Post/Page. Only website users who are currently logged in and can edit this Post/Page will see this. To turn off this message, remove the <code>debug</code> attribute from the shortcode.", 'timed-content' ) . "</p>\n";
793
794 if ( $show == TIMED_CONTENT_ZERO_TIME )
795 $debug_message .= "<p>" . __( 'The <code>show</code> attribute is not set.', 'timed-content') . "</p>\n";
796 else
797 $debug_message .= "<p>" . __( 'The <code>show</code> attribute is currently set to', 'timed-content') . ": " . $show . ",<br />\n "
798 . __( 'The Timed Content plugin thinks the intended date/time is', 'timed-content') . ": " . date_i18n( TIMED_CONTENT_DT_FORMAT, $show_t )
799 . " (" . $show_diff_str . ")</p>\n";
800
801 if ( $hide == TIMED_CONTENT_END_TIME )
802 $debug_message .= "<p>" . __( 'The <code>hide</code> attribute is not set.' , 'timed-content') . "</p>\n";
803 else
804 $debug_message .= "<p>" . __( 'The <code>hide</code> attribute is currently set to', 'timed-content') . ": " . $hide . ",<br />\n"
805 . __( 'The Timed Content plugin thinks the intended date/time is', 'timed-content') . ": " . date_i18n( TIMED_CONTENT_DT_FORMAT, $hide_t )
806 . " (" . $hide_diff_str . ").</p>\n";
807
808 $debug_message .= "<p>" . __( 'Current time', 'timed-content') . " : " . $right_now . "</p>\n";
809 $debug_message .= "<p>" . __( 'Content', "Noun", 'timed-content') . " : " . $content . "</p>\n";
810
811 $debug_message .= "</div>\n";
812
813 date_default_timezone_set( $temp_tz );
814 }
815
816 if ( ( $show_t <= $right_now_t ) && ( $right_now_t <= $hide_t ) )
817 return $debug_message . do_shortcode( $content ) . "\n";
818 else
819 return $debug_message . "\n";
820
821 }
822
823 /**
824 * Processes the [timed-content-rule] shortcode.
825 *
826 * @param array $atts
827 * @param null $content
828 * @return string
829 */
830 function rulesShowHTML( $atts, $content = null ) {
831 extract( shortcode_atts( array( 'id' => '0' ), $atts ) );
832 if ( TIMED_CONTENT_RULE_TYPE != get_post_type( $id ) ) return;
833
834 $prefix = TIMED_CONTENT_RULE_POSTMETA_PREFIX;
835 $right_now_t = time();
836 $rule_is_active = false;
837
838 $active_periods = $this->getRulePeriodsById( $id, false );
839 $action_is_show = (bool) get_post_meta( $id, $prefix . 'action', true );
840
841 foreach ( $active_periods as $period ) {
842 if ( ( $period['start'] <= $right_now_t ) && ( $right_now_t <= $period['end'] ) ) {
843 $rule_is_active = true;
844 break;
845 }
846 }
847
848 if ( ( ( $rule_is_active == true ) && ( $action_is_show == true ) ) || ( ( $rule_is_active == false ) && ( $action_is_show == false ) ) )
849 return do_shortcode( $content );
850 else
851 return "";
852 }
853
854 /**
855 * Enqueues the JavaScript code necessary for the functionality of the [timed-content-client] shortcode.
856 */
857 function addHeaderCode() {
858 if ( ! is_admin() ) {
859 wp_enqueue_style( 'timed-content-css', TIMED_CONTENT_CSS );
860 wp_enqueue_script( 'timed-content_js', TIMED_CONTENT_PLUGIN_URL . '/js/timed-content.js', array( 'jquery' ), TIMED_CONTENT_VERSION );
861 }
862 }
863
864 /**
865 * Enqueues the CSS code necessary for custom icons for the Timed Content Rules management screens. Echo'd to output.
866 */
867 function addPostTypeIcons() {
868 ?>
869 <style type="text/css" media="screen">
870 #menu-posts-<?php echo TIMED_CONTENT_RULE_TYPE; ?> .wp-menu-image {
871 background: url(<?php echo TIMED_CONTENT_PLUGIN_URL; ?>/img/clock_icon.png) no-repeat 6px 6px !important;
872 }
873 #menu-posts-<?php echo TIMED_CONTENT_RULE_TYPE; ?>:hover .wp-menu-image, #menu-posts-<?php echo TIMED_CONTENT_RULE_TYPE; ?>.wp-has-current-submenu .wp-menu-image {
874 background-position: -22px 6px !important;
875 }
876 #icon-edit.icon32-posts-<?php echo TIMED_CONTENT_RULE_TYPE; ?> {background: url(<?php echo TIMED_CONTENT_PLUGIN_URL; ?>/img/clock_32x32.png) no-repeat;}
877 </style>
878 <?php
879 }
880
881 /**
882 * Enqueues the JavaScript code necessary for the functionality of the Timed Content Rules management screens.
883 */
884 function addAdminHeaderCode() {
885 if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == TIMED_CONTENT_RULE_TYPE )
886 || ( isset( $post_type ) && $post_type == TIMED_CONTENT_RULE_TYPE )
887 || ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == TIMED_CONTENT_RULE_TYPE ) ) {
888 wp_enqueue_style( 'timed-content-css', TIMED_CONTENT_CSS );
889 // Enqueue the JavaScript file that manages the meta box UI
890 wp_enqueue_script( 'timed-content-admin_js', TIMED_CONTENT_PLUGIN_URL . '/js/timed-content-admin.js', array( 'jquery' ), TIMED_CONTENT_VERSION );
891 // Enqueue the JavaScript file that makes AJAX requests
892 wp_enqueue_script( 'timed-content-ajax_js', TIMED_CONTENT_PLUGIN_URL . '/js/timed-content-ajax.js', array( 'jquery', 'jquery-ui-dialog' ), TIMED_CONTENT_VERSION );
893
894 // Set up local variables used in the AJAX JavaScript file
895 wp_localize_script( 'timed-content-ajax_js', 'timedContentRuleAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ),
896 'start_label' => _x( 'Start', 'Scheduled Dates/Times dialog - Beginning of active period table header', 'timed-content' ),
897 'end_label' => _x( 'End', 'Scheduled Dates/Times dialog - End of active period table header', 'timed-content' ),
898 'dialog_label' => _x( 'Scheduled Dates/Times', 'Scheduled Dates/Times dialog - dialog header', 'timed-content' ),
899 'dialog_width' => 800,
900 'dialog_height' => 500,
901 'close_label' => _x( 'Close', 'Scheduled Dates/Times dialog - Close button HTML label', 'timed-content' ),
902 'loadingimg' => TIMED_CONTENT_PLUGIN_URL . '/img/wpspin.gif',
903 'error' => __( "Error", 'timed-content' ),
904 'error_desc' => __( "Something unexpected has happened along the way. The specific details are below:", 'timed-content' ) ) );
905 }
906 }
907
908 /**
909 * Initializes the TinyMCE plugin bundled with this Wordpress plugin
910 */
911 function initTinyMCEPlugin() {
912 if ( ( ! current_user_can( 'edit_posts' ) ) && ( ! current_user_can( 'edit_pages' ) ) )
913 return;
914
915 // Add only in Rich Editor mode
916 if ( get_user_option( 'rich_editing' ) == 'true' ) {
917 add_filter( "mce_external_plugins", array( &$this, "addTimedContentTinyMCEPlugin" ) );
918 add_filter( "mce_buttons", array( &$this, "registerTinyMCEButton" ) );
919 }
920 }
921
922 /**
923 * Sets up variables to use in the TinyMCE plugin's editor_plugin_src.js.
924 *
925 */
926 function setTinyMCEPluginVars() {
927 if ( ( ! current_user_can( 'edit_posts' ) ) && ( ! current_user_can( 'edit_pages' ) ) )
928 return;
929
930 // Add only in Rich Editor mode
931 if ( get_user_option( 'rich_editing' ) == 'true' ) {
932 wp_enqueue_script( 'timed-content-admin_tinymce_js', TIMED_CONTENT_PLUGIN_URL . '/js/timed-content-admin-tinymce.js', array(), TIMED_CONTENT_VERSION );
933 wp_localize_script( 'timed-content-admin_tinymce_js',
934 'timedContentAdminTinyMCEOptionsVars',
935 array( 'version' => TIMED_CONTENT_VERSION,
936 'desc' => __( "Add Timed Content shortcodes", 'timed-content' ) ) );
937 }
938 }
939
940 /**
941 * Sets up the button for the associated TinyMCE plugin for use in the editor menubar.
942 * @param array $buttons Array of menu buttons already registered with TinyMCE
943 * @return array The array of TinyMCE menu buttons with ours now loaded in as well
944 */
945 function registerTinyMCEButton( $buttons ) {
946 array_push( $buttons, "|", "timed_content" );
947 return $buttons;
948 }
949
950 /**
951 * Loads the associated TinyMCE plugin into TinyMCE's plugin array
952 *
953 * @param array $plugin_array Array of plugins already registered with TinyMCE
954 * @return array The array of TinyMCE plugins with ours now loaded in as well
955 */
956 function addTimedContentTinyMCEPlugin( $plugin_array ) {
957 $plugin_array['timed_content'] = TIMED_CONTENT_PLUGIN_URL . "/tinymce_plugin/editor_plugin.js";
958 return $plugin_array;
959 }
960
961 /**
962 * Generates JavaScript array of objects describing Timed Content Rules. Used in the dialog box created by
963 * timedContentPlugin::timedContentPluginGetTinyMCEDialog().
964 *
965 * @return string
966 */
967 function __getRulesJS() {
968 $the_js = "var rules = [\n";
969 $args = array( 'post_type' => TIMED_CONTENT_RULE_TYPE, 'posts_per_page' => -1, 'post_status' => 'publish' );
970 $the_rules = get_posts( $args );
971 foreach ( $the_rules as $rule ) {
972 $desc = $this->getScheduleDescriptionById( $rule->ID );
973 // Only add a rule if there's no errors or warnings
974 if ( false === strpos( $desc, "tcr-warning" ) )
975 $the_js .= " { 'ID': " . $rule->ID . ", 'title': '" . esc_js( $rule->post_title ) . "', 'desc': '" . esc_js( $desc ) . "' },\n";
976 }
977 $the_js .= "];\n";
978 return $the_js;
979 }
980 /**
981 * Display a dialog box for this plugin's associated TinyMCE plugin. Called from TinyMCE via AJAX.
982 *
983 */
984 function timedContentPluginGetTinyMCEDialog() {
985 wp_enqueue_style( 'timed-content-jquery-ui-css', TIMED_CONTENT_JQUERY_UI_CSS );
986 wp_enqueue_script( 'jquery-ui-datepicker' );
987 wp_register_style( 'timed-content-jquery-ui-timepicker-css', TIMED_CONTENT_JQUERY_UI_TIMEPICKER_CSS );
988 wp_enqueue_style( 'timed-content-jquery-ui-timepicker-css' );
989 wp_register_script( 'timed-content-jquery-ui-timepicker-js', TIMED_CONTENT_JQUERY_UI_TIMEPICKER_JS, array('jquery', 'jquery-ui-datepicker'), TIMED_CONTENT_VERSION );
990 wp_enqueue_script( 'timed-content-jquery-ui-timepicker-js' );
991
992 ob_start();
993 include("tinymce_plugin/dialog.php");
994 $content = ob_get_contents();
995 ob_end_clean();
996 echo $content;
997 die();
998 }
999
1000 /**
1001 * Displays a count of Timed Content Rules of the Dashboard's Right now widget
1002 *
1003 */
1004 function i18nInit() {
1005 $plugin_dir = basename( dirname( __FILE__ ) ) . "/lang/";
1006 load_plugin_textdomain( 'timed-content', null, $plugin_dir );
1007 }
1008
1009 /**
1010 * Add custom columns to the Timed Content Rules overview page
1011 *
1012 */
1013 function addDescColumnHead( $defaults ) {
1014 unset( $defaults['date'] );
1015 $defaults['description'] = __( 'Description', 'timed-content' );
1016 $defaults['shortcode'] = __( 'Shortcode', 'timed-content' );
1017 return $defaults;
1018 }
1019
1020 /**
1021 * Display content associated with custom columns on the Timed Content Rules overview page
1022 *
1023 * @param $column_name Name of the column to be displayed
1024 * @param $post_ID ID of the Timed Content Rule being listed
1025 */
1026 function addDescColumnContent( $column_name, $post_ID ) {
1027 if ( $column_name == 'shortcode' ) {
1028 echo '<code>[' . TIMED_CONTENT_RULE_TAG . ' id="' . $post_ID . '"]...[/' . TIMED_CONTENT_RULE_TAG . ']</code>';
1029 }
1030 if ( $column_name == 'description' ) {
1031 $desc = $this->getScheduleDescriptionById( $post_ID );
1032 if ( $desc ) {
1033 echo '<em>' . $desc . '</em>';
1034 }
1035 }
1036 }
1037
1038
1039 /**
1040 * Display a count of Timed Content Rules of the Dashboard's Right now widget
1041 *
1042 */
1043 function addRulesCount() {
1044 if ( !post_type_exists( TIMED_CONTENT_RULE_TYPE ) ) {
1045 return;
1046 }
1047
1048 $num_posts = wp_count_posts( TIMED_CONTENT_RULE_TYPE );
1049 $num = number_format_i18n( $num_posts->publish );
1050 $text = _n( 'Timed Content Rule', 'Timed Content Rules', intval( $num_posts->publish ) );
1051 if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) {
1052 $num = "<a href='edit.php?post_type=" . TIMED_CONTENT_RULE_TYPE . "'>" . $num . "</a>";
1053 $text = "<a href='edit.php?post_type=" . TIMED_CONTENT_RULE_TYPE . "'>" . $text . "</a>";
1054 }
1055 echo '<tr>';
1056 echo '<td class="first b b-' . TIMED_CONTENT_RULE_TYPE . '">' . $num . '</td>';
1057 echo '<td class="t ' . TIMED_CONTENT_RULE_TYPE . '">' . $text . '</td>';
1058 echo '</tr>';
1059
1060 if ( $num_posts->pending > 0 ) {
1061 $num = number_format_i18n( $num_posts->pending );
1062 $text = _n( 'Timed Content Rule Pending', 'Timed Content Rules Pending', intval( $num_posts->pending ) );
1063 if ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) {
1064 $num = "<a href='edit.php?post_status=pending&post_type=" . TIMED_CONTENT_RULE_TYPE . "'>" . $num . "</a>";
1065 $text = "<a href='edit.php?post_status=pending&post_type=" . TIMED_CONTENT_RULE_TYPE . "'>" . $text . "</a>";
1066 }
1067 echo '<tr>';
1068 echo '<td class="first b b-' . TIMED_CONTENT_RULE_TYPE . '">' . $num . '</td>';
1069 echo '<td class="t ' . TIMED_CONTENT_RULE_TYPE . '">' . $text . '</td>';
1070 echo '</tr>';
1071 }
1072 }
1073 }
1074
1075 } //End Class timedContentPlugin
1076
1077 // Initialize plugin
1078 if ( class_exists( "timedContentPlugin" ) ) {
1079 $timedContentPluginInstance = new timedContentPlugin();
1080 }
1081
1082 // Actions and Filters
1083 if ( isset( $timedContentPluginInstance ) ) {
1084 $scf = new customFieldsInterface( "cfi_s",
1085 __( 'Rule Description/Schedule', 'timed-content' ),
1086 "<div id=\"schedule_desc\" style=\"font-style: italic;\">"
1087 . ( isset( $_GET['post'] ) && ( TIMED_CONTENT_RULE_TYPE === get_post_type( $_GET['post'] ) ) ? $timedContentPluginInstance->getScheduleDescriptionById( intval( $_GET['post'] ) ) : $timedContentPluginInstance->getScheduleDescriptionById( intval( 0 ) ) )
1088 . "</div>"
1089 . "<div style=\"padding-top: 10px;\"><input type=\"button\" class=\"button-primary\" id=\"timed_content_rule_test\" value=\"Show Projected Dates/Times\" /></div>",
1090 TIMED_CONTENT_RULE_POSTMETA_PREFIX,
1091 array( TIMED_CONTENT_RULE_TYPE ),
1092 array() );
1093 $ocf = new customFieldsInterface( "cfi_o",
1094 __( 'Action/Initial Event', 'timed-content' ),
1095 __( 'Set the action to be taken and when it should first run.', 'timed-content' ),
1096 TIMED_CONTENT_RULE_POSTMETA_PREFIX,
1097 array( TIMED_CONTENT_RULE_TYPE ),
1098 $timed_content_rule_occurrence_custom_fields );
1099 $pcf = new customFieldsInterface( "cfi_p",
1100 __( 'Repeating Pattern', 'timed-content' ),
1101 __( 'Set how often the action should repeat.', 'timed-content' ),
1102 TIMED_CONTENT_RULE_POSTMETA_PREFIX,
1103 array( TIMED_CONTENT_RULE_TYPE ),
1104 $timed_content_rule_pattern_custom_fields );
1105 $rcf = new customFieldsInterface( "cfi_r",
1106 __( 'Stopping Condition', 'timed-content' ),
1107 __( 'Set how long or how many times the action should occur.', 'timed-content' ),
1108 TIMED_CONTENT_RULE_POSTMETA_PREFIX,
1109 array( TIMED_CONTENT_RULE_TYPE ),
1110 $timed_content_rule_recurrence_custom_fields );
1111 add_action( "init", array( &$timedContentPluginInstance, "i18nInit" ), 1 );
1112 add_action( "init", array( &$timedContentPluginInstance, "timedContentRuleTypeInit" ), 2 );
1113 add_action( "right_now_content_table_end", array( &$timedContentPluginInstance, "addRulesCount" ) );
1114 add_action( "wp_head", array( &$timedContentPluginInstance, "addHeaderCode" ), 1 );
1115 add_filter( "manage_" . TIMED_CONTENT_RULE_TYPE . "_posts_columns", array( &$timedContentPluginInstance, "addDescColumnHead" ) );
1116 add_action( "manage_" . TIMED_CONTENT_RULE_TYPE . "_posts_custom_column", array( &$timedContentPluginInstance, "addDescColumnContent" ), 10, 2);
1117 add_action( "admin_enqueue_scripts", array( &$timedContentPluginInstance, "addAdminHeaderCode" ), 1 );
1118 add_action( "admin_init", array( &$timedContentPluginInstance, "setTinyMCEPluginVars" ), 1 );
1119 add_action( "admin_init", array( &$timedContentPluginInstance, "initTinyMCEPlugin" ), 2 );
1120 add_action( "admin_head", array( &$timedContentPluginInstance, "addPostTypeIcons" ), 1 );
1121 add_action( 'wp_ajax_timedContentPluginGetTinyMCEDialog', array( &$timedContentPluginInstance, "timedContentPluginGetTinyMCEDialog" ), 1 );
1122 add_action( 'wp_ajax_timedContentPluginGetRulePeriodsAjax', array( &$timedContentPluginInstance, "timedContentPluginGetRulePeriodsAjax" ), 1 );
1123 add_action( 'wp_ajax_timedContentPluginGetScheduleDescriptionAjax', array( &$timedContentPluginInstance, "timedContentPluginGetScheduleDescriptionAjax" ), 1 );
1124 add_filter( "post_updated_messages", array( &$timedContentPluginInstance, "timedContentRuleUpdatedMessages" ), 1 );
1125 add_shortcode( TIMED_CONTENT_CLIENT_TAG, array( &$timedContentPluginInstance, "clientShowHTML" ), 1 );
1126 add_shortcode( TIMED_CONTENT_SERVER_TAG, array( &$timedContentPluginInstance, "serverShowHTML" ), 1 );
1127 add_shortcode( TIMED_CONTENT_RULE_TAG, array( &$timedContentPluginInstance, "rulesShowHTML" ), 1 );
1128 }
1129 ?>