| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Send XML response back to Ajax request. 4 * 5 * @package WordPress 6 * @since 2.1.0 7 */ 8 #[AllowDynamicProperties] 9 class WP_Ajax_Response { 10 /** 11 * Store XML responses to send. 12 * 13 * @since 2.1.0 14 * @var array 15 */ 16 public $responses = array(); 17 18 /** 19 * Constructor - Passes args to WP_Ajax_Response::add(). 20 * 21 * @since 2.1.0 22 * 23 * @see WP_Ajax_Response::add() 24 * 25 * @param string|array $args Optional. Will be passed to add() method. 26 */ 27 public function __construct( $args = '' ) { 28 if ( ! empty( $args ) ) { 29 $this->add( $args ); 30 } 31 } 32 33 /** 34 * Appends data to an XML response based on given arguments. 35 * 36 * With `$args` defaults, extra data output would be: 37 * 38 * <response action='{$action}_$id'> 39 * <$what id='$id' position='$position'> 40 * <response_data><![CDATA[$data]]></response_data> 41 * </$what> 42 * </response> 43 * 44 * @since 2.1.0 45 * 46 * @param string|array $args { 47 * Optional. An array or string of XML response arguments. 48 * 49 * @type string $what XML-RPC response type. Used as a child element of `<response>`. 50 * Default 'object' (`<object>`). 51 * @type string|false $action Value to use for the `action` attribute in `<response>`. Will be 52 * appended with `_$id` on output. If false, `$action` will default to 53 * the value of `$_POST['action']`. Default false. 54 * @type int|string|WP_Error $id The response ID, used as the response type `id` attribute. Also 55 * accepts a `WP_Error` object if the ID does not exist. Default 0. 56 * @type int|false $old_id The previous response ID. Used as the value for the response type 57 * `old_id` attribute. False hides the attribute. Default false. 58 * @type int|string $position Value of the response type `position` attribute. Accepts 1 (bottom), 59 * -1 (top), HTML ID (after), or -HTML ID (before). Default 1 (bottom). 60 * @type string|WP_Error $data The response content/message. Also accepts a WP_Error object if the 61 * ID does not exist. Default empty. 62 * @type array $supplemental An array of extra strings that will be output within a `<supplemental>` 63 * element as CDATA. Default empty array. 64 * } 65 * @return string XML response. 66 * 67 * @phpstan-return non-falsy-string 68 */ 69 public function add( $args = '' ) { 70 $defaults = array( 71 'what' => 'object', 72 'action' => false, 73 'id' => '0', 74 'old_id' => false, 75 'position' => 1, 76 'data' => '', 77 'supplemental' => array(), 78 ); 79 80 $parsed_args = wp_parse_args( $args, $defaults ); 81 82 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] ); 83 $id = $parsed_args['id']; 84 $what = $parsed_args['what']; 85 $action = $parsed_args['action']; 86 $old_id = $parsed_args['old_id']; 87 $data = $parsed_args['data']; 88 89 if ( is_wp_error( $id ) ) { 90 $data = $id; 91 $id = 0; 92 } 93 94 $response = ''; 95 if ( is_wp_error( $data ) ) { 96 foreach ( (array) $data->get_error_codes() as $code ) { 97 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>'; 98 $error_data = $data->get_error_data( $code ); 99 if ( ! $error_data ) { 100 continue; 101 } 102 $class = ''; 103 if ( is_object( $error_data ) ) { 104 $class = ' class="' . get_class( $error_data ) . '"'; 105 $error_data = get_object_vars( $error_data ); 106 } 107 108 $response .= "<wp_error_data code='$code'$class>"; 109 110 if ( is_scalar( $error_data ) ) { 111 $response .= "<![CDATA[$error_data]]>"; 112 } elseif ( is_array( $error_data ) ) { 113 foreach ( $error_data as $k => $v ) { 114 $response .= "<$k><![CDATA[$v]]></$k>"; 115 } 116 } 117 118 $response .= '</wp_error_data>'; 119 } 120 } else { 121 $response = "<response_data><![CDATA[$data]]></response_data>"; 122 } 123 124 $s = ''; 125 if ( is_array( $parsed_args['supplemental'] ) ) { 126 foreach ( $parsed_args['supplemental'] as $k => $v ) { 127 $s .= "<$k><![CDATA[$v]]></$k>"; 128 } 129 $s = "<supplemental>$s</supplemental>"; 130 } 131 132 if ( false === $action ) { 133 $action = $_POST['action']; 134 } 135 $x = ''; 136 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action. 137 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; 138 $x .= $response; 139 $x .= $s; 140 $x .= "</$what>"; 141 $x .= '</response>'; 142 143 $this->responses[] = $x; 144 return $x; 145 } 146 147 /** 148 * Display XML formatted responses. 149 * 150 * Sets the content type header to text/xml. 151 * 152 * @since 2.1.0 153 * 154 * @return never 155 */ 156 public function send() { 157 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); 158 echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>"; 159 foreach ( (array) $this->responses as $response ) { 160 echo $response; 161 } 162 echo '</wp_ajax>'; 163 if ( wp_doing_ajax() ) { 164 wp_die(); 165 } else { 166 die(); 167 } 168 } 169 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Oct 22 08:20:34 2026 | Cross-referenced by PHPXref |