class-wpseo-endpoint-factory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Class WPSEO_Endpoint_Factory.
  9. */
  10. class WPSEO_Endpoint_Factory {
  11. /**
  12. * The valid HTTP methods.
  13. *
  14. * @var array
  15. */
  16. private $valid_http_methods = [
  17. 'GET',
  18. 'PATCH',
  19. 'POST',
  20. 'PUT',
  21. 'DELETE',
  22. ];
  23. /**
  24. * The arguments.
  25. *
  26. * @var array
  27. */
  28. protected $args = [];
  29. /**
  30. * The namespace.
  31. *
  32. * @var string
  33. */
  34. private $namespace;
  35. /**
  36. * The endpoint URL.
  37. *
  38. * @var string
  39. */
  40. private $endpoint;
  41. /**
  42. * The callback to execute if the endpoint is called.
  43. *
  44. * @var callable
  45. */
  46. private $callback;
  47. /**
  48. * The permission callback to execute to determine permissions.
  49. *
  50. * @var callable
  51. */
  52. private $permission_callback;
  53. /**
  54. * The HTTP method to use.
  55. *
  56. * @var string
  57. */
  58. private $method;
  59. /**
  60. * WPSEO_Endpoint_Factory constructor.
  61. *
  62. * @param string $namespace The endpoint's namespace.
  63. * @param string $endpoint The endpoint's URL.
  64. * @param callable $callback The callback function to execute.
  65. * @param callable $permission_callback The permission callback to execute to determine permissions.
  66. * @param string $method The HTTP method to use. Defaults to GET.
  67. *
  68. * @throws WPSEO_Invalid_Argument_Exception The invalid argument exception.
  69. */
  70. public function __construct( $namespace, $endpoint, $callback, $permission_callback, $method = WP_REST_Server::READABLE ) {
  71. if ( ! WPSEO_Validator::is_string( $namespace ) ) {
  72. throw WPSEO_Invalid_Argument_Exception::invalid_string_parameter( $namespace, 'namespace' );
  73. }
  74. $this->namespace = $namespace;
  75. if ( ! WPSEO_Validator::is_string( $endpoint ) ) {
  76. throw WPSEO_Invalid_Argument_Exception::invalid_string_parameter( $endpoint, 'endpoint' );
  77. }
  78. $this->endpoint = $endpoint;
  79. if ( ! is_callable( $callback ) ) {
  80. throw WPSEO_Invalid_Argument_Exception::invalid_callable_parameter( $callback, 'callback' );
  81. }
  82. $this->callback = $callback;
  83. if ( ! is_callable( $permission_callback ) ) {
  84. throw WPSEO_Invalid_Argument_Exception::invalid_callable_parameter( $permission_callback, 'callback' );
  85. }
  86. $this->permission_callback = $permission_callback;
  87. $this->method = $this->validate_method( $method );
  88. }
  89. /**
  90. * Gets the associated arguments.
  91. *
  92. * @return array The arguments.
  93. */
  94. public function get_arguments() {
  95. return $this->args;
  96. }
  97. /**
  98. * Determines whether or not there are any arguments present.
  99. *
  100. * @return bool Whether or not any arguments are present.
  101. */
  102. public function has_arguments() {
  103. return count( $this->args ) > 0;
  104. }
  105. /**
  106. * Registers the endpoint with WordPress.
  107. *
  108. * @return void
  109. */
  110. public function register() {
  111. $config = [
  112. 'methods' => $this->method,
  113. 'callback' => $this->callback,
  114. 'permission_callback' => $this->permission_callback,
  115. ];
  116. if ( $this->has_arguments() ) {
  117. $config['args'] = $this->args;
  118. }
  119. register_rest_route( $this->namespace, $this->endpoint, $config );
  120. }
  121. /**
  122. * Validates the method parameter.
  123. *
  124. * @param string $method The set method parameter.
  125. *
  126. * @return string The validated method.
  127. *
  128. * @throws WPSEO_Invalid_Argument_Exception The invalid argument exception.
  129. * @throws InvalidArgumentException The invalid argument exception.
  130. */
  131. protected function validate_method( $method ) {
  132. if ( ! WPSEO_Validator::is_string( $method ) ) {
  133. throw WPSEO_Invalid_Argument_Exception::invalid_string_parameter( $method, 'method' );
  134. }
  135. if ( ! in_array( $method, $this->valid_http_methods, true ) ) {
  136. throw new InvalidArgumentException( sprintf( '%s is not a valid HTTP method', $method ) );
  137. }
  138. return $method;
  139. }
  140. /**
  141. * Adds an argument to the endpoint.
  142. *
  143. * @param string $name The name of the argument.
  144. * @param string $description The description associated with the argument.
  145. * @param string $type The type of value that can be assigned to the argument.
  146. * @param bool $required Whether or not it's a required argument. Defaults to true.
  147. *
  148. * @return void
  149. */
  150. protected function add_argument( $name, $description, $type, $required = true ) {
  151. if ( in_array( $name, array_keys( $this->args ), true ) ) {
  152. return;
  153. }
  154. $this->args[ $name ] = [
  155. 'description' => $description,
  156. 'type' => $type,
  157. 'required' => $required,
  158. ];
  159. }
  160. }