PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.2
Pods – Custom Content Types and Fields v3.2.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 / Abstracts / Main_Abstract.php
pods / src / Pods / REST / Abstracts Last commit date
Main_Abstract.php 2 years ago
Main_Abstract.php
177 lines
1 <?php
2
3 namespace Pods\REST\Abstracts;
4
5 use WP_Rewrite;
6
7 /**
8 * Main abstract.
9 *
10 * @credit The Events Calendar team - https://github.com/the-events-calendar/tribe-common
11 *
12 * @since 3.0
13 */
14 abstract class Main_Abstract {
15
16 /**
17 * The REST APIs URL namespace.
18 *
19 * @since 3.0
20 *
21 * @var string
22 */
23 protected $namespace = 'pods';
24
25 /**
26 * Returns the namespace of REST APIs.
27 *
28 * @since 3.0
29 *
30 * @return string
31 */
32 public function get_namespace() {
33 return $this->namespace;
34 }
35
36 /**
37 * Returns the REST API URL prefix.
38 *
39 * @since 3.0
40 *
41 * @return string The REST API URL prefix.
42 */
43 public function get_url_prefix() {
44 $use_builtin = $this->use_builtin();
45
46 if ( $use_builtin ) {
47 $prefix = rest_get_url_prefix();
48 } else {
49 $prefix = apply_filters( 'rest_url_prefix', 'wp-json' );
50 }
51
52 $default_pods_prefix = $this->namespace . '/' . trim( $this->url_prefix(), '/' );
53 $prefix = rtrim( $prefix, '/' ) . '/' . trim( $default_pods_prefix, '/' );
54
55 /**
56 * Allow filtering the REST API URL prefix.
57 *
58 * @since 3.0
59 *
60 * @param string $prefix The complete URL prefix.
61 * @param string $default_pods_prefix The default URL prefix appended to the REST URL.
62 */
63 return apply_filters( 'pods_rest_url_prefix', $prefix, $default_pods_prefix );
64 }
65
66 /**
67 * Retrieves the URL to a REST endpoint on a site.
68 *
69 * Note: The returned URL is NOT escaped.
70 *
71 * @since 3.0
72 *
73 * @global WP_Rewrite $wp_rewrite
74 *
75 * @param string $path Optional. The REST route. Default '/'.
76 * @param string $scheme Optional. The sanitization scheme. Default 'rest'.
77 * @param int $blog_id Optional. The blog ID. Default of null returns URL for current blog.
78 *
79 * @return string Full URL to the endpoint.
80 */
81 public function get_url( $path = '/', $scheme = 'rest', $blog_id = null ) {
82 if ( empty( $path ) ) {
83 $path = '/';
84 }
85
86 $pods_path = '/' . trim( $this->namespace, '/' ) . $this->url_prefix() . '/' . ltrim( $path, '/' );
87
88 if ( $this->use_builtin() ) {
89 $url = get_rest_url( $blog_id, $pods_path, $scheme );
90 } else {
91 if ( ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) ) || get_option( 'permalink_structure' ) ) {
92 global $wp_rewrite;
93
94 if ( $wp_rewrite->using_index_permalinks() ) {
95 $url = get_home_url( $blog_id, $wp_rewrite->index . '/' . self::get_url_prefix(), $scheme );
96 } else {
97 $url = get_home_url( $blog_id, self::get_url_prefix(), $scheme );
98 }
99
100 $url .= '/' . ltrim( $path, '/' );
101 } else {
102 $url = get_home_url( $blog_id, 'index.php', $scheme );
103
104 $url = add_query_arg( 'rest_route', $pods_path, $url );
105 }
106
107 if ( is_ssl() ) {
108 // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
109 if ( $_SERVER['SERVER_NAME'] === parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) ) {
110 $url = set_url_scheme( $url, 'https' );
111 }
112 }
113 }
114
115 /**
116 * Allow filtering the REST URL.
117 *
118 * @since 3.0
119 *
120 * @param string $url The REST URL.
121 * @param string $path The REST route.
122 * @param int $blog_id The blog ID.
123 * @param string $scheme The sanitization scheme.
124 */
125 return apply_filters( 'pods_rest_url', $url, $path, $blog_id, $scheme );
126 }
127
128 /**
129 * Whether built-in WP REST API functions and functionalities should/can be used or not.
130 *
131 * @since 3.0
132 *
133 * @return bool
134 */
135 protected function use_builtin() {
136 /**
137 * Allow filtering whether builtin WordPress REST API functions should be used or not.
138 *
139 * @since 3.0
140 *
141 * @param bool $use_builtin Whether builtin WordPress REST API functions should be used or not.
142 */
143 $use_builtin = apply_filters( 'pods_rest_use_builtin', true );
144
145 return $use_builtin && function_exists( 'get_rest_url' );
146 }
147
148 /**
149 * Returns the REST API URL prefix that will be appended to the namespace.
150 *
151 * The prefix should be in the `/some/path` format.
152 *
153 * @since 3.0
154 *
155 * @return string
156 */
157 abstract protected function url_prefix();
158
159 /**
160 * Returns the string indicating the REST API version.
161 *
162 * @since 3.0
163 *
164 * @return string
165 */
166 abstract public function get_version();
167
168 /**
169 * Returns the URL where the API users will find the API documentation.
170 *
171 * @since 3.0
172 *
173 * @return string
174 */
175 abstract public function get_reference_url();
176 }
177