| 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 | |
| 15 | |
| 16 | |
| 17 | function get_default_cache_exclusions() { |
| 18 | return join( ',', array( 'cart', 'checkout', 'wp-admin', rest_get_url_prefix() ) ); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | function get_cache_level() { |
| 27 | return absint( get_option( CacheManager::OPTION_CACHE_LEVEL, 2 ) ); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | function get_cache_exclusion() { |
| 39 | $value = get_option( CacheExclusion::OPTION_CACHE_EXCLUSION, get_default_cache_exclusions() ); |
| 40 | $normalized = CacheExclusion::normalize( $value ); |
| 41 | |
| 42 | |
| 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 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | function get_skip404_option() { |
| 57 | return (bool) get_option( Skip404::OPTION_NAME, true ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | function should_cache_pages() { |
| 66 | return get_cache_level() > 1; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | function should_cache_assets() { |
| 75 | return get_cache_level() > 0; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | function 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 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | function 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 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | function to_studly_case( $value ) { |
| 134 | return str_replace( ' ', '', ucwords( str_replace( array( '-', '_' ), ' ', $value ) ) ); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | function get_styles_path() { |
| 143 | return 'vendor/newfold-labs/wp-module-performance/styles/styles.css'; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | function 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 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | function 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 | } |