PluginProbe
Timed Content / 2.1
Timed Content v2.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, at timed-content.php

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