wp/wp-includes/class-wp-ajax-response.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Send XML response back to AJAX request.
     3  * Send XML response back to Ajax request.
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @since 2.1.0
     6  * @since 2.1.0
     7  */
     7  */
     8 class WP_Ajax_Response {
     8 class WP_Ajax_Response {
    13 	 * @var array
    13 	 * @var array
    14 	 */
    14 	 */
    15 	public $responses = array();
    15 	public $responses = array();
    16 
    16 
    17 	/**
    17 	/**
    18 	 * Constructor - Passes args to {@link WP_Ajax_Response::add()}.
    18 	 * Constructor - Passes args to WP_Ajax_Response::add().
    19 	 *
    19 	 *
    20 	 * @since 2.1.0
    20 	 * @since 2.1.0
    21 	 * @see WP_Ajax_Response::add()
    21 	 * @see WP_Ajax_Response::add()
    22 	 *
    22 	 *
    23 	 * @param string|array $args Optional. Will be passed to add() method.
    23 	 * @param string|array $args Optional. Will be passed to add() method.
    26 		if ( !empty($args) )
    26 		if ( !empty($args) )
    27 			$this->add($args);
    27 			$this->add($args);
    28 	}
    28 	}
    29 
    29 
    30 	/**
    30 	/**
    31 	 * Append to XML response based on given arguments.
    31 	 * Appends data to an XML response based on given arguments.
    32 	 *
    32 	 *
    33 	 * The arguments that can be passed in the $args parameter are below. It is
    33 	 * With `$args` defaults, extra data output would be:
    34 	 * also possible to pass a WP_Error object in either the 'id' or 'data'
       
    35 	 * argument. The parameter isn't actually optional, content should be given
       
    36 	 * in order to send the correct response.
       
    37 	 *
    34 	 *
    38 	 * 'what' argument is a string that is the XMLRPC response type.
    35 	 *     <response action='{$action}_$id'>
    39 	 * 'action' argument is a boolean or string that acts like a nonce.
    36 	 *      <$what id='$id' position='$position'>
    40 	 * 'id' argument can be WP_Error or an integer.
    37 	 *          <response_data><![CDATA[$data]]></response_data>
    41 	 * 'old_id' argument is false by default or an integer of the previous ID.
    38 	 *      </$what>
    42 	 * 'position' argument is an integer or a string with -1 = top, 1 = bottom,
    39 	 *     </response>
    43 	 * html ID = after, -html ID = before.
       
    44 	 * 'data' argument is a string with the content or message.
       
    45 	 * 'supplemental' argument is an array of strings that will be children of
       
    46 	 * the supplemental element.
       
    47 	 *
    40 	 *
    48 	 * @since 2.1.0
    41 	 * @since 2.1.0
    49 	 *
    42 	 *
    50 	 * @param string|array $args Override defaults.
    43 	 * @param string|array $args {
       
    44 	 *     Optional. An array or string of XML response arguments.
       
    45 	 *
       
    46 	 *     @type string          $what         XML-RPC response type. Used as a child element of `<response>`.
       
    47 	 *                                         Default 'object' (`<object>`).
       
    48 	 *     @type string|false    $action       Value to use for the `action` attribute in `<response>`. Will be
       
    49 	 *                                         appended with `_$id` on output. If false, `$action` will default to
       
    50 	 *                                         the value of `$_POST['action']`. Default false.
       
    51 	 *     @type int|WP_Error    $id           The response ID, used as the response type `id` attribute. Also
       
    52 	 *                                         accepts a `WP_Error` object if the ID does not exist. Default 0.
       
    53 	 *     @type int|false       $old_id       The previous response ID. Used as the value for the response type
       
    54 	 *                                         `old_id` attribute. False hides the attribute. Default false.
       
    55 	 *     @type string          $position     Value of the response type `position` attribute. Accepts 1 (bottom),
       
    56 	 *                                         -1 (top), html ID (after), or -html ID (before). Default 1 (bottom).
       
    57 	 *     @type string|WP_Error $data         The response content/message. Also accepts a WP_Error object if the
       
    58 	 *                                         ID does not exist. Default empty.
       
    59 	 *     @type array           $supplemental An array of extra strings that will be output within a `<supplemental>`
       
    60 	 *                                         element as CDATA. Default empty array.
       
    61 	 * }
    51 	 * @return string XML response.
    62 	 * @return string XML response.
    52 	 */
    63 	 */
    53 	public function add( $args = '' ) {
    64 	public function add( $args = '' ) {
    54 		$defaults = array(
    65 		$defaults = array(
    55 			'what' => 'object', 'action' => false,
    66 			'what' => 'object', 'action' => false,
   135 		header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
   146 		header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
   136 		echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
   147 		echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
   137 		foreach ( (array) $this->responses as $response )
   148 		foreach ( (array) $this->responses as $response )
   138 			echo $response;
   149 			echo $response;
   139 		echo '</wp_ajax>';
   150 		echo '</wp_ajax>';
   140 		if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
   151 		if ( wp_doing_ajax() )
   141 			wp_die();
   152 			wp_die();
   142 		else
   153 		else
   143 			die();
   154 			die();
   144 	}
   155 	}
   145 }
   156 }