| @@ -1,11 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hurrytimer; |
| 4 | 4 | |
| 5 | -use DateInterval; | |
| 6 | -use DatePeriod; | |
| 7 | -use DateTime; | |
| 8 | 5 | use Hurrytimer\Dependencies\Carbon\Carbon; |
| 9 | 6 | use Hurrytimer\Dependencies\Carbon\CarbonPeriod; |
| 10 | 7 | use Hurrytimer\Utils\Helpers; |
| 11 | 8 | |
| @@ -42,12 +39,9 @@ | ||
| 42 | 39 | * |
| 43 | 40 | * @var int |
| 44 | 41 | */ |
| 45 | 42 | public $recurringDuration; |
| 46 | - public $recurringPauseDuration = ['days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0]; | |
| 47 | 43 | |
| 48 | - public $recurringDurationOption = 'none'; | |
| 49 | - | |
| 50 | 44 | /** |
| 51 | 45 | * Recurrence start date/time |
| 52 | 46 | * |
| 53 | 47 | * @var string |
| @@ -305,9 +299,9 @@ | ||
| 305 | 299 | * |
| 306 | 300 | * @var int |
| 307 | 301 | */ |
| 308 | 302 | |
| 309 | - public $blockSize = 'auto'; | |
| 303 | + public $blockSize = 50; | |
| 310 | 304 | |
| 311 | 305 | /** |
| 312 | 306 | * Block background color. |
| 313 | 307 | * |
| @@ -505,9 +499,9 @@ | ||
| 505 | 499 | |
| 506 | 500 | public $recurringMonthlyDayType = C::RECURRING_MONTHLY_DAY_OF_MONTH; |
| 507 | 501 | |
| 508 | 502 | |
| 509 | - public $recurringUnselectedDaysAction = 'skip'; | |
| 503 | + public $recurringUnselectedDaysAction = 'hide'; | |
| 510 | 504 | |
| 511 | 505 | public function __construct($id) |
| 512 | 506 | { |
| 513 | 507 | $this->recurringStartTime = Carbon::now(hurryt_tz())->format('Y-m-d h:i A'); |
| @@ -569,9 +563,9 @@ | ||
| 569 | 563 | break; |
| 570 | 564 | } |
| 571 | 565 | } |
| 572 | 566 | return apply_filters('hurryt_show_sticky_bar', false, $this->get_id()); |
| 573 | - | |
| 567 | + | |
| 574 | 568 | case 'exclude_pages': |
| 575 | 569 | $pageIds = $this->getStickyExcludePages(); |
| 576 | 570 | |
| 577 | 571 | if (!empty($pageId) && !empty($pageIds) && in_array($pageId, $pageIds, true)) { |
| @@ -701,13 +695,8 @@ | ||
| 701 | 695 | * @param $data |
| 702 | 696 | */ |
| 703 | 697 | public function storeSettings($data) |
| 704 | 698 | { |
| 705 | - | |
| 706 | - if (!hurrytimer_allow_unfiltered_html()) { | |
| 707 | - $data = wp_kses_post_deep($data); | |
| 708 | - } | |
| 709 | - | |
| 710 | 699 | foreach ($data as $prop => $value) { |
| 711 | 700 | $method = Helpers::snakeToCamelCase("set_{$prop}"); |
| 712 | 701 | if (method_exists($this, $method)) { |
| 713 | 702 | $this->$method($value ?: $this->$prop); |
| @@ -721,9 +710,9 @@ | ||
| 721 | 710 | if (!isset($data['sticky_bar_pages'])) { |
| 722 | 711 | $this->setStickyBarPages([]); |
| 723 | 712 | } |
| 724 | 713 | |
| 725 | - CSS_Builder::get_instance()->generate_css(); | |
| 714 | + CSS_Builder::get_instance()->build(); | |
| 726 | 715 | } |
| 727 | 716 | |
| 728 | 717 | //removeIf(pro) |
| 729 | 718 | |
| @@ -732,11 +721,11 @@ | ||
| 732 | 721 | * ::PROP |
| 733 | 722 | */ |
| 734 | 723 | public function setMode($mode) |
| 735 | 724 | { |
| 736 | - // if ($mode == C::MODE_RECURRING) { | |
| 737 | - // return; | |
| 738 | - // } | |
| 725 | + if ( $mode == C::MODE_RECURRING ) { | |
| 726 | + return; | |
| 727 | + } | |
| 739 | 728 | $this->set_prop('mode', $mode); |
| 740 | 729 | } |
| 741 | 730 | //endRemoveIf(pro) |
| 742 | 731 | |
| @@ -1013,9 +1002,8 @@ | ||
| 1013 | 1002 | return array_pad(array_map('intval', $duration), 4, 0); |
| 1014 | 1003 | } |
| 1015 | 1004 | |
| 1016 | 1005 | |
| 1017 | - | |
| 1018 | 1006 | public function getRestartDuration($asSeconds = false) |
| 1019 | 1007 | { |
| 1020 | 1008 | $duration = (array)$this->get_prop('hurryt_restart_duration', false); |
| 1021 | 1009 | |
| @@ -1032,27 +1020,8 @@ | ||
| 1032 | 1020 | } |
| 1033 | 1021 | return $duration; |
| 1034 | 1022 | } |
| 1035 | 1023 | |
| 1036 | - | |
| 1037 | - public function getRecurringPauseDuration($asSeconds = false) | |
| 1038 | - { | |
| 1039 | - $duration = (array)$this->get_prop('_hurryt_recurring_pause_duration', false); | |
| 1040 | - | |
| 1041 | - $duration = wp_parse_args($duration, $this->recurringPauseDuration); | |
| 1042 | - foreach ($duration as $unit => $value) { | |
| 1043 | - $duration[$unit] = (int)$value; | |
| 1044 | - } | |
| 1045 | - | |
| 1046 | - if ($asSeconds) { | |
| 1047 | - $duration = $duration['days'] * DAY_IN_SECONDS + | |
| 1048 | - $duration['hours'] * HOUR_IN_SECONDS + | |
| 1049 | - $duration['minutes'] * MINUTE_IN_SECONDS + | |
| 1050 | - $duration['seconds']; | |
| 1051 | - } | |
| 1052 | - return $duration; | |
| 1053 | - } | |
| 1054 | - | |
| 1055 | 1024 | public function setRestartDuration($duration_array) |
| 1056 | 1025 | { |
| 1057 | 1026 | $this->set_prop('hurryt_restart_duration', $duration_array); |
| 1058 | 1027 | } |
| @@ -1064,13 +1033,10 @@ | ||
| 1064 | 1033 | public function setRecurringDuration($value) |
| 1065 | 1034 | { |
| 1066 | 1035 | $this->set_prop('_hurryt_recurring_duration', $value); |
| 1067 | 1036 | } |
| 1068 | - public function setRecurringPauseDuration($value) | |
| 1069 | - { | |
| 1070 | - $this->set_prop('_hurryt_recurring_pause_duration', $value); | |
| 1071 | - } | |
| 1072 | 1037 | |
| 1038 | + | |
| 1073 | 1039 | /** |
| 1074 | 1040 | * Returns evergreen duration. |
| 1075 | 1041 | * |
| 1076 | 1042 | * @return array |
| @@ -1206,43 +1172,13 @@ | ||
| 1206 | 1172 | $this->loadSettings(); |
| 1207 | 1173 | |
| 1208 | 1174 | if ($this->isMonthlyRecurring()) { |
| 1209 | 1175 | $current = $this->getRecurrenceEndDate(); |
| 1210 | - | |
| 1211 | 1176 | $next = $this->getRecurrenceEndDate(true); |
| 1212 | - | |
| 1213 | - $duration = $this->durationInSeconds($this->getRecurringDuration()); | |
| 1214 | - $use_custom = $this->recurringDurationOption === 'custom' && $duration > 0; | |
| 1215 | - | |
| 1216 | - if ($use_custom) { | |
| 1217 | - $time_remaining = $next->diffInSeconds($current); | |
| 1218 | - } else { | |
| 1219 | - $time_remaining = $this->getRecurringPauseDuration(true); | |
| 1220 | - } | |
| 1221 | - return $time_remaining; | |
| 1177 | + return absint($next->diffInSeconds($current)); | |
| 1222 | 1178 | } |
| 1223 | - | |
| 1224 | 1179 | list($duration, $pause, $period) = $this->getRecurringDurationInSeconds(); |
| 1225 | - | |
| 1226 | - $t = $pause ?: 0; | |
| 1227 | - | |
| 1228 | - $waitBeforeRestart = $this->getRecurringPauseDuration(true); | |
| 1229 | - | |
| 1230 | - $t += $waitBeforeRestart; | |
| 1231 | - | |
| 1232 | - if (!$this->has_skipped_days()) { | |
| 1233 | - return $t; | |
| 1234 | - } | |
| 1235 | - | |
| 1236 | - if ($this->isWeeklyRecurring()) { | |
| 1237 | - return $t; | |
| 1238 | - } | |
| 1239 | - | |
| 1240 | - if ($this->recurringUnselectedDaysAction === 'hide') { | |
| 1241 | - return $t; | |
| 1242 | - } | |
| 1243 | - | |
| 1244 | - return $waitBeforeRestart; | |
| 1180 | + return $pause ?: 0; | |
| 1245 | 1181 | } |
| 1246 | 1182 | |
| 1247 | 1183 | /** |
| 1248 | 1184 | * Return true if the current campaign can recur today. |
| @@ -1360,24 +1296,13 @@ | ||
| 1360 | 1296 | |
| 1361 | 1297 | /** |
| 1362 | 1298 | * Returns the recurrence end date based on the current date/time. |
| 1363 | 1299 | * |
| 1364 | - * @param bool $next Return the next recurrence end date. | |
| 1365 | - * | |
| 1366 | 1300 | * @return Carbon |
| 1367 | 1301 | */ |
| 1368 | - | |
| 1369 | 1302 | public function getRecurrenceEndDate($next = false) |
| 1370 | 1303 | { |
| 1371 | - | |
| 1372 | - $cached_key = $next ? 'recur_end_date__next' : 'recur_end_date'; | |
| 1373 | - $cached = wp_cache_get($cached_key, 'hurrytimer'); | |
| 1374 | - if ($cached instanceof Carbon) { | |
| 1375 | - return $cached; | |
| 1376 | - } | |
| 1377 | - | |
| 1378 | 1304 | try { |
| 1379 | - | |
| 1380 | 1305 | $this->loadSettings(); |
| 1381 | 1306 | $now = Carbon::now(hurryt_tz()); |
| 1382 | 1307 | $startDate = Carbon::parse($this->recurringStartTime, hurryt_tz()); |
| 1383 | 1308 | $interval = (int)$this->recurringInterval; |
| @@ -1384,8 +1309,9 @@ | ||
| 1384 | 1309 | |
| 1385 | 1310 | |
| 1386 | 1311 | // Handle monthly recurring |
| 1387 | 1312 | if ($this->isMonthlyRecurring()) { |
| 1313 | + | |
| 1388 | 1314 | $start_date_modified = $startDate->copy(); |
| 1389 | 1315 | |
| 1390 | 1316 | if ($startDate->day >= 28) { |
| 1391 | 1317 | $start_date_modified->startOfMonth(); |
| @@ -1400,12 +1326,8 @@ | ||
| 1400 | 1326 | if ($this->shouldEndRecurringByDate()) { |
| 1401 | 1327 | $endDate = Carbon::parse($this->recurringUntil, hurryt_tz()); |
| 1402 | 1328 | } |
| 1403 | 1329 | |
| 1404 | - if ($this->isDayOfWeekRecurring()) { | |
| 1405 | - $endDate = $endDate->addMonths($interval); | |
| 1406 | - } | |
| 1407 | - | |
| 1408 | 1330 | $period = CarbonPeriod::create($start_date_modified, 'P' . $interval . 'M', $endDate); |
| 1409 | 1331 | |
| 1410 | 1332 | if ($this->shouldEndRecurringByRecurrences()) { |
| 1411 | 1333 | $period->setRecurrences($this->getRecurringRecurrences()); |
| @@ -1414,79 +1336,44 @@ | ||
| 1414 | 1336 | if ($period->count() === 0) { |
| 1415 | 1337 | return $period->getEndDate(); |
| 1416 | 1338 | } |
| 1417 | 1339 | |
| 1418 | - $dates = iterator_to_array($period); | |
| 1340 | + $date = $period->first(); | |
| 1341 | + $i = 0; | |
| 1419 | 1342 | |
| 1420 | - $dates = array_map(function ($date) use ($startDate) { | |
| 1421 | - $is_overflow = $startDate->day >= $date->daysInMonth; | |
| 1422 | - if ($this->isDayOfMonthRecurring() && $is_overflow) { | |
| 1343 | + while ($period->valid()) { | |
| 1344 | + | |
| 1345 | + $date = $period->current(); | |
| 1346 | + | |
| 1347 | + $isOverflow = $startDate->day >= $date->daysInMonth; | |
| 1348 | + | |
| 1349 | + if ($this->isDayOfMonthRecurring() && $isOverflow) { | |
| 1423 | 1350 | $date->endOfMonth(); |
| 1424 | 1351 | } |
| 1352 | + | |
| 1425 | 1353 | if ($this->isDayOfWeekRecurring()) { |
| 1426 | 1354 | $date->modifyWeekOfMonth($startDate); |
| 1427 | 1355 | } |
| 1356 | + | |
| 1428 | 1357 | $date->setTimeFrom($startDate); |
| 1429 | 1358 | |
| 1430 | - return $date; | |
| 1431 | - }, $dates); | |
| 1359 | + if ($date->greaterThan($now)) { | |
| 1432 | 1360 | |
| 1433 | - | |
| 1434 | - $out_date = end($dates) ?: $period->getEndDate(); | |
| 1435 | - | |
| 1436 | - reset($dates); | |
| 1437 | - | |
| 1438 | - foreach ($dates as $i => $date) { | |
| 1439 | - $duration = $this->durationInSeconds($this->getRecurringDuration()); | |
| 1440 | - $custom = $this->recurringDurationOption === 'custom' && $duration > 0; | |
| 1441 | - | |
| 1442 | - if ($custom && !$next) { | |
| 1443 | - $custom_end_date = $date->copy()->addSeconds($duration); | |
| 1444 | - | |
| 1445 | - if ($custom_end_date->greaterThan($now)) { | |
| 1446 | - $out_date = $custom_end_date; | |
| 1447 | - | |
| 1448 | - break; | |
| 1361 | + if (!$next || $i === 1) { | |
| 1362 | + return $date; | |
| 1449 | 1363 | } |
| 1450 | 1364 | |
| 1451 | - $next_date = isset($dates[$i + 1]) ? $dates[$i + 1] : $period->getEndDate(); | |
| 1452 | - | |
| 1453 | - if ($now->lessThan($next_date)) { | |
| 1454 | - $out_date = $custom_end_date; | |
| 1455 | - break; | |
| 1456 | - } | |
| 1457 | - | |
| 1458 | - continue; | |
| 1365 | + $i++; | |
| 1459 | 1366 | } |
| 1460 | 1367 | |
| 1461 | - if ($date->greaterThan($now)) { | |
| 1462 | - $pause_duration = $this->getRecurringPauseDuration(true); | |
| 1368 | + $period->next(); | |
| 1369 | + } | |
| 1463 | 1370 | |
| 1464 | - if ($next && !$custom) { | |
| 1465 | - $out_date = isset($dates[$i + 1]) ? $dates[$i + 1] : $out_date; | |
| 1466 | - | |
| 1467 | - if ($pause_duration > 0) { | |
| 1468 | - $out_date = $date; | |
| 1469 | - } | |
| 1470 | - } else { | |
| 1471 | - $out_date = $date; | |
| 1472 | - $prev_end_date = isset($dates[$i - 1]) ? $dates[$i - 1] : null; | |
| 1473 | - if ($prev_end_date && $prev_end_date->copy()->diffInSeconds($now) < $pause_duration) { | |
| 1474 | - $out_date = $prev_end_date; | |
| 1475 | - } | |
| 1476 | - } | |
| 1477 | - | |
| 1478 | - break; | |
| 1479 | - } | |
| 1371 | + if ($this->shouldEndRecurringByDate() && $date->lessThan($now)) { | |
| 1372 | + return $period->getEndDate(); | |
| 1480 | 1373 | } |
| 1481 | - if ($this->shouldEndRecurringByDate() && $out_date->lessThan($now)) { | |
| 1482 | - // TODO: Use the last recurrence instead of `getEndDate()` | |
| 1483 | - $out_date = $period->getEndDate(); | |
| 1484 | - } | |
| 1485 | 1374 | |
| 1486 | - wp_cache_set($cached_key, $out_date, 'hurrytimer'); | |
| 1487 | - | |
| 1488 | - return $out_date; | |
| 1375 | + return $date; | |
| 1489 | 1376 | } // Monthly recurring |
| 1490 | 1377 | |
| 1491 | 1378 | |
| 1492 | 1379 | // Prevent minutely recurring from affecting performance |
| @@ -1499,9 +1386,9 @@ | ||
| 1499 | 1386 | |
| 1500 | 1387 | |
| 1501 | 1388 | list($duration, $pause) = $this->getRecurringDurationInSeconds(); |
| 1502 | 1389 | |
| 1503 | - $period = CarbonPeriod::start($startDate, true); | |
| 1390 | + $period = CarbonPeriod::start($startDate); | |
| 1504 | 1391 | |
| 1505 | 1392 | $period->seconds($duration + $pause); |
| 1506 | 1393 | |
| 1507 | 1394 | if ($this->shouldEndRecurringByRecurrences()) { |
| @@ -1507,41 +1394,38 @@ | ||
| 1507 | 1394 | if ($this->shouldEndRecurringByRecurrences()) { |
| 1508 | 1395 | $period->setRecurrences($this->getRecurringRecurrences()); |
| 1509 | 1396 | } else { |
| 1510 | 1397 | $endDate = $this->getRecurringPeriodEndDate(); |
| 1511 | - $dateperiod_interval = new DateInterval('PT' .($duration + $pause) . 'S'); | |
| 1512 | - $period = new DatePeriod($startDate->toDateTime(), $dateperiod_interval, $endDate->addDays(7 * $interval)->toDateTime()); | |
| 1513 | - // $period->until($endDate->addDays(7 * $interval)); | |
| 1514 | - | |
| 1398 | + $period->until($endDate->addDays(7 * $interval)); | |
| 1515 | 1399 | } |
| 1516 | 1400 | |
| 1517 | - $date = null; | |
| 1401 | + $date = false; | |
| 1402 | + $skip = false; | |
| 1403 | + while ($period->valid()) { | |
| 1518 | 1404 | |
| 1519 | - $i = 0; | |
| 1405 | + $date = $period->current(); | |
| 1406 | + $skip_day = $this->should_skip_day($date); | |
| 1407 | + if ($date->greaterThan($now)) { | |
| 1520 | 1408 | |
| 1521 | - $pause_duration = $this->getRecurringPauseDuration(true); | |
| 1409 | + if ($skip_day) { | |
| 1522 | 1410 | |
| 1523 | - foreach ($period as $start) { | |
| 1411 | + $skip = true; | |
| 1524 | 1412 | |
| 1525 | - $start = Carbon::instance($start); | |
| 1526 | - | |
| 1527 | - $end = $start->copy()->addSeconds($this->getRecurringFrequencyInSeconds() - 1); | |
| 1413 | + if ($this->recurringUnselectedDaysAction === 'hide' || $this->isWeeklyRecurring()) { | |
| 1414 | + return false; | |
| 1415 | + } | |
| 1528 | 1416 | |
| 1529 | - if ($now->isBetween($start, $end->copy()->addSeconds($pause_duration))) { | |
| 1530 | - $date = $start->copy()->addSeconds($duration - 1); | |
| 1531 | - if (!in_array($date->format('w'), $this->recurringDays)) { | |
| 1417 | + $period->next(); | |
| 1532 | 1418 | continue; |
| 1533 | 1419 | } |
| 1534 | - break; | |
| 1535 | - } | |
| 1536 | 1420 | |
| 1537 | - if ($now->isBefore($end->copy()->addSeconds($pause_duration))) { | |
| 1538 | - $date = $start; | |
| 1539 | - if (!in_array($date->format('w'), $this->recurringDays)) { | |
| 1540 | - continue; | |
| 1421 | + if (!$skip) { | |
| 1422 | + $date = $pause ? $date->subSeconds($pause) : $date; | |
| 1541 | 1423 | } |
| 1542 | 1424 | break; |
| 1543 | 1425 | } |
| 1426 | + | |
| 1427 | + $period->next(); | |
| 1544 | 1428 | } |
| 1545 | 1429 | |
| 1546 | 1430 | if (empty($date)) { |
| 1547 | 1431 | return $period->getEndDate(); |
| @@ -1551,9 +1435,8 @@ | ||
| 1551 | 1435 | |
| 1552 | 1436 | if ($this->shouldEndRecurringByDate() && $stop_date->lessThan($date)) { |
| 1553 | 1437 | return $stop_date; |
| 1554 | 1438 | } |
| 1555 | - | |
| 1556 | 1439 | return $date; |
| 1557 | 1440 | } catch (\Exception $e) { |
| 1558 | 1441 | return false; |
| 1559 | 1442 | } |
| @@ -1558,37 +1441,14 @@ | ||
| 1558 | 1441 | return false; |
| 1559 | 1442 | } |
| 1560 | 1443 | } |
| 1561 | 1444 | |
| 1562 | - | |
| 1563 | - function should_hide_today($date, $now) | |
| 1445 | + function should_skip_day($date) | |
| 1564 | 1446 | { |
| 1565 | - return ($date->format('w') === $now->format('w')) && ($this->isWeeklyRecurring() || $this->recurringUnselectedDaysAction === 'hide'); | |
| 1566 | - } | |
| 1567 | - function has_skipped_days() | |
| 1568 | - { | |
| 1569 | 1447 | $recur_days = array_map('intval', $this->recurringDays); |
| 1570 | - return count($recur_days) < 7; | |
| 1448 | + return !in_array($date->format('w'), $recur_days); | |
| 1571 | 1449 | } |
| 1572 | - function can_recur_on($date) | |
| 1573 | - { | |
| 1574 | - $recur_days = array_map('intval', $this->recurringDays); | |
| 1575 | 1450 | |
| 1576 | - if (in_array($date->format('w'), $recur_days)) { | |
| 1577 | - return true; | |
| 1578 | - } | |
| 1579 | - | |
| 1580 | - if (($date->isStartOfDay() && in_array($this->get_previous_day($date->format('w')), $recur_days))) { | |
| 1581 | - return true; | |
| 1582 | - } | |
| 1583 | - | |
| 1584 | - return false; | |
| 1585 | - } | |
| 1586 | - private function get_previous_day($current_day) | |
| 1587 | - { | |
| 1588 | - $current_day === 0 ? 6 : ($current_day - 1); | |
| 1589 | - } | |
| 1590 | - | |
| 1591 | 1451 | /** |
| 1592 | 1452 | * Returns compaign publish datetime. |
| 1593 | 1453 | * |
| 1594 | 1454 | * @return mixed |
| @@ -1781,10 +1641,10 @@ | ||
| 1781 | 1641 | if ($this->is_recurring()) { |
| 1782 | 1642 | $start_date = Carbon::parse($this->recurringStartTime, hurryt_tz()); |
| 1783 | 1643 | $now = Carbon::now(hurryt_tz()); |
| 1784 | 1644 | $now->second = $start_date->second; |
| 1645 | + | |
| 1785 | 1646 | if ($now->lessThan($start_date)) { |
| 1786 | - | |
| 1787 | 1647 | return true; |
| 1788 | 1648 | } |
| 1789 | 1649 | } |
| 1790 | 1650 | |