PluginProbe
Visual Composer Website Builder / 45.16.4
Visual Composer Website Builder v45.16.4
45.16.4 45.16.3 45.16.2 45.16.1 45.2-beta.1 45.2-beta.2 45.2.0 45.3-beta.1 45.3.0 45.4-beta.1 45.4.0 45.4.1 45.4.1-beta.2 45.5-beta.1 45.5.0 45.6-beta.1 45.6.0 45.7-beta.1 45.7.0 45.8-beta.1 45.8.0 45.9.0 trunk 45.0 45.0-beta.1 All 42 releases
← All changes | visualcomposer/Helpers/File.php +122 -55 45.0 → 45.16.4 View file →
@@ -15,11 +15,25 @@
15 15 */
16 16 class File implements Helper
17 17 {
18 18 /**
19 + * File system object.
20 + *
21 + * @note we always have object of file system cos we check it in our plugin requirements.
22 + *
23 + * @var object WP_Filesystem_Base
24 + */
25 + public $fileSystem;
26 +
27 + public function __construct()
28 + {
29 + $this->fileSystem = $this->getFileSystem();
30 + }
31 +
32 + /**
19 33 * @param $filePath
20 34 *
21 - * @return mixed
35 + * @return bool|string
22 36 */
23 37 public function getContents($filePath)
24 38 {
25 39 if (!$this->isFile($filePath)) {
@@ -25,82 +39,79 @@
25 39 if (!$this->isFile($filePath)) {
26 40 return false;
27 41 }
28 42
29 - return file_get_contents($filePath);
43 + // get content from using file system
44 + return $this->fileSystem->get_contents($filePath);
30 45 }
31 46
32 - /**
33 - * @param $filePath
34 - * @param $contents
35 - *
36 - * @return mixed
37 - */
38 - public function setContents($filePath, $contents)
47 + public function exists($filePath)
39 48 {
40 - return file_put_contents($filePath, $contents);
49 + return $this->fileSystem->exists($filePath);
41 50 }
42 51
43 - /**
44 - * Check does file exist
45 - *
46 - * @param $filePath
47 - *
48 - * @return bool
49 - */
50 52 public function isFile($filePath)
51 53 {
52 - return is_file($filePath);
54 + return $this->fileSystem->is_file($filePath);
53 55 }
54 56
57 + public function isDir($filePath)
58 + {
59 + return $this->fileSystem->is_dir($filePath);
60 + }
61 +
62 + public function rename($oldName, $newName)
63 + {
64 + $this->checkDir(dirname($newName));
65 +
66 + return $this->fileSystem->move($oldName, $newName);
67 + }
68 +
55 69 /**
56 - * Check does directory exist
70 + * @param $filePath
71 + * @param $contents
57 72 *
58 - * @param $dirPath
59 - *
60 - * @return bool
73 + * @return false|int
61 74 */
62 - public function isDir($dirPath)
75 + public function setContents($filePath, $contents)
63 76 {
64 - return is_dir($dirPath);
77 + $this->checkDir(dirname($filePath));
78 +
79 + // set content using file system
80 + return $this->fileSystem->put_contents($filePath, $contents);
65 81 }
66 82
67 83 /**
68 - * Check does directory exists and if not create it
84 + * Check does directory exist and if not create it
69 85 *
70 86 * @param $dirPath
71 - * @param int $permissions
72 87 *
73 88 * @return bool
74 89 */
75 - public function checkDir($dirPath, $permissions = 0777)
90 + public function checkDir($dirPath)
76 91 {
77 - return !$this->isDir($dirPath) ? mkdir($dirPath, $permissions, true) : true;
92 + return wp_mkdir_p($dirPath);
78 93 }
79 94
80 95 public function download($url)
81 96 {
82 97 require_once(ABSPATH . '/wp-admin/includes/file.php');
83 - $downloadedArchive = download_url($url);
84 -
85 - return $downloadedArchive;
98 + return download_url($url);
86 99 }
87 100
88 101 public function unzip($file, $destination, $overwrite = false)
89 102 {
90 - $fileSystem = $this->getFileSystem();
91 - if (!$fileSystem) {
92 - return false;
93 - }
94 103 if ($overwrite && is_dir($destination)) {
95 - $fileSystem->delete($destination);
104 + $this->fileSystem->delete($destination);
96 105 }
97 - $result = unzip_file($file, $destination);
98 -
99 - return $result;
106 + return unzip_file($file, $destination);
100 107 }
101 108
102 109 /**
110 + * Get wp filesystem object.
111 + *
112 + * @note we always should check if we have access to filesystem before ise it.
113 + *
103 114 * @return \WP_Filesystem_Base|bool
104 115 */
105 116 public function getFileSystem()
106 117 {
@@ -105,8 +116,9 @@
105 116 public function getFileSystem()
106 117 {
107 118 // @codingStandardsIgnoreLine
108 119 global $wp_filesystem;
120 +
109 121 $status = true;
110 122 // @codingStandardsIgnoreLine
111 123 if (!$wp_filesystem || !is_object($wp_filesystem)) {
112 124 require_once(ABSPATH . '/wp-admin/includes/file.php');
@@ -118,27 +130,18 @@
118 130 }
119 131
120 132 public function removeDirectory($dir, $recursive = true)
121 133 {
122 - $fileSystem = $this->getFileSystem();
123 - if (!$fileSystem) {
124 - return false;
125 - }
126 -
127 - return $fileSystem->rmdir($dir, $recursive);
134 + return $this->fileSystem->rmdir($dir, $recursive);
128 135 }
129 136
130 137 public function removeFile($file)
131 138 {
132 - return wp_delete_file($file);
139 + wp_delete_file($file);
133 140 }
134 141
135 142 public function copyDirectory($from, $to, $overwrite = true)
136 143 {
137 - $fileSystem = $this->getFileSystem();
138 - if (!$fileSystem) {
139 - return false;
140 - }
141 144 if ($overwrite) {
142 145 $this->removeDirectory($to);
143 146 }
144 147 $this->createDirectory($to);
@@ -147,17 +150,81 @@
147 150 }
148 151
149 152 public function copyFile($from, $to, $overwrite = true)
150 153 {
151 - $fileSystem = $this->getFileSystem();
152 - if (!$fileSystem) {
154 + return $this->fileSystem->copy($from, $to, $overwrite);
155 + }
156 +
157 + public function createDirectory($dir)
158 + {
159 + return wp_mkdir_p($dir);
160 + }
161 +
162 + /**
163 + * We use it to get file contents from url.
164 + *
165 + * @note We also check the case when link to file lead to local server upload folder like
166 + *
167 + * @return string
168 + */
169 + public function getRemoteContents($url)
170 + {
171 + $remoteContent = $this->doRequest($url);
172 + if ($remoteContent) {
173 + return $remoteContent;
174 + }
175 +
176 + // in case when file is on localhost but all other cases do not working for it.
177 + // we try to get file path to uploads from url and then get his content.
178 + $remoteContent = $this->getContentsUrlAsLocalUpload($url);
179 + if ($remoteContent) {
180 + return $remoteContent;
181 + }
182 +
183 + return false;
184 + }
185 +
186 + /**
187 + * Process request to specific url.
188 + *
189 + * @param string $url
190 + * @param array $args
191 + *
192 + * @return false|string
193 + */
194 + public function doRequest($url, $args = [])
195 + {
196 + $response = wp_remote_request($url, $args);
197 +
198 + if (is_wp_error($response)) {
153 199 return false;
154 200 }
155 201
156 - return $fileSystem->copy($from, $to, $overwrite);
202 + $responseCode = wp_remote_retrieve_response_code($response);
203 +
204 + // Do nothing, response code is okay.
205 + if ($responseCode === 200 || strpos($responseCode, '200') !== false) {
206 + $response = wp_remote_retrieve_body($response);
207 + } else {
208 + $response = false;
209 + }
210 +
211 + return $response;
157 212 }
158 213
159 - public function createDirectory($dir)
214 + /**
215 + * Try to convert url to wp uploads file path and then get content of it.
216 + *
217 + * @param string $url
218 + *
219 + * @return bool|string
220 + */
221 + public function getContentsUrlAsLocalUpload($url)
160 222 {
161 - return $this->getFileSystem()->mkdir($dir);
223 + $basedirUpload = wp_upload_dir()['basedir'];
224 + $siteUrl = get_site_url();
225 +
226 + $file = str_replace($siteUrl . '/wp-content/uploads', $basedirUpload, $url);
227 +
228 + return $this->getContents($file);
162 229 }
163 230 }