mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-29 12:37:57 +01:00
c03305852e
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0. There are a number of ways to mitigate this: * If it is an accidental typo for a declared property: fix the typo. * For known properties: declare them on the class. * For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in. * For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes. Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1. To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP. The logic used for this commit is as follows: * If a class already has the attribute: no action needed. * If a class does not `extend`: add the attribute. * If a class does `extend`: - If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties). - If it extends a PHP native class: add the attribute. - If it extends a class from one of WP's external dependencies: add the attribute. * In all other cases: no action — the attribute should not be needed as child classes inherit from the parent. Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details. This commit only affects classes in the `src` directory of WordPress core. * Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033. * While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately. Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties]. Follow-up to [53922]. Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath. See #56513, #56034. Built from https://develop.svn.wordpress.org/trunk@54133 git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
166 lines
5.1 KiB
PHP
166 lines
5.1 KiB
PHP
<?php
|
|
/**
|
|
* Send XML response back to Ajax request.
|
|
*
|
|
* @package WordPress
|
|
* @since 2.1.0
|
|
*/
|
|
#[AllowDynamicProperties]
|
|
class WP_Ajax_Response {
|
|
/**
|
|
* Store XML responses to send.
|
|
*
|
|
* @since 2.1.0
|
|
* @var array
|
|
*/
|
|
public $responses = array();
|
|
|
|
/**
|
|
* Constructor - Passes args to WP_Ajax_Response::add().
|
|
*
|
|
* @since 2.1.0
|
|
*
|
|
* @see WP_Ajax_Response::add()
|
|
*
|
|
* @param string|array $args Optional. Will be passed to add() method.
|
|
*/
|
|
public function __construct( $args = '' ) {
|
|
if ( ! empty( $args ) ) {
|
|
$this->add( $args );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Appends data to an XML response based on given arguments.
|
|
*
|
|
* With `$args` defaults, extra data output would be:
|
|
*
|
|
* <response action='{$action}_$id'>
|
|
* <$what id='$id' position='$position'>
|
|
* <response_data><![CDATA[$data]]></response_data>
|
|
* </$what>
|
|
* </response>
|
|
*
|
|
* @since 2.1.0
|
|
*
|
|
* @param string|array $args {
|
|
* Optional. An array or string of XML response arguments.
|
|
*
|
|
* @type string $what XML-RPC response type. Used as a child element of `<response>`.
|
|
* Default 'object' (`<object>`).
|
|
* @type string|false $action Value to use for the `action` attribute in `<response>`. Will be
|
|
* appended with `_$id` on output. If false, `$action` will default to
|
|
* the value of `$_POST['action']`. Default false.
|
|
* @type int|WP_Error $id The response ID, used as the response type `id` attribute. Also
|
|
* accepts a `WP_Error` object if the ID does not exist. Default 0.
|
|
* @type int|false $old_id The previous response ID. Used as the value for the response type
|
|
* `old_id` attribute. False hides the attribute. Default false.
|
|
* @type string $position Value of the response type `position` attribute. Accepts 1 (bottom),
|
|
* -1 (top), HTML ID (after), or -HTML ID (before). Default 1 (bottom).
|
|
* @type string|WP_Error $data The response content/message. Also accepts a WP_Error object if the
|
|
* ID does not exist. Default empty.
|
|
* @type array $supplemental An array of extra strings that will be output within a `<supplemental>`
|
|
* element as CDATA. Default empty array.
|
|
* }
|
|
* @return string XML response.
|
|
*/
|
|
public function add( $args = '' ) {
|
|
$defaults = array(
|
|
'what' => 'object',
|
|
'action' => false,
|
|
'id' => '0',
|
|
'old_id' => false,
|
|
'position' => 1,
|
|
'data' => '',
|
|
'supplemental' => array(),
|
|
);
|
|
|
|
$parsed_args = wp_parse_args( $args, $defaults );
|
|
|
|
$position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] );
|
|
$id = $parsed_args['id'];
|
|
$what = $parsed_args['what'];
|
|
$action = $parsed_args['action'];
|
|
$old_id = $parsed_args['old_id'];
|
|
$data = $parsed_args['data'];
|
|
|
|
if ( is_wp_error( $id ) ) {
|
|
$data = $id;
|
|
$id = 0;
|
|
}
|
|
|
|
$response = '';
|
|
if ( is_wp_error( $data ) ) {
|
|
foreach ( (array) $data->get_error_codes() as $code ) {
|
|
$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>';
|
|
$error_data = $data->get_error_data( $code );
|
|
if ( ! $error_data ) {
|
|
continue;
|
|
}
|
|
$class = '';
|
|
if ( is_object( $error_data ) ) {
|
|
$class = ' class="' . get_class( $error_data ) . '"';
|
|
$error_data = get_object_vars( $error_data );
|
|
}
|
|
|
|
$response .= "<wp_error_data code='$code'$class>";
|
|
|
|
if ( is_scalar( $error_data ) ) {
|
|
$response .= "<![CDATA[$error_data]]>";
|
|
} elseif ( is_array( $error_data ) ) {
|
|
foreach ( $error_data as $k => $v ) {
|
|
$response .= "<$k><![CDATA[$v]]></$k>";
|
|
}
|
|
}
|
|
|
|
$response .= '</wp_error_data>';
|
|
}
|
|
} else {
|
|
$response = "<response_data><![CDATA[$data]]></response_data>";
|
|
}
|
|
|
|
$s = '';
|
|
if ( is_array( $parsed_args['supplemental'] ) ) {
|
|
foreach ( $parsed_args['supplemental'] as $k => $v ) {
|
|
$s .= "<$k><![CDATA[$v]]></$k>";
|
|
}
|
|
$s = "<supplemental>$s</supplemental>";
|
|
}
|
|
|
|
if ( false === $action ) {
|
|
$action = $_POST['action'];
|
|
}
|
|
$x = '';
|
|
$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action.
|
|
$x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
|
|
$x .= $response;
|
|
$x .= $s;
|
|
$x .= "</$what>";
|
|
$x .= '</response>';
|
|
|
|
$this->responses[] = $x;
|
|
return $x;
|
|
}
|
|
|
|
/**
|
|
* Display XML formatted responses.
|
|
*
|
|
* Sets the content type header to text/xml.
|
|
*
|
|
* @since 2.1.0
|
|
*/
|
|
public function send() {
|
|
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
|
|
echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
|
|
foreach ( (array) $this->responses as $response ) {
|
|
echo $response;
|
|
}
|
|
echo '</wp_ajax>';
|
|
if ( wp_doing_ajax() ) {
|
|
wp_die();
|
|
} else {
|
|
die();
|
|
}
|
|
}
|
|
}
|