class-term-indexable.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Indexables
  6. */
  7. /**
  8. * Class WPSEO_Term_Indexable.
  9. */
  10. class WPSEO_Term_Indexable extends WPSEO_Indexable {
  11. /**
  12. * The updateable fields.
  13. *
  14. * @var array
  15. */
  16. protected $updateable_fields = [
  17. 'canonical',
  18. 'title',
  19. 'description',
  20. 'breadcrumb_title',
  21. 'og_title',
  22. 'og_description',
  23. 'og_image',
  24. 'twitter_title',
  25. 'twitter_description',
  26. 'twitter_image',
  27. 'is_robots_noindex',
  28. 'primary_focus_keyword',
  29. 'primary_focus_keyword',
  30. 'primary_focus_keyword_score',
  31. 'readability_score',
  32. ];
  33. /**
  34. * Creates a new Indexable from a passed object.
  35. *
  36. * @param int $object_id The object ID to create the object for.
  37. *
  38. * @return WPSEO_Indexable The indexable.
  39. *
  40. * @throws WPSEO_Invalid_Argument_Exception Thrown if the passed ID is not for an object of type 'term'.
  41. */
  42. public static function from_object( $object_id ) {
  43. $term = WPSEO_Term_Object_Type::from_object( $object_id );
  44. $term_object_id = $term->get_id();
  45. return new self(
  46. [
  47. 'object_id' => $term_object_id,
  48. 'object_type' => $term->get_type(),
  49. 'object_subtype' => $term->get_subtype(),
  50. 'permalink' => $term->get_permalink(),
  51. 'canonical' => self::get_meta_value( 'canonical', $term ),
  52. 'title' => self::get_meta_value( 'title', $term ),
  53. 'description' => self::get_meta_value( 'desc', $term ),
  54. 'breadcrumb_title' => self::get_meta_value( 'bctitle', $term ),
  55. 'og_title' => self::get_meta_value( 'opengraph-title', $term ),
  56. 'og_description' => self::get_meta_value( 'opengraph-description', $term ),
  57. 'og_image' => self::get_meta_value( 'opengraph-image', $term ),
  58. 'twitter_title' => self::get_meta_value( 'twitter-title', $term ),
  59. 'twitter_description' => self::get_meta_value( 'twitter-description', $term ),
  60. 'twitter_image' => self::get_meta_value( 'twitter-image', $term ),
  61. 'is_robots_noindex' => self::get_robots_noindex_value( self::get_meta_value( 'noindex', $term ) ),
  62. 'is_robots_nofollow' => null,
  63. 'is_robots_noarchive' => null,
  64. 'is_robots_noimageindex' => null,
  65. 'is_robots_nosnippet' => null,
  66. 'primary_focus_keyword' => self::get_meta_value( 'focuskw', $term ),
  67. 'primary_focus_keyword_score' => (int) self::get_meta_value( 'linkdex', $term ),
  68. 'readability_score' => (int) self::get_meta_value( 'content_score', $term ),
  69. 'is_cornerstone' => false,
  70. 'link_count' => null,
  71. 'incoming_link_count' => null,
  72. 'created_at' => null,
  73. 'updated_at' => null,
  74. ]
  75. );
  76. }
  77. /**
  78. * Updates the data and returns a new instance.
  79. *
  80. * @param array $data The data to update into a new instance.
  81. *
  82. * @return WPSEO_Indexable A new instance with the updated data.
  83. */
  84. public function update( $data ) {
  85. $data = array_merge( $this->data, $this->filter_updateable_data( $data ) );
  86. return new self( $data );
  87. }
  88. /**
  89. * Returns the needed term meta field.
  90. *
  91. * @param string $field The requested field.
  92. * @param WPSEO_Term_Object_Type $term The term object.
  93. *
  94. * @return bool|mixed The value of the requested field.
  95. */
  96. protected static function get_meta_value( $field, $term ) {
  97. return WPSEO_Taxonomy_Meta::get_term_meta( $term->get_id(), $term->get_subtype(), $field );
  98. }
  99. /**
  100. * Converts the meta value to a boolean value.
  101. *
  102. * @param string $value The value to convert.
  103. *
  104. * @return bool|null The converted value.
  105. */
  106. protected static function get_robots_noindex_value( $value ) {
  107. if ( $value === 'noindex' ) {
  108. return true;
  109. }
  110. if ( $value === 'index' ) {
  111. return false;
  112. }
  113. return null;
  114. }
  115. }