class-indexable.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $provider = $this->get_provider( strtolower( $object_type ) );
  21. if ( $provider === null ) {
  22. return new WP_REST_Response(
  23. sprintf(
  24. /* translators: %1$s expands to the requested indexable type */
  25. __( 'Unknown type %1$s', 'wordpress-seo' ),
  26. $object_type
  27. ),
  28. 400
  29. );
  30. }
  31. $object_id = $request->get_param( 'object_id' );
  32. if ( ! $provider->is_indexable( $object_id ) ) {
  33. return new WP_REST_Response(
  34. sprintf(
  35. /* translators: %1$s expands to the requested indexable type. %2$s expands to the request id */
  36. __( 'Object %1$s with id %2$s not found', 'wordpress-seo' ),
  37. $object_type,
  38. $object_id
  39. ),
  40. 404
  41. );
  42. }
  43. return new WP_REST_Response( $provider->get( $object_id ) );
  44. }
  45. /**
  46. * Returns a provider based on the given object type.
  47. *
  48. * @param string $object_type The object type to get the provider for.
  49. *
  50. * @return null|WPSEO_Indexable_Service_Provider Instance of the service provider.
  51. */
  52. protected function get_provider( $object_type ) {
  53. if ( $object_type === 'post' ) {
  54. return new WPSEO_Indexable_Service_Post_Provider();
  55. }
  56. if ( $object_type === 'term' ) {
  57. return new WPSEO_Indexable_Service_Term_Provider();
  58. }
  59. return null;
  60. }
  61. }