class-indexable-post-provider.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 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' => 'metadesc',
  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' => 'meta-robots-noindex',
  26. 'is_robots_nofollow' => 'meta-robots-nofollow',
  27. 'primary_focus_keyword' => 'focuskw',
  28. 'primary_focus_keyword_score' => 'linkdex',
  29. 'readability_score' => 'content_score',
  30. ];
  31. /**
  32. * Returns an array with data for the target object.
  33. *
  34. * @param integer $object_id The target object id.
  35. * @param bool $as_object Optional. Whether or not to return the indexable
  36. * as an object. Defaults to false.
  37. *
  38. * @return array|WPSEO_Post_Indexable The retrieved data. Defaults to an array format.
  39. *
  40. * @throws WPSEO_Invalid_Argument_Exception The invalid argument exception.
  41. */
  42. public function get( $object_id, $as_object = false ) {
  43. if ( ! $this->is_indexable( $object_id ) ) {
  44. return [];
  45. }
  46. $indexable = WPSEO_Post_Indexable::from_object( $object_id );
  47. if ( $as_object === true ) {
  48. return $indexable;
  49. }
  50. return $indexable->to_array();
  51. }
  52. /**
  53. * Handles the patching of values for an existing indexable.
  54. *
  55. * @param int $object_id The ID of the object.
  56. * @param array $requestdata The request data to store.
  57. *
  58. * @return array The patched indexable.
  59. *
  60. * @throws WPSEO_Invalid_Indexable_Exception The invalid argument exception.
  61. * @throws WPSEO_REST_Request_Exception Exception that is thrown if patching the object has failed.
  62. */
  63. public function patch( $object_id, $requestdata ) {
  64. $indexable = $this->get( $object_id, true );
  65. if ( $indexable === [] ) {
  66. throw WPSEO_Invalid_Indexable_Exception::non_existing_indexable( $object_id );
  67. }
  68. $new_indexable = $indexable->update( $requestdata );
  69. $stored_indexable = $this->store_indexable( $new_indexable );
  70. if ( $stored_indexable === true ) {
  71. return $new_indexable->to_array();
  72. }
  73. throw WPSEO_REST_Request_Exception::patch( 'Post', $object_id );
  74. }
  75. /**
  76. * Stores the indexable object.
  77. *
  78. * @param WPSEO_Indexable $indexable The indexable object to store.
  79. *
  80. * @return bool True if saving was successful.
  81. */
  82. protected function store_indexable( WPSEO_Indexable $indexable ) {
  83. $values = $this->convert_indexable_data( $indexable->to_array() );
  84. $renamed_values = $this->rename_indexable_data( $values );
  85. foreach ( $renamed_values as $key => $item ) {
  86. WPSEO_Meta::set_value( $key, $item, $values['object_id'] );
  87. }
  88. return true;
  89. }
  90. /**
  91. * Checks if the given object id belongs to an indexable.
  92. *
  93. * @param int $object_id The object id.
  94. *
  95. * @return bool Whether the object id is indexable.
  96. */
  97. public function is_indexable( $object_id ) {
  98. if ( get_post( $object_id ) === null ) {
  99. return false;
  100. }
  101. if ( wp_is_post_autosave( $object_id ) ) {
  102. return false;
  103. }
  104. if ( wp_is_post_revision( $object_id ) ) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. /**
  110. * Converts some of the indexable data to its database variant.
  111. *
  112. * @param array $indexable_data The indexable data to convert.
  113. *
  114. * @return array The converted indexable data.
  115. */
  116. protected function convert_indexable_data( $indexable_data ) {
  117. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_nofollow' ) ) {
  118. $indexable_data['is_robots_nofollow'] = $this->convert_nofollow( $indexable_data['is_robots_nofollow'] );
  119. }
  120. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noindex' ) ) {
  121. $indexable_data['is_robots_noindex'] = $this->convert_noindex( $indexable_data['is_robots_noindex'] );
  122. }
  123. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_cornerstone' ) ) {
  124. $indexable_data['is_cornerstone'] = $this->convert_cornerstone( $indexable_data['is_cornerstone'] );
  125. }
  126. $indexable_data['meta-robots-adv'] = $this->convert_advanced( $indexable_data );
  127. return $indexable_data;
  128. }
  129. /**
  130. * Converts the cornerstone value to its database variant.
  131. *
  132. * @param string $cornerstone_value The cornerstone value.
  133. *
  134. * @return string The converted indexable cornerstone value.
  135. */
  136. protected function convert_cornerstone( $cornerstone_value ) {
  137. if ( $cornerstone_value === 'true' ) {
  138. return '1';
  139. }
  140. return null;
  141. }
  142. /**
  143. * Converts the advanced meta settings to its database variant.
  144. *
  145. * @param array $indexable_data The indexable data to convert the advanced meta settings from.
  146. *
  147. * @return string The converted advanced meta settings.
  148. */
  149. protected function convert_advanced( &$indexable_data ) {
  150. $translated_advanced_data = [];
  151. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_nosnippet' ) && (bool) $indexable_data['is_robots_nosnippet'] === true ) {
  152. $translated_advanced_data[] = 'nosnippet';
  153. unset( $indexable_data['is_robots_nosnippet'] );
  154. }
  155. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noarchive' ) && (bool) $indexable_data['is_robots_noarchive'] === true ) {
  156. $translated_advanced_data[] = 'noarchive';
  157. unset( $indexable_data['is_robots_noarchive'] );
  158. }
  159. if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noimageindex' ) && (bool) $indexable_data['is_robots_noimageindex'] === true ) {
  160. $translated_advanced_data[] = 'noimageindex';
  161. unset( $indexable_data['is_robots_noimageindex'] );
  162. }
  163. return implode( ',', $translated_advanced_data );
  164. }
  165. /**
  166. * Converts the nofollow value to a database compatible one.
  167. *
  168. * @param bool $nofollow The current nofollow value.
  169. *
  170. * @return string The converted value.
  171. */
  172. protected function convert_nofollow( $nofollow ) {
  173. if ( $nofollow === 'true' ) {
  174. return '1';
  175. }
  176. return '0';
  177. }
  178. /**
  179. * Converts the noindex value to a database compatible one.
  180. *
  181. * @param string $noindex The current noindex value.
  182. *
  183. * @return string|null The converted value.
  184. */
  185. protected function convert_noindex( $noindex ) {
  186. if ( $noindex === 'false' ) {
  187. return '2';
  188. }
  189. if ( $noindex === 'true' ) {
  190. return '1';
  191. }
  192. return null;
  193. }
  194. }