Skip to content

Commit 9b6c234

Browse files
Code Modernization: Address no-op function deprecations in PHP 8.5.
Several PHP functions that have not been doing anything since PHP 8.0/8.1, specifically: * `finfo_close()` since the `ext/fileinfo` migration in PHP 8.1 * `xml_parser_free()` since the `ext/xml` migration in PHP 8.0 * `curl_close()` since the `ext/curl` migration in PHP 8.0 * `curl_share_close()` since the `ext/curl` migration in PHP 8.0 * `imagedestroy()` since the `ext/gd` migration in PHP 8.0 will be deprecated in PHP 8.5 and will thus be throwing warnings. This commit adds conditional checks to only call these functions on the relevant PHP versions. Reference: [https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_no-op_functions_from_the_resource_to_object_conversion PHP RFC: Deprecations for PHP 8.5: Deprecate no-op functions from the resource to object conversion]. Props TobiasBg, SergeyBiryukov. See #63061. git-svn-id: https://develop.svn.wordpress.org/trunk@60703 602fd350-edb4-49c9-b593-d223f7449a82
1 parent cfcb4e9 commit 9b6c234

File tree

11 files changed

+119
-29
lines changed

11 files changed

+119
-29
lines changed

src/wp-admin/includes/image-edit.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ function _rotate_image_resource( $img, $angle ) {
545545
$rotated = imagerotate( $img, $angle, 0 );
546546

547547
if ( is_gd_image( $rotated ) ) {
548-
imagedestroy( $img );
548+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
549+
imagedestroy( $img );
550+
}
551+
549552
$img = $rotated;
550553
}
551554
}
@@ -580,7 +583,10 @@ function _flip_image_resource( $img, $horz, $vert ) {
580583
$sh = $horz ? -$h : $h;
581584

582585
if ( imagecopyresampled( $dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
583-
imagedestroy( $img );
586+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
587+
imagedestroy( $img );
588+
}
589+
584590
$img = $dst;
585591
}
586592
}
@@ -606,7 +612,10 @@ function _crop_image_resource( $img, $x, $y, $w, $h ) {
606612

607613
if ( is_gd_image( $dst ) ) {
608614
if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
609-
imagedestroy( $img );
615+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
616+
imagedestroy( $img );
617+
}
618+
610619
$img = $dst;
611620
}
612621
}

src/wp-admin/link-parse-opml.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ function endElement( $parser, $tag_name ) { // phpcs:ignore WordPress.NamingConv
9696
);
9797
}
9898

99-
// Free up memory used by the XML parser.
100-
xml_parser_free( $xml_parser );
99+
if ( PHP_VERSION_ID < 80000 ) { // xml_parser_free() has no effect as of PHP 8.0.
100+
// Free up memory used by the XML parser.
101+
xml_parser_free( $xml_parser );
102+
}
103+
101104
unset( $xml_parser );

src/wp-includes/IXR/class-IXR-message.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ function parse()
119119
$this->message = substr($this->message, $chunk_size);
120120

121121
if (!xml_parse($this->_parser, $part, $final)) {
122-
xml_parser_free($this->_parser);
122+
if (PHP_VERSION_ID < 80000) { // xml_parser_free() has no effect as of PHP 8.0.
123+
xml_parser_free($this->_parser);
124+
}
125+
123126
unset($this->_parser);
124127
return false;
125128
}
@@ -129,7 +132,10 @@ function parse()
129132
}
130133
} while (true);
131134

132-
xml_parser_free($this->_parser);
135+
if (PHP_VERSION_ID < 80000) { // xml_parser_free() has no effect as of PHP 8.0.
136+
xml_parser_free($this->_parser);
137+
}
138+
133139
unset($this->_parser);
134140

135141
// Grab the error messages, if any

src/wp-includes/atomlib.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ function parse() {
184184
}
185185
fclose($fp);
186186

187-
xml_parser_free($parser);
187+
if (PHP_VERSION_ID < 80000) { // xml_parser_free() has no effect as of PHP 8.0.
188+
xml_parser_free($parser);
189+
}
190+
188191
unset($parser);
189192

190193
restore_error_handler();

src/wp-includes/class-wp-http-curl.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,27 @@ public function request( $url, $args = array() ) {
240240
curl_exec( $handle );
241241

242242
$curl_error = curl_error( $handle );
243+
243244
if ( $curl_error ) {
244-
curl_close( $handle );
245+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
246+
curl_close( $handle );
247+
}
248+
245249
return new WP_Error( 'http_request_failed', $curl_error );
246250
}
251+
247252
if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) {
248-
curl_close( $handle );
253+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
254+
curl_close( $handle );
255+
}
256+
249257
return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
250258
}
251259

252-
curl_close( $handle );
260+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
261+
curl_close( $handle );
262+
}
263+
253264
return array(
254265
'headers' => array(),
255266
'body' => '',
@@ -278,28 +289,45 @@ public function request( $url, $args = array() ) {
278289
if ( CURLE_WRITE_ERROR /* 23 */ === $curl_error ) {
279290
if ( ! $this->max_body_length || $this->max_body_length !== $bytes_written_total ) {
280291
if ( $parsed_args['stream'] ) {
281-
curl_close( $handle );
292+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
293+
curl_close( $handle );
294+
}
295+
282296
fclose( $this->stream_handle );
297+
283298
return new WP_Error( 'http_request_failed', __( 'Failed to write request to temporary file.' ) );
284299
} else {
285-
curl_close( $handle );
300+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
301+
curl_close( $handle );
302+
}
303+
286304
return new WP_Error( 'http_request_failed', curl_error( $handle ) );
287305
}
288306
}
289307
} else {
290308
$curl_error = curl_error( $handle );
309+
291310
if ( $curl_error ) {
292-
curl_close( $handle );
311+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
312+
curl_close( $handle );
313+
}
314+
293315
return new WP_Error( 'http_request_failed', $curl_error );
294316
}
295317
}
318+
296319
if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) {
297-
curl_close( $handle );
320+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
321+
curl_close( $handle );
322+
}
323+
298324
return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
299325
}
300326
}
301327

302-
curl_close( $handle );
328+
if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
329+
curl_close( $handle );
330+
}
303331

304332
if ( $parsed_args['stream'] ) {
305333
fclose( $this->stream_handle );

src/wp-includes/class-wp-image-editor-gd.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
2323

2424
public function __destruct() {
2525
if ( $this->image ) {
26-
// We don't need the original in memory anymore.
27-
imagedestroy( $this->image );
26+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
27+
// We don't need the original in memory anymore.
28+
imagedestroy( $this->image );
29+
}
2830
}
2931
}
3032

@@ -188,8 +190,12 @@ public function resize( $max_w, $max_h, $crop = false ) {
188190
$resized = $this->_resize( $max_w, $max_h, $crop );
189191

190192
if ( is_gd_image( $resized ) ) {
191-
imagedestroy( $this->image );
193+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
194+
imagedestroy( $this->image );
195+
}
196+
192197
$this->image = $resized;
198+
193199
return true;
194200

195201
} elseif ( is_wp_error( $resized ) ) {
@@ -324,7 +330,10 @@ public function make_subsize( $size_data ) {
324330
$saved = $resized;
325331
} else {
326332
$saved = $this->_save( $resized );
327-
imagedestroy( $resized );
333+
334+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
335+
imagedestroy( $resized );
336+
}
328337
}
329338

330339
$this->size = $orig_size;
@@ -382,9 +391,13 @@ public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = nu
382391
imagecopyresampled( $dst, $this->image, 0, 0, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h );
383392

384393
if ( is_gd_image( $dst ) ) {
385-
imagedestroy( $this->image );
394+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
395+
imagedestroy( $this->image );
396+
}
397+
386398
$this->image = $dst;
387399
$this->update_size();
400+
388401
return true;
389402
}
390403

@@ -408,9 +421,14 @@ public function rotate( $angle ) {
408421
if ( is_gd_image( $rotated ) ) {
409422
imagealphablending( $rotated, true );
410423
imagesavealpha( $rotated, true );
411-
imagedestroy( $this->image );
424+
425+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
426+
imagedestroy( $this->image );
427+
}
428+
412429
$this->image = $rotated;
413430
$this->update_size();
431+
414432
return true;
415433
}
416434
}
@@ -439,8 +457,12 @@ public function flip( $horz, $vert ) {
439457
$sh = $horz ? -$h : $h;
440458

441459
if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
442-
imagedestroy( $this->image );
460+
if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
461+
imagedestroy( $this->image );
462+
}
463+
443464
$this->image = $dst;
465+
444466
return true;
445467
}
446468
}

src/wp-includes/feed.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,11 @@ function prep_atom_text_construct( $data ) {
599599
$parser = xml_parser_create();
600600
xml_parse( $parser, '<div>' . $data . '</div>', true );
601601
$code = xml_get_error_code( $parser );
602-
xml_parser_free( $parser );
602+
603+
if ( PHP_VERSION_ID < 80000 ) { // xml_parser_free() has no effect as of PHP 8.0.
604+
xml_parser_free( $parser );
605+
}
606+
603607
unset( $parser );
604608

605609
if ( ! $code ) {

src/wp-includes/functions.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,10 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
31723172
if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
31733173
$finfo = finfo_open( FILEINFO_MIME_TYPE );
31743174
$real_mime = finfo_file( $finfo, $file );
3175-
finfo_close( $finfo );
3175+
3176+
if ( PHP_VERSION_ID < 80100 ) { // finfo_close() has no effect as of PHP 8.1.
3177+
finfo_close( $finfo );
3178+
}
31763179

31773180
$google_docs_types = array(
31783181
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
@@ -3401,7 +3404,10 @@ function wp_get_image_mime( $file ) {
34013404
if ( extension_loaded( 'fileinfo' ) ) {
34023405
$fileinfo = finfo_open( FILEINFO_MIME_TYPE );
34033406
$mime_type = finfo_file( $fileinfo, $file );
3404-
finfo_close( $fileinfo );
3407+
3408+
if ( PHP_VERSION_ID < 80100 ) { // finfo_close() has no effect as of PHP 8.1.
3409+
finfo_close( $fileinfo );
3410+
}
34053411

34063412
if ( wp_is_heic_image_mime_type( $mime_type ) ) {
34073413
$mime = $mime_type;

src/wp-includes/rss.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ function __construct( $source ) {
9393
}
9494
}
9595

96-
xml_parser_free( $this->parser );
96+
if ( PHP_VERSION_ID < 80000 ) { // xml_parser_free() has no effect as of PHP 8.0.
97+
xml_parser_free( $this->parser );
98+
}
99+
97100
unset( $this->parser );
98101

99102
$this->normalize();

tests/phpunit/includes/utils.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ public function parse( $in ) {
311311
xml_get_current_line_number( $this->xml )
312312
)
313313
);
314-
xml_parser_free( $this->xml );
314+
315+
if ( PHP_VERSION_ID < 80000 ) { // xml_parser_free() has no effect as of PHP 8.0.
316+
xml_parser_free( $this->xml );
317+
}
315318
}
316319
return true;
317320
}

0 commit comments

Comments
 (0)