Skip to content

Commit 2e3248f

Browse files
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: [https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods 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. git-svn-id: https://develop.svn.wordpress.org/trunk@60796 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 38be7a2 commit 2e3248f

7 files changed

+98
-9
lines changed

src/wp-includes/class-wp-block-bindings-registry.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,13 @@ public function is_registered( $source_name ) {
257257
}
258258

259259
/**
260-
* Wakeup magic method.
260+
* Unserialize magic method.
261261
*
262-
* @since 6.5.0
262+
* @since 6.9.0
263+
*
264+
* @param array $data Data to unserialize.
263265
*/
264-
public function __wakeup() {
266+
public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
265267
if ( ! $this->sources ) {
266268
return;
267269
}
@@ -275,6 +277,15 @@ public function __wakeup() {
275277
}
276278
}
277279

280+
/**
281+
* Wakeup magic method.
282+
*
283+
* @since 6.5.0
284+
*/
285+
public function __wakeup() {
286+
$this->__unserialize( array() );
287+
}
288+
278289
/**
279290
* Utility method to retrieve the main instance of the class.
280291
*

src/wp-includes/class-wp-block-bindings-source.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,23 @@ public function get_value( array $source_args, $block_instance, string $attribut
9898
return apply_filters( 'block_bindings_source_value', $value, $this->name, $source_args, $block_instance, $attribute_name );
9999
}
100100

101+
/**
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+
101112
/**
102113
* Wakeup magic method.
103114
*
104115
* @since 6.5.0
105116
*/
106117
public function __wakeup() {
107-
throw new \LogicException( __CLASS__ . ' should never be unserialized' );
118+
$this->__unserialize( array() );
108119
}
109120
}

src/wp-includes/class-wp-block-patterns-registry.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,14 @@ public function is_registered( $pattern_name ) {
245245
return isset( $this->registered_patterns[ $pattern_name ] );
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;
251258
}
@@ -260,6 +267,15 @@ public function __wakeup() {
260267
$this->registered_patterns_outside_init = array();
261268
}
262269

270+
/**
271+
* Wakeup magic method.
272+
*
273+
* @since 6.4.0
274+
*/
275+
public function __wakeup() {
276+
$this->__unserialize( array() );
277+
}
278+
263279
/**
264280
* Utility method to retrieve the main instance of the class.
265281
*

src/wp-includes/class-wp-block-type-registry.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ public function is_registered( $name ) {
168168
return isset( $this->registered_block_types[ $name ] );
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;
174181
}
@@ -182,6 +189,15 @@ public function __wakeup() {
182189
}
183190
}
184191

192+
/**
193+
* Wakeup magic method.
194+
*
195+
* @since 6.4.0
196+
*/
197+
public function __wakeup() {
198+
$this->__unserialize( array() );
199+
}
200+
185201
/**
186202
* Utility method to retrieve the main instance of the class.
187203
*

src/wp-includes/class-wp-theme.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,12 @@ public function parent() {
783783
* Perform reinitialization tasks.
784784
*
785785
* Prevents a callback from being injected during unserialization of an object.
786+
*
787+
* @since 6.9.0
788+
*
789+
* @param array $data Data to unserialize.
786790
*/
787-
public function __wakeup() {
791+
public function __unserialize( $data ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
788792
if ( $this->parent && ! $this->parent instanceof self ) {
789793
throw new UnexpectedValueException();
790794
}
@@ -799,6 +803,15 @@ public function __wakeup() {
799803
$this->headers_sanitized = array();
800804
}
801805

806+
/**
807+
* Wakeup magic method.
808+
*
809+
* @since 6.4.0
810+
*/
811+
public function __wakeup() {
812+
$this->__unserialize( array() );
813+
}
814+
802815
/**
803816
* Adds theme data to cache.
804817
*

src/wp-includes/html-api/class-wp-html-open-elements.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,23 @@ public function clear_to_table_row_context(): void {
841841
}
842842
}
843843

844+
/**
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+
844855
/**
845856
* Wakeup magic method.
846857
*
847858
* @since 6.6.0
848859
*/
849860
public function __wakeup() {
850-
throw new \LogicException( __CLASS__ . ' should never be unserialized' );
861+
$this->__unserialize( array() );
851862
}
852863
}

src/wp-includes/html-api/class-wp-html-token.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,23 @@ public function __destruct() {
115115
}
116116
}
117117

118+
/**
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+
118129
/**
119130
* Wakeup magic method.
120131
*
121132
* @since 6.4.2
122133
*/
123134
public function __wakeup() {
124-
throw new \LogicException( __CLASS__ . ' should never be unserialized' );
135+
$this->__unserialize( array() );
125136
}
126137
}

0 commit comments

Comments
 (0)