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