Make WordPress Core

Changeset 60796


Ignore:
Timestamp:
09/23/2025 07:26:03 PM (12 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Address __sleep() and __wakeup() deprecations in PHP 8.5.

PHP 8.5 deprecates the __sleep() and __wakeup() magic methods in favor of __serialize() and __unserialize():

Deprecated: The __wakeup() serialization magic method has been deprecated. Implement __unserialize() instead (or in addition, if support for old PHP versions is necessary)

For PHP < 7.4 compatibility, __sleep() and __wakeup() need to be kept for the time being.

This commit moves the logic of __wakeup() methods in core to __unserialize(), and turns the former into wrappers. WordPress core does not use __sleep() methods, so these are the only changes required.

Reference: PHP RFC: Deprecations for PHP 8.5: Deprecate the __sleep() and __wakeup() magic methods.

Follow-up to [56835], [60787], [60795].

Props TobiasBg, tusharbharti, swissspidy, dmsnell, SergeyBiryukov.
Fixes #63962. See #63061.

Location:
trunk/src/wp-includes
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-bindings-registry.php

    r59080 r60796  
    258258
    259259        /**
    260          * Wakeup magic method.
    261          *
    262          * @since 6.5.0
    263          */
    264         public function __wakeup() {
     260         * Unserialize magic method.
     261         *
     262         * @since 6.9.0
     263         *
     264         * @param array $data Data to unserialize.
     265         */
     266        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    265267                if ( ! $this->sources ) {
    266268                        return;
     
    277279
    278280        /**
     281         * Wakeup magic method.
     282         *
     283         * @since 6.5.0
     284         */
     285        public function __wakeup() {
     286                $this->__unserialize( array() );
     287        }
     288
     289        /**
    279290         * Utility method to retrieve the main instance of the class.
    280291         *
  • trunk/src/wp-includes/class-wp-block-bindings-source.php

    r58972 r60796  
    100100
    101101        /**
     102         * Unserialize magic method.
     103         *
     104         * @since 6.9.0
     105         *
     106         * @param array $data Data to unserialize.
     107         */
     108        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
     109                throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     110        }
     111
     112        /**
    102113         * Wakeup magic method.
    103114         *
     
    105116         */
    106117        public function __wakeup() {
    107                 throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     118                $this->__unserialize( array() );
    108119        }
    109120}
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r60275 r60796  
    246246        }
    247247
    248         public function __wakeup() {
     248        /**
     249         * Unserialize magic method.
     250         *
     251         * @since 6.9.0
     252         *
     253         * @param array $data Data to unserialize.
     254         */
     255        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    249256                if ( ! $this->registered_patterns ) {
    250257                        return;
     
    259266                }
    260267                $this->registered_patterns_outside_init = array();
     268        }
     269
     270        /**
     271         * Wakeup magic method.
     272         *
     273         * @since 6.4.0
     274         */
     275        public function __wakeup() {
     276                $this->__unserialize( array() );
    261277        }
    262278
  • trunk/src/wp-includes/class-wp-block-type-registry.php

    r56835 r60796  
    169169        }
    170170
    171         public function __wakeup() {
     171        /**
     172         * Unserialize magic method.
     173         *
     174         * @since 6.9.0
     175         *
     176         * @param array $data Data to unserialize.
     177         */
     178        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    172179                if ( ! $this->registered_block_types ) {
    173180                        return;
     
    184191
    185192        /**
     193         * Wakeup magic method.
     194         *
     195         * @since 6.4.0
     196         */
     197        public function __wakeup() {
     198                $this->__unserialize( array() );
     199        }
     200
     201        /**
    186202         * Utility method to retrieve the main instance of the class.
    187203         *
  • trunk/src/wp-includes/class-wp-theme.php

    r60142 r60796  
    784784         *
    785785         * Prevents a callback from being injected during unserialization of an object.
    786          */
    787         public function __wakeup() {
     786         *
     787         * @since 6.9.0
     788         *
     789         * @param array $data Data to unserialize.
     790         */
     791        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    788792                if ( $this->parent && ! $this->parent instanceof self ) {
    789793                        throw new UnexpectedValueException();
     
    798802                }
    799803                $this->headers_sanitized = array();
     804        }
     805
     806        /**
     807         * Wakeup magic method.
     808         *
     809         * @since 6.4.0
     810         */
     811        public function __wakeup() {
     812                $this->__unserialize( array() );
    800813        }
    801814
  • trunk/src/wp-includes/html-api/class-wp-html-open-elements.php

    r59391 r60796  
    843843
    844844        /**
     845         * Unserialize magic method.
     846         *
     847         * @since 6.9.0
     848         *
     849         * @param array $data Data to unserialize.
     850         */
     851        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
     852                throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     853        }
     854
     855        /**
    845856         * Wakeup magic method.
    846857         *
     
    848859         */
    849860        public function __wakeup() {
    850                 throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     861                $this->__unserialize( array() );
    851862        }
    852863}
  • trunk/src/wp-includes/html-api/class-wp-html-token.php

    r58867 r60796  
    117117
    118118        /**
     119         * Unserialize magic method.
     120         *
     121         * @since 6.9.0
     122         *
     123         * @param array $data Data to unserialize.
     124         */
     125        public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
     126                throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     127        }
     128
     129        /**
    119130         * Wakeup magic method.
    120131         *
     
    122133         */
    123134        public function __wakeup() {
    124                 throw new \LogicException( __CLASS__ . ' should never be unserialized' );
     135                $this->__unserialize( array() );
    125136        }
    126137}
Note: See TracChangeset for help on using the changeset viewer.