class-indexable-term-provider.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 extends WPSEO_Indexable_Provider {
  11. /**
  12. * List of fields that need to be renamed.
  13. *
  14. * @var array
  15. */
  16. protected $renameable_fields = [
  17. 'description' => 'desc',
  18. 'breadcrumb_title' => 'bctitle',
  19. 'og_title' => 'opengraph-title',
  20. 'og_description' => 'opengraph-description',
  21. 'og_image' => 'opengraph-image',
  22. 'twitter_title' => 'twitter-title',
  23. 'twitter_description' => 'twitter-description',
  24. 'twitter_image' => 'twitter-image',
  25. 'is_robots_noindex' => 'noindex',
  26. 'primary_focus_keyword' => 'focuskw',
  27. 'primary_focus_keyword_score' => 'linkdex',
  28. 'readability_score' => 'content_score',
  29. ];
  30. /**
  31. * Returns an array with data for the target object.
  32. *
  33. * @param integer $object_id The target object id.
  34. * @param bool $as_object Optional. Whether or not to return the indexable
  35. * as an object. Defaults to false.
  36. *
  37. * @return array|WPSEO_Term_Indexable The retrieved data. Defaults to an array format.
  38. */
  39. public function get( $object_id, $as_object = false ) {
  40. if ( ! $this->is_indexable( $object_id ) ) {
  41. return [];
  42. }
  43. $indexable = WPSEO_Term_Indexable::from_object( $object_id );
  44. if ( $as_object === true ) {
  45. return $indexable;
  46. }
  47. return $indexable->to_array();
  48. }
  49. /**
  50. * Handles the patching of values for an existing indexable.
  51. *
  52. * @param int $object_id The ID of the object.
  53. * @param array $requestdata The request data to store.
  54. *
  55. * @return array The patched indexable.
  56. *
  57. * @throws WPSEO_Invalid_Indexable_Exception The indexable exception.
  58. * @throws WPSEO_REST_Request_Exception Exception that is thrown if patching the object has failed.
  59. */
  60. public function patch( $object_id, $requestdata ) {
  61. $indexable = $this->get( $object_id, true );
  62. if ( $indexable === [] ) {
  63. throw WPSEO_Invalid_Indexable_Exception::non_existing_indexable( $object_id );
  64. }
  65. $new_indexable = $indexable->update( $requestdata );
  66. $stored_indexable = $this->store_indexable( $new_indexable );
  67. if ( $stored_indexable === true ) {
  68. return $new_indexable->to_array();
  69. }
  70. throw WPSEO_REST_Request_Exception::patch( 'Term', $object_id );
  71. }
  72. /**
  73. * Stores the indexable object.
  74. *
  75. * @param WPSEO_Indexable $indexable The indexable object to store.
  76. *
  77. * @return bool True if the indexable object was successfully stored.
  78. */
  79. protected function store_indexable( WPSEO_Indexable $indexable ) {
  80. $values = $this->convert_indexable_data( $indexable->to_array() );
  81. $renamed_values = $this->rename_indexable_data( $values );
  82. $prefixed_values = $this->prefix_indexable_data( $renamed_values );
  83. WPSEO_Taxonomy_Meta::set_values( $values['object_id'], $values['object_subtype'], $prefixed_values );
  84. return true;
  85. }
  86. /**
  87. * Prefixes the indexable data to make it compatible with the database.
  88. *
  89. * @param array $indexable_data The indexable data to prefix.
  90. *
  91. * @return array The compatible indexable data.
  92. */
  93. protected function prefix_indexable_data( $indexable_data ) {
  94. $converted_data = [];
  95. foreach ( $indexable_data as $key => $item ) {
  96. if ( substr( strtolower( $key ), 0, 6 ) !== 'wpseo_' ) {
  97. $key = 'wpseo_' . $key;
  98. }
  99. $converted_data[ $key ] = $item;
  100. }
  101. return $converted_data;
  102. }
  103. /**
  104. * Converts the indexable data to make it compatible with the database.
  105. *
  106. * @param array $indexable_data The indexable data to prepare.
  107. *
  108. * @return array The converted indexable data.
  109. */
  110. protected function convert_indexable_data( $indexable_data ) {
  111. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noindex' ) ) {
  112. $indexable_data['is_robots_noindex'] = $this->convert_noindex( $indexable_data['is_robots_noindex'] );
  113. }
  114. return $indexable_data;
  115. }
  116. /**
  117. * Checks if the given object id belongs to an indexable.
  118. *
  119. * @param int $object_id The object id.
  120. *
  121. * @return bool Whether the object id is indexable.
  122. */
  123. public function is_indexable( $object_id ) {
  124. $term = get_term( $object_id );
  125. return ( $term !== null && ! is_wp_error( $term ) );
  126. }
  127. /**
  128. * Converts the noindex value to a database compatible one.
  129. *
  130. * @param bool $noindex The current noindex value.
  131. *
  132. * @return string|null The converted value.
  133. */
  134. protected function convert_noindex( $noindex ) {
  135. if ( $noindex === 'false' ) {
  136. return 'index';
  137. }
  138. if ( $noindex === 'true' ) {
  139. return 'noindex';
  140. }
  141. return 'default';
  142. }
  143. }