Skip to content

Commit

Permalink
Merge pull request #2108 from sandeshjangam/bug/wp/filesystem-functions
Browse files Browse the repository at this point in the history
Closes #1265
  • Loading branch information
GaryJones committed Dec 19, 2022
2 parents 69f6e72 + 10a814d commit a88f0eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
31 changes: 29 additions & 2 deletions WordPress/Sniffs/WP/AlternativeFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,37 @@ public function getGroups() {
),
),

'file_system_read' => array(
'unlink' => array(
'type' => 'warning',
'message' => 'File operations should use WP_Filesystem methods instead of direct PHP filesystem calls. Found: %s()',
'message' => '%s() is discouraged. Use wp_delete_file() to delete a file.',
'since' => '4.2.0',
'functions' => array(
'unlink',
),
),

'rename' => array(
'type' => 'warning',
'message' => '%s() is discouraged. Use WP_Filesystem::move() to rename a file.',
'since' => '2.5.0',
'functions' => array(
'rename',
),
),

'file_system_operations' => array(
'type' => 'warning',
'message' => 'File operations should use WP_Filesystem methods instead of direct PHP filesystem calls. Found: %s().',
'since' => '2.5.0',
'functions' => array(
'chgrp',
'chmod',
'chown',
'is_writable',
'is_writeable',
'mkdir',
'rmdir',
'touch',
'readfile',
'fclose',
'fopen',
Expand All @@ -139,6 +165,7 @@ public function getGroups() {
'file_put_contents',
'fsockopen',
'pfsockopen',
'fputs',
),
),

Expand Down
12 changes: 12 additions & 0 deletions WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ function curl_version_ssl() {} // OK.
use function curl_version; // OK.
use function something as curl_version; // OK.
use function curl_init as curl_version; // Bad.

unlink(); // Warning.
rename(); // Warning.
chgrp(); // Warning.
chmod(); // Warning.
chown(); // Warning.
is_writable(); // Warning.
is_writeable(); // Warning.
mkdir(); // Warning.
rmdir(); // Warning.
touch(); // Warning.
fputs(); // Warning.
11 changes: 11 additions & 0 deletions WordPress/Tests/WP/AlternativeFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public function getWarningList() {
68 => 1,
73 => 1,
80 => 1,
82 => 1,
83 => 1,
84 => 1,
85 => 1,
86 => 1,
87 => 1,
88 => 1,
89 => 1,
90 => 1,
91 => 1,
92 => 1,
);
}

Expand Down

0 comments on commit a88f0eb

Please sign in to comment.