query_vars['rest_route'] ) ? $wp->query_vars['rest_route'] : ''; // Check if this is core WP REST namespace. return strpos( $rest_route, '/wp/v2' ) === 0; } /** * Check if this is a cron request. * * @return boolean */ function is_cron() { return defined( 'DOING_CRON' ) && DOING_CRON; } /** * Check if this is an AJAX request. * * @return boolean */ function is_ajax() { return defined( 'DOING_AJAX' ) && DOING_AJAX; } /** * Check if this is a frontend request. * * @return boolean */ function is_frontend() { // Cached. static $is_frontend; // phpcs:ignore if ( isset( $is_frontend ) ) { // Not fully tested, but if we have a cached value, we can return it. // return $is_frontend;. $temp = $is_frontend; } $query = get_query(); $wp_query = $query instanceof \WP_Query ? $query : null; $request_uri = isset( $_SERVER['REQUEST_URI'] ) && is_string( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; if ( // Check if is CLI. ( defined( 'WP_CLI' ) && WP_CLI ) || // Check if is REST. is_admin() || is_ajax() || is_cron() || ( $wp_query && $wp_query->is_admin ) || ( $wp_query && $wp_query->is_favicon() ) || ( $wp_query && $wp_query->is_robots() ) || strpos( $request_uri, 'favicon.ico' ) !== false || strpos( $request_uri, 'robots.txt' ) !== false ) { $is_frontend = false; } else { $is_frontend = true; } return $is_frontend; } /** * Change camelCase to snake_case. * * @param string $str String to convert. * * @return string Converted string. */ function camel_case_to_snake_case( $str ) { return strtolower( preg_replace( '/(? $data Array to fetch from. * @param string|null $key_case Case to use for key (snake_case|camelCase). * * @return mixed|null */ function fetch_key_from_array( $key, $data, $key_case = null ) { // If key is .notation, then we need to traverse the array. $dotted_keys = explode( '.', $key ); foreach ( $dotted_keys as $key ) { if ( $key_case ) { switch ( $key_case ) { case 'snake_case': // Check if key is camelCase & convert to snake_case. $key = camel_case_to_snake_case( $key ); break; case 'camelCase': // Check if key is snake_case & convert to camelCase. $key = snake_case_to_camel_case( $key ); break; } } if ( ! isset( $data[ $key ] ) ) { return null; } $data = $data[ $key ]; } return $data ? $data : null; } /** * Convert hex to rgba. * * @param string $hex_code Hex code to convert. * @param float $opacity Opacity to use. * * @return string Converted rgba string. */ function convert_hex_to_rgba( $hex_code, $opacity = 1 ) { $hex = str_replace( '#', '', $hex_code ); if ( strlen( $hex ) === 3 ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } $r = hexdec( substr( $hex, 0, 2 ) ); $g = hexdec( substr( $hex, 2, 2 ) ); $b = hexdec( substr( $hex, 4, 2 ) ); /* Backward compatibility for whole number based opacity values. */ if ( $opacity > 1 && $opacity <= 100 ) { $opacity = $opacity / 100; } return "rgba($r,$g,$b,$opacity)"; } /** * Function that deeply cleans arrays for wp_maybe_serialize * * Gets rid of Closure and other invalid data types. * * @param array $arr Array to clean. * * @return array Cleaned array. */ function deep_clean_array( $arr ) { // Clean \Closure values deeply. array_walk_recursive( $arr, function ( &$value ) { if ( $value instanceof \Closure ) { $value = null; } } ); return $arr; }