| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class representing a feature toggle. |
| 10 |
*/ |
| 11 |
class Yoast_Feature_Toggle { |
| 12 |
|
| 13 |
/** |
| 14 |
* Feature toggle identifier. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $name = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* Name of the setting the feature toggle is associated with. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $setting = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Whether the feature is premium or not. |
| 29 |
* |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
protected $premium = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Feature toggle label. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $label = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* URL to learn more about the feature. |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $read_more_url = ''; |
| 47 |
|
| 48 |
/** |
| 49 |
* URL to learn more about the premium feature. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
protected $premium_url = ''; |
| 54 |
|
| 55 |
/** |
| 56 |
* Label for the learn more link. |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
protected $read_more_label = ''; |
| 61 |
|
| 62 |
/** |
| 63 |
* Additional help content for the feature. |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
protected $extra = ''; |
| 68 |
|
| 69 |
/** |
| 70 |
* Additional content to be rendered after the toggle. |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
protected $after = ''; |
| 75 |
|
| 76 |
/** |
| 77 |
* Value to specify the feature toggle order. |
| 78 |
* |
| 79 |
* @var string |
| 80 |
*/ |
| 81 |
protected $order = 100; |
| 82 |
|
| 83 |
/** |
| 84 |
* Constructor. |
| 85 |
* |
| 86 |
* Sets the feature toggle arguments. |
| 87 |
* |
| 88 |
* @param array $args { |
| 89 |
* Feature toggle arguments. |
| 90 |
* |
| 91 |
* @type string $name Required. Feature toggle identifier. |
| 92 |
* @type string $setting Required. Name of the setting the feature toggle is associated with. |
| 93 |
* @type string $label Feature toggle label. |
| 94 |
* @type string $read_more_url URL to learn more about the feature. Default empty string. |
| 95 |
* @type string $read_more_label Label for the learn more link. Default empty string. |
| 96 |
* @type string $extra Additional help content for the feature. Default empty string. |
| 97 |
* @type int $order Value to specify the feature toggle order. A lower value indicates |
| 98 |
* a higher priority. Default 100. |
| 99 |
* } |
| 100 |
* |
| 101 |
* @throws InvalidArgumentException Thrown when a required argument is missing. |
| 102 |
*/ |
| 103 |
public function __construct( array $args ) { |
| 104 |
$required_keys = [ 'name', 'setting' ]; |
| 105 |
|
| 106 |
foreach ( $required_keys as $key ) { |
| 107 |
if ( empty( $args[ $key ] ) ) { |
| 108 |
/* translators: %s: argument name */ |
| 109 |
throw new InvalidArgumentException( sprintf( __( '%s is a required feature toggle argument.', 'wordpress-seo' ), $key ) ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
foreach ( $args as $key => $value ) { |
| 114 |
if ( property_exists( $this, $key ) ) { |
| 115 |
$this->$key = $value; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Magic isset-er. |
| 122 |
* |
| 123 |
* @param string $key Key to check whether a value for it is set. |
| 124 |
* |
| 125 |
* @return bool True if set, false otherwise. |
| 126 |
*/ |
| 127 |
public function __isset( $key ) { |
| 128 |
return isset( $this->$key ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Magic getter. |
| 133 |
* |
| 134 |
* @param string $key Key to get the value for. |
| 135 |
* |
| 136 |
* @return mixed Value for the key, or null if not set. |
| 137 |
*/ |
| 138 |
public function __get( $key ) { |
| 139 |
if ( isset( $this->$key ) ) { |
| 140 |
return $this->$key; |
| 141 |
} |
| 142 |
|
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Checks whether the feature for this toggle is enabled. |
| 148 |
* |
| 149 |
* @return bool True if the feature is enabled, false otherwise. |
| 150 |
*/ |
| 151 |
public function is_enabled() { |
| 152 |
return (bool) WPSEO_Options::get( $this->setting ); |
| 153 |
} |
| 154 |
} |
| 155 |
|