class-indexable-term-provider.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Services
  6. */
  7. /**
  8. * Represents the indexable term service.
  9. */
  10. class WPSEO_Indexable_Service_Term_Provider implements WPSEO_Indexable_Service_Provider {
  11. /**
  12. * Returns an array with data for the target object.
  13. *
  14. * @param integer $object_id The target object id.
  15. *
  16. * @return array The retrieved data.
  17. */
  18. public function get( $object_id ) {
  19. $term = get_term( $object_id );
  20. if ( ! $this->is_indexable( $object_id ) ) {
  21. return array();
  22. }
  23. return array(
  24. 'object_id' => (int) $object_id,
  25. 'object_type' => 'term',
  26. 'object_subtype' => $term->taxonomy,
  27. 'permalink' => get_term_link( $term ),
  28. 'canonical' => $this->get_meta_value( 'canonical', $term ),
  29. 'title' => $this->get_meta_value( 'title', $term ),
  30. 'description' => $this->get_meta_value( 'desc', $term ),
  31. 'breadcrumb_title' => $this->get_meta_value( 'bctitle', $term ),
  32. 'og_title' => $this->get_meta_value( 'opengraph-title', $term ),
  33. 'og_description' => $this->get_meta_value( 'opengraph-description', $term ),
  34. 'og_image' => $this->get_meta_value( 'opengraph-image', $term ),
  35. 'twitter_title' => $this->get_meta_value( 'twitter-title', $term ),
  36. 'twitter_description' => $this->get_meta_value( 'twitter-description', $term ),
  37. 'twitter_image' => $this->get_meta_value( 'twitter-image', $term ),
  38. 'is_robots_noindex' => $this->get_robots_noindex_value( $this->get_meta_value( 'noindex', $term ) ),
  39. 'is_robots_nofollow' => null,
  40. 'is_robots_noarchive' => null,
  41. 'is_robots_noimageindex' => null,
  42. 'is_robots_nosnippet' => null,
  43. 'primary_focus_keyword' => $this->get_meta_value( 'focuskw', $term ),
  44. 'primary_focus_keyword_score' => (int) $this->get_meta_value( 'linkdex', $term ),
  45. 'readability_score' => (int) $this->get_meta_value( 'content_score', $term ),
  46. 'is_cornerstone' => false,
  47. 'link_count' => null,
  48. 'incoming_link_count' => null,
  49. 'created_at' => null,
  50. 'updated_at' => null,
  51. );
  52. }
  53. /**
  54. * Checks if the given object id belongs to an indexable.
  55. *
  56. * @param int $object_id The object id.
  57. *
  58. * @return bool Whether the object id is indexable.
  59. */
  60. public function is_indexable( $object_id ) {
  61. $term = get_term( $object_id );
  62. return ( $term !== null && ! is_wp_error( $term ) );
  63. }
  64. /**
  65. * Translates the meta value to a boolean value.
  66. *
  67. * @param string $value The value to translate.
  68. *
  69. * @return bool|null The translated value.
  70. */
  71. protected function get_robots_noindex_value( $value ) {
  72. if ( $value === 'noindex' ) {
  73. return true;
  74. }
  75. if ( $value === 'index' ) {
  76. return false;
  77. }
  78. return null;
  79. }
  80. /**
  81. * Returns the needed term meta field.
  82. *
  83. * @param string $field The requested field.
  84. * @param mixed $term The term object.
  85. *
  86. * @return bool|mixed The value of the requested field.
  87. */
  88. protected function get_meta_value( $field, $term ) {
  89. return WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, $field );
  90. }
  91. }