| @@ -92,46 +92,41 @@ | ||
| 92 | 92 | $diff = array_diff( $first, $second ); |
| 93 | 93 | return implode( ' ', $diff ); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - // From August 2025: The New 'is_rest' function, more reliable, much cleaner. | |
| 96 | + // Originally created by matzeeable, modified by jordymeow | |
| 97 | 97 | public static function is_rest() { |
| 98 | 98 | |
| 99 | - // WP 6.5+: only reliable after parse_request; otherwise skip this branch. | |
| 100 | - if ( function_exists( 'wp_is_serving_rest_request' ) && did_action( 'parse_request' ) && wp_is_serving_rest_request() ) { | |
| 99 | + // WP_REST_Request init. | |
| 100 | + $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; | |
| 101 | + if ( $is_rest_request ) { | |
| 101 | 102 | MeowCommon_Rest::init_once(); |
| 102 | 103 | return true; |
| 103 | 104 | } |
| 104 | 105 | |
| 105 | - // Classic flag set during REST bootstrap (safe at any time). | |
| 106 | - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { | |
| 106 | + // Plain permalinks. | |
| 107 | + $prefix = rest_get_url_prefix(); | |
| 108 | + $request_contains_rest = isset( $_GET['rest_route'] ) && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0; | |
| 109 | + if ( $request_contains_rest ) { | |
| 107 | 110 | MeowCommon_Rest::init_once(); |
| 108 | 111 | return true; |
| 109 | 112 | } |
| 110 | 113 | |
| 111 | - // Plain permalinks: ?rest_route=/... (route does NOT include the prefix). | |
| 112 | - if ( isset( $_GET['rest_route'] ) ) { | |
| 113 | - MeowCommon_Rest::init_once(); | |
| 114 | - return true; | |
| 114 | + // It can happen that WP_Rewrite is not yet initialized, so better to do it. | |
| 115 | + global $wp_rewrite; | |
| 116 | + if ( $wp_rewrite === null ) { | |
| 117 | + $wp_rewrite = new WP_Rewrite(); | |
| 115 | 118 | } |
| 119 | + $rest_url = wp_parse_url( trailingslashit( get_rest_url() ) ); | |
| 120 | + $current_url = wp_parse_url( add_query_arg( [] ) ); | |
| 121 | + if ( !$rest_url || !$current_url ) { | |
| 122 | + return false; | |
| 123 | + } | |
| 116 | 124 | |
| 117 | - // Pretty permalinks: compare request PATH to the site's REST base path, without using rest_url(). | |
| 118 | - // This works early in the bootstrap (no dependency on $wp_rewrite). | |
| 119 | - $req_path = isset( $_SERVER['REQUEST_URI'] ) ? wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) : null; | |
| 120 | - if ( $req_path ) { | |
| 121 | - $home_path = wp_parse_url( home_url( '/' ), PHP_URL_PATH ); // e.g. '/' or '/blog/' | |
| 122 | - $home_path = trailingslashit( $home_path ? $home_path : '/' ); | |
| 123 | - | |
| 124 | - $prefix = trim( rest_get_url_prefix(), '/' ); // e.g. 'wp-json' or custom | |
| 125 | - | |
| 126 | - // /wp-json/ or /blog/wp-json/ | |
| 127 | - $base = $home_path . trailingslashit( $prefix ); | |
| 128 | - | |
| 129 | - // /index.php/wp-json/ or /blog/index.php/wp-json/ (index permalinks) | |
| 130 | - $base_index = $home_path . 'index.php/' . trailingslashit( $prefix ); | |
| 131 | - | |
| 132 | - $path = trailingslashit( $req_path ); | |
| 133 | - if ( strpos( $path, $base ) === 0 || strpos( $path, $base_index ) === 0 ) { | |
| 125 | + // URL Path begins with wp-json. | |
| 126 | + if ( !empty( $current_url['path'] ) && !empty( $rest_url['path'] ) ) { | |
| 127 | + $request_contains_rest = strpos( $current_url['path'], $rest_url['path'], 0 ) === 0; | |
| 128 | + if ( $request_contains_rest ) { | |
| 134 | 129 | MeowCommon_Rest::init_once(); |
| 135 | 130 | return true; |
| 136 | 131 | } |
| 137 | 132 | } |
| @@ -137,49 +132,8 @@ | ||
| 137 | 132 | } |
| 138 | 133 | |
| 139 | 134 | return false; |
| 140 | 135 | } |
| 141 | - | |
| 142 | - // Originally created by matzeeable, modified by jordymeow | |
| 143 | - // public static function is_rest() { | |
| 144 | - | |
| 145 | - // // WP_REST_Request init. | |
| 146 | - // $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; | |
| 147 | - // if ( $is_rest_request ) { | |
| 148 | - // MeowCommon_Rest::init_once(); | |
| 149 | - // return true; | |
| 150 | - // } | |
| 151 | - | |
| 152 | - // // Plain permalinks. | |
| 153 | - // $prefix = rest_get_url_prefix(); | |
| 154 | - // $request_contains_rest = isset( $_GET['rest_route'] ) && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0; | |
| 155 | - // if ( $request_contains_rest ) { | |
| 156 | - // MeowCommon_Rest::init_once(); | |
| 157 | - // return true; | |
| 158 | - // } | |
| 159 | - | |
| 160 | - // // It can happen that WP_Rewrite is not yet initialized, so better to do it. | |
| 161 | - // global $wp_rewrite; | |
| 162 | - // if ( $wp_rewrite === null ) { | |
| 163 | - // $wp_rewrite = new WP_Rewrite(); | |
| 164 | - // } | |
| 165 | - // $rest_url = wp_parse_url( trailingslashit( get_rest_url() ) ); | |
| 166 | - // $current_url = wp_parse_url( add_query_arg( [] ) ); | |
| 167 | - // if ( !$rest_url || !$current_url ) { | |
| 168 | - // return false; | |
| 169 | - // } | |
| 170 | - | |
| 171 | - // // URL Path begins with wp-json. | |
| 172 | - // if ( !empty( $current_url['path'] ) && !empty( $rest_url['path'] ) ) { | |
| 173 | - // $request_contains_rest = strpos( $current_url['path'], $rest_url['path'], 0 ) === 0; | |
| 174 | - // if ( $request_contains_rest ) { | |
| 175 | - // MeowCommon_Rest::init_once(); | |
| 176 | - // return true; | |
| 177 | - // } | |
| 178 | - // } | |
| 179 | - | |
| 180 | - // return false; | |
| 181 | - // } | |
| 182 | 136 | |
| 183 | 137 | public static function test_error( $error = 'timeout', $diceSides = 1 ) { |
| 184 | 138 | if ( mt_rand( 1, $diceSides ) === 1 ) { |
| 185 | 139 | if ( $error === 'timeout' ) { |