Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 12
CRAP
n/a
0 / 0
NewfoldLabs\WP\Module\Performance\get_default_cache_exclusions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\get_cache_level
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\get_cache_exclusion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\get_skip404_option
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\should_cache_pages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\should_cache_assets
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\remove_directory
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
NewfoldLabs\WP\Module\Performance\to_snake_case
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
NewfoldLabs\WP\Module\Performance\to_studly_case
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\get_styles_path
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
NewfoldLabs\WP\Module\Performance\get_scripts_path
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
NewfoldLabs\WP\Module\Performance\is_settings_page
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance;
4
5use NewfoldLabs\WP\Module\Performance\Skip404\Skip404;
6use NewfoldLabs\WP\Module\Performance\Cache\CacheManager;
7use NewfoldLabs\WP\Module\Performance\Cache\CacheExclusion;
8
9/**
10 * Return defaul exclusions.
11 *
12 * @return array
13 */
14function get_default_cache_exclusions() {
15    return join( ',', array( 'cart', 'checkout', 'wp-admin', rest_get_url_prefix() ) );
16}
17
18/**
19 * Get the current cache level.
20 *
21 * @return int Cache level.
22 */
23function get_cache_level() {
24    return absint( get_option( CacheManager::OPTION_CACHE_LEVEL, 2 ) );
25}
26
27/**
28 * Get the cache exclusion.
29 *
30 * @return int Cache exclusion.
31 */
32function get_cache_exclusion() {
33    return get_option( CacheExclusion::OPTION_CACHE_EXCLUSION, get_default_cache_exclusions() );
34}
35
36/**
37 * Get the "Skip WordPress 404 Handling for Static Files" option.
38 *
39 * @return bool Whether to skip 404 handling for static files.
40 */
41function get_skip404_option() {
42    return (bool) get_option( Skip404::OPTION_NAME, true );
43}
44
45/**
46 * Check if page caching is enabled.
47 *
48 * @return bool
49 */
50function should_cache_pages() {
51    return get_cache_level() > 1;
52}
53
54/**
55 * Check if asset caching is enabled.
56 *
57 * @return bool
58 */
59function should_cache_assets() {
60    return get_cache_level() > 0;
61}
62
63/**
64 * Remove a directory.
65 *
66 * @param string $path Path to the directory.
67 */
68function remove_directory( $path ) {
69    global $wp_filesystem;
70
71    if ( ! function_exists( 'WP_Filesystem' ) ) {
72        require_once ABSPATH . 'wp-admin/includes/file.php';
73    }
74    WP_Filesystem();
75
76    if ( ! $wp_filesystem || ! $wp_filesystem->is_dir( $path ) ) {
77        return;
78    }
79
80    $files = $wp_filesystem->dirlist( $path );
81
82    foreach ( $files as $file => $file_info ) {
83        $file_path = trailingslashit( $path ) . $file;
84        if ( 'f' === $file_info['type'] ) {
85            $wp_filesystem->delete( $file_path );
86        } elseif ( 'd' === $file_info['type'] ) {
87            remove_directory( $file_path );
88        }
89    }
90
91    $wp_filesystem->rmdir( $path );
92}
93
94/**
95 * Convert a string to snake case.
96 *
97 * @param string $value     String to be converted.
98 * @param string $delimiter Delimiter (can be a dash for conversion to kebab case).
99 *
100 * @return string
101 */
102function to_snake_case( string $value, string $delimiter = '_' ) {
103    if ( ! ctype_lower( $value ) ) {
104        $value = preg_replace( '/(\s+)/u', '', ucwords( $value ) );
105        $value = trim( mb_strtolower( preg_replace( '/([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)/u', '$1' . $delimiter, $value ), 'UTF-8' ), $delimiter );
106    }
107
108    return $value;
109}
110
111/**
112 * Convert a string to studly case.
113 *
114 * @param string $value String to be converted.
115 *
116 * @return string
117 */
118function to_studly_case( $value ) {
119    return str_replace( ' ', '', ucwords( str_replace( array( '-', '_' ), ' ', $value ) ) );
120}
121
122/**
123 * Get styles path.
124 *
125 * return string
126 */
127function get_styles_path() {
128    return 'vendor/newfold-labs/wp-module-performance/styles/styles.css';
129}
130
131/**
132 * Get js script path.
133 *
134 * @param string $script_name Script name.
135 * return string
136 */
137function get_scripts_path( $script_name = '' ) {
138    $basePath = 'vendor/newfold-labs/wp-module-performance/scripts/';
139    if ( empty( $script_name ) ) {
140        return $basePath;
141    }
142    $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
143    return "vendor/newfold-labs/wp-module-performance/scripts/$script_name$suffix.js";
144}
145
146/**
147 * Detect if the current page is a Brand Plugin settings page.
148 *
149 * @param string $brand The expected settings page identifier.
150 * @return boolean True if the current page matches the brand settings page, false otherwise.
151 */
152function is_settings_page( $brand ) {
153
154    $current_url = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http' ) .
155    '://' .
156    ( isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '' ) .
157    ( isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' );
158
159    $parsed_url = wp_parse_url( $current_url );
160
161    if ( ! isset( $parsed_url['query'] ) ) {
162        return false;
163    }
164
165    parse_str( $parsed_url['query'], $query_params );
166
167    if ( ! isset( $query_params['page'] ) || $query_params['page'] !== $brand ) {
168        return false;
169    }
170
171    return true;
172}