| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Plans\Infrastructure\Add_Ons; |
| 5 |
|
| 6 |
use InvalidArgumentException; |
| 7 |
use WPSEO_Addon_Manager; |
| 8 |
use Yoast\WP\SEO\Plans\Domain\Add_Ons\Add_On_Interface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Represents a managed add-on. |
| 12 |
* Uses the WPSEO_Addon_Manager to check if the add-on is installed and activated, and if it has a valid license. |
| 13 |
*/ |
| 14 |
abstract class Managed_Add_On implements Add_On_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The slug of the add-on. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected const SLUG = ''; |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the WPSEO_Addon_Manager. |
| 25 |
* |
| 26 |
* @var WPSEO_Addon_Manager |
| 27 |
*/ |
| 28 |
private $addon_manager; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructs the instance. |
| 32 |
* |
| 33 |
* @param WPSEO_Addon_Manager $addon_manager The WPSEO_Addon_Manager. |
| 34 |
* |
| 35 |
* @throws InvalidArgumentException If the slug is not set. |
| 36 |
*/ |
| 37 |
public function __construct( WPSEO_Addon_Manager $addon_manager ) { |
| 38 |
if ( static::SLUG === '' ) { |
| 39 |
throw new InvalidArgumentException( 'The add-on slug must be set.' ); |
| 40 |
} |
| 41 |
|
| 42 |
$this->addon_manager = $addon_manager; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns whether the add-on is installed and activated. |
| 47 |
* |
| 48 |
* @return bool |
| 49 |
*/ |
| 50 |
public function is_active(): bool { |
| 51 |
return $this->addon_manager->is_installed( static::SLUG ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns whether the add-on has an valid license. |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function has_license(): bool { |
| 60 |
return $this->addon_manager->has_valid_subscription( static::SLUG ); |
| 61 |
} |
| 62 |
} |
| 63 |
|