| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cache Permission Notice class. |
| 4 |
* |
| 5 |
* Displays an admin notice when the asset optimization cache directory |
| 6 |
* is not writable, and disables optimization until resolved. |
| 7 |
* |
| 8 |
* @package RadiusTheme\SB |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace RadiusTheme\SB\Controllers\Admin\Notice; |
| 12 |
|
| 13 |
use RadiusTheme\SB\Helpers\Fns; |
| 14 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 15 |
|
| 16 |
// Do not allow directly accessing this file. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit( 'This script cannot be accessed directly.' ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Cache Permission Notice class. |
| 23 |
*/ |
| 24 |
class CachePermission { |
| 25 |
|
| 26 |
use SingletonTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class constructor. |
| 30 |
*/ |
| 31 |
private function __construct() { |
| 32 |
add_action( 'admin_init', [ $this, 'check_cache_dir_permission' ] ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Check if the cache directory is writable. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function check_cache_dir_permission() { |
| 41 |
if ( ! Fns::is_optimization_setting_on() ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$upload = wp_upload_dir(); |
| 46 |
$cache_dir = trailingslashit( $upload['basedir'] ) . 'shopbuilder_uploads/cache/'; |
| 47 |
|
| 48 |
if ( ! file_exists( $cache_dir ) ) { |
| 49 |
wp_mkdir_p( $cache_dir ); |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! wp_is_writable( $cache_dir ) ) { |
| 53 |
add_action( 'admin_notices', [ $this, 'display_notice' ] ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Display the admin notice. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function display_notice() { |
| 63 |
$upload = wp_upload_dir(); |
| 64 |
$cache_dir = trailingslashit( $upload['basedir'] ) . 'shopbuilder_uploads/cache/'; |
| 65 |
?> |
| 66 |
<div class="notice notice-error"> |
| 67 |
<p> |
| 68 |
<strong><?php esc_html_e( 'ShopBuilder:', 'shopbuilder' ); ?></strong> |
| 69 |
<?php |
| 70 |
printf( |
| 71 |
/* translators: %s: cache directory path */ |
| 72 |
esc_html__( 'The cache directory %s is not writable. The Asset Optimization setting will not work until the server has write permission to this directory. Please contact your hosting provider to fix the file permissions.', 'shopbuilder' ), |
| 73 |
'<code>' . esc_html( $cache_dir ) . '</code>' |
| 74 |
); |
| 75 |
?> |
| 76 |
</p> |
| 77 |
</div> |
| 78 |
<?php |
| 79 |
} |
| 80 |
} |
| 81 |
|