Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
1.85% |
1 / 54 |
|
12.50% |
1 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Config | |
1.85% |
1 / 54 |
|
12.50% |
1 / 8 |
523.15 | |
0.00% |
0 / 1 |
| get_static_cache_dir_path | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_legacy_cache_dir_path | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| get_abspath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| can_use_static_cache | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| can_use_cache | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| ensure_dir_exists | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| is_dir_writable | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
| log_error | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib\Minify; |
| 4 | |
| 5 | /** |
| 6 | * Configuration management for the minification system. |
| 7 | */ |
| 8 | class Config { |
| 9 | /** |
| 10 | * Get the directory path for storing static cache files. |
| 11 | */ |
| 12 | public static function get_static_cache_dir_path() { |
| 13 | return WP_CONTENT_DIR . '/boost-cache/static'; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Get the directory path for storing cache files. |
| 18 | */ |
| 19 | public static function get_legacy_cache_dir_path() { |
| 20 | if ( defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) ) { |
| 21 | if ( empty( \PAGE_OPTIMIZE_CACHE_DIR ) ) { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | return \PAGE_OPTIMIZE_CACHE_DIR; |
| 26 | } |
| 27 | |
| 28 | return WP_CONTENT_DIR . '/cache/page_optimize'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get WordPress ABSPATH, with support for custom configuration. |
| 33 | */ |
| 34 | public static function get_abspath() { |
| 35 | return defined( 'PAGE_OPTIMIZE_ABSPATH' ) ? \PAGE_OPTIMIZE_ABSPATH : \ABSPATH; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Check if static cache can be used. |
| 40 | */ |
| 41 | public static function can_use_static_cache() { |
| 42 | $cache_dir = static::get_static_cache_dir_path(); |
| 43 | |
| 44 | if ( ! static::ensure_dir_exists( $cache_dir ) ) { |
| 45 | static::log_error( |
| 46 | sprintf( |
| 47 | /* translators: a filesystem path to a directory */ |
| 48 | __( "Disabling concatenate static cache. Unable to create cache directory '%s'.", 'jetpack-boost' ), |
| 49 | $cache_dir |
| 50 | ) |
| 51 | ); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | if ( ! static::is_dir_writable( $cache_dir ) ) { |
| 56 | static::log_error( |
| 57 | sprintf( |
| 58 | /* translators: a filesystem path to a directory */ |
| 59 | __( "Disabling concatenate static cache. Unable to write to cache directory '%s'.", 'jetpack-boost' ), |
| 60 | $cache_dir |
| 61 | ) |
| 62 | ); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if cache can be used. |
| 71 | */ |
| 72 | public static function can_use_cache() { |
| 73 | $cache_dir = static::get_legacy_cache_dir_path(); |
| 74 | |
| 75 | if ( empty( $cache_dir ) ) { |
| 76 | static::log_error( __( 'Disabling page-optimize cache. Cache directory not defined.', 'jetpack-boost' ) ); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if ( ! static::ensure_dir_exists( $cache_dir ) ) { |
| 81 | static::log_error( |
| 82 | sprintf( |
| 83 | /* translators: a filesystem path to a directory */ |
| 84 | __( "Disabling page-optimize cache. Unable to create cache directory '%s'.", 'jetpack-boost' ), |
| 85 | $cache_dir |
| 86 | ) |
| 87 | ); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if ( ! static::is_dir_writable( $cache_dir ) ) { |
| 92 | static::log_error( |
| 93 | sprintf( |
| 94 | /* translators: a filesystem path to a directory */ |
| 95 | __( "Disabling page-optimize cache. Unable to write to cache directory '%s'.", 'jetpack-boost' ), |
| 96 | $cache_dir |
| 97 | ) |
| 98 | ); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Ensure a directory exists. |
| 107 | * |
| 108 | * @param string $dir The directory to check. |
| 109 | * @return bool True if the directory exists, false otherwise. |
| 110 | */ |
| 111 | private static function ensure_dir_exists( $dir ) { |
| 112 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 113 | if ( ! is_dir( $dir ) && ! mkdir( $dir, 0775, true ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Ensure a directory is writable. |
| 122 | * |
| 123 | * @param string $dir The directory to check. |
| 124 | * @return bool True if the directory is writable, false otherwise. |
| 125 | */ |
| 126 | private static function is_dir_writable( $dir ) { |
| 127 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
| 128 | if ( ! is_dir( $dir ) || ! is_writable( $dir ) || ! is_executable( $dir ) ) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Log an error message if WP_DEBUG is enabled. |
| 137 | * |
| 138 | * @param string $message The error message to log. |
| 139 | */ |
| 140 | private static function log_error( $message ) { |
| 141 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 142 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 143 | error_log( $message ); |
| 144 | } |
| 145 | } |
| 146 | } |