class-post-indexable.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Indexables
  6. */
  7. /**
  8. * Class WPSEO_Post_Indexable.
  9. */
  10. class WPSEO_Post_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. 'is_robots_nofollow',
  29. 'is_robots_noarchive',
  30. 'is_robots_noimageindex',
  31. 'is_robots_nosnippet',
  32. 'primary_focus_keyword',
  33. 'primary_focus_keyword',
  34. 'primary_focus_keyword_score',
  35. 'readability_score',
  36. 'is_cornerstone',
  37. ];
  38. /**
  39. * Creates a new Indexable from a passed object.
  40. *
  41. * @param int $object_id The object ID to create the object for.
  42. *
  43. * @return WPSEO_Indexable The indexable.
  44. *
  45. * @throws WPSEO_Invalid_Argument_Exception Thrown if the passed ID is not for an object of type 'post'.
  46. */
  47. public static function from_object( $object_id ) {
  48. $post = WPSEO_Post_Object_Type::from_object( $object_id );
  49. $link_count = new WPSEO_Link_Column_Count();
  50. $link_count->set( [ $object_id ] );
  51. $post_object_id = $post->get_id();
  52. return new self(
  53. [
  54. 'object_id' => $post_object_id,
  55. 'object_type' => $post->get_type(),
  56. 'object_subtype' => $post->get_subtype(),
  57. 'permalink' => $post->get_permalink(),
  58. 'canonical' => WPSEO_Meta::get_value( 'canonical', $post_object_id ),
  59. 'title' => WPSEO_Meta::get_value( 'title', $post_object_id ),
  60. 'description' => WPSEO_Meta::get_value( 'metadesc', $post_object_id ),
  61. 'breadcrumb_title' => WPSEO_Meta::get_value( 'bctitle', $post_object_id ),
  62. 'og_title' => WPSEO_Meta::get_value( 'opengraph-title', $post_object_id ),
  63. 'og_description' => WPSEO_Meta::get_value( 'opengraph-description', $post_object_id ),
  64. 'og_image' => WPSEO_Meta::get_value( 'opengraph-image', $post_object_id ),
  65. 'twitter_title' => WPSEO_Meta::get_value( 'twitter-title', $post_object_id ),
  66. 'twitter_description' => WPSEO_Meta::get_value( 'twitter-description', $post_object_id ),
  67. 'twitter_image' => WPSEO_Meta::get_value( 'twitter-image', $post_object_id ),
  68. 'is_robots_noindex' => self::get_robots_noindex_value( WPSEO_Meta::get_value( 'meta-robots-noindex', $post_object_id ) ),
  69. 'is_robots_nofollow' => WPSEO_Meta::get_value( 'meta-robots-nofollow', $post_object_id ) === '1',
  70. 'is_robots_noarchive' => self::has_advanced_meta_value( $post_object_id, 'noarchive' ),
  71. 'is_robots_noimageindex' => self::has_advanced_meta_value( $post_object_id, 'noimageindex' ),
  72. 'is_robots_nosnippet' => self::has_advanced_meta_value( $post_object_id, 'nosnippet' ),
  73. 'primary_focus_keyword' => WPSEO_Meta::get_value( 'focuskw', $post_object_id ),
  74. 'primary_focus_keyword_score' => (int) WPSEO_Meta::get_value( 'linkdex', $post_object_id ),
  75. 'readability_score' => (int) WPSEO_Meta::get_value( 'content_score', $post_object_id ),
  76. 'is_cornerstone' => WPSEO_Meta::get_value( 'is_cornerstone', $post_object_id ) === '1',
  77. 'link_count' => (int) $link_count->get( $post_object_id ),
  78. 'incoming_link_count' => (int) $link_count->get( $post_object_id, 'incoming_link_count' ),
  79. 'created_at' => null,
  80. 'updated_at' => null,
  81. ]
  82. );
  83. }
  84. /**
  85. * Updates the data and returns a new instance.
  86. *
  87. * @param array $data The data to update into a new instance.
  88. *
  89. * @return WPSEO_Indexable A new instance with the updated data.
  90. */
  91. public function update( $data ) {
  92. $data = array_merge( $this->data, $this->filter_updateable_data( $data ) );
  93. return new self( $data );
  94. }
  95. }