[ 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|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 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 public function add( $args = '' ) { 68 $defaults = array( 69 'what' => 'object', 70 'action' => false, 71 'id' => '0', 72 'old_id' => false, 73 'position' => 1, 74 'data' => '', 75 'supplemental' => array(), 76 ); 77 78 $parsed_args = wp_parse_args( $args, $defaults ); 79 80 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] ); 81 $id = $parsed_args['id']; 82 $what = $parsed_args['what']; 83 $action = $parsed_args['action']; 84 $old_id = $parsed_args['old_id']; 85 $data = $parsed_args['data']; 86 87 if ( is_wp_error( $id ) ) { 88 $data = $id; 89 $id = 0; 90 } 91 92 $response = ''; 93 if ( is_wp_error( $data ) ) { 94 foreach ( (array) $data->get_error_codes() as $code ) { 95 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>'; 96 $error_data = $data->get_error_data( $code ); 97 if ( ! $error_data ) { 98 continue; 99 } 100 $class = ''; 101 if ( is_object( $error_data ) ) { 102 $class = ' class="' . get_class( $error_data ) . '"'; 103 $error_data = get_object_vars( $error_data ); 104 } 105 106 $response .= "<wp_error_data code='$code'$class>"; 107 108 if ( is_scalar( $error_data ) ) { 109 $response .= "<![CDATA[$error_data]]>"; 110 } elseif ( is_array( $error_data ) ) { 111 foreach ( $error_data as $k => $v ) { 112 $response .= "<$k><![CDATA[$v]]></$k>"; 113 } 114 } 115 116 $response .= '</wp_error_data>'; 117 } 118 } else { 119 $response = "<response_data><![CDATA[$data]]></response_data>"; 120 } 121 122 $s = ''; 123 if ( is_array( $parsed_args['supplemental'] ) ) { 124 foreach ( $parsed_args['supplemental'] as $k => $v ) { 125 $s .= "<$k><![CDATA[$v]]></$k>"; 126 } 127 $s = "<supplemental>$s</supplemental>"; 128 } 129 130 if ( false === $action ) { 131 $action = $_POST['action']; 132 } 133 $x = ''; 134 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action. 135 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; 136 $x .= $response; 137 $x .= $s; 138 $x .= "</$what>"; 139 $x .= '</response>'; 140 141 $this->responses[] = $x; 142 return $x; 143 } 144 145 /** 146 * Display XML formatted responses. 147 * 148 * Sets the content type header to text/xml. 149 * 150 * @since 2.1.0 151 */ 152 public function send() { 153 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); 154 echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>"; 155 foreach ( (array) $this->responses as $response ) { 156 echo $response; 157 } 158 echo '</wp_ajax>'; 159 if ( wp_doing_ajax() ) { 160 wp_die(); 161 } else { 162 die(); 163 } 164 } 165 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |