Skip to content

Commit d05c008

Browse files
OlegApanovichorkunaybek
authored andcommitted
VC-3299 fix php fatal error with enviroment without wp_filesystem and php 8.0+ version
1 parent eb9e478 commit d05c008

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

visualcomposer/Helpers/File.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,55 @@ public function getContents($filePath)
2626
return false;
2727
}
2828

29+
$fileSystem = $this->getFileSystem();
30+
if (!$fileSystem) {
31+
return false;
32+
}
33+
2934
// get content from using file system
30-
return $this->getFileSystem()->get_contents($filePath);
35+
return $fileSystem->get_contents($filePath);
3136
}
3237

3338
public function exists($filePath)
3439
{
35-
return $this->getFileSystem()->exists($filePath);
40+
$fileSystem = $this->getFileSystem();
41+
if (!$fileSystem) {
42+
return false;
43+
}
44+
45+
return $fileSystem->exists($filePath);
3646
}
3747

3848
public function isFile($filePath)
3949
{
40-
return $this->getFileSystem()->is_file($filePath);
50+
$fileSystem = $this->getFileSystem();
51+
if (!$fileSystem) {
52+
return false;
53+
}
54+
55+
return $fileSystem->is_file($filePath);
4156
}
4257

4358
public function isDir($filePath)
4459
{
45-
return $this->getFileSystem()->is_dir($filePath);
60+
$fileSystem = $this->getFileSystem();
61+
if (!$fileSystem) {
62+
return false;
63+
}
64+
65+
return $fileSystem->is_dir($filePath);
4666
}
4767

4868
public function rename($oldName, $newName)
4969
{
5070
$this->checkDir(dirname($newName));
51-
return $this->getFileSystem()->move($oldName, $newName);
71+
72+
$fileSystem = $this->getFileSystem();
73+
if (!$fileSystem) {
74+
return false;
75+
}
76+
77+
return $fileSystem->move($oldName, $newName);
5278
}
5379

5480
/**
@@ -60,8 +86,14 @@ public function rename($oldName, $newName)
6086
public function setContents($filePath, $contents)
6187
{
6288
$this->checkDir(dirname($filePath));
89+
90+
$fileSystem = $this->getFileSystem();
91+
if (!$fileSystem) {
92+
return false;
93+
}
94+
6395
// set content using file system
64-
return $this->getFileSystem()->put_contents($filePath, $contents);
96+
return $fileSystem->put_contents($filePath, $contents);
6597
}
6698

6799
/**
@@ -99,12 +131,17 @@ public function unzip($file, $destination, $overwrite = false)
99131
}
100132

101133
/**
134+
* Get wp filesystem object.
135+
*
136+
* @note we always should check if we have access to filesystem before ise it.
137+
*
102138
* @return \WP_Filesystem_Base|bool
103139
*/
104140
public function getFileSystem()
105141
{
106142
// @codingStandardsIgnoreLine
107143
global $wp_filesystem;
144+
108145
$status = true;
109146
// @codingStandardsIgnoreLine
110147
if (!$wp_filesystem || !is_object($wp_filesystem)) {

visualcomposer/Modules/Assets/FileController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ protected function parseGlobalElementsCssFile(
124124
if ($bundleUrl) {
125125
if (!empty($previousCssFile) && empty($previousCssHash)) {
126126
$assetsPath = $assetsHelper->getFilePath($previousCssFile);
127-
if (!empty($assetsPath)) {
128-
$fileHelper->getFileSystem()->delete($assetsPath);
127+
$fileSystem = $fileHelper->getFileSystem();
128+
if ($fileSystem && !empty($assetsPath)) {
129+
$fileSystem->delete($assetsPath);
129130
}
130131
}
131132
}

visualcomposer/Modules/Migrations/Assets22Migration.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ protected function moveFiles(File $fileHelper, Options $optionsHelper)
6868
true
6969
);
7070
if (!is_wp_error($result) && $result) {
71-
$resultMove = $fileHelper->getFileSystem()->move(
72-
VCV_PLUGIN_ASSETS_DIR_PATH . '-temp',
73-
VCV_PLUGIN_ASSETS_DIR_PATH
74-
);
71+
$fileSystem = $fileHelper->getFileSystem();
72+
if ($fileSystem) {
73+
$resultMove = $fileSystem->move(
74+
VCV_PLUGIN_ASSETS_DIR_PATH . '-temp',
75+
VCV_PLUGIN_ASSETS_DIR_PATH
76+
);
77+
}
7578
$responseMove = !is_wp_error($resultMove) && $result;
7679

7780
return $responseMove;

visualcomposer/Modules/Migrations/UpdateGlobalElementsMigration.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ protected function run()
8080

8181
if (!empty($previousCssFile) && empty($previousCssHash)) {
8282
$assetsPath = $assetsHelper->getFilePath($previousCssFile);
83-
if (!empty($assetsPath)) {
84-
$fileHelper->getFileSystem()->delete($assetsPath);
83+
$fileSystem = $fileHelper->getFileSystem();
84+
if ($fileSystem && !empty($assetsPath)) {
85+
$fileSystem->delete($assetsPath);
8586
}
8687
}
8788
}

0 commit comments

Comments
 (0)