class-yoast-feature-toggle.php 2.9 KB

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