WPML_Cache_Directory

The WooCommerce Multilingual WPML Cache Directory class.

Defined (1)

The class is defined in the following location(s).

/vendor/wpml/commons/src/cache/class-wpml-cache-directory.php  
  1. class WPML_Cache_Directory { 
  2.  
  3. const DIR_PERMISSIONS = 0775; 
  4. const MAIN_DIRECTORY_NAME = 'wpml'; 
  5. const NOTICE_GROUP = 'wpml-cache-directory'; 
  6. const NOTICE_INVALID_CACHE = 'invalid-cache'; 
  7. private $cache_disabled = false; 
  8.  
  9. /** 
  10. * @var WPML_WP_API 
  11. */ 
  12. private $wp_api; 
  13.  
  14. /** 
  15. * @var WP_Filesystem_Direct 
  16. */ 
  17. private $filesystem; 
  18.  
  19. /** 
  20. * WPML_Cache_Directory constructor. 
  21. * @param WPML_WP_API $wp_api 
  22. */ 
  23. public function __construct( WPML_WP_API $wp_api ) { 
  24. $this->wp_api = $wp_api; 
  25. $this->filesystem = $wp_api->get_wp_filesystem_direct(); 
  26.  
  27. /** 
  28. * @return string 
  29. */ 
  30. private function get_main_directory_path() { 
  31. $main_directory_path = null; 
  32. $cache_path_root = $this->wp_api->constant( 'WPML_CACHE_PATH_ROOT' ); 
  33.  
  34. if ( $cache_path_root ) { 
  35. $main_directory_path = trailingslashit( $cache_path_root ) . self::MAIN_DIRECTORY_NAME; 
  36. return trailingslashit( $main_directory_path ); 
  37. }else { 
  38. $upload_dir = wp_upload_dir(); 
  39.  
  40. if ( empty( $upload_dir['error'] ) ) { 
  41. $base_dir = $upload_dir['basedir']; 
  42. $main_directory_path = trailingslashit( $base_dir ) . 'cache/' . self::MAIN_DIRECTORY_NAME; 
  43. return trailingslashit( $main_directory_path ); 
  44.  
  45. return null; 
  46.  
  47. /** 
  48. * The function `wp_mkdir_p` will create directories recursively 
  49. * @param string $absolute_path 
  50. * @return string|bool absolute path or false if we can't have a writable and readable directory 
  51. */ 
  52. private function maybe_create_directory( $absolute_path ) { 
  53. $result = true; 
  54.  
  55. if ( ! $this->filesystem->is_dir( $absolute_path ) ) { 
  56. $result = wp_mkdir_p( $absolute_path ); 
  57.  
  58. return $result ? $absolute_path : false; 
  59.  
  60. /** 
  61. * @param string $relative_path 
  62. * @return string|bool absolute path or false if we can't have a writable and readable directory 
  63. */ 
  64. public function get( $relative_path = '' ) { 
  65. $absolute_path = false; 
  66. $main_directory_path = $this->maybe_create_directory( $this->get_main_directory_path() ); 
  67.  
  68. if ( $main_directory_path ) { 
  69. $absolute_path = trailingslashit( $main_directory_path . ltrim( $relative_path, '/\\' ) ); 
  70. $absolute_path = $this->maybe_create_directory( $absolute_path ); 
  71.  
  72. return $absolute_path; 
  73.  
  74. /** 
  75. * @param string $relative_path 
  76. */ 
  77. public function remove( $relative_path = '' ) { 
  78. $main_directory_path = $this->get_main_directory_path(); 
  79. if ( $main_directory_path ) { 
  80. $absolute_path = trailingslashit( $main_directory_path . ltrim( $relative_path, '/\\' ) ); 
  81. $this->filesystem->delete( $absolute_path, true );