class-indexable-post-provider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Services
  6. */
  7. /**
  8. * Represents the indexable post service.
  9. */
  10. class WPSEO_Indexable_Service_Post_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. if ( ! $this->is_indexable( $object_id ) ) {
  20. return array();
  21. }
  22. $link_count = new WPSEO_Link_Column_Count();
  23. $link_count->set( array( $object_id ) );
  24. return array(
  25. 'object_id' => (int) $object_id,
  26. 'object_type' => 'post',
  27. 'object_subtype' => get_post_type( $object_id ),
  28. 'permalink' => get_permalink( $object_id ),
  29. 'canonical' => $this->get_meta_value( 'canonical', $object_id ),
  30. 'title' => $this->get_meta_value( 'title', $object_id ),
  31. 'description' => $this->get_meta_value( 'metadesc', $object_id ),
  32. 'breadcrumb_title' => $this->get_meta_value( 'bctitle', $object_id ),
  33. 'og_title' => $this->get_meta_value( 'opengraph-title', $object_id ),
  34. 'og_description' => $this->get_meta_value( 'opengraph-description', $object_id ),
  35. 'og_image' => $this->get_meta_value( 'opengraph-image', $object_id ),
  36. 'twitter_title' => $this->get_meta_value( 'twitter-title', $object_id ),
  37. 'twitter_description' => $this->get_meta_value( 'twitter-description', $object_id ),
  38. 'twitter_image' => $this->get_meta_value( 'twitter-image', $object_id ),
  39. 'is_robots_noindex' => $this->get_robots_noindex_value( $this->get_meta_value( 'meta-robots-noindex', $object_id ) ),
  40. 'is_robots_nofollow' => $this->get_meta_value( 'meta-robots-nofollow', $object_id ) === '1',
  41. 'is_robots_noarchive' => strpos( $this->get_meta_value( 'meta-robots-adv', $object_id ), 'noarchive' ) !== false,
  42. 'is_robots_noimageindex' => strpos( $this->get_meta_value( 'meta-robots-adv', $object_id ), 'noimageindex' ) !== false,
  43. 'is_robots_nosnippet' => strpos( $this->get_meta_value( 'meta-robots-adv', $object_id ), 'nosnippet' ) !== false,
  44. 'primary_focus_keyword' => $this->get_meta_value( 'focuskw', $object_id ),
  45. 'primary_focus_keyword_score' => (int) $this->get_meta_value( 'linkdex', $object_id ),
  46. 'readability_score' => (int) $this->get_meta_value( 'content_score', $object_id ),
  47. 'is_cornerstone' => $this->get_meta_value( 'is_cornerstone', $object_id ) === '1',
  48. 'link_count' => (int) $link_count->get( $object_id ),
  49. 'incoming_link_count' => (int) $link_count->get( $object_id, 'incoming_link_count' ),
  50. 'created_at' => null,
  51. 'updated_at' => null,
  52. );
  53. }
  54. /**
  55. * Checks if the given object id belongs to an indexable.
  56. *
  57. * @param int $object_id The object id.
  58. *
  59. * @return bool Whether the object id is indexable.
  60. */
  61. public function is_indexable( $object_id ) {
  62. if ( get_post( $object_id ) === null ) {
  63. return false;
  64. }
  65. if ( wp_is_post_autosave( $object_id ) ) {
  66. return false;
  67. }
  68. if ( wp_is_post_revision( $object_id ) ) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * Translates the meta value to a boolean value.
  75. *
  76. * @param string $value The value to translate.
  77. *
  78. * @return bool|null The translated value.
  79. */
  80. protected function get_robots_noindex_value( $value ) {
  81. if ( $value === '1' ) {
  82. return true;
  83. }
  84. if ( $value === '2' ) {
  85. return false;
  86. }
  87. return null;
  88. }
  89. /**
  90. * Returns the needed post meta field.
  91. *
  92. * @param string $field The requested field.
  93. * @param int $post_id The post id.
  94. *
  95. * @return bool|mixed The value of the requested field.
  96. */
  97. protected function get_meta_value( $field, $post_id ) {
  98. return WPSEO_Meta::get_value( $field, $post_id );
  99. }
  100. }