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