Skip to content

Commit 8e2de82

Browse files
committed
General: Fix various issues flagged by the PHPCompatibilityWP PHPCS ruleset.
As part of the continued effort to improve PHP compatibility, the following improvments are being made: - Removing deprecated PHP `safe_mode` checks not found in bundled external libraries. - Change the remaining `while` loops using `each()` to `foreach` loops. - Prevent false positives from being flagged for the `sodium_compat` library being caused by loading this in a non-standard way. - Add inline comments to not flag deprecated PHP directives in the getID3 library. Props desrosj, earnjam, dryanpress. See #49922. git-svn-id: https://develop.svn.wordpress.org/trunk@47735 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0542357 commit 8e2de82

4 files changed

Lines changed: 35 additions & 26 deletions

File tree

phpcompat.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@
4040
-->
4141
<exclude-pattern>/vendor/*</exclude-pattern>
4242

43+
<!--
44+
PHPCompatibilityParagonieSodiumCompat prevents false positives in `sodium_compat`.
45+
However, because these files are included in a non-standard path, false positives are triggered in WordPress Core.
46+
-->
47+
<exclude-pattern>src/wp-includes/sodium_compat/lib/php72compat_const\.php$</exclude-pattern>
48+
49+
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.sodium_crypto_sign_keypair_from_secretkey_and_publickeyFound">
50+
<exclude-pattern>/sodium_compat/src/Compat\.php$</exclude-pattern>
51+
</rule>
52+
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.sodium_padFound">
53+
<exclude-pattern>/sodium_compat/src/Compat\.php$</exclude-pattern>
54+
</rule>
55+
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.sodium_unpadFound">
56+
<exclude-pattern>/sodium_compat/src/Compat\.php$</exclude-pattern>
57+
</rule>
58+
4359
<!--
4460
PHPCompatibilityParagonieRandomCompat prevents false positives in `random_compat`.
4561
However, because these files are included in a non-standard path, false positives are triggered in WordPress Core.

src/wp-admin/includes/schema.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,6 @@ function populate_options( array $options = array() ) {
369369
*/
370370
do_action( 'populate_options' );
371371

372-
if ( ini_get( 'safe_mode' ) ) {
373-
// Safe mode can break mkdir() so use a flat structure by default.
374-
$uploads_use_yearmonth_folders = 0;
375-
} else {
376-
$uploads_use_yearmonth_folders = 1;
377-
}
378-
379372
// If WP_DEFAULT_THEME doesn't exist, fall back to the latest core default theme.
380373
$stylesheet = WP_DEFAULT_THEME;
381374
$template = WP_DEFAULT_THEME;
@@ -466,7 +459,7 @@ function populate_options( array $options = array() ) {
466459
'db_version' => $wp_db_version,
467460

468461
// 2.0.1
469-
'uploads_use_yearmonth_folders' => $uploads_use_yearmonth_folders,
462+
'uploads_use_yearmonth_folders' => 1,
470463
'upload_path' => '',
471464

472465
// 2.1

src/wp-includes/ID3/getid3.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function __construct() {
285285
}
286286

287287
// Check safe_mode off
288-
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
288+
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) { // phpcs:ignore // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.get_magic_quotes_runtimeDeprecated
289289
$this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
290290
}
291291

@@ -300,13 +300,13 @@ public function __construct() {
300300
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
301301
// Check for magic_quotes_runtime
302302
if (function_exists('get_magic_quotes_runtime')) {
303-
if (get_magic_quotes_runtime()) {
303+
if (get_magic_quotes_runtime()) { // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.get_magic_quotes_runtimeDeprecated
304304
$this->startup_error .= 'magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).'."\n";
305305
}
306306
}
307307
// Check for magic_quotes_gpc
308308
if (function_exists('get_magic_quotes_gpc')) {
309-
if (get_magic_quotes_gpc()) {
309+
if (get_magic_quotes_gpc()) { // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.get_magic_quotes_gpcDeprecated
310310
$this->startup_error .= 'magic_quotes_gpc must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_gpc(0) and set_magic_quotes_gpc(1).'."\n";
311311
}
312312
}
@@ -1560,7 +1560,7 @@ public function getHashdata($algorithm) {
15601560
// page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
15611561
// currently vorbiscomment only works on OggVorbis files.
15621562

1563-
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
1563+
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) { // phpcs:ignore // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.get_magic_quotes_runtimeDeprecated
15641564

15651565
$this->warning('Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)');
15661566
$this->info[$algorithm.'_data'] = false;

src/wp-includes/class-snoopy.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function fetch($URI)
183183
$frameurls = $this->_frameurls;
184184
$this->_frameurls = array();
185185

186-
while(list(,$frameurl) = each($frameurls))
186+
foreach ( $frameurls as $frameurl )
187187
{
188188
if($this->_framedepth < $this->maxframes)
189189
{
@@ -243,7 +243,7 @@ function fetch($URI)
243243
$frameurls = $this->_frameurls;
244244
$this->_frameurls = array();
245245

246-
while(list(,$frameurl) = each($frameurls))
246+
foreach ( $frameurls as $frameurl )
247247
{
248248
if($this->_framedepth < $this->maxframes)
249249
{
@@ -341,7 +341,7 @@ function submit($URI, $formvars="", $formfiles="")
341341
$frameurls = $this->_frameurls;
342342
$this->_frameurls = array();
343343

344-
while(list(,$frameurl) = each($frameurls))
344+
foreach ( $frameurls as $frameurl )
345345
{
346346
if($this->_framedepth < $this->maxframes)
347347
{
@@ -408,7 +408,7 @@ function submit($URI, $formvars="", $formfiles="")
408408
$frameurls = $this->_frameurls;
409409
$this->_frameurls = array();
410410

411-
while(list(,$frameurl) = each($frameurls))
411+
foreach ( $frameurls as $frameurl )
412412
{
413413
if($this->_framedepth < $this->maxframes)
414414
{
@@ -629,13 +629,13 @@ function _striplinks($document)
629629

630630
// catenate the non-empty matches from the conditional subpattern
631631

632-
while(list($key,$val) = each($links[2]))
632+
foreach ( $links[2] as $key => $val )
633633
{
634634
if(!empty($val))
635635
$match[] = $val;
636636
}
637637

638-
while(list($key,$val) = each($links[3]))
638+
foreach ( $links[3] as $key => $val )
639639
{
640640
if(!empty($val))
641641
$match[] = $val;
@@ -821,7 +821,7 @@ function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
821821
{
822822
if(!is_array($this->rawheaders))
823823
$this->rawheaders = (array)$this->rawheaders;
824-
while(list($headerKey,$headerVal) = each($this->rawheaders))
824+
foreach ( $this->rawheaders as $headerKey => $headerVal )
825825
$headers .= $headerKey.": ".$headerVal."\r\n";
826826
}
827827
if(!empty($content_type)) {
@@ -985,7 +985,7 @@ function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
985985
{
986986
if(!is_array($this->rawheaders))
987987
$this->rawheaders = (array)$this->rawheaders;
988-
while(list($headerKey,$headerVal) = each($this->rawheaders))
988+
foreach ( $this->rawheaders as $headerKey => $headerVal )
989989
$headers[] = $headerKey.": ".$headerVal;
990990
}
991991
if(!empty($content_type)) {
@@ -1204,9 +1204,9 @@ function _prepare_post_body($formvars, $formfiles)
12041204
switch ($this->_submit_type) {
12051205
case "application/x-www-form-urlencoded":
12061206
reset($formvars);
1207-
while(list($key,$val) = each($formvars)) {
1207+
foreach ( $formvars as $key => $val ) {
12081208
if (is_array($val) || is_object($val)) {
1209-
while (list($cur_key, $cur_val) = each($val)) {
1209+
foreach ( $val as $cur_key => $cur_val ) {
12101210
$postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
12111211
}
12121212
} else
@@ -1218,9 +1218,9 @@ function _prepare_post_body($formvars, $formfiles)
12181218
$this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
12191219

12201220
reset($formvars);
1221-
while(list($key,$val) = each($formvars)) {
1221+
foreach ( $formvars as $key => $val ) {
12221222
if (is_array($val) || is_object($val)) {
1223-
while (list($cur_key, $cur_val) = each($val)) {
1223+
foreach ( $val as $cur_key => $cur_val ) {
12241224
$postdata .= "--".$this->_mime_boundary."\r\n";
12251225
$postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
12261226
$postdata .= "$cur_val\r\n";
@@ -1233,9 +1233,9 @@ function _prepare_post_body($formvars, $formfiles)
12331233
}
12341234

12351235
reset($formfiles);
1236-
while (list($field_name, $file_names) = each($formfiles)) {
1236+
foreach ( $formfiles as $field_name => $file_names ) {
12371237
settype($file_names, "array");
1238-
while (list(, $file_name) = each($file_names)) {
1238+
foreach ( $file_names as $file_name ) {
12391239
if (!is_readable($file_name)) continue;
12401240

12411241
$fp = fopen($file_name, "r");

0 commit comments

Comments
 (0)