class-indexable.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Indexables
  6. */
  7. /**
  8. * Class WPSEO_Indexable.
  9. */
  10. abstract class WPSEO_Indexable {
  11. /**
  12. * The updateable fields.
  13. *
  14. * @var array
  15. */
  16. protected $updateable_fields = [];
  17. /**
  18. * The indexable's data.
  19. *
  20. * @var array
  21. */
  22. protected $data;
  23. /**
  24. * The available validators to run.
  25. *
  26. * @var array
  27. */
  28. protected $validators = [
  29. 'WPSEO_Object_Type_Validator',
  30. 'WPSEO_Link_Validator',
  31. 'WPSEO_Keyword_Validator',
  32. 'WPSEO_Meta_Values_Validator',
  33. 'WPSEO_OpenGraph_Validator',
  34. 'WPSEO_Robots_Validator',
  35. 'WPSEO_Twitter_Validator',
  36. ];
  37. /**
  38. * Indexable constructor.
  39. *
  40. * @param array $data The data to use to construct the indexable.
  41. */
  42. public function __construct( $data ) {
  43. $this->validate_data( $data );
  44. $this->data = $data;
  45. }
  46. /**
  47. * Converts the meta value to a boolean value.
  48. *
  49. * @param string $value The value to convert.
  50. *
  51. * @return bool|null The converted value.
  52. */
  53. protected static function get_robots_noindex_value( $value ) {
  54. if ( $value === '1' ) {
  55. return true;
  56. }
  57. if ( $value === '2' ) {
  58. return false;
  59. }
  60. return null;
  61. }
  62. /**
  63. * Determines whether the advanced robot metas value contains the passed value.
  64. *
  65. * @param int $object_id The ID of the object to check.
  66. * @param string $value The name of the advanced robots meta value to look for.
  67. *
  68. * @return bool Whether or not the advanced robots meta values contains the passed string.
  69. */
  70. protected static function has_advanced_meta_value( $object_id, $value ) {
  71. return strpos( WPSEO_Meta::get_value( 'meta-robots-adv', $object_id ), $value ) !== false;
  72. }
  73. /**
  74. * Validates the data.
  75. *
  76. * @param array $data The data to validate.
  77. *
  78. * @return bool True if all validators have successfully validated.
  79. */
  80. protected function validate_data( $data ) {
  81. foreach ( $this->validators as $validator ) {
  82. // This is necessary to run under PHP 5.2.
  83. $validator_instance = new $validator();
  84. $validator_instance->validate( $data );
  85. }
  86. return true;
  87. }
  88. /**
  89. * Updates the data and returns a new instance.
  90. *
  91. * @param array $data The data to update into a new instance.
  92. *
  93. * @return WPSEO_Indexable A new instance with the updated data.
  94. */
  95. abstract public function update( $data );
  96. /**
  97. * Filters out data that isn't considered updateable and returns a valid dataset.
  98. *
  99. * @param array $data The dataset to filter.
  100. *
  101. * @return array The updateable dataset.
  102. */
  103. public function filter_updateable_data( $data ) {
  104. return array_intersect_key( $data, array_flip( $this->updateable_fields ) );
  105. }
  106. /**
  107. * Returns the data as an array.
  108. *
  109. * @return array The data as an array.
  110. */
  111. public function to_array() {
  112. return $this->data;
  113. }
  114. }