PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.2
Pods – Custom Content Types and Fields v3.3.2
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / REST / V1 / Service_Provider.php
pods / src / Pods / REST / V1 Last commit date
Endpoints 1 year ago Validator 2 years ago Main.php 1 year ago Messages.php 3 years ago Post_Repository.php 2 years ago Service_Provider.php 2 years ago
Service_Provider.php
159 lines
1 <?php
2
3 namespace Pods\REST\V1;
4
5 use Exception;
6 use Pods\REST\V1\Endpoints\Field;
7 use Pods\REST\V1\Endpoints\Field_Slug;
8 use Pods\REST\V1\Endpoints\Fields;
9 use Pods\REST\V1\Endpoints\Group;
10 use Pods\REST\V1\Endpoints\Group_Slug;
11 use Pods\REST\V1\Endpoints\Groups;
12 use Pods\REST\V1\Endpoints\Pod;
13 use Pods\REST\V1\Endpoints\Pod_Slug;
14 use Pods\REST\V1\Endpoints\Pods;
15 use Pods\REST\V1\Endpoints\Swagger_Documentation;
16 use Pods\REST\V1\Validator\Base as Base_Validator;
17 use Pods\REST\Interfaces\Swagger\Builder_Interface;
18 use WP_REST_Server;
19
20 /**
21 * Class Service_Provider
22 *
23 * Add REST API endpoints and objects.
24 *
25 * @since 2.8.0
26 */
27 class Service_Provider extends \Pods\Service_Provider_Base {
28
29 /**
30 * Binds and sets up implementations.
31 */
32 public $namespace;
33
34 /**
35 * Registers the classes and functionality needed for the REST API.
36 *
37 * @since 2.8.0
38 */
39 public function register() {
40 $this->container->singleton( 'pods.rest-v1.main', Main::class );
41 $this->container->singleton( 'pods.rest-v1.messages', Messages::class );
42 $this->container->singleton( 'pods.rest-v1.validator', Base_Validator::class );
43
44 $messages = pods_container( 'pods.rest-v1.messages' );
45 $validator = pods_container( 'pods.rest-v1.validator' );
46
47 $this->container->singleton( 'pods.rest-v1.repository', new Post_Repository( $messages ) );
48
49 $post_repository = pods_container( 'pods.rest-v1.repository' );
50
51 $endpoints = $this->get_endpoints();
52
53 foreach ( $endpoints as $key => $endpoint ) {
54 if ( is_numeric( $key ) ) {
55 $key = $endpoint;
56 }
57
58 $after_build_methods = [];
59
60 if ( method_exists( $endpoint, 'hook' ) ) {
61 $after_build_methods[] = 'hook';
62 }
63
64 $this->container->singleton( $key, new $endpoint( $messages, $post_repository, $validator ), $after_build_methods );
65 }
66
67 $this->hooks();
68 }
69
70 /**
71 * Get the list of endpoints.
72 *
73 * @since 2.8.11
74 *
75 * @return string[] The list of endpoints.
76 */
77 public function get_endpoints() {
78 $endpoints = [
79 'pods.rest-v1.endpoints.pods' => Pods::class,
80 'pods.rest-v1.endpoints.pod' => Pod::class,
81 'pods.rest-v1.endpoints.pod-slug' => Pod_Slug::class,
82 'pods.rest-v1.endpoints.fields' => Fields::class,
83 'pods.rest-v1.endpoints.field' => Field::class,
84 'pods.rest-v1.endpoints.field-slug' => Field_Slug::class,
85 'pods.rest-v1.endpoints.groups' => Groups::class,
86 'pods.rest-v1.endpoints.group' => Group::class,
87 'pods.rest-v1.endpoints.group-slug' => Group_Slug::class,
88 'pods.rest-v1.endpoints.documentation' => Swagger_Documentation::class,
89 ];
90
91 return (array) apply_filters( 'pods_rest_v1_endpoints', $endpoints );
92 }
93
94 /**
95 * Registers the REST API endpoints.
96 *
97 * @since 2.8.0
98 */
99 public function register_endpoints() {
100 /** @var Main $main */
101 $main = pods_container( 'pods.rest-v1.main' );
102
103 $this->namespace = $main->get_pods_route_namespace();
104
105 $endpoints = $this->get_endpoints();
106
107 foreach ( $endpoints as $key => $endpoint ) {
108 if ( is_numeric( $key ) ) {
109 $key = $endpoint;
110 }
111
112 try {
113 $endpoint_obj = pods_container( $key );
114
115 if ( method_exists( $endpoint_obj, 'register_routes' ) ) {
116 $endpoint_obj->register_routes( $this->namespace, true );
117 }
118 } catch ( Exception $exception ) {
119 pods_debug_log( $exception );
120 }
121 }
122
123 $this->register_endpoint_documentation();
124 }
125
126 /**
127 * Builds, registers and returns the Swagger.io documentation provider endpoint.
128 *
129 * @since 2.8.0
130 *
131 * @return Builder_Interface
132 */
133 protected function register_endpoint_documentation() {
134 /** @var Builder_Interface $endpoint */
135 $endpoint = pods_container( 'pods.rest-v1.endpoints.documentation' );
136
137 register_rest_route( $this->namespace, '/doc', [
138 'methods' => WP_REST_Server::READABLE,
139 'callback' => [ $endpoint, 'get' ],
140 'permission_callback' => '__return_true',
141 ] );
142
143 //$endpoint->register_definition_provider( 'XYZ', new XYZ_Definition_Provider() );
144
145 $endpoint->register_documentation_provider( '/doc', $endpoint );
146
147 return $endpoint;
148 }
149
150 /**
151 * Hooks all the methods and actions the class needs.
152 *
153 * @since 2.8.0
154 */
155 protected function hooks() {
156 add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
157 }
158 }
159