PluginProbe
Timed Content / 2.81
Timed Content v2.81
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
← All changes | lib/class-timedcontentplugin.php +103 -102 trunk2.81 View file →
@@ -39,13 +39,14 @@
39 39 add_action( 'wp_ajax_timedContentPluginGetTinyMCEDialog', array( $this, 'timed_content_plugin_get_tinymce_dialog' ), 1 );
40 40 add_action( 'wp_ajax_timedContentPluginGetRulePeriodsAjax', array( $this, 'timed_content_plugin_get_rule_periods_ajax' ), 1 );
41 41 add_action( 'wp_ajax_timedContentPluginGetScheduleDescriptionAjax', array( $this, 'timed_content_plugin_get_schedule_description_ajax' ), 1 );
42 42 add_action( 'admin_head', array( $this, 'add_post_type_icons' ), 1 );
43 +
43 44 add_shortcode( TIMED_CONTENT_SHORTCODE_CLIENT, array( $this, 'client_show_html' ) );
44 45 add_shortcode( TIMED_CONTENT_SHORTCODE_SERVER, array( $this, 'server_show_html' ) );
45 46 add_shortcode( TIMED_CONTENT_SHORTCODE_RULE, array( $this, 'rules_show_html' ) );
46 47
47 - $this->set_format_timezone( wp_timezone_string() );
48 + $this->set_format_timezone( date_default_timezone_get() );
48 49 }
49 50
50 51 /**
51 52 * Initialise plugin
@@ -78,9 +79,8 @@
78 79 1 => __( 'second', 'timed-content' ),
79 80 2 => __( 'third', 'timed-content' ),
80 81 3 => __( 'fourth', 'timed-content' ),
81 82 4 => __( 'last', 'timed-content' ),
82 - 5 => __( 'fifth', 'timed-content' ),
83 83 );
84 84
85 85 $this->rule_ordinal_days_array = array(
86 86 0 => __( 'Sunday', 'timed-content' ),
@@ -337,68 +337,107 @@
337 337 }
338 338
339 339 /**
340 340 * Advances a date by a set number of months. When the date of the first active period lies
341 - * on the 29th, 30th, or 31st of the month, this function will return a date on the last day
341 + * on the 29th, 30th, or 31st of the month, this function will return a date on the the last day
342 342 * of the month for those months not containing those days.
343 343 */
344 344 function get_next_month( $current, $start, $interval_multiplier ) {
345 - $dateCurrent = new DateTime();
346 - $dateCurrent->setTimestamp( $current );
347 - $dateCurrent->setTimezone($this->get_format_timezone());
345 + // For most days in the month, it's pretty easy. Get the day of month of the starting date.
346 + $start_day = $this->format_timestamp( 'j', $start );
348 347
349 - $dateStart = new DateTime();
350 - $dateStart->setTimestamp( $start );
351 - $dateStart->setTimezone($this->get_format_timezone());
348 + // If it's before or on the 28th, just jump the number of months and be done with it.
349 + if ( $start_day <= 28 ) {
350 + return strtotime( $interval_multiplier . ' month', $current );
351 + }
352 352
353 - $day = $dateCurrent->format( 'j' );
354 - $month = $dateCurrent->format( 'n' );
355 - $year = $dateCurrent->format( 'Y' );
353 + // If it's on the 29th, 30th, or 31st, it gets tricky. Some months don't have those days - so on those
354 + // months we need to repeat on the last day of the month instead, but we also need to jump back to the
355 + // correct day the following month. Let's say we want to repeat something on the 31st every month: this
356 + // is what we expect to see for a pattern:
357 + //
358 + // .
359 + // .
360 + // .
361 + // December 31st
362 + // January 31st
363 + // February 28th
364 + // March 31st
365 + // April 30th
366 + // .
367 + // .
368 + // .
369 + //
370 + // Unfortunately, PHP relative date handling isn't that smart (add "+1 month" to January 31st, and you
371 + // end up in March), so we'll have to figure it out ourselves by figuring out how many days to jump instead.
356 372
357 - // It can happen, that previous months limited the day of the month to a lower value (28, 29, 30)
358 - // so we make sure, that we use the original day if possible
359 - $dayStart = $dateStart->format( 'j' );
360 - if ($dayStart > $day) {
361 - $day = $dayStart;
362 - }
373 + // We'll need to calculate this for each interval and return the timestamp after the last jump.
374 + $temp_current = $current;
375 + for ( $i = 0; $i < $interval_multiplier; $i++ ) {
376 + // The pattern for jumping will be different in each interval.
377 + /** @noinspection PhpUnusedLocalVariableInspection */
378 + $temp_pattern = '';
363 379
364 - $month++;
365 - if ( $month > 12 ) {
366 - $month = 1;
367 - $year++;
368 - }
380 + // Get the number of days in the month of the current date.
381 + $last_day_this_month = $this->format_timestamp( 't', strtotime( 'this month', $temp_current ) );
369 382
370 - if ( $day > 28 ) {
371 - $dateCurrent->setDate($year, $month, 1);
372 - $day = $dateCurrent->format('t');
373 - if ($day > $dayStart) {
374 - $day = $dayStart;
375 - }
376 - }
383 + // Get the number of days for the next month relative to the current date .
384 + // Subtract 3 days from the next month to counter known month skipping bugs in PHP's relative date
385 + // handling, that being the difference between the shortest possible month (non-leap February - 28 days)
386 + // and the longest (Jan., Mar., May, Jul., Aug., Oct., Dec. - 31 days). This may be fixed in PHP 5.3.x
387 + // but this should be backwards-compatible anyway.
388 + $last_day_next_month = $this->format_timestamp( 't', strtotime( '-3 day next month', $temp_current ) );
377 389
378 - $dateCurrent->setDate($year, $month, $day);
390 + // If the current month is longer than next month, follow this block
391 + if ( $last_day_this_month > $last_day_next_month ) {
392 + // If we're repeating on the last day of this month, jump the number of days next month
393 + if ( $start_day === $last_day_this_month ) {
394 + $temp_pattern = $last_day_next_month . ' days';
395 + } elseif ( $start_day > $last_day_next_month ) {
396 + // If the start day doesn't exist in the next month (i.e., no "31st" in June), jump the
397 + // number of days next month plus the difference between the start day and the number of days this month
398 + $temp_pattern = ( $last_day_this_month + $last_day_next_month - $start_day ) . ' days';
399 + } else {
400 + // Otherwise, jump ahead the number of days in this month
401 + $temp_pattern = $last_day_this_month . ' days';
402 + }
403 + } elseif ( $last_day_this_month < $last_day_next_month ) {
404 + // Or, if the current month is shorter than next month
379 405
380 - return $dateCurrent->getTimestamp();
406 + // If the start day doesn't exist in this month (i.e., no "31st" in June), jump the
407 + // number of days next month plus the difference between the start day and the number of days this month
408 + if ( $start_day >= $last_day_this_month ) {
409 + $temp_pattern = $start_day . ' days';
410 + } else {
411 + // Otherwise, jump ahead the number of days in this month
412 + $temp_pattern = $last_day_this_month . ' days';
413 + }
414 + } else {
415 + // If the current month and next month are equally long, jumping by "1 month" is fine
416 + $temp_pattern = '1 month';
417 + }
418 +
419 + $temp_current = strtotime( $temp_pattern, $temp_current );
420 + }
421 +
422 + return $temp_current;
381 423 }
382 424
383 425 /**
384 426 * Advances a date to the 'n'th weekday of the next month (eg., first Wednesday, third Monday, last Friday, etc.).
385 427 *
386 - * Note: If $ordinal is set to '4' and $day is set to '7', it will return the last day of the month.
428 + * Note: If $ordinal is set to '4' and $day is set to '7', it wil return the last day of the month.
387 429 */
388 430 function get_nth_weekday_of_month( $current, $ordinal, $day ) {
389 431 // First, get the month/year we need to work with
390 - $the_month = $this->format_timestamp( 'm', $current );
432 + $the_month = $this->format_timestamp( 'F', $current );
391 433 $the_year = $this->format_timestamp( 'Y', $current );
392 434 $last_day_this_month = $this->format_timestamp( 't', $current );
393 435
394 436 // Get the time for the $current timestamp
395 - $the_hour = $this->format_timestamp( 'H', $current );
396 - $the_minute = $this->format_timestamp( 'i', $current );
437 + $current_time = $this->format_timestamp( 'g:i A', $current );
438 + $the_day = '';
397 439
398 - // The target day
399 - $the_day = '';
400 -
401 440 if ( 7 === $day ) { // If $day is "day of the month", get the day of month based on the ordinal
402 441 switch ( $ordinal ) {
403 442 case 0:
404 443 $the_day = '1';
@@ -412,9 +451,8 @@
412 451 case 3:
413 452 $the_day = '4';
414 453 break; // Fourth day of the month //
415 454 case 4:
416 - case 5:
417 455 $the_day = $last_day_this_month;
418 456 break; // Last day of the month //
419 457 default:
420 458 $the_day = '1';
@@ -435,9 +473,8 @@
435 473 case 3:
436 474 $day_range = range( 22, 28 );
437 475 break; // Fourth 7 days of the month //
438 476 case 4:
439 - case 5:
440 477 $day_range = range( $last_day_this_month - 6, $last_day_this_month );
441 478 break; // Last 7 days of the month //
442 479 default:
443 480 $day_range = range( 1, 7 );
@@ -443,45 +480,20 @@
443 480 $day_range = range( 1, 7 );
444 481 break;
445 482 }
446 483 foreach ( $day_range as $a_day ) { // ...and find the matching weekday in that range.
447 - $date_test = date_create_from_format('Y-m-d H:i', sprintf( '%04d-%02d-%02d 00:00', $the_year, $the_month, $a_day ), $this->get_format_timezone() );
448 - if ( $date_test->format( 'w') === $day ) {
484 + if ( $this->format_timestamp( 'w', strtotime( $the_month . ' ' . $a_day . ', ' . $the_year ) ) === $day ) {
449 485 $the_day = $a_day;
450 486 break;
451 487 }
452 488 }
489 + }
453 490
454 - // If this should be the fifth day of the month, it may happen
455 - // that the fourth and last day are identical - then you gave
456 - // go on to the next month
457 - if ('5' === $ordinal) {
458 - $day_range = range( 22, 28 );
459 - foreach ( $day_range as $a_day ) { // ...and find the matching weekday in that range.
460 - $date_test = date_create_from_format('Y-m-d H:i', sprintf( '%04d-%02d-%02d 00:00', $the_year, $the_month, $a_day ), $this->get_format_timezone() );
461 - if ( $date_test->format( 'w') === $day ) {
462 - $the_day_previous = $a_day;
463 - break;
464 - }
465 - }
491 + // Build the date string for the correct day and return its timestamp
492 + $pattern = $the_month . ' ' . $the_day . ', ' . $the_year . ', ' . $current_time;
466 493
467 - if ($the_day_previous === $the_day) {
468 - $the_day = 1;
469 - $the_month++;
470 - if ($the_month > 12) {
471 - $the_month = 1;
472 - $the_year++;
473 - }
474 - $date = date_create_from_format( 'Y-m-d H:i', sprintf( '%04d-%02d-%02d %02d:%02d', $the_year, $the_month, $the_day, $the_hour, $the_minute ), $this->get_format_timezone() );
475 - $current_next_month = $date->getTimestamp();
494 + return strtotime( $pattern );
476 495
477 - return $this->get_nth_weekday_of_month( $current_next_month, $ordinal, $day );
478 - }
479 - }
480 - }
481 -
482 - $date = date_create_from_format( 'Y-m-d H:i', sprintf( '%04d-%02d-%02d %02d:%02d', $the_year, $the_month, $the_day, $the_hour, $the_minute ), $this->get_format_timezone() );
483 - return $date->getTimestamp();
484 496 }
485 497
486 498 /**
487 499 * Advances a date by a set number of years
@@ -581,9 +593,9 @@
581 593 $active_periods = array();
582 594 $period_count = 0;
583 595
584 596 $human_readable = (bool) $args['human_readable'];
585 - $freq = (int) $args['freq'];
597 + $freq = $args['freq'];
586 598 $timezone = $args['timezone'];
587 599 $recurr_type = $args['recurr_type'];
588 600 $num_repeat = intval( $args['num_repeat'] );
589 601 $end_date = $args['end_date'];
@@ -666,19 +678,16 @@
666 678 }
667 679 }
668 680
669 681 if ( ! $exception_period && $current > $right_now_t - $day_limit * 86400 ) {
670 - // Adjust current date offset if start DST differs from current DST and we don't have a monthly pattern
682 + // Adjust current date offset if start DST differs from current DST
671 683 $current_adjusted = $current;
672 - if (TIMED_CONTENT_FREQ_MONTHLY !== $freq) {
673 - $current_has_dst = $this->format_timestamp('I', $current);
674 - if ('1' === $start_has_dst && '0' === $current_has_dst) {
675 - $current_adjusted += 3600;
676 - }
677 - if ('0' === $start_has_dst && '1' === $current_has_dst) {
678 - $current_adjusted -= 3600;
679 - }
680 - }
684 + $current_has_dst = $this->format_timestamp( 'I', $current );
685 + if ( '1' === $start_has_dst && '0' === $current_has_dst ) {
686 + $current_adjusted += 3600;
687 + } if ( '0' === $start_has_dst && '1' === $current_has_dst ) {
688 + $current_adjusted -= 3600;
689 + }
681 690
682 691 if ( $current > $right_now_t ) {
683 692 $future_repeats++;
684 693 }
@@ -743,9 +752,9 @@
743 752 );
744 753 } else {
745 754 $current = $temp_current;
746 755 }
747 - break;
756 + break;
748 757 default: // TIMED_CONTENT_FREQ_YEARLY:
749 758 $current = $this->get_next_year( $current, $interval_multiplier );
750 759 break;
751 760 }
@@ -900,9 +909,9 @@
900 909 $action = __( 'Show the content', 'timed-content' );
901 910 } else {
902 911 $action = __( 'Hide the content', 'timed-content' );
903 912 }
904 - $freq = (int)$args['freq'];
913 + $freq = $args['freq'];
905 914 $timezone = $args['timezone'];
906 915 $recurr_type = $args['recurr_type'];
907 916 $num_repeat = intval( $args['num_repeat'] );
908 917 $end_date = $args['end_date'];
@@ -930,9 +939,9 @@
930 939 $instance_end_date,
931 940 $instance_end_time
932 941 );
933 942
934 - if ( TIMED_CONTENT_FREQ_HOURLY === $freq ) {
943 + if ( 0 === $freq ) {
935 944 $desc .= '<br />' . sprintf(
936 945 /* translators: %d numerical hours value */
937 946 _n(
938 947 'Repeat this action every %d hour.',
@@ -941,9 +950,9 @@
941 950 'timed-content'
942 951 ),
943 952 $interval_multiplier
944 953 );
945 - } elseif ( TIMED_CONTENT_FREQ_DAILY === $freq ) {
954 + } elseif ( 1 === $freq ) {
946 955 $desc .= '<br />' . sprintf(
947 956 /* translators: %d numerical days value */
948 957 _n(
949 958 'Repeat this action every %d day.',
@@ -952,9 +961,9 @@
952 961 'timed-content'
953 962 ),
954 963 $interval_multiplier
955 964 );
956 - } elseif ( TIMED_CONTENT_FREQ_WEEKLY === $freq ) {
965 + } elseif ( 2 === $freq ) {
957 966 if ( ( $days_of_week ) && ( is_array( $days_of_week ) ) ) {
958 967 $days = array();
959 968 $days_list = '';
960 969 foreach ( $days_of_week as $v ) {
@@ -1081,9 +1090,9 @@
1081 1090 ),
1082 1091 $interval_multiplier
1083 1092 );
1084 1093 }
1085 - } elseif ( TIMED_CONTENT_FREQ_MONTHLY === $freq ) {
1094 + } elseif ( 3 === $freq ) {
1086 1095 if ( 'yes' === $monthly_pattern ) {
1087 1096 if ( 1 === $interval_multiplier ) {
1088 1097 $desc .= '<br />' . sprintf(
1089 1098 /* translators: %1$s: recurrence, %2$s: weekday */
@@ -1119,9 +1128,9 @@
1119 1128 ),
1120 1129 $interval_multiplier
1121 1130 );
1122 1131 }
1123 - } elseif ( TIMED_CONTENT_FREQ_YEARLY === $freq ) {
1132 + } elseif ( 4 === $freq ) {
1124 1133 $desc .= '<br />' . sprintf(
1125 1134 /* translators: %d: year count */
1126 1135 _n(
1127 1136 'Repeat this action every %d year.',
@@ -1491,9 +1500,9 @@
1491 1500 $show_time = substr( $show, 0, $pos );
1492 1501 $show_tzname = substr( $show, $pos + 1 );
1493 1502 } else {
1494 1503 $show_time = $show;
1495 - $show_tzname = wp_timezone_string();
1504 + $show_tzname = date_default_timezone_get();
1496 1505 }
1497 1506 try {
1498 1507 $show_tz = new DateTimeZone( $show_tzname );
1499 1508 } catch ( Exception $e ) {
@@ -1506,9 +1515,9 @@
1506 1515 $hide_time = substr( $hide, 0, $pos );
1507 1516 $hide_tzname = substr( $hide, $pos + 1 );
1508 1517 } else {
1509 1518 $hide_time = $hide;
1510 - $hide_tzname = wp_timezone_string();
1519 + $hide_tzname = date_default_timezone_get();
1511 1520 }
1512 1521 try {
1513 1522 $hide_tz = new DateTimeZone( $hide_tzname );
1514 1523 } catch ( Exception $e ) {
@@ -2275,9 +2284,9 @@
2275 2284 array(
2276 2285 'name' => 'monthly_nth_weekday_of_month_nth',
2277 2286 'display' => 'none',
2278 2287 'title' => __( 'Weekday ordinal', 'timed-content' ),
2279 - 'description' => __( 'Select a value for week of the month (for example "first", "second", etc.). Please note, that "fifth" will skip months, where the day is not repeated five times!', 'timed-content' ),
2288 + 'description' => __( 'Select a value for week of the month (for example "first", "second", etc.).', 'timed-content' ),
2280 2289 'type' => 'list',
2281 2290 'default' => 0,
2282 2291 'values' => $this->rule_ordinal_array,
2283 2292 'scope' => array( TIMED_CONTENT_RULE_TYPE ),
@@ -2510,16 +2519,8 @@
2510 2519 if ( false === $this->current_timezone ) {
2511 2520 $this->current_timezone = new DateTimeZone( 'UTC' );
2512 2521 }
2513 2522 }
2514 -
2515 - /**
2516 - * Get the timezone to be used for format_date()
2517 - */
2518 - function get_format_timezone() {
2519 - return $this->current_timezone;
2520 - }
2521 -
2522 2523 /**
2523 2524 * Format a given timestamp with the specified timezone
2524 2525 */
2525 2526 function format_timestamp( $format, $timestamp ) {
@@ -2524,10 +2525,10 @@
2524 2525 */
2525 2526 function format_timestamp( $format, $timestamp ) {
2526 2527 try {
2527 2528 $dt = new DateTime();
2528 - $dt->setTimestamp( $timestamp );
2529 - $dt->setTimezone( $this->current_timezone );
2529 + $dt->setTimezone( $this->current_timezone );
2530 + $dt->setTimestamp( $timestamp );
2530 2531 } catch ( Exception $e ) {
2531 2532 return '';
2532 2533 }
2533 2534