PluginProbe
Force Refresh / trunk
Force Refresh vtrunk
3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 2.12.1 All 53 releases
force-refresh / includes / api / classes / class-api-handler.php

class-api-handler.php in Force Refresh trunk, at includes/api/classes/class-api-handler.php

89 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Our API handler used for smaller services.
4 *
5 * @package ForceRefresh
6 */
7
8 namespace JordanLeven\Plugins\ForceRefresh\Api;
9
10 use JordanLeven\Plugins\ForceRefresh\Api\Interfaces\Api_Handler_Interface;
11
12 /**
13 * Abstract class used for API handlers.
14 */
15 abstract class Api_Handler implements Api_Handler_Interface {
16
17 /**
18 * The route namespace for our plugin.
19 *
20 * @var string
21 */
22 const NAMESPACE = 'force-refresh';
23
24 /**
25 * Method for getting the namespace for our API endpoints.
26 *
27 * @param int $version The endpoint version.
28 *
29 * @return string The namespace
30 */
31 private static function get_namespace_endpoint( int $version ): string {
32 return sprintf( '%s/v%d', self::NAMESPACE, $version );
33 }
34
35 /**
36 * Method for
37 *
38 * @param string $route The endpoint route.
39 * @param int $version The endpoint version.
40 *
41 * @return string The formatted ReST endpoint.
42 */
43 public static function get_formatted_rest_endpoint( string $route, int $version ): string {
44 $current_blog_id = get_current_blog_id();
45 $namespace = self::get_namespace_endpoint( $version );
46 return get_rest_url( $current_blog_id, $namespace . $route );
47 }
48
49 /**
50 * Method for registering a ReST endpoint in WordPress.
51 *
52 * @param string $route The endpoint to register.
53 * @param int $version The route version number.
54 * @param array $args The arguments.
55 * @param boolean $override True to override existing endpoints.
56 *
57 * @return void
58 */
59 public function register_rest_endpoint( string $route, int $version, $args = array(), $override = false ): void {
60 $namespace = self::get_namespace_endpoint( $version );
61 register_rest_route(
62 $namespace,
63 $route,
64 $args,
65 $override
66 );
67 }
68
69 /**
70 * Method to reply to the client with a response.
71 *
72 * @param int $status_code The standardized HTTP status code.
73 * @param string $message The plaintext message displayed to the user
74 * that explains the status.
75 * @param array $data An array of data to sent as the response.
76 *
77 * @return \WP_REST_Response
78 */
79 public function return_api_response( int $status_code, string $message, $data = array() ): \WP_REST_Response {
80 return new \WP_REST_Response(
81 array(
82 'data' => $data,
83 'message' => $message,
84 ),
85 $status_code
86 );
87 }
88 }
89