Make WordPress Core

Changeset 60703


Ignore:
Timestamp:
09/03/2025 12:16:17 PM (14 months ago)
Author:
SergeyBiryukov
Message:

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: ​PHP RFC: Deprecations for PHP 8.5: Deprecate no-op functions from the resource to object conversion.

Props TobiasBg, SergeyBiryukov.
See #63061.

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image-edit.php

    r60640 r60703  
    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                }
    … …  
    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                }
    … …  
    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                }
  • trunk/src/wp-admin/link-parse-opml.php

    r58409 r60703  
    9797}
    9898
    99 // Free up memory used by the XML parser.
    100 xml_parser_free( $xml_parser );
     99if ( 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
    101104unset( $xml_parser );
  • trunk/src/wp-includes/IXR/class-IXR-message.php

    r60659 r60703  
    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;
    … …  
    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
  • trunk/src/wp-includes/atomlib.php

    r59062 r60703  
    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
  • trunk/src/wp-includes/class-wp-http-curl.php

    r56655 r60703  
    241241
    242242                        $curl_error = curl_error( $handle );
     243
    243244                        if ( $curl_error ) {
     245                                if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
     246                                        curl_close( $handle );
     247                                }
     248
     249                                return new WP_Error( 'http_request_failed', $curl_error );
     250                        }
     251
     252                        if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) {
     253                                if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
     254                                        curl_close( $handle );
     255                                }
     256
     257                                return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
     258                        }
     259
     260                        if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
    244261                                curl_close( $handle );
    245                                 return new WP_Error( 'http_request_failed', $curl_error );
    246                         }
    247                         if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) {
    248                                 curl_close( $handle );
    249                                 return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) );
    250                         }
    251 
    252                         curl_close( $handle );
     262                        }
     263
    253264                        return array(
    254265                                'headers'  => array(),
    … …  
    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                                        }
    … …  
    289307                        } else {
    290308                                $curl_error = curl_error( $handle );
     309
    291310                                if ( $curl_error ) {
     311                                        if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
     312                                                curl_close( $handle );
     313                                        }
     314
     315                                        return new WP_Error( 'http_request_failed', $curl_error );
     316                                }
     317                        }
     318
     319                        if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) {
     320                                if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0.
    292321                                        curl_close( $handle );
    293                                         return new WP_Error( 'http_request_failed', $curl_error );
    294                                 }
    295                         }
    296                         if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) {
    297                                 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'] ) {
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r59473 r60703  
    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        }
    … …  
    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
    … …  
    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
    … …  
    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                }
    … …  
    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                        }
    … …  
    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                        }
  • trunk/src/wp-includes/feed.php

    r59382 r60703  
    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
  • trunk/src/wp-includes/functions.php

    r60696 r60703  
    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(
    … …  
    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 ) ) {
  • trunk/src/wp-includes/rss.php

    r59063 r60703  
    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
  • trunk/tests/phpunit/includes/utils.php

    r59109 r60703  
    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;
  • trunk/tests/phpunit/tests/image/functions.php

    r58849 r60703  
    361361                $ret  = wp_save_image_file( $file, $img, 'image/jpeg', 1 );
    362362
    363                 imagedestroy( $img );
     363                if ( PHP_VERSION_ID < 80000 ) { // imagedestroy() has no effect as of PHP 8.0.
     364                        imagedestroy( $img );
     365                }
     366
    364367                unlink( $file );
    365368
Note: See TracChangeset for help on using the changeset viewer.