PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.16
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.16
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
← All changes | includes/Campaign.php +510 -209 2.1.52.2.16 View file →
@@ -1,9 +1,11 @@
1 1 <?php
2 2
3 3 namespace Hurrytimer;
4 4
5 -use \Hurrytimer\Utils\Helpers;
5 +use Carbon\Carbon;
6 +use Carbon\CarbonPeriod;
7 +use Hurrytimer\Utils\Helpers;
6 8
7 9 class Campaign
8 10 {
9 11
@@ -32,8 +34,73 @@
32 34 */
33 35 public $duration;
34 36
35 37 /**
38 + * Recurrence duration
39 + *
40 + * @var int
41 + */
42 + public $recurringDuration;
43 +
44 + /**
45 + * Recurrence start date/time
46 + *
47 + * @var string
48 + */
49 + public $recurringStartTime;
50 +
51 + /**
52 + * Recurrence end option
53 + *
54 + * @var int
55 + */
56 + public $recurringEnd = C::RECURRING_END_NEVER;
57 +
58 + /**
59 + * Recurrence frequency
60 + *
61 + * @see C
62 + * @var string
63 + */
64 + public $recurringFrequency = C::RECURRING_DAILY;
65 +
66 + /**
67 + * Recurrence interval
68 + *
69 + * @var int
70 + */
71 + public $recurringInterval = 1;
72 +
73 + /**
74 + * Recurrence count
75 + *
76 + * @var int
77 + */
78 + public $recurringCount = 2;
79 +
80 + /**
81 + * Recurrence end date/time
82 + *
83 + * @var
84 + */
85 + public $recurringUntil;
86 +
87 + /**
88 + * Recurrence allowed days
89 + *
90 + * @var array
91 + */
92 + public $recurringDays = [ 0, 1, 2, 3, 4, 5, 6 ];
93 +
94 + /**
95 + * Recurrence timezone
96 + *
97 + * @todo Not used
98 + * @var mixed|string|void
99 + */
100 + public $recurringTimezone;
101 +
102 + /**
36 103 * Restart option after expiration.
37 104 *
38 105 * @see \Hurrytimer\CampaignRestart
39 106 *
@@ -125,14 +192,14 @@
125 192 *
126 193 * @var array
127 194 */
128 195 public $labels
129 - = [
130 - 'days' => 'days',
131 - 'hours' => 'hrs',
132 - 'minutes' => 'mins',
133 - 'seconds' => 'secs',
134 - ];
196 + = [
197 + 'days' => 'days',
198 + 'hours' => 'hrs',
199 + 'minutes' => 'mins',
200 + 'seconds' => 'secs',
201 + ];
135 202
136 203 /**
137 204 * Digit color.
138 205 *
@@ -202,8 +269,10 @@
202 269 * @var int
203 270 */
204 271 public $wcProductsSelectionType;
205 272
273 + public $wcConditions;
274 +
206 275 /**
207 276 * Timer block border color.
208 277 *
209 278 * @var string
@@ -343,21 +412,21 @@
343 412 *
344 413 * @var array
345 414 */
346 415 public $callToAction
347 - = [
348 - 'enabled' => C::NO,
349 - 'new_tab' => C::NO,
350 - 'url' => '',
351 - 'text' => 'Learn More',
352 - 'text_size' => 15,
353 - 'text_color' => '#fff',
354 - 'bg_color' => '#000',
355 - 'y_padding' => 10,
356 - 'x_padding' => 15,
357 - 'border_radius' => 3,
358 - 'spacing' => 5,
359 - ];
416 + = [
417 + 'enabled' => C::NO,
418 + 'new_tab' => C::NO,
419 + 'url' => '',
420 + 'text' => 'Learn More',
421 + 'text_size' => 15,
422 + 'text_color' => '#fff',
423 + 'bg_color' => '#000',
424 + 'y_padding' => 10,
425 + 'x_padding' => 15,
426 + 'border_radius' => 3,
427 + 'spacing' => 5,
428 + ];
360 429
361 430 /**
362 431 * Campaign elements display
363 432 * values: block,inline
@@ -394,12 +463,14 @@
394 463 * @var integer
395 464 */
396 465 public $campaignYPadding = 10;
397 466
398 - public function __construct($id)
467 + public function __construct( $id )
399 468 {
400 -
401 - $this->id = $id;
469 + // Default recurring timezone.
470 + $this->recurringTimezone = Utils\Helpers::getSiteTimezone();
471 + $this->recurringStartTime = Carbon::now( hurryt_tz() )->format( 'Y-m-d h:i A' );
472 + $this->id = $id;
402 473 }
403 474
404 475 /**
405 476 * Returns compaign post iD.
@@ -410,61 +481,69 @@
410 481 {
411 482 return $this->id;
412 483 }
413 484
414 - public function showStickyBarOn($pageId)
485 + public function showStickyBarOn( $pageId )
415 486 {
416 - if ($this->enableSticky === C::NO) {
417 - return false;
487 + if ( $this->enableSticky === C::NO ) {
488 + return true;
418 489 }
419 490
420 - switch ($this->getStickyBarDisplayOn()) {
491 + $can_show = false;
492 + switch ( $this->getStickyBarDisplayOn() ) {
421 493 case 'all_pages':
422 - return true;
494 + $can_show = true;
495 + break;
423 496 case 'wc_products_pages':
424 -
425 - if (function_exists('is_product') && is_product()) {
497 + if ( function_exists( 'is_product' ) && is_product() ) {
426 498 $wc_campaign = new WCCampaign();
427 - return ($wc_campaign->hasActiveCampaign($this, $pageId));
499 + $can_show = ( $wc_campaign->hasActiveCampaign( $this, $pageId ) );
500 + } else {
501 + $can_show = false;
428 502 }
429 - return false;
430 -
503 + break;
431 504 case 'specific_pages':
432 - $pages = $this->getStickyBarPages();
433 - foreach ($pages as $page) {
434 - if ($page == $pageId) {
435 - return true;
436 - }
437 - }
505 + $pages = $this->getStickyBarPages();
506 + $can_show = in_array( $pageId, $pages );
507 + break;
438 508 }
439 509
440 - return false;
510 + return apply_filters( 'hurryt_show_sticky_bar', $can_show, $this->getId() );
441 511
442 512 }
443 513
444 514 public function getStickyBarPages()
445 515 {
446 - return $this->getRawSetting('sticky_bar_pages');
516 + $pagesIds = $this->getRawSetting( 'sticky_bar_pages' );
517 +
518 + return array_filter( array_map( 'intval', $pagesIds ) );
447 519 }
448 520
521 + public function setStickyBarPages( $value )
522 + {
523 + $this->storeRawSetting( 'sticky_bar_pages', $value );
524 + }
525 +
449 526 public function getStickyBarDisplayOn()
450 527 {
451 528
452 529 // backward compat.
453 - $all_pages = $this->getRawSetting('sticky_bar_show_on_all_pages', false);
530 + $all_pages = $this->getRawSetting( 'sticky_bar_show_on_all_pages', false );
454 531
455 - if ($all_pages === C::YES) {
456 - $this->setStickyBarDisplayOn('all_pages');
532 + if ( $all_pages === C::YES ) {
533 + $this->setStickyBarDisplayOn( 'all_pages' );
457 534 }
458 535
459 - $this->deleteRawSetting('sticky_bar_show_on_all_pages');
536 + $this->deleteRawSetting( 'sticky_bar_show_on_all_pages' );
460 537
461 - return $this->getRawSetting('_hurryt_sticky_bar_display_on');
538 + $value = $this->getRawSetting( '_hurryt_sticky_bar_display_on', false );
539 +
540 + return ! empty( $value ) ? $value : $this->stickyBarDisplayOn;
462 541 }
463 542
464 - public function setStickyBarDisplayOn($value)
543 + public function setStickyBarDisplayOn( $value )
465 544 {
466 - $this->storeRawSetting('_hurryt_sticky_bar_display_on', $value);
545 + $this->storeRawSetting( '_hurryt_sticky_bar_display_on', $value );
467 546 }
468 547
469 548 /**
470 549 * Get raw setting.
@@ -473,25 +552,30 @@
473 552 * @param boolean
474 553 *
475 554 * @return mixed
476 555 */
477 - public function getRawSetting($name, $useDefault = true)
556 + public function getRawSetting( $name, $useDefault = true )
478 557 {
479 - $value = get_post_meta($this->id, $name, true);
480 - if (!empty($value)) {
558 + $value = get_post_meta( $this->id, $name, true );
559 + if ( ! empty( $value ) ) {
481 560 return $value;
482 561 }
483 - if ($useDefault) {
484 - return $this->{Helpers::snakeToCamelCase($name)};
562 + if ( $useDefault ) {
563 + return $this->{Helpers::snakeToCamelCase( $name )};
485 564 }
486 565
487 566 return $value;
488 567 }
489 568
490 - public function storeRawSetting($name, $value)
569 + public function storeRawSetting( $name, $value )
491 570 {
492 - $value = !empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)};
493 - update_post_meta($this->id, $name, $value);
571 + $value = ! empty( $value ) ? $value : '';
572 + $getterMethod = Helpers::snakeToCamelCase( $name );
573 + if ( empty( $value ) && method_exists( $this, $getterMethod ) ) {
574 + $value = $this->{$getterMethod}();
575 + }
576 +
577 + update_post_meta( $this->id, $name, $value );
494 578 }
495 579
496 580 /**
497 581 * Bulk save for compaign settings.
@@ -497,24 +581,40 @@
497 581 * Bulk save for compaign settings.
498 582 *
499 583 * @param $data
500 584 */
501 - public function storeSettings($data)
585 + public function storeSettings( $data )
502 586 {
503 -
504 - foreach ($data as $prop => $value) {
505 - $method = Helpers::snakeToCamelCase("set_{$prop}");
506 - if (method_exists($this, $method)) {
507 - $this->$method($value ?: $this->$prop);
508 - } elseif (property_exists(__CLASS__, Helpers::snakeToCamelCase($prop))) {
509 - $this->storeRawSetting($prop, $value);
587 + foreach ( $data as $prop => $value ) {
588 + $method = Helpers::snakeToCamelCase( "set_{$prop}" );
589 + if ( method_exists( $this, $method ) ) {
590 + $this->$method( $value ?: $this->$prop );
591 + } elseif ( property_exists( __CLASS__, Helpers::snakeToCamelCase( $prop ) ) ) {
592 + $this->storeRawSetting( $prop, $value );
510 593 }
511 594 }
512 -
595 + if ( ! isset( $data[ 'wc_conditions' ] ) ) {
596 + $this->setWcConditions( [] );
597 + }
598 + if ( ! isset( $data[ 'sticky_bar_pages' ] ) ) {
599 + $this->setStickyBarPages( [] );
600 + }
513 601 self::buildCss();
514 602 }
515 603
604 + //removeIf(pro)
516 605 /**
606 + * @param int $mode
607 + */
608 + public function setMode($mode)
609 + {
610 + if($mode == C::MODE_RECURRING){
611 + return;
612 + }
613 + $this->storeRawSetting('mode',$mode);
614 + }
615 + //endRemoveIf(pro)
616 + /**
517 617 * Build User CSS,
518 618 * Then merge it with base one.
519 619 *
520 620 * @return void
@@ -524,33 +624,36 @@
524 624
525 625 // Build and return CSS
526 626 ob_start();
527 627 include HURRYT_DIR . 'includes/css.php';
528 - $css = ob_get_clean();
529 - $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css');
628 + $css = ob_get_clean();
629 + $base = file_get_contents( HURRYT_DIR . '/assets/css/base.css' );
530 630
531 631 // Merge built and base css.
532 632 $base .= $css;
533 633
534 634 // Save to public file.
535 - file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base);
635 + file_put_contents( HURRYT_DIR . '/assets/css/hurrytimer.css', $base );
536 636 }
537 637
538 - /**
539 - * Load settings from database,
540 - * and set object props according their matched name.
541 - */
638 + private function setRecurringDates( $dates )
639 + {
640 + foreach ( $dates as $d ) {
641 + add_post_meta( $this->getId(), '_hurryt_recurring_dates', $d );
642 + }
643 + }
644 +
542 645 public function loadSettings()
543 646 {
544 647
545 - $reflection = new \ReflectionObject($this);
546 - foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
547 - $name = $prop->getName();
548 - $method = 'get' . ucfirst($name);
549 - if (method_exists($this, $method)) {
648 + $reflection = new \ReflectionObject( $this );
649 + foreach ( $reflection->getProperties( \ReflectionProperty::IS_PUBLIC ) as $prop ) {
650 + $name = $prop->getName();
651 + $method = 'get' . ucfirst( $name );
652 + if ( method_exists( $this, $method ) ) {
550 653 $this->{$name} = $this->$method();
551 654 } else {
552 - $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name));
655 + $this->{$name} = $this->getRawSetting( Helpers::camelToSnakeCase( $name ) );
553 656 }
554 657 }
555 658 }
556 659
@@ -560,14 +663,14 @@
560 663 * @return mixed
561 664 */
562 665 public function getHeadline()
563 666 {
564 - return $this->id ? get_the_title($this->id) : $this->headline;
667 + return $this->id ? get_the_title( $this->id ) : $this->headline;
565 668 }
566 669
567 670 public function isPublished()
568 671 {
569 - return get_post_status($this->id) === "publish";
672 + return get_post_status( $this->id ) === "publish";
570 673 }
571 674
572 675 public function isWcEnabled()
573 676 {
@@ -580,24 +683,30 @@
580 683 * @return bool
581 684 */
582 685 public function isEvergreen()
583 686 {
584 - return $this->getRawSetting('mode') == C::MODE_EVERGREEN;
687 + return $this->getRawSetting( 'mode' ) == C::MODE_EVERGREEN;
585 688 }
586 689
587 690 public function isRegular()
588 691 {
589 - return $this->getRawSetting('mode') == C::MODE_REGULAR;
692 + return $this->getRawSetting( 'mode' ) == C::MODE_REGULAR;
590 693 }
591 694
695 + public function isRecurring()
696 + {
697 + return $this->getRawSetting( 'mode' ) == C::MODE_RECURRING;
698 +
699 + }
700 +
592 701 /**
593 702 * Save compaign endtime for regular mode.
594 703 *
595 704 * @param $date
596 705 */
597 - public function setEndDatetime($date)
706 + public function setEndDatetime( $date )
598 707 {
599 - $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime());
708 + $this->storeRawSetting( 'end_datetime', $date ?: $this->defaultEndDatetime() );
600 709 }
601 710
602 711 /**
603 712 * Returns default end datetime for regular mode.
@@ -605,9 +714,9 @@
605 714 * @return false|string
606 715 */
607 716 private function defaultEndDatetime()
608 717 {
609 - return date('Y-m-d h:i A', strtotime('+1 week'));
718 + return date( 'Y-m-d h:i A', strtotime( '+1 week' ) );
610 719 }
611 720
612 721 /**
613 722 * Save digit color.
@@ -613,11 +722,11 @@
613 722 * Save digit color.
614 723 *
615 724 * @param $color
616 725 */
617 - public function setDigitColor($color)
726 + public function setDigitColor( $color )
618 727 {
619 - $this->storeRawSetting('digit_color', $color);
728 + $this->storeRawSetting( 'digit_color', $color );
620 729 }
621 730
622 731 /**
623 732 * Returns headline visibility.
@@ -628,22 +737,23 @@
628 737 public function getHeadlineVisibility()
629 738 {
630 739 $meta = 'headline_visibility';
631 740
632 - $value = $this->getRawSetting($meta, false);
633 - if ($value === C::NO || $value === C::YES) {
741 + $value = $this->getRawSetting( $meta, false );
742 + if ( $value === C::NO || $value === C::YES ) {
634 743 return $value;
635 744 }
636 745
637 - $legacyMeta = 'display_headline';
638 - $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN);
746 + $legacyMeta = 'display_headline';
747 + $legacyValue = filter_var( $this->getLegacySetting( $legacyMeta ),
748 + FILTER_VALIDATE_BOOLEAN );
639 749 $legacyValue = $legacyValue ? C::YES : C::NO;
640 750
641 - $this->storeRawSetting($meta, $legacyValue);
751 + $this->storeRawSetting( $meta, $legacyValue );
642 752
643 - $this->deleteRawSetting($legacyMeta);
753 + $this->deleteRawSetting( $legacyMeta );
644 754
645 - return $this->getRawSetting($meta);
755 + return $this->getRawSetting( $meta );
646 756 }
647 757
648 758 /**
649 759 * Returns digit color.
@@ -652,31 +762,31 @@
652 762 * @return mixed
653 763 */
654 764 public function getDigitColor()
655 765 {
656 - $value = $this->getRawSetting('digit_color', false);
657 - if ($value) {
766 + $value = $this->getRawSetting( 'digit_color', false );
767 + if ( $value ) {
658 768 return $value;
659 769 }
660 770
661 - $legacy = $this->getLegacySetting('text_color');
662 - if (!$legacy) {
771 + $legacy = $this->getLegacySetting( 'text_color' );
772 + if ( ! $legacy ) {
663 773 return $this->digitColor;
664 774 }
665 775
666 - $this->setDigitColor($legacy);
667 - if (!$this->getRawSetting('label_color', false)) {
668 - $this->setLabelColor($legacy);
776 + $this->setDigitColor( $legacy );
777 + if ( ! $this->getRawSetting( 'label_color', false ) ) {
778 + $this->setLabelColor( $legacy );
669 779 }
670 780
671 - $this->deleteRawSetting('text_color');
781 + $this->deleteRawSetting( 'text_color' );
672 782
673 - return $this->getRawSetting('digit_color');
783 + return $this->getRawSetting( 'digit_color' );
674 784 }
675 785
676 - public function setLabelColor($color)
786 + public function setLabelColor( $color )
677 787 {
678 - $this->storeRawSetting('label_color', $color);
788 + $this->storeRawSetting( 'label_color', $color );
679 789 }
680 790
681 791 /**
682 792 * Get label color
@@ -684,26 +794,26 @@
684 794 * @return mixed
685 795 */
686 796 public function getLabelColor()
687 797 {
688 - $value = $this->getRawSetting('label_color', false);
689 - if ($value) {
798 + $value = $this->getRawSetting( 'label_color', false );
799 + if ( $value ) {
690 800 return $value;
691 801 }
692 802
693 - $legacy = $this->getLegacySetting('text_color');
694 - if (!$legacy) {
803 + $legacy = $this->getLegacySetting( 'text_color' );
804 + if ( ! $legacy ) {
695 805 return $this->labelColor;
696 806 }
697 807
698 - $this->setLabelColor($legacy);
699 - if (!$this->getRawSetting('digit_color', false)) {
700 - $this->setDigitColor($legacy);
808 + $this->setLabelColor( $legacy );
809 + if ( ! $this->getRawSetting( 'digit_color', false ) ) {
810 + $this->setDigitColor( $legacy );
701 811 }
702 812
703 - $this->deleteRawSetting('text_color');
813 + $this->deleteRawSetting( 'text_color' );
704 814
705 - return $this->getRawSetting('label_color');
815 + return $this->getRawSetting( 'label_color' );
706 816
707 817 }
708 818
709 819 /**
@@ -715,24 +825,24 @@
715 825 * @since 1.2.4
716 826 */
717 827 public function getDigitSize()
718 828 {
719 - $meta = 'digit_size';
720 - $value = $this->getRawSetting($meta, false);
721 - if (!empty($value)) {
829 + $meta = 'digit_size';
830 + $value = $this->getRawSetting( $meta, false );
831 + if ( ! empty( $value ) ) {
722 832 return $value;
723 833 }
724 - $legacy = $this->getLegacySetting('text_size');
834 + $legacy = $this->getLegacySetting( 'text_size' );
725 835
726 - if (empty($legacy)) {
836 + if ( empty( $legacy ) ) {
727 837 return $this->digitSize;
728 838 }
729 839
730 - $this->setDigitSize($legacy);
840 + $this->setDigitSize( $legacy );
731 841
732 - $this->deleteRawSetting('text_size');
842 + $this->deleteRawSetting( 'text_size' );
733 843
734 - return $this->getRawSetting('digit_size');
844 + return $this->getRawSetting( 'digit_size' );
735 845 }
736 846
737 847 /**
738 848 * Save timer digit size.
@@ -738,11 +848,11 @@
738 848 * Save timer digit size.
739 849 *
740 850 * @param $size
741 851 */
742 - public function setDigitSize($size)
852 + public function setDigitSize( $size )
743 853 {
744 - $this->storeRawSetting('digit_size', $size);
854 + $this->storeRawSetting( 'digit_size', $size );
745 855 }
746 856
747 857 /**
748 858 * Save timer label size.
@@ -748,16 +858,16 @@
748 858 * Save timer label size.
749 859 *
750 860 * @param $size
751 861 */
752 - public function setLabelSize($size)
862 + public function setLabelSize( $size )
753 863 {
754 - $this->storeRawSetting('label_size', $size);
864 + $this->storeRawSetting( 'label_size', $size );
755 865 }
756 866
757 - public function setHeadlineSize($size)
867 + public function setHeadlineSize( $size )
758 868 {
759 - $this->storeRawSetting('headline_size', $size);
869 + $this->storeRawSetting( 'headline_size', $size );
760 870 }
761 871
762 872 /**
763 873 * Returns evergreen duration.
@@ -765,20 +875,46 @@
765 875 * @return array
766 876 */
767 877 public function getDuration()
768 878 {
769 - $default = [0, 0, 0, 0];
770 - $duration = $this->getRawSetting('duration');
771 - if (is_array($duration)) {
772 - $duration = array_merge($duration, $default);
879 + $default = [ 0, 0, 0, 0 ];
880 + $duration = $this->getRawSetting( 'duration' );
881 + if ( is_array( $duration ) ) {
882 + $duration = array_merge( $duration, $default );
773 883
774 - return array_map('intval', $duration);
884 + return array_map( 'intval', $duration );
775 885 }
776 886
777 887 return $default;
778 888 }
779 889
890 + public function setRecurringDuration( $value )
891 + {
892 + $this->storeRawSetting( '_hurryt_recurring_duration', $value );
893 + }
894 +
895 + public function setRecurringPauseDuration( $value )
896 + {
897 + $this->storeRawSetting( '_hurryt_recurring_pause_duration', $value );
898 + }
899 +
780 900 /**
901 + * Returns evergreen duration.
902 + *
903 + * @return array
904 + */
905 + public function getRecurringDuration()
906 + {
907 + $default = [ 0, 0, 0, 0 ];
908 + $duration = $this->getRawSetting( '_hurryt_recurring_duration', false );
909 + if ( is_array( $duration ) ) {
910 + return array_map( 'intval', array_merge( $duration, $default ) );
911 + }
912 +
913 + return $default;
914 + }
915 +
916 + /**
781 917 * Returns actions array.
782 918 *
783 919 * @return array
784 920 */
@@ -783,62 +919,64 @@
783 919 * @return array
784 920 */
785 921 public function getActions()
786 922 {
787 - $legacy = $this->getLegacySetting('end_action');
788 - if ($legacy) {
789 - $redirectUrl = $this->getLegacySetting('redirect_url');
790 - $actions = [
923 + $legacy = $this->getLegacySetting( 'end_action' );
924 + if ( $legacy ) {
925 + $redirectUrl = $this->getLegacySetting( 'redirect_url' );
926 + $actions = [
791 927 [
792 - 'id' => intval($legacy),
928 + 'id' => intval( $legacy ),
793 929 'redirectUrl' => $redirectUrl,
794 930 ],
795 931 ];
796 - $this->storeRawSetting('actions', $actions);
797 - $this->deleteRawSetting('end_action');
798 - $this->deleteRawSetting('redirect_url');
932 + $this->storeRawSetting( 'actions', $actions );
933 + $this->deleteRawSetting( 'end_action' );
934 + $this->deleteRawSetting( 'redirect_url' );
799 935
800 - return $this->mergeActions($actions);
936 + return $this->mergeActions( $actions );
801 937 }
802 938
803 - return $this->mergeActions($this->getRawSetting('actions'));
939 + return $this->mergeActions( $this->getRawSetting( 'actions' ) );
804 940 }
805 941
806 - private function mergeActions($actions)
942 + private function mergeActions( $actions )
807 943 {
808 944 $defaults = [
809 945 [
810 - 'id' => C::ACTION_NONE,
811 - 'redirectUrl' => '',
812 - 'message' => '',
946 + 'id' => C::ACTION_NONE,
947 + 'redirectUrl' => '',
948 + 'message' => '',
813 949 'wcStockStatus' => '',
814 950 ],
815 951 ];
816 952
817 - if (count($actions) === 0) {
953 + if ( count( $actions ) === 0 ) {
818 954 return $defaults;
819 955 }
820 956
821 - return array_map(function ($action) use ($defaults) {
822 - $action['id'] = (int) $action['id'];
957 + return array_map( function ( $action ) use ( $defaults ) {
958 + $action[ 'id' ] = (int)$action[ 'id' ];
823 959
824 - return array_merge($defaults[0], $action);
825 - }, $actions);
960 + return array_merge( $defaults[ 0 ], $action );
961 + }, $actions );
826 962 }
827 963
828 964 /**
829 965 * Returns evergreen duration in seconds.
830 966 *
967 + * @param array $duration
968 + *
831 969 * @return int
832 970 */
833 - public function getDurationInSeconds()
971 + public function durationArrayToSeconds( $duration = [] )
834 972 {
835 - list($d, $h, $m, $s) = $this->getDuration();
973 + list( $d, $h, $m, $s ) = empty( $duration ) ? $this->getDuration() : $duration;
836 974
837 975 return $d * DAY_IN_SECONDS +
838 - $h * HOUR_IN_SECONDS +
839 - $m * MINUTE_IN_SECONDS +
840 - $s;
976 + $h * HOUR_IN_SECONDS +
977 + $m * MINUTE_IN_SECONDS +
978 + $s;
841 979 }
842 980
843 981 /**
844 982 * Returns end datetime.
@@ -846,12 +984,147 @@
846 984 * @return string
847 985 */
848 986 public function getEndDatetime()
849 987 {
850 - return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime();
988 + return $this->getRawSetting( 'end_datetime' ) ?: $this->defaultEndDatetime();
851 989 }
852 990
853 991 /**
992 + * Return true if the recurrence is expired.
993 + *
994 + * @return bool
995 + */
996 + public function isRecurringExpired()
997 + {
998 + $deadline = $this->getRecurringDeadline();
999 + if ( ! ( $deadline instanceof Carbon ) ) {
1000 + return false;
1001 + }
1002 +
1003 + $now = Carbon::now( hurryt_tz() );
1004 +
1005 + return $now->isAfter( $deadline );
1006 + }
1007 +
1008 + function getTimeToNextRecurrence()
1009 + {
1010 + $this->loadSettings();
1011 + $interval = absint( $this->recurringInterval );
1012 +
1013 + if ( $this->recurringFrequency === C::RECURRING_DAILY ) {
1014 + $frequencyInSeconds = DAY_IN_SECONDS;
1015 + } elseif ( $this->recurringFrequency === C::RECURRING_WEEKLY ) {
1016 + $frequencyInSeconds = WEEK_IN_SECONDS;
1017 +
1018 + } elseif ( $this->recurringFrequency === C::RECURRING_HOURLY ) {
1019 + $frequencyInSeconds = HOUR_IN_SECONDS;
1020 +
1021 + } else {
1022 + $frequencyInSeconds = MINUTE_IN_SECONDS;
1023 + }
1024 +
1025 + $duration = $this->durationArrayToSeconds( $this->getRecurringDuration() );
1026 +
1027 + return max( 0, ( $frequencyInSeconds * $interval ) - $duration );
1028 + }
1029 +
1030 + /**
1031 + * Return true if the current campaign can recur today.
1032 + *
1033 + * @return bool
1034 + */
1035 + public function canRecurToday()
1036 + {
1037 + $this->loadSettings();
1038 + $day = absint( Carbon::now( hurryt_tz() )->format( 'w' ) );
1039 + $recur = in_array( $day, array_map( 'absint', $this->recurringDays ) );
1040 +
1041 + return apply_filters( 'hurryt_recur_today', $recur, $this->getId() );
1042 + }
1043 +
1044 + /**
1045 + * Return current recurrence date
1046 + *
1047 + * @return \Carbon\CarbonInterface|null
1048 + */
1049 + public function getRecurringCurrentDate()
1050 + {
1051 + $this->loadSettings();
1052 + $dtStart = Carbon::parse( $this->recurringStartTime, hurryt_tz() );
1053 + $now = Carbon::now( hurryt_tz() );
1054 +
1055 + $range = CarbonPeriod::since( $dtStart );
1056 + $interval = absint( $this->recurringInterval );
1057 +
1058 + if ( $this->recurringFrequency === C::RECURRING_DAILY ) {
1059 + $range->days( $interval );
1060 + } elseif ( $this->recurringFrequency === C::RECURRING_WEEKLY ) {
1061 + $range->weeks( $interval );
1062 + } elseif ( $this->recurringFrequency === C::RECURRING_HOURLY ) {
1063 + $range->hours( $interval );
1064 + } else {
1065 + $range->minutes( $interval );
1066 + }
1067 +
1068 + // Recurs forever
1069 + if ( absint( $this->recurringEnd ) === C::RECURRING_END_NEVER ) {
1070 + $range->until( $now );
1071 + // Recurs until
1072 + } elseif ( absint( $this->recurringEnd ) === C::RECURRING_END_TIME ) {
1073 + $range->until( Carbon::parse( $this->recurringUntil, hurryt_tz() ) );
1074 +
1075 + $range->addFilter( function ( $date ) use ( $now ) {
1076 + /**
1077 + * @var Carbon $date
1078 + */
1079 + return $date->isBefore( $now );
1080 + } );
1081 +
1082 + // Recurs N times.
1083 + } elseif ( absint( $this->recurringEnd ) === C::RECURRING_END_OCCURRENCES ) {
1084 +
1085 + // Virtual endDate
1086 + $range->until( Carbon::now( hurryt_tz() )->addCenturies( 1 ) );
1087 +
1088 + // Limit to the number of occurences in `$this->recurringCount`
1089 + $range->setRecurrences( absint( $this->recurringCount ) );
1090 +
1091 + // Set the endDate to the last occurence date
1092 + $range->setEndDate( $range->last() );
1093 + $range->addFilter( function ( $date ) use ( $now ) {
1094 +
1095 + /**
1096 + * @var Carbon $date
1097 + */
1098 + return $date->isBefore( $now );
1099 + } );
1100 +
1101 + }
1102 +
1103 + return $range->last();
1104 + }
1105 +
1106 + /**
1107 + * Get the next/current deadline based on the current date.
1108 + *
1109 + * @return null|Carbon
1110 + */
1111 + public function getRecurringDeadline()
1112 + {
1113 + $date = $this->getRecurringCurrentDate();
1114 + if ( ! $date ) {
1115 + return null;
1116 + }
1117 + $date = clone Carbon::instance( $date );
1118 +
1119 + $duration = $this->durationArrayToSeconds( $this->getRecurringDuration() );
1120 +
1121 + $date->addSeconds( $duration );
1122 +
1123 + return $date;
1124 + }
1125 +
1126 + /**
854 1127 * Returns compaign publish datetime.
855 1128 *
856 1129 * @return mixed
857 1130 */
@@ -856,9 +1129,9 @@
856 1129 * @return mixed
857 1130 */
858 1131 public function getStartTimestamp()
859 1132 {
860 - return get_the_date($this->id);
1133 + return get_the_date( $this->id );
861 1134 }
862 1135
863 1136 /**
864 1137 * Returns position in WooCommerce product page.
@@ -866,22 +1139,22 @@
866 1139 * @return mixed
867 1140 */
868 1141 public function getWcPosition()
869 1142 {
870 - $legacy = $this->getLegacySetting('position');
1143 + $legacy = $this->getLegacySetting( 'position' );
871 1144
872 - if (!$legacy) {
873 - return $this->getRawSetting('wc_position');
1145 + if ( ! $legacy ) {
1146 + return $this->getRawSetting( 'wc_position' );
874 1147 }
875 - $this->storeRawSetting('wc_position', $legacy);
876 - $this->deleteRawSetting('position');
1148 + $this->storeRawSetting( 'wc_position', $legacy );
1149 + $this->deleteRawSetting( 'position' );
877 1150
878 1151 return $legacy;
879 1152 }
880 1153
881 - private function getLegacySetting($name)
1154 + private function getLegacySetting( $name )
882 1155 {
883 - return get_post_meta($this->id, $name, true);
1156 + return get_post_meta( $this->id, $name, true );
884 1157 }
885 1158
886 1159 /**
887 1160 * Returns true if WooCommerce integration is enabled.
@@ -889,22 +1162,22 @@
889 1162 * @return mixed
890 1163 */
891 1164 public function getWcEnable()
892 1165 {
893 - $value = $this->getRawSetting('wc_enable', false);
1166 + $value = $this->getRawSetting( 'wc_enable', false );
894 1167
895 - if ($value === C::YES || $value === C::NO) {
1168 + if ( $value === C::YES || $value === C::NO ) {
896 1169 return $value;
897 1170 }
898 1171
899 - $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN);
900 - if ($legacy) {
901 - $this->storeRawSetting('wc_enable', C::YES);
1172 + $legacy = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
1173 + if ( $legacy ) {
1174 + $this->storeRawSetting( 'wc_enable', C::YES );
902 1175 } else {
903 - $this->storeRawSetting('wc_enable', C::NO);
1176 + $this->storeRawSetting( 'wc_enable', C::NO );
904 1177 }
905 1178
906 - return $this->getRawSetting('wc_enable');
1179 + return $this->getRawSetting( 'wc_enable' );
907 1180 }
908 1181
909 1182 /**
910 1183 * Returns true if label should be displayed.
@@ -912,20 +1185,21 @@
912 1185 * @return mixed
913 1186 */
914 1187 public function getLabelVisibility()
915 1188 {
916 - $meta = 'label_visibility';
917 - $value = $this->getRawSetting($meta, false);
918 - if ($value === C::NO || $value === C::YES) {
1189 + $meta = 'label_visibility';
1190 + $value = $this->getRawSetting( $meta, false );
1191 + if ( $value === C::NO || $value === C::YES ) {
919 1192 return $value;
920 1193 }
921 1194
922 - $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN);
1195 + $legacy = filter_var( $this->getLegacySetting( 'display_labels' ),
1196 + FILTER_VALIDATE_BOOLEAN );
923 1197 $legacy = $legacy ? C::YES : C::NO;
924 - $this->storeRawSetting($meta, $legacy);
925 - $this->deleteRawSetting('display_labels');
1198 + $this->storeRawSetting( $meta, $legacy );
1199 + $this->deleteRawSetting( 'display_labels' );
926 1200
927 - return $this->getRawSetting($meta);
1201 + return $this->getRawSetting( $meta );
928 1202
929 1203 }
930 1204
931 1205 /**
@@ -935,9 +1209,9 @@
935 1209 * @return int
936 1210 */
937 1211 public function getRestart()
938 1212 {
939 - return $this->getRawSetting('restart') ?: C::RESTART_NONE;
1213 + return $this->getRawSetting( 'restart' ) ?: C::RESTART_NONE;
940 1214 }
941 1215
942 1216 /**
943 1217 * Returns WooCommerce products selection type.
@@ -945,14 +1219,14 @@
945 1219 * @return string
946 1220 */
947 1221 public function getWcProductsSelectionType()
948 1222 {
949 - $legacy = $this->getLegacySetting('products_type');
950 - if (!$legacy) {
951 - return $this->getRawSetting('wc_products_selection_type');
1223 + $legacy = $this->getLegacySetting( 'products_type' );
1224 + if ( ! $legacy ) {
1225 + return $this->getRawSetting( 'wc_products_selection_type' );
952 1226 }
953 - $this->storeRawSetting('wc_products_selection_type', $legacy);
954 - $this->deleteRawSetting('products_type');
1227 + $this->storeRawSetting( 'wc_products_selection_type', $legacy );
1228 + $this->deleteRawSetting( 'products_type' );
955 1229
956 1230 return $legacy;
957 1231 }
958 1232
@@ -960,11 +1234,11 @@
960 1234 * Delete setting item.
961 1235 *
962 1236 * @param $name
963 1237 */
964 - public function deleteRawSetting($name)
1238 + public function deleteRawSetting( $name )
965 1239 {
966 - delete_post_meta($this->id, $name);
1240 + delete_post_meta( $this->id, $name );
967 1241 }
968 1242
969 1243 /**
970 1244 * Returns products selection IDs.
@@ -972,14 +1246,14 @@
972 1246 * @return array
973 1247 */
974 1248 public function getWcProductsSelection()
975 1249 {
976 - $legacy = $this->getLegacySetting('products');
977 - if (!$legacy) {
978 - return $this->getRawSetting('wc_products_selection');
1250 + $legacy = $this->getLegacySetting( 'products' );
1251 + if ( ! $legacy ) {
1252 + return $this->getRawSetting( 'wc_products_selection' );
979 1253 }
980 - $this->storeRawSetting('wc_products_selection', $legacy);
981 - $this->deleteRawSetting('products');
1254 + $this->storeRawSetting( 'wc_products_selection', $legacy );
1255 + $this->deleteRawSetting( 'products' );
982 1256
983 1257 return $legacy;
984 1258 }
985 1259
@@ -987,12 +1261,12 @@
987 1261 * Store custom labels.
988 1262 *
989 1263 * @param $labels
990 1264 */
991 - public function setLabels($labels)
1265 + public function setLabels( $labels )
992 1266 {
993 - $labels = array_merge($this->labels, array_filter($labels));
994 - update_post_meta($this->id, 'labels', $labels);
1267 + $labels = array_merge( $this->labels, array_filter( $labels ) );
1268 + update_post_meta( $this->id, 'labels', $labels );
995 1269 }
996 1270
997 1271 /**
998 1272 * Returns true if compaign can be published.
@@ -1000,9 +1274,10 @@
1000 1274 * @return bool
1001 1275 */
1002 1276 public function isActive()
1003 1277 {
1004 - return get_post_status($this->id) === "publish" || get_post_status($this->id) === "future";
1278 + return get_post_status( $this->id ) === "publish"
1279 + || get_post_status( $this->id ) === "future";
1005 1280
1006 1281 }
1007 1282
1008 1283 /**
@@ -1011,29 +1286,44 @@
1011 1286 * @return bool
1012 1287 */
1013 1288 public function isScheduled()
1014 1289 {
1015 - return get_post_status($this->getId()) === "future";
1290 + $scheduled = get_post_status( $this->getId() ) === "future";
1291 + if ( $scheduled ) {
1292 + return true;
1293 + }
1294 +
1295 + if ( $this->isRecurring() ) {
1296 + $start_date = Carbon::parse( $this->recurringStartTime, hurryt_tz() );
1297 + $now = Carbon::now( hurryt_tz() );
1298 + if ( $now->isBefore( $start_date ) ) {
1299 + return true;
1300 + }
1301 + }
1302 +
1303 + return $scheduled;
1016 1304 }
1017 1305
1018 1306 /**
1019 1307 * Returns true if fixed campaign datetime is expired.
1020 1308 *
1309 + * @param string|null $date
1310 + *
1021 1311 * @return bool
1022 1312 */
1023 - public function isExpired()
1313 + public function isExpired( $date = null )
1024 1314 {
1025 - $endDate = date_create($this->endDatetime)->format("Y-m-d H:i");
1026 - $today = date_create(current_time("mysql"))->format("Y-m-d H:i");
1315 + $endDate = $date === null ? date_create( $this->endDatetime ) : $date;
1316 + $today = date_create( current_time( "mysql" ) );
1027 1317
1028 1318 return $endDate < $today;
1029 1319 }
1030 1320
1031 - public function isDismissed()
1321 + public function isStickyDismissed()
1032 1322 {
1033 - return isset($_COOKIE[CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed'])
1034 - && $this->stickyBarDismissible === C::YES
1035 - && $this->enableSticky === C::YES;
1323 + return isset( $_COOKIE[ CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed' ] )
1324 + && $this->stickyBarDismissible === C::YES
1325 + && $this->enableSticky === C::YES;
1036 1326 }
1037 1327
1038 1328 /**
1039 1329 * Returns compaign template wrapper.
@@ -1041,13 +1331,13 @@
1041 1331 * @param string
1042 1332 *
1043 1333 * @return string
1044 1334 */
1045 - public function wrapTemplate($content)
1335 + public function wrapTemplate( $content )
1046 1336 {
1047 - $campaignBuilder = new CampaignBuilder($this);
1337 + $campaignBuilder = new CampaignBuilder( $this );
1048 1338
1049 - return $campaignBuilder->build($content);
1339 + return $campaignBuilder->build( $content );
1050 1340 }
1051 1341
1052 1342 /*
1053 1343 * Returns campaign template content.
@@ -1053,9 +1343,20 @@
1053 1343 * Returns campaign template content.
1054 1344 */
1055 1345 public function buildTemplate()
1056 1346 {
1057 - $campaignBuilder = new CampaignBuilder($this);
1347 + $campaignBuilder = new CampaignBuilder( $this );
1058 1348
1059 1349 return $campaignBuilder->template();
1060 1350 }
1351 +
1352 + public function setWcConditions( $value )
1353 + {
1354 + $this->storeRawSetting( '_hurryt_wc_conditions', ! empty( $value ) ? $value : [] );
1355 + }
1356 +
1357 + public function getWcConditions()
1358 + {
1359 + return $this->getRawSetting( '_hurryt_wc_conditions', false );
1360 + }
1361 +
1061 1362 }