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

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