PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 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
Documentation 3 weeks ago Endpoints 3 weeks ago Validator 3 weeks ago Main.php 3 weeks ago Messages.php 3 weeks ago Post_Repository.php 3 weeks ago Service_Provider.php 3 weeks ago
Service_Provider.php
157 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 Tribe__Documentation__Swagger__Builder_Interface as 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 \tad_DI52_ServiceProvider {
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 $this->container->singleton( 'pods.rest-v1.repository', Post_Repository::class );
44
45 $messages = pods_container( 'pods.rest-v1.messages' );
46 $post_repository = pods_container( 'pods.rest-v1.repository' );
47 $validator = pods_container( 'pods.rest-v1.validator' );
48
49 $endpoints = $this->get_endpoints();
50
51 foreach ( $endpoints as $key => $endpoint ) {
52 if ( is_numeric( $key ) ) {
53 $key = $endpoint;
54 }
55
56 $after_build_methods = [];
57
58 if ( method_exists( $endpoint, 'hook' ) ) {
59 $after_build_methods[] = 'hook';
60 }
61
62 $this->container->singleton( $key, new $endpoint( $messages, $post_repository, $validator ), $after_build_methods );
63 }
64
65 $this->hooks();
66 }
67
68 /**
69 * Get the list of endpoints.
70 *
71 * @since 2.8.11
72 *
73 * @return string[] The list of endpoints.
74 */
75 public function get_endpoints() {
76 $endpoints = [
77 'pods.rest-v1.endpoints.pods' => Pods::class,
78 'pods.rest-v1.endpoints.pod' => Pod::class,
79 'pods.rest-v1.endpoints.pod-slug' => Pod_Slug::class,
80 'pods.rest-v1.endpoints.fields' => Fields::class,
81 'pods.rest-v1.endpoints.field' => Field::class,
82 'pods.rest-v1.endpoints.field-slug' => Field_Slug::class,
83 'pods.rest-v1.endpoints.groups' => Groups::class,
84 'pods.rest-v1.endpoints.group' => Group::class,
85 'pods.rest-v1.endpoints.group-slug' => Group_Slug::class,
86 'pods.rest-v1.endpoints.documentation' => Swagger_Documentation::class,
87 ];
88
89 return (array) apply_filters( 'pods_rest_v1_endpoints', $endpoints );
90 }
91
92 /**
93 * Registers the REST API endpoints.
94 *
95 * @since 2.8.0
96 */
97 public function register_endpoints() {
98 /** @var Main $main */
99 $main = pods_container( 'pods.rest-v1.main' );
100
101 $this->namespace = $main->get_pods_route_namespace();
102
103 $endpoints = $this->get_endpoints();
104
105 foreach ( $endpoints as $key => $endpoint ) {
106 if ( is_numeric( $key ) ) {
107 $key = $endpoint;
108 }
109
110 try {
111 $endpoint_obj = pods_container( $key );
112
113 if ( method_exists( $endpoint_obj, 'register_routes' ) ) {
114 $endpoint_obj->register_routes( $this->namespace, true );
115 }
116 } catch ( Exception $exception ) {
117 // Do nothing.
118 }
119 }
120
121 $this->register_endpoint_documentation();
122 }
123
124 /**
125 * Builds, registers and returns the Swagger.io documentation provider endpoint.
126 *
127 * @since 2.8.0
128 *
129 * @return Swagger_Builder_Interface
130 */
131 protected function register_endpoint_documentation() {
132 /** @var Swagger_Builder_Interface $endpoint */
133 $endpoint = pods_container( 'pods.rest-v1.endpoints.documentation' );
134
135 register_rest_route( $this->namespace, '/doc', [
136 'methods' => WP_REST_Server::READABLE,
137 'callback' => [ $endpoint, 'get' ],
138 'permission_callback' => '__return_true',
139 ] );
140
141 //$endpoint->register_definition_provider( 'XYZ', new Tribe__REST__V1__Documentation__XYZ_Definition_Provider() );
142
143 $endpoint->register_documentation_provider( '/doc', $endpoint );
144
145 return $endpoint;
146 }
147
148 /**
149 * Hooks all the methods and actions the class needs.
150 *
151 * @since 2.8.0
152 */
153 protected function hooks() {
154 add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
155 }
156 }
157