Endpoints
2 weeks ago
Exceptions
2 weeks ago
Headers
2 weeks ago
Main.php
2 weeks ago
Messages_Interface.php
2 weeks ago
Post_Repository.php
2 weeks ago
Post_Repository_Interface.php
2 weeks ago
System.php
2 weeks ago
Main.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Class Tribe__REST__Main |
| 6 | * |
| 7 | * The main entry point for a The Events Calendar REST API implementation. |
| 8 | * |
| 9 | * This class should not contain business logic and merely set up and start the REST API support. |
| 10 | */ |
| 11 | abstract class Tribe__REST__Main { |
| 12 | |
| 13 | /** |
| 14 | * The Events Calendar REST APIs URL namespace. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $namespace = 'tribe'; |
| 19 | |
| 20 | /** |
| 21 | * Returns the namespace of The Events Calendar REST APIs. |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | public function get_namespace() { |
| 26 | return $this->namespace; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Returns the REST API URL prefix. |
| 31 | * |
| 32 | * @return string The REST API URL prefix. |
| 33 | */ |
| 34 | public function get_url_prefix() { |
| 35 | $use_builtin = $this->use_builtin(); |
| 36 | |
| 37 | if ( $use_builtin ) { |
| 38 | $prefix = rest_get_url_prefix(); |
| 39 | } else { |
| 40 | $prefix = apply_filters( 'rest_url_prefix', 'wp-json' ); |
| 41 | } |
| 42 | |
| 43 | $default_tec_prefix = $this->namespace . '/' . trim( $this->url_prefix(), '/' ); |
| 44 | $prefix = rtrim( $prefix, '/' ) . '/' . trim( $default_tec_prefix, '/' ); |
| 45 | |
| 46 | /** |
| 47 | * Filters the TEC REST API URL prefix |
| 48 | * |
| 49 | * @param string $prefix The complete URL prefix. |
| 50 | * @param string $default_tec_prefix The default URL prefix appended to the REST URL by The Events Calendar. |
| 51 | */ |
| 52 | return apply_filters( 'tribe_events_rest_url_prefix', $prefix, $default_tec_prefix ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Retrieves the URL to a TEC REST endpoint on a site. |
| 57 | * |
| 58 | * Note: The returned URL is NOT escaped. |
| 59 | * |
| 60 | * @global WP_Rewrite $wp_rewrite |
| 61 | * |
| 62 | * @param string $path Optional. TEC REST route. Default '/'. |
| 63 | * @param string $scheme Optional. Sanitization scheme. Default 'rest'. |
| 64 | * @param int $blog_id Optional. Blog ID. Default of null returns URL for current blog. |
| 65 | * |
| 66 | * @return string Full URL to the endpoint. |
| 67 | */ |
| 68 | public function get_url( $path = '/', $scheme = 'rest', $blog_id = null ) { |
| 69 | if ( empty( $path ) ) { |
| 70 | $path = '/'; |
| 71 | } |
| 72 | |
| 73 | $tec_path = '/' . trim( $this->namespace, '/' ) . $this->url_prefix() . '/' . ltrim( $path, '/' ); |
| 74 | |
| 75 | if ( $this->use_builtin() ) { |
| 76 | $url = get_rest_url( $blog_id, $tec_path, $scheme ); |
| 77 | } else { |
| 78 | if ( ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) ) || get_option( 'permalink_structure' ) ) { |
| 79 | global $wp_rewrite; |
| 80 | |
| 81 | if ( $wp_rewrite->using_index_permalinks() ) { |
| 82 | $url = get_home_url( $blog_id, $wp_rewrite->index . '/' . self::get_url_prefix(), $scheme ); |
| 83 | } else { |
| 84 | $url = get_home_url( $blog_id, self::get_url_prefix(), $scheme ); |
| 85 | } |
| 86 | |
| 87 | $url .= '/' . ltrim( $path, '/' ); |
| 88 | } else { |
| 89 | $url = get_home_url( $blog_id, 'index.php', $scheme ); |
| 90 | |
| 91 | $url = add_query_arg( 'rest_route', $tec_path, $url ); |
| 92 | } |
| 93 | |
| 94 | if ( is_ssl() ) { |
| 95 | // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS. |
| 96 | if ( $_SERVER['SERVER_NAME'] === parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) ) { |
| 97 | $url = set_url_scheme( $url, 'https' ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Filters The Events Calendar REST URL. |
| 104 | * |
| 105 | * @param string $url TEC REST URL. |
| 106 | * @param string $path REST route. |
| 107 | * @param int $blog_id Blog ID. |
| 108 | * @param string $scheme Sanitization scheme. |
| 109 | */ |
| 110 | return apply_filters( 'tribe_rest_url', $url, $path, $blog_id, $scheme ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Whether built-in WP REST API functions and functionalities should/can be used or not. |
| 115 | * |
| 116 | * @return bool |
| 117 | */ |
| 118 | protected function use_builtin() { |
| 119 | /** |
| 120 | * Filters whether builtin WordPress REST API functions should be used or not if available. |
| 121 | */ |
| 122 | $use_builtin = apply_filters( 'tribe_events_rest_use_builtin', true ); |
| 123 | |
| 124 | return $use_builtin && function_exists( 'get_rest_url' ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the REST API URL prefix that will be appended to the namespace. |
| 129 | * |
| 130 | * The prefix should be in the `/some/path` format. |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | abstract protected function url_prefix(); |
| 135 | |
| 136 | /** |
| 137 | * Returns the string indicating the REST API version. |
| 138 | * |
| 139 | * @return string |
| 140 | */ |
| 141 | abstract public function get_version(); |
| 142 | |
| 143 | /** |
| 144 | * Returns the URL where the API users will find the API documentation. |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | abstract public function get_reference_url(); |
| 149 | } |
| 150 |