class-indexable.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Services
  6. */
  7. /**
  8. * Represents the indexable service.
  9. */
  10. class WPSEO_Indexable_Service {
  11. /**
  12. * Retrieves an indexable.
  13. *
  14. * @param WP_REST_Request $request The request object.
  15. *
  16. * @return WP_REST_Response The response.
  17. */
  18. public function get_indexable( WP_REST_Request $request ) {
  19. $object_type = $request->get_param( 'object_type' );
  20. $object_id = $request->get_param( 'object_id' );
  21. try {
  22. $provider = $this->get_provider( $object_type );
  23. $indexable = $provider->get( $object_id );
  24. return new WP_REST_Response( $indexable );
  25. }
  26. catch ( Exception $exception ) {
  27. return new WP_REST_Response( $exception->getMessage(), 500 );
  28. }
  29. }
  30. /**
  31. * Patches an indexable with the request parameters.
  32. *
  33. * @param WP_REST_Request $request The REST API request to process.
  34. *
  35. * @return WP_REST_Response The REST response.
  36. */
  37. public function patch_indexable( WP_REST_Request $request ) {
  38. $object_type = $request->get_param( 'object_type' );
  39. $object_id = $request->get_param( 'object_id' );
  40. try {
  41. $provider = $this->get_provider( $object_type );
  42. $patched_result = $provider->patch( $object_id, $request->get_params() );
  43. return new WP_REST_Response( $patched_result );
  44. }
  45. catch ( Exception $exception ) {
  46. return new WP_REST_Response( $exception->getMessage(), 500 );
  47. }
  48. }
  49. /**
  50. * Returns a provider based on the given object type.
  51. *
  52. * @param string $object_type The object type to get the provider for.
  53. *
  54. * @return WPSEO_Indexable_Service_Provider Instance of the service provider.
  55. *
  56. * @throws WPSEO_Invalid_Argument_Exception The invalid argument exception.
  57. */
  58. protected function get_provider( $object_type ) {
  59. $object_type = strtolower( $object_type );
  60. if ( $object_type === 'post' ) {
  61. return new WPSEO_Indexable_Service_Post_Provider();
  62. }
  63. if ( $object_type === 'term' ) {
  64. return new WPSEO_Indexable_Service_Term_Provider();
  65. }
  66. throw WPSEO_Invalid_Argument_Exception::invalid_callable_parameter( $object_type, 'provider' );
  67. }
  68. /**
  69. * Handles the situation when the object type is unknown.
  70. *
  71. * @param string $object_type The unknown object type.
  72. *
  73. * @return WP_REST_Response The response.
  74. */
  75. protected function handle_unknown_object_type( $object_type ) {
  76. return new WP_REST_Response(
  77. sprintf(
  78. /* translators: %1$s expands to the requested indexable type */
  79. __( 'Unknown type %1$s', 'wordpress-seo' ),
  80. $object_type
  81. ),
  82. 400
  83. );
  84. }
  85. }