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

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