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