Skip to content

Commit 30e8c06

Browse files
authored
Merge pull request #255 from codepress/release/3.4.8
Release/3.4.8
2 parents 40d4cf0 + 498a924 commit 30e8c06

File tree

15 files changed

+277
-161
lines changed

15 files changed

+277
-161
lines changed

assets/css/admin-page-columns.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/css/select2.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/select2.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/Admin/Entity/DateRange.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace AC\Admin\Entity;
4+
5+
use DateTime;
6+
7+
class DateRange {
8+
9+
/** @var DateTime */
10+
private $start;
11+
12+
/** @var DateTime */
13+
private $end;
14+
15+
public function __construct( DateTime $start, DateTime $end ) {
16+
$this->start = $start;
17+
$this->end = $end;
18+
}
19+
20+
/**
21+
* @return DateTime
22+
*/
23+
public function get_start() {
24+
return $this->start;
25+
}
26+
27+
/**
28+
* @return DateTime
29+
*/
30+
public function get_end() {
31+
return $this->end;
32+
}
33+
34+
/**
35+
* @param DateTime|null $date
36+
*
37+
* @return bool
38+
*/
39+
public function in_range( DateTime $date = null ) {
40+
if ( null === $date ) {
41+
$date = new DateTime();
42+
}
43+
44+
return $date >= $this->start && $date <= $this->end;
45+
}
46+
47+
}

classes/Admin/Parts/Banner.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,19 @@
33
namespace AC\Admin\Parts;
44

55
use AC\Admin;
6-
use AC\Autoloader;
76
use AC\Integrations;
87
use AC\PluginInformation;
98
use AC\View;
109

1110
class Banner {
1211

1312
/**
14-
* @return Admin\Promo|false
13+
* @return Admin\Promo|null
1514
*/
1615
private function get_active_promotion() {
17-
$classes = Autoloader::instance()->get_class_names_from_dir( 'AC\Admin\Promo' );
16+
$promos = new Admin\PromoCollection();
1817

19-
foreach ( $classes as $class ) {
20-
21-
/* @var Admin\Promo $promo */
22-
$promo = new $class;
23-
24-
if ( $promo->is_active() ) {
25-
return $promo;
26-
}
27-
}
28-
29-
return false;
18+
return $promos->find_active();
3019
}
3120

3221
/**

classes/Admin/Promo.php

Lines changed: 22 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace AC\Admin;
44

5+
use AC\Admin\Entity\DateRange;
6+
57
abstract class Promo {
68

7-
/**
8-
* @var array
9-
*/
10-
private $date_ranges;
9+
/** @var string */
10+
private $slug;
1111

1212
/**
1313
* @var string
@@ -19,10 +19,15 @@ abstract class Promo {
1919
*/
2020
private $discount;
2121

22-
/**
23-
* @var string
24-
*/
25-
private $url;
22+
/** @var DateRange */
23+
private $date_range;
24+
25+
public function __construct( $slug, $title, $discount, DateRange $date_range ) {
26+
$this->slug = sanitize_key( $slug );
27+
$this->title = $title;
28+
$this->discount = $discount;
29+
$this->date_range = $date_range;
30+
}
2631

2732
/**
2833
* @return string
@@ -31,13 +36,6 @@ public function get_title() {
3136
return $this->title;
3237
}
3338

34-
/**
35-
* @param string $title
36-
*/
37-
public function set_title( $title ) {
38-
$this->title = $title;
39-
}
40-
4139
/**
4240
* @return int
4341
*/
@@ -46,94 +44,31 @@ public function get_discount() {
4644
}
4745

4846
/**
49-
* @param int $discount
47+
* @return string
5048
*/
51-
public function set_discount( $discount ) {
52-
$this->discount = $discount;
49+
public function get_slug() {
50+
return $this->slug;
5351
}
5452

5553
/**
56-
* @return int
54+
* @return string
5755
*/
5856
public function get_url() {
59-
if ( null === $this->url ) {
60-
61-
$campaign = str_replace( get_parent_class( $this ) . '_', '', get_class( $this ) );
62-
63-
$this->set_url( ac_get_site_utm_url( 'pricing-purchase', 'promo', null, $campaign ) );
64-
}
65-
66-
return $this->url;
67-
}
68-
69-
/**
70-
* @param int $url
71-
*/
72-
public function set_url( $url ) {
73-
$this->url = $url;
57+
return ac_get_site_utm_url( 'pricing-purchase', 'promo', null, $this->slug );
7458
}
7559

7660
/**
77-
* @param string $start_date
78-
* @param string $end_date
61+
* @return Entity\DateRange
7962
*/
80-
public function add_date_range( $start_date, $end_date ) {
81-
if ( ! $start_date || ! $end_date ) {
82-
return;
83-
}
84-
85-
$this->date_ranges[] = array(
86-
'start' => $start_date,
87-
'end' => $end_date,
88-
);
63+
public function get_date_range() {
64+
return $this->date_range;
8965
}
9066

9167
/**
9268
* @return bool True when promo is active
9369
*/
9470
public function is_active() {
95-
return $this->get_active_date_range() ? true : false;
96-
}
97-
98-
/**
99-
* Active date range
100-
* @return array|false
101-
*/
102-
private function get_active_date_range() {
103-
$today = date( 'Y-m-d' );
104-
105-
foreach ( $this->date_ranges as $date_range ) {
106-
if ( $today >= $date_range['start'] && $today <= $date_range['end'] ) {
107-
return $date_range;
108-
}
109-
}
110-
111-
return false;
112-
}
113-
114-
/**
115-
* @return bool|string
116-
*/
117-
public function end_date() {
118-
$date_range = $this->get_active_date_range();
119-
120-
return $date_range ? date_i18n( get_option( 'date_format' ), strtotime( $date_range['end'] ) ) : false;
121-
}
122-
123-
/**
124-
* Render HTML
125-
*/
126-
public function display() { ?>
127-
<h3>
128-
<?php echo esc_html( $this->get_title() ); ?>
129-
</h3>
130-
<a target="_blank" href="<?php echo esc_url( $this->get_url() ); ?>" class="acp-button">
131-
<?php echo esc_html( sprintf( __( 'Get %s Off!', 'codepress-admin-columns' ), $this->get_discount() . '%' ) ); ?>
132-
</a>
133-
<p class="nomargin">
134-
<?php echo esc_html( sprintf( __( "Discount is valid until %s", 'codepress-admin-columns' ), $this->end_date() ) ); ?>
135-
</p>
136-
<?php
71+
return $this->date_range->in_range();
13772
}
13873

13974
}

classes/Admin/Promo/BlackFriday.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
namespace AC\Admin\Promo;
44

5+
use AC\Admin\Entity\DateRange;
56
use AC\Admin\Promo;
67

78
class BlackFriday extends Promo {
89

9-
public function __construct() {
10+
public function __construct( DateRange $date_range ) {
1011

11-
$this->set_title( '30% Off from Black Friday until Cyber Monday' );
12-
$this->set_discount( 30 );
13-
14-
// 2016
15-
$this->add_date_range( '2016-11-25', '2016-11-29' );
16-
17-
// 2017
18-
$this->add_date_range( '2017-11-24', '2017-11-28' );
19-
20-
// 2018
21-
$this->add_date_range( '2018-11-23', '2018-11-27' );
12+
parent::__construct(
13+
'black-friday',
14+
__( '30% Off from Black Friday until Cyber Monday', 'codepress-admin-columns' ),
15+
30,
16+
$date_range
17+
);
2218
}
2319

2420
}

classes/Admin/PromoCollection.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace AC\Admin;
3+
4+
use AC\Admin\Entity\DateRange;
5+
use AC\Admin\Promo\BlackFriday;
6+
use AC\ArrayIterator;
7+
use DateTime;
8+
9+
class PromoCollection extends ArrayIterator {
10+
11+
public function __construct() {
12+
parent::__construct( [
13+
new BlackFriday( new DateRange( new DateTime( '2019-11-28' ), new DateTime( '2019-12-03' ) ) ),
14+
new BlackFriday( new DateRange( new DateTime( '2020-11-26' ), new DateTime( '2020-11-31' ) ) ),
15+
] );
16+
}
17+
18+
/**
19+
* Returns the first active promotion it finds
20+
* @return Promo|null
21+
*/
22+
public function find_active() {
23+
/** @var Promo $promo */
24+
foreach ( $this->array as $promo ) {
25+
if ( $promo->is_active() ) {
26+
return $promo;
27+
}
28+
}
29+
30+
return null;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)