| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if ( !class_exists( 'MeowKit_MWCODE_Helpers' ) ) { | |
| 3 | +if ( !class_exists( 'MeowCommon_Helpers' ) ) { | |
| 4 | 4 | |
| 5 | - class MeowKit_MWCODE_Helpers { | |
| 6 | - //public static $version = MeowKit_MWCODE_Admin::version; | |
| 5 | + class MeowCommon_Helpers { | |
| 6 | + //public static $version = MeowCommon_Admin::version; | |
| 7 | 7 | private static $startTimes = []; |
| 8 | 8 | private static $startQueries = []; |
| 9 | 9 | |
| 10 | 10 | public static function is_divi_builder() { |
| @@ -92,43 +92,42 @@ | ||
| 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() ) { | |
| 101 | - MeowKit_MWCODE_Rest::init_once(); | |
| 99 | + // WP_REST_Request init. | |
| 100 | + $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; | |
| 101 | + if ( $is_rest_request ) { | |
| 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 ) { | |
| 107 | - MeowKit_MWCODE_Rest::init_once(); | |
| 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 ) { | |
| 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 | - MeowKit_MWCODE_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 | - $prefix = rest_get_url_prefix(); // e.g. 'wp-json' or custom | |
| 122 | - | |
| 123 | - // Check if the path contains /wp-json/ or /index.php/wp-json/ | |
| 124 | - // This handles multilingual plugins (WPML, Polylang) that add language prefixes like /fr/wp-json/ | |
| 125 | - $path = trailingslashit( $req_path ); | |
| 126 | - $rest_pattern = '/' . trim( $prefix, '/' ) . '/'; | |
| 127 | - $rest_pattern_index = '/index.php/' . trim( $prefix, '/' ) . '/'; | |
| 128 | - | |
| 129 | - if ( strpos( $path, $rest_pattern ) !== false || strpos( $path, $rest_pattern_index ) !== false ) { | |
| 130 | - MeowKit_MWCODE_Rest::init_once(); | |
| 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 ) { | |
| 129 | + MeowCommon_Rest::init_once(); | |
| 131 | 130 | return true; |
| 132 | 131 | } |
| 133 | 132 | } |
| 134 | 133 | |
| @@ -134,49 +133,8 @@ | ||
| 134 | 133 | |
| 135 | 134 | return false; |
| 136 | 135 | } |
| 137 | 136 | |
| 138 | - // Originally created by matzeeable, modified by jordymeow | |
| 139 | - // public static function is_rest() { | |
| 140 | - | |
| 141 | - // // WP_REST_Request init. | |
| 142 | - // $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; | |
| 143 | - // if ( $is_rest_request ) { | |
| 144 | - // MeowKit_MWCODE_Rest::init_once(); | |
| 145 | - // return true; | |
| 146 | - // } | |
| 147 | - | |
| 148 | - // // Plain permalinks. | |
| 149 | - // $prefix = rest_get_url_prefix(); | |
| 150 | - // $request_contains_rest = isset( $_GET['rest_route'] ) && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0; | |
| 151 | - // if ( $request_contains_rest ) { | |
| 152 | - // MeowKit_MWCODE_Rest::init_once(); | |
| 153 | - // return true; | |
| 154 | - // } | |
| 155 | - | |
| 156 | - // // It can happen that WP_Rewrite is not yet initialized, so better to do it. | |
| 157 | - // global $wp_rewrite; | |
| 158 | - // if ( $wp_rewrite === null ) { | |
| 159 | - // $wp_rewrite = new WP_Rewrite(); | |
| 160 | - // } | |
| 161 | - // $rest_url = wp_parse_url( trailingslashit( get_rest_url() ) ); | |
| 162 | - // $current_url = wp_parse_url( add_query_arg( [] ) ); | |
| 163 | - // if ( !$rest_url || !$current_url ) { | |
| 164 | - // return false; | |
| 165 | - // } | |
| 166 | - | |
| 167 | - // // URL Path begins with wp-json. | |
| 168 | - // if ( !empty( $current_url['path'] ) && !empty( $rest_url['path'] ) ) { | |
| 169 | - // $request_contains_rest = strpos( $current_url['path'], $rest_url['path'], 0 ) === 0; | |
| 170 | - // if ( $request_contains_rest ) { | |
| 171 | - // MeowKit_MWCODE_Rest::init_once(); | |
| 172 | - // return true; | |
| 173 | - // } | |
| 174 | - // } | |
| 175 | - | |
| 176 | - // return false; | |
| 177 | - // } | |
| 178 | - | |
| 179 | 137 | public static function test_error( $error = 'timeout', $diceSides = 1 ) { |
| 180 | 138 | if ( mt_rand( 1, $diceSides ) === 1 ) { |
| 181 | 139 | if ( $error === 'timeout' ) { |
| 182 | 140 | header( 'HTTP/1.0 408 Request Timeout' ); |
| @@ -276,19 +234,19 @@ | ||
| 276 | 234 | // } |
| 277 | 235 | } |
| 278 | 236 | |
| 279 | 237 | public static function timer_start( $timerName = 'default' ) { |
| 280 | - MeowKit_MWCODE_Helpers::$startQueries[ $timerName ] = get_num_queries(); | |
| 281 | - MeowKit_MWCODE_Helpers::$startTimes[ $timerName ] = microtime( true ); | |
| 238 | + MeowCommon_Helpers::$startQueries[ $timerName ] = get_num_queries(); | |
| 239 | + MeowCommon_Helpers::$startTimes[ $timerName ] = microtime( true ); | |
| 282 | 240 | } |
| 283 | 241 | |
| 284 | 242 | public static function timer_elapsed( $timerName = 'default' ) { |
| 285 | - return microtime( true ) - MeowKit_MWCODE_Helpers::$startTimes[ $timerName ]; | |
| 243 | + return microtime( true ) - MeowCommon_Helpers::$startTimes[ $timerName ]; | |
| 286 | 244 | } |
| 287 | 245 | |
| 288 | 246 | public static function timer_log_elapsed( $timerName = 'default' ) { |
| 289 | - $elapsed = MeowKit_MWCODE_Helpers::timer_elapsed( $timerName ); | |
| 290 | - $queries = get_num_queries() - MeowKit_MWCODE_Helpers::$startQueries[ $timerName ]; | |
| 247 | + $elapsed = MeowCommon_Helpers::timer_elapsed( $timerName ); | |
| 248 | + $queries = get_num_queries() - MeowCommon_Helpers::$startQueries[ $timerName ]; | |
| 291 | 249 | error_log( $timerName . ': ' . $elapsed . 'ms (' . $queries . ' queries)' ); |
| 292 | 250 | } |
| 293 | 251 | } |
| 294 | 252 | |
| @@ -293,8 +251,8 @@ | ||
| 293 | 251 | } |
| 294 | 252 | |
| 295 | 253 | // Asked by WP Security Team to remove this. |
| 296 | 254 | |
| 297 | - // if ( MeowKit_MWCODE_Helpers::is_rest() ) { | |
| 255 | + // if ( MeowCommon_Helpers::is_rest() ) { | |
| 298 | 256 | // ini_set( 'display_errors', 0 ); |
| 299 | 257 | // } |
| 300 | 258 | } |