PluginProbe
Themify Builder / trunk
Themify Builder vtrunk
7.8.1 7.8.0 7.7.9 7.7.8 7.7.7 7.7.6 7.7.5 7.7.4 7.7.3 7.7.2 trunk 7.6.0 7.6.1 7.6.2 7.6.3 7.6.4 7.6.5 7.6.6 7.6.7 7.6.8 7.6.9 7.7.0 7.7.1
themify-builder / themify / class-themify-filesystem.php

class-themify-filesystem.php in Themify Builder trunk, at themify/class-themify-filesystem.php

234 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The file that defines File System class
5 *
6 * Themify_Filesystem class provide instance of Filesystem Api to do some file operation.
7 * Based on WP_Filesystem the class method will remain same.
8 *
9 *
10 * @package Themify
11 * @subpackage Filesystem
12 */
13 if (!class_exists('Themify_Filesystem',false)) {
14
15 /**
16 * The Themify_Filesystem class.
17 *
18 * This is used to initialize WP_Filesytem Api instance
19 * check for filesytem method and return correct filesystem method
20 *
21 * @package Themify
22 * @subpackage Filesystem
23 * @author Themify
24 */
25 class Themify_Filesystem {
26
27 /**
28 * Instance of WP_Filesytem api class.
29 *
30 * @access public
31 * @var $execute Store the instance of WP_Filesystem class being used.
32 */
33 public $execute = null;
34
35 /**
36 * Class constructor.
37 *
38 * @access public
39 */
40 private function __construct() {
41 $this->initialize();
42 }
43
44 /**
45 * Return an instance of this class.
46 *
47 * @return object A single instance of this class.
48 */
49 public static function get_instance() {
50 static $instance = null;
51 if ($instance === null) {
52 $instance = new self;
53 }
54 return $instance;
55 }
56
57 /**
58 * Initialize filesystem method.
59 */
60 private function initialize() {
61 if (!defined('FS_METHOD')) {
62 define('FS_METHOD', 'direct');
63 }
64 // Load WP Filesystem
65 if (!function_exists('WP_Filesystem')) {
66 require_once ABSPATH . 'wp-admin/includes/file.php';
67 }
68 WP_Filesystem();
69 global $wp_filesystem;
70 if (is_a($wp_filesystem, 'WP_Filesystem_Direct')) {
71 $this->execute = $wp_filesystem;
72 } else {
73 $this->execute = self::load_direct();
74 }
75 }
76
77 /**
78 * Initialize Filesystem direct method.
79 */
80 private static function load_direct() {
81 require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
82 require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
83 return new WP_Filesystem_Direct(array());
84 }
85
86 public static function get_file_content($file, $check = false) {
87 static $site_url = null;
88 static $content_url = null;
89 if ($site_url === null) {
90 $site_url = rtrim(site_url(), '/') . '/';
91 }
92 if ($content_url === null) {
93 $content_url = rtrim(content_url(), '/');
94 }
95 $upload_dir = themify_upload_dir();
96 if (strpos($file, $site_url) !== false || strpos($file, $upload_dir['baseurl']) !== false || strpos($file, $content_url) !== false) {
97 $file = urldecode($file);
98 if (strpos($file, $upload_dir['baseurl']) !== false) {
99 $dir = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file);
100 } elseif (strpos($file, '/' . WPINC . '/') !== false || strpos($file, '/wp-admin/') !== false) {
101 $dir = str_replace($site_url, ABSPATH, $file);
102 } elseif (strpos($file, $site_url) !== false) {
103 $dir = str_replace($site_url, rtrim(dirname(WP_CONTENT_DIR), '/') . '/', $file);
104 } else {
105 $dir = str_replace($content_url, rtrim(WP_CONTENT_DIR, '/'), $file);
106 }
107 unset($upload_dir);
108 $dir = strtok($dir, '?');
109 } else {
110 return null;
111 }
112 if ($check === true) {
113 return self::is_readable($dir);
114 }
115 $data = self::get_contents($dir);
116 return !empty($data) ? $data : null;
117 }
118
119 public static function is_dir($dir) {
120 return is_dir($dir);
121 }
122
123 public static function is_file($file) {
124 return is_file($file);
125 }
126
127 public static function is_readable($dir) {
128 return is_readable($dir);
129 }
130
131 public static function is_writable($dir) {
132 return is_writable($dir);
133 }
134
135 public static function exists($file) {
136 return file_exists($file);
137 }
138
139 public static function size($file) {
140 return filesize($file);
141 }
142
143 public static function delete($dir, $type = false) {
144 try {
145 set_error_handler(array(__CLASS__, 'errorHandler'), E_WARNING);
146 $removed = self::remove($dir, $type);
147 restore_error_handler();
148 if ($removed) {
149 return true;
150 }
151 return false;
152 } catch (Throwable $e) {
153 restore_error_handler();
154 return true;
155 }
156 }
157
158 private static function remove($dir, $type = false) {
159 if ($type !== 'f' && self::is_dir($dir)) {
160 $dirHandle = opendir($dir);
161 if (empty($dirHandle)) {
162 return false;
163 }
164 $dir=trailingslashit($dir);
165 while (false !== ($f = readdir($dirHandle))) {
166 if ($f !== '.' && $f !== '..') {
167 $d = $dir . $f;
168 if (self::is_dir($d)) {
169 self::remove($d);
170 } elseif (self::is_file($d) || is_link($d)) {
171 unlink($d);
172 }
173 }
174 }
175 closedir($dirHandle);
176 $dirHandle = null;
177 return rmdir($dir);
178 } elseif (self::is_file($dir) || is_link($dir)) {
179 return unlink($dir);
180 }
181 return true;
182 }
183
184 public static function put_contents($file, $contents, $mode = false) {
185 $instance = self::get_instance();
186 if ($mode === false) {
187 $mode = FS_CHMOD_FILE;
188 }
189 return $instance->execute->put_contents($file, $contents, $mode);
190 }
191
192 public static function get_contents($file) {
193 return self::is_file($file) ? file_get_contents($file) : '';
194 }
195
196 public static function errorHandler($errno, $errstr, $errfile, $errline) {
197 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
198 }
199
200 public static function mkdir($dir, $r = false, $perm = 0755) {
201 if (!self::is_dir($dir)) {
202 try {
203 set_error_handler(array(__CLASS__, 'errorHandler'), E_WARNING);
204 $check = mkdir($dir, $perm, $r);
205 restore_error_handler();
206 if ($check) {
207 return true;
208 }
209 return false;
210 } catch (Throwable $e) {
211 restore_error_handler();
212 return self::is_dir($dir);
213 }
214 }
215 return true;
216 }
217
218 public static function rename($from, $to) {
219 try {
220 set_error_handler(array(__CLASS__, 'errorHandler'), E_WARNING);
221 $check = rename($from, $to);
222 restore_error_handler();
223 if ($check) {
224 return true;
225 }
226 return false;
227 } catch (Throwable $e) {
228 restore_error_handler();
229 return self::is_dir($to) && !self::is_dir($from);
230 }
231 }
232 }
233
234 }