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.5 1.3.8.6 1.3.8.7 1.3.8.8 1.3.8.9 1.3.9.0 1.3.9.1 1.3.9.2 1.3.9.3 1.3.9.4 1.3.9.5 1.3.9.6 1.3.9.7 1.3.9.8 1.3.9.9 1.4 1.4.0.1 1.4.0.2 1.4.0.3 1.4.0.4
wp-asset-clean-up / classes / FileSystem.php
wp-asset-clean-up / classes Last commit date
Admin 1 month ago OptimiseAssets 1 month ago ThirdParty 1 year ago AdminBar.php 1 year ago AssetsManager.php 1 month ago BulkChanges.php 1 year ago CleanUp.php 1 month ago Debug.php 1 month ago FileSystem.php 1 year ago HardcodedAssets.php 1 month ago Lite.php 3 years ago Main.php 1 month ago MainFront.php 1 month ago Maintenance.php 1 month ago Menu.php 1 month ago MetaBoxes.php 1 month ago Misc.php 1 month ago ObjectCache.php 1 month ago OwnAssets.php 1 month ago PluginNotifications.php 1 year ago PluginTracking.php 1 month ago Preloads.php 1 month ago Settings.php 1 month ago Tips.php 1 year ago Update.php 1 month ago
FileSystem.php
118 lines
1 <?php
2 /** @noinspection MultipleReturnStatementsInspection */
3
4 namespace WpAssetCleanUp;
5
6 use WpAssetCleanUp\OptimiseAssets\CombineCssImports;
7 use WpAssetCleanUp\OptimiseAssets\OptimizeCss;
8 use WpAssetCleanUp\OptimiseAssets\OptimizeJs;
9
10 /**
11 * Class FileSystem
12 * @package WpAssetCleanUp
13 */
14 class FileSystem
15 {
16 /**
17 * @return bool|\WP_Filesystem_Direct
18 */
19 public static function init()
20 {
21 if ( ! defined('WPACU_FS_USED') && ! class_exists('\WP_Filesystem_Base') && ! class_exists('\WP_Filesystem_Direct') ) {
22 $wpFileSystemBase = ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
23 $wpFileSystemDirect = ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
24
25 if (is_file($wpFileSystemBase) && is_file($wpFileSystemDirect)) {
26 // Make sure to use the 'direct' method as it's the most effective in this scenario
27 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
28 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
29 define('WPACU_FS_USED', true);
30 } else {
31 // Do not use WordPress FileSystem Direct (fallback to default PHP functions)
32 define('WPACU_FS_USED', false);
33 }
34 }
35
36 if (wpacuIsDefinedConstant('WPACU_FS_USED')) {
37 return new \WP_Filesystem_Direct( new \StdClass() );
38 }
39
40 return false;
41 }
42
43 /**
44 * @param string $localPathToFile
45 * @param string $alter
46 *
47 * @return false|string
48 */
49 public static function fileGetContents($localPathToFile, $alter = '')
50 {
51 // ONLY relevant for CSS files
52 if ($alter === 'combine_css_imports') {
53 $cssContent = self::fileJustGetContents($localPathToFile);
54
55 if (stripos($cssContent, '@import') !== false) {
56 // This custom class does not minify as it's custom-made for combining @import
57 return (new CombineCssImports($localPathToFile))->minify();
58 }
59
60 return $cssContent; // No '@import' found? Just return it
61 }
62
63 return self::fileJustGetContents($localPathToFile);
64 }
65
66 /**
67 * Fetch the contents of the targeted file without any alteration
68 *
69 * @param $localPathToFile
70 *
71 * @return false|string
72 */
73 public static function fileJustGetContents($localPathToFile)
74 {
75 // Fallback
76 if (! self::init()) {
77 return @file_get_contents($localPathToFile);
78 }
79
80 return self::init()->get_contents($localPathToFile);
81 }
82
83 /**
84 * @param $localPathToFile
85 * @param $contents
86 *
87 * @return bool
88 */
89 public static function filePutContents($localPathToFile, $contents)
90 {
91 $return = false; // default
92
93 if ( (strpos($localPathToFile, WP_CONTENT_DIR . OptimizeCss::getRelPathCssCacheDir()) !== false && ! is_dir(dirname($localPathToFile)))
94 || (strpos($localPathToFile, WP_CONTENT_DIR . OptimizeJs::getRelPathJsCacheDir()) !== false && ! is_dir(dirname($localPathToFile)))
95 ) {
96 $dirToCreate = dirname( $localPathToFile );
97 try {
98 mkdir( $dirToCreate, FS_CHMOD_DIR, true );
99 } catch (\Exception $e) {
100 error_log( WPACU_PLUGIN_TITLE . ': Could not make directory ' . $dirToCreate . ' / Error: '.$e->getMessage() );
101 }
102 }
103
104 // Fallback
105 try {
106 if ( ! self::init() ) {
107 $return = file_put_contents( $localPathToFile, $contents );
108 } else {
109 $return = self::init()->put_contents( $localPathToFile, $contents, FS_CHMOD_FILE );
110 }
111 } catch ( \Exception $e ) {
112 error_log( WPACU_PLUGIN_TITLE . ': Could not write to ' . $localPathToFile . ' / Error: '.$e->getMessage() );
113 }
114
115 return $return;
116 }
117 }
118