PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / inc / functions / compatibility.php

compatibility.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.12, at inc/functions/compatibility.php

156 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_functions = ini_get( 'disable_functions' );
23
24 $disabled = $disabled_functions ? explode( ',', $disabled_functions ) : [];
25
26 return in_array( $func, $disabled, true );
27 }
28
29 /**
30 * Checks if the current request is a WP REST API request.
31 *
32 * Case #1: After WP_REST_Request initialisation
33 * Case #2: Support "plain" permalink settings and check if `rest_route` starts with `/`
34 * Case #3: It can happen that WP_Rewrite is not yet initialized,
35 * so do this (wp-settings.php)
36 * Case #4: URL Path begins with wp-json/ (your REST prefix)
37 * Also supports WP installations in subfolders
38 *
39 * @returns boolean
40 * @author matzeeable
41 *
42 * @return bool
43 */
44 function is_rest() {
45 // phpcs:disable WordPress.Security.NonceVerification.Recommended
46 if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
47 || isset( $_GET['rest_route'] ) // (#2)
48 && strpos( sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ), '/', 0 ) === 0 ) {
49 return true;
50 }
51
52 // (#4)
53 $rest_url = wp_parse_url( trailingslashit( rest_url() ) );
54 $current_url = wp_parse_url( add_query_arg( [] ) );
55
56 if ( ! $rest_url || ! $current_url ) {
57 return false;
58 }
59
60 $current_path = isset( $current_url['path'] ) ? $current_url['path'] : false;
61 $rest_path = isset( $rest_url['path'] ) ? $rest_url['path'] : false;
62
63 // If one of the URLs failed to parse, then the current request isn't a REST request.
64 if ( ! $current_path || ! $rest_path ) {
65 return false;
66 }
67
68 return strpos( $current_path, $rest_path, 0 ) === 0;
69 // phpcs:enable WordPress.Security.NonceVerification.Recommended
70 }
71
72 /**
73 * Check if this is a cron request.
74 *
75 * @return boolean
76 */
77 function is_cron() {
78 return defined( 'DOING_CRON' ) && DOING_CRON;
79 }
80
81 /**
82 * Check if this is an AJAX request.
83 *
84 * @return boolean
85 */
86 function is_ajax() {
87 return defined( 'DOING_AJAX' ) && DOING_AJAX;
88 }
89
90 /**
91 * Check if this is a frontend request.
92 *
93 * @return boolean
94 */
95 function is_frontend() {
96 global $wp_rewrite;
97
98 $query = get_query();
99
100 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
101
102 if (
103 // Check if is CLI.
104 ( defined( 'WP_CLI' ) && WP_CLI ) ||
105 // Check if is REST.
106 is_admin() ||
107 is_ajax() ||
108 is_cron() ||
109 ( $query && $query->is_admin ) ||
110 ( $query && $query->is_favicon() ) ||
111 ( $query && $query->is_robots() ) ||
112 strpos( $request_uri, 'favicon.ico' ) !== false ||
113 strpos( $request_uri, 'robots.txt' ) !== false ||
114 ( $wp_rewrite && is_rest() )
115 ) {
116 return false;
117 }
118
119 return true;
120 }
121
122 /**
123 * Change camelCase to snake_case.
124 *
125 * @param string $str String to convert.
126 *
127 * @return string Converted string.
128 */
129 function camel_case_to_snake_case( $str ) {
130 return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $str ) );
131 }
132
133
134 /**
135 * Function that deeply cleans arrays for wp_maybe_serialize
136 *
137 * Gets rid of Closure and other invalid data types.
138 *
139 * @param array<mixed> $arr Array to clean.
140 *
141 * @return array<mixed> Cleaned array.
142 */
143 function deep_clean_array( $arr ) {
144 // Clean \Closure values deeply.
145 array_walk_recursive(
146 $arr,
147 function ( &$value ) {
148 if ( $value instanceof \Closure ) {
149 $value = null;
150 }
151 }
152 );
153
154 return $arr;
155 }
156