| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
|
| 5 |
namespace Yoast\WP\SEO\AI\Free_Sparks\Application; |
| 6 |
|
| 7 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Handles the free sparks started on timestamp. |
| 11 |
*/ |
| 12 |
class Free_Sparks_Handler implements Free_Sparks_Handler_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The key used to store the timestamp when the user started using free sparks. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public const OPTION_KEY = 'ai_free_sparks_started_on'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Holds the options helper instance. |
| 23 |
* |
| 24 |
* @var Options_Helper |
| 25 |
*/ |
| 26 |
private $options_helper; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class constructor. |
| 30 |
* |
| 31 |
* @param Options_Helper $options_helper The options helper. |
| 32 |
*/ |
| 33 |
public function __construct( Options_Helper $options_helper ) { |
| 34 |
$this->options_helper = $options_helper; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Retrieves the timestamp. |
| 39 |
* |
| 40 |
* @param string $format The format in which to return the timestamp. Defaults to 'Y-m-d H:i:s'. |
| 41 |
* |
| 42 |
* @return ?string The timestamp when the user started using free sparks, or null if not set. |
| 43 |
*/ |
| 44 |
public function get( string $format = 'Y-m-d H:i:s' ): ?string { |
| 45 |
$timestamp = $this->options_helper->get( self::OPTION_KEY, null ); |
| 46 |
if ( $timestamp === null ) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
return \gmdate( $format, (int) $timestamp ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Registers the starting of the free sparks. |
| 55 |
* |
| 56 |
* @param ?int $timestamp The timestamp when the user started using free sparks. If null, the current time will be |
| 57 |
* used. |
| 58 |
* |
| 59 |
* @return bool True if the operation was successful, false otherwise. |
| 60 |
*/ |
| 61 |
public function start( ?int $timestamp = null ): bool { |
| 62 |
return (bool) $this->options_helper->set( self::OPTION_KEY, ( $timestamp === null ) ? \time() : $timestamp, 'wpseo' ); |
| 63 |
} |
| 64 |
} |
| 65 |
|