1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Performance; |
4 | |
5 | use NewfoldLabs\WP\Module\Performance\Skip404\Skip404; |
6 | use NewfoldLabs\WP\Module\Performance\Cache\CacheManager; |
7 | use NewfoldLabs\WP\Module\Performance\Cache\CacheExclusion; |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | function get_default_cache_exclusions() { |
15 | return join( ',', array( 'cart', 'checkout', 'wp-admin', rest_get_url_prefix() ) ); |
16 | } |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | function get_cache_level() { |
24 | return absint( get_option( CacheManager::OPTION_CACHE_LEVEL, 2 ) ); |
25 | } |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | function get_cache_exclusion() { |
33 | return get_option( CacheExclusion::OPTION_CACHE_EXCLUSION, get_default_cache_exclusions() ); |
34 | } |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | function get_skip404_option() { |
42 | return (bool) get_option( Skip404::OPTION_NAME, true ); |
43 | } |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | function should_cache_pages() { |
51 | return get_cache_level() > 1; |
52 | } |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | function should_cache_assets() { |
60 | return get_cache_level() > 0; |
61 | } |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | function 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 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | function 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 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | function to_studly_case( $value ) { |
119 | return str_replace( ' ', '', ucwords( str_replace( array( '-', '_' ), ' ', $value ) ) ); |
120 | } |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | function get_styles_path() { |
128 | return 'vendor/newfold-labs/wp-module-performance/styles/styles.css'; |
129 | } |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | function 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 | |
148 | |
149 | |
150 | |
151 | |
152 | function 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 | } |