| 1 |
<?php |
| 2 |
/** |
| 3 |
* Compatibility functions. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl; |
| 9 |
|
| 10 |
use function ContentControl\get_query; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Checks whether function is disabled. |
| 16 |
* |
| 17 |
* @param string $func Name of the function. |
| 18 |
* |
| 19 |
* @return bool Whether or not function is disabled. |
| 20 |
*/ |
| 21 |
function is_func_disabled( $func ) { |
| 22 |
$disabled = explode( ',', ini_get( 'disable_functions' ) ); |
| 23 |
|
| 24 |
return in_array( $func, $disabled, true ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Checks if the current request is a WP REST API request. |
| 29 |
* |
| 30 |
* Case #1: After WP_REST_Request initialisation |
| 31 |
* Case #2: Support "plain" permalink settings and check if `rest_route` starts with `/` |
| 32 |
* Case #3: It can happen that WP_Rewrite is not yet initialized, |
| 33 |
* so do this (wp-settings.php) |
| 34 |
* Case #4: URL Path begins with wp-json/ (your REST prefix) |
| 35 |
* Also supports WP installations in subfolders |
| 36 |
* |
| 37 |
* @returns boolean |
| 38 |
* @author matzeeable |
| 39 |
*/ |
| 40 |
function is_rest() { |
| 41 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 42 |
if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1) |
| 43 |
|| isset( $_GET['rest_route'] ) // (#2) |
| 44 |
&& strpos( sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ), '/', 0 ) === 0 ) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
// (#3) |
| 49 |
global $wp_rewrite; |
| 50 |
if ( null === $wp_rewrite ) { |
| 51 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 52 |
$wp_rewrite = new \WP_Rewrite(); |
| 53 |
} |
| 54 |
|
| 55 |
// (#4) |
| 56 |
$rest_url = wp_parse_url( trailingslashit( rest_url() ) ); |
| 57 |
$current_url = wp_parse_url( add_query_arg( [] ) ); |
| 58 |
return strpos( $current_url['path'] ? $current_url['path'] : '/', $rest_url['path'], 0 ) === 0; |
| 59 |
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check if this is a cron request. |
| 64 |
* |
| 65 |
* @return boolean |
| 66 |
*/ |
| 67 |
function is_cron() { |
| 68 |
return defined( 'DOING_CRON' ) && DOING_CRON; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if this is an AJAX request. |
| 73 |
* |
| 74 |
* @return boolean |
| 75 |
*/ |
| 76 |
function is_ajax() { |
| 77 |
return defined( 'DOING_AJAX' ) && DOING_AJAX; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Check if this is a frontend request. |
| 82 |
* |
| 83 |
* @return boolean |
| 84 |
*/ |
| 85 |
function is_frontend() { |
| 86 |
/** |
| 87 |
* WP Query. |
| 88 |
* |
| 89 |
* @var \WP_Query $query |
| 90 |
*/ |
| 91 |
$query = get_query(); |
| 92 |
|
| 93 |
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 94 |
|
| 95 |
if ( |
| 96 |
is_rest() || |
| 97 |
is_cron() || |
| 98 |
is_ajax() || |
| 99 |
is_admin() || |
| 100 |
$query->is_admin() || |
| 101 |
$query->is_favicon() || |
| 102 |
strpos( $request_uri, 'favicon.ico' ) || |
| 103 |
$query->is_robots() || |
| 104 |
strpos( $request_uri, 'robots.txt' ) |
| 105 |
) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Change camelCase to snake_case. |
| 114 |
* |
| 115 |
* @param string $str String to convert. |
| 116 |
* |
| 117 |
* @return string Converted string. |
| 118 |
*/ |
| 119 |
function camel_case_to_snake_case( $str ) { |
| 120 |
return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $str ) ); |
| 121 |
} |
| 122 |
|