Merged in feature/from-pantheon (pull request #16)
code from pantheon * code from pantheon
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Application;
|
||||
|
||||
/**
|
||||
* Interface for the promotion manager.
|
||||
*/
|
||||
interface Promotion_Manager_Interface {
|
||||
|
||||
/**
|
||||
* Whether the promotion is effective.
|
||||
*
|
||||
* @param string $promotion_name The name of the promotion.
|
||||
*
|
||||
* @return bool Whether the promotion is effective.
|
||||
*/
|
||||
public function is( string $promotion_name ) : bool;
|
||||
|
||||
/**
|
||||
* Get the list of promotions.
|
||||
*
|
||||
* @return array The list of promotions.
|
||||
*/
|
||||
public function get_promotions_list() : array;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Application;
|
||||
|
||||
use Yoast\WP\SEO\Promotions\Domain\Promotion_Interface;
|
||||
|
||||
/**
|
||||
* Class to manage promotional promotions.
|
||||
*
|
||||
* @makePublic
|
||||
*/
|
||||
class Promotion_Manager implements Promotion_Manager_Interface {
|
||||
|
||||
/**
|
||||
* The centralized list of promotions: all promotions should be passed to the constructor.
|
||||
*
|
||||
* @var array<Abstract_Promotion>
|
||||
*/
|
||||
private $promotions_list = [];
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param Promotion_Interface ...$promotions list of promotions.
|
||||
*/
|
||||
public function __construct( Promotion_Interface ...$promotions ) {
|
||||
$this->promotions_list = $promotions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the promotion is effective.
|
||||
*
|
||||
* @param string $promotion_name The name of the promotion.
|
||||
*
|
||||
* @return bool Whether the promotion is effective.
|
||||
*/
|
||||
public function is( string $promotion_name ) : bool {
|
||||
$time = \time();
|
||||
|
||||
foreach ( $this->promotions_list as $promotion ) {
|
||||
if ( $promotion->get_promotion_name() === $promotion_name ) {
|
||||
return $promotion->get_time_interval()->contains( $time );
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of promotions.
|
||||
*
|
||||
* @return array<Abstract_Promotion> The list of promotions.
|
||||
*/
|
||||
public function get_promotions_list() : array {
|
||||
return $this->promotions_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the names of currently active promotions.
|
||||
*
|
||||
* @return array<string> The list of promotions.
|
||||
*/
|
||||
public function get_current_promotions() : array {
|
||||
$current_promotions = [];
|
||||
$time = \time();
|
||||
foreach ( $this->promotions_list as $promotion ) {
|
||||
if ( $promotion->get_time_interval()->contains( $time ) ) {
|
||||
$current_promotions[] = $promotion->get_promotion_name();
|
||||
}
|
||||
}
|
||||
|
||||
return $current_promotions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Domain;
|
||||
|
||||
/**
|
||||
* Abstract class for a promotion.
|
||||
*/
|
||||
abstract class Abstract_Promotion implements Promotion_Interface {
|
||||
|
||||
/**
|
||||
* The promotion name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $promotion_name;
|
||||
|
||||
/**
|
||||
* The time interval in which the promotion is active.
|
||||
*
|
||||
* @var Time_Interval
|
||||
*/
|
||||
private $time_interval;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param string $promotion_name The promotion name.
|
||||
* @param Time_Interval $time_interval The time interval in which the promotion is active.
|
||||
*/
|
||||
public function __construct( string $promotion_name, Time_Interval $time_interval ) {
|
||||
$this->promotion_name = $promotion_name;
|
||||
$this->time_interval = $time_interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the promotion name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_promotion_name() {
|
||||
return $this->promotion_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time interval in which the promotion is active.
|
||||
*
|
||||
* @return Time_Interval
|
||||
*/
|
||||
public function get_time_interval() {
|
||||
return $this->time_interval;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Domain;
|
||||
|
||||
/**
|
||||
* Class to manage the Black Friday checklist promotion.
|
||||
*/
|
||||
class Black_Friday_Checklist_Promotion extends Abstract_Promotion implements Promotion_Interface {
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'black-friday-2023-checklist',
|
||||
new Time_Interval(
|
||||
\gmmktime( 11, 00, 00, 9, 19, 2023 ),
|
||||
\gmmktime( 11, 00, 00, 10, 31, 2023 )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Domain;
|
||||
|
||||
/**
|
||||
* Class to manage the Black Friday promotion.
|
||||
*/
|
||||
class Black_Friday_Promotion extends Abstract_Promotion implements Promotion_Interface {
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'black-friday-2023-promotion',
|
||||
new Time_Interval( \gmmktime( 11, 00, 00, 11, 23, 2023 ), \gmmktime( 11, 00, 00, 11, 28, 2023 ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Domain;
|
||||
|
||||
/**
|
||||
* Interface for a Promotion.
|
||||
*/
|
||||
interface Promotion_Interface {}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Promotions\Domain;
|
||||
|
||||
/**
|
||||
* Class Time_Interval
|
||||
*
|
||||
* Value object for a time interval.
|
||||
*/
|
||||
class Time_Interval {
|
||||
|
||||
/**
|
||||
* The starting time of the interval as a Unix timestamp.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $time_start;
|
||||
|
||||
/**
|
||||
* The ending time of the interval as a Unix timestamp.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $time_end;
|
||||
|
||||
/**
|
||||
* Time_Interval constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param int $time_start Interval start time.
|
||||
* @param int $time_end Interval end time.
|
||||
*/
|
||||
public function __construct( int $time_start, int $time_end ) {
|
||||
$this->time_start = $time_start;
|
||||
$this->time_end = $time_end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given time is within the interval.
|
||||
*
|
||||
* @param int $time The time to check.
|
||||
*
|
||||
* @return bool Whether the given time is within the interval.
|
||||
*/
|
||||
public function contains( int $time ): bool {
|
||||
return ( ( $time > $this->time_start ) && ( $time < $this->time_end ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the interval astarting date.
|
||||
*
|
||||
* @param int $time_start The interval start time.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_start_date( int $time_start ) {
|
||||
$this->time_start = $time_start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the interval ending date.
|
||||
*
|
||||
* @param int $time_end The interval end time.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_end_date( int $time_end ) {
|
||||
$this->time_end = $time_end;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user