wp/wp-includes/class-wp-ajax-response.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     9 	/**
     9 	/**
    10 	 * Store XML responses to send.
    10 	 * Store XML responses to send.
    11 	 *
    11 	 *
    12 	 * @since 2.1.0
    12 	 * @since 2.1.0
    13 	 * @var array
    13 	 * @var array
    14 	 * @access private
       
    15 	 */
    14 	 */
    16 	var $responses = array();
    15 	public $responses = array();
    17 
    16 
    18 	/**
    17 	/**
    19 	 * Constructor - Passes args to {@link WP_Ajax_Response::add()}.
    18 	 * Constructor - Passes args to {@link WP_Ajax_Response::add()}.
    20 	 *
    19 	 *
    21 	 * @since 2.1.0
    20 	 * @since 2.1.0
    22 	 * @see WP_Ajax_Response::add()
    21 	 * @see WP_Ajax_Response::add()
    23 	 *
    22 	 *
    24 	 * @param string|array $args Optional. Will be passed to add() method.
    23 	 * @param string|array $args Optional. Will be passed to add() method.
    25 	 * @return WP_Ajax_Response
       
    26 	 */
    24 	 */
    27 	function __construct( $args = '' ) {
    25 	public function __construct( $args = '' ) {
    28 		if ( !empty($args) )
    26 		if ( !empty($args) )
    29 			$this->add($args);
    27 			$this->add($args);
    30 	}
    28 	}
    31 
    29 
    32 	/**
    30 	/**
    50 	 * @since 2.1.0
    48 	 * @since 2.1.0
    51 	 *
    49 	 *
    52 	 * @param string|array $args Override defaults.
    50 	 * @param string|array $args Override defaults.
    53 	 * @return string XML response.
    51 	 * @return string XML response.
    54 	 */
    52 	 */
    55 	function add( $args = '' ) {
    53 	public function add( $args = '' ) {
    56 		$defaults = array(
    54 		$defaults = array(
    57 			'what' => 'object', 'action' => false,
    55 			'what' => 'object', 'action' => false,
    58 			'id' => '0', 'old_id' => false,
    56 			'id' => '0', 'old_id' => false,
    59 			'position' => 1,
    57 			'position' => 1,
    60 			'data' => '', 'supplemental' => array()
    58 			'data' => '', 'supplemental' => array()
    61 		);
    59 		);
    62 
    60 
    63 		$r = wp_parse_args( $args, $defaults );
    61 		$r = wp_parse_args( $args, $defaults );
    64 		extract( $r, EXTR_SKIP );
       
    65 		$position = preg_replace( '/[^a-z0-9:_-]/i', '', $position );
       
    66 
    62 
    67 		if ( is_wp_error($id) ) {
    63 		$position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] );
       
    64 		$id = $r['id'];
       
    65 		$what = $r['what'];
       
    66 		$action = $r['action'];
       
    67 		$old_id = $r['old_id'];
       
    68 		$data = $r['data'];
       
    69 
       
    70 		if ( is_wp_error( $id ) ) {
    68 			$data = $id;
    71 			$data = $id;
    69 			$id = 0;
    72 			$id = 0;
    70 		}
    73 		}
    71 
    74 
    72 		$response = '';
    75 		$response = '';
    73 		if ( is_wp_error($data) ) {
    76 		if ( is_wp_error( $data ) ) {
    74 			foreach ( (array) $data->get_error_codes() as $code ) {
    77 			foreach ( (array) $data->get_error_codes() as $code ) {
    75 				$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
    78 				$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . "]]></wp_error>";
    76 				if ( !$error_data = $data->get_error_data($code) )
    79 				if ( ! $error_data = $data->get_error_data( $code ) ) {
    77 					continue;
    80 					continue;
       
    81 				}
    78 				$class = '';
    82 				$class = '';
    79 				if ( is_object($error_data) ) {
    83 				if ( is_object( $error_data ) ) {
    80 					$class = ' class="' . get_class($error_data) . '"';
    84 					$class = ' class="' . get_class( $error_data ) . '"';
    81 					$error_data = get_object_vars($error_data);
    85 					$error_data = get_object_vars( $error_data );
    82 				}
    86 				}
    83 
    87 
    84 				$response .= "<wp_error_data code='$code'$class>";
    88 				$response .= "<wp_error_data code='$code'$class>";
    85 
    89 
    86 				if ( is_scalar($error_data) ) {
    90 				if ( is_scalar( $error_data ) ) {
    87 					$response .= "<![CDATA[$error_data]]>";
    91 					$response .= "<![CDATA[$error_data]]>";
    88 				} elseif ( is_array($error_data) ) {
    92 				} elseif ( is_array( $error_data ) ) {
    89 					foreach ( $error_data as $k => $v )
    93 					foreach ( $error_data as $k => $v ) {
    90 						$response .= "<$k><![CDATA[$v]]></$k>";
    94 						$response .= "<$k><![CDATA[$v]]></$k>";
       
    95 					}
    91 				}
    96 				}
    92 
    97 
    93 				$response .= "</wp_error_data>";
    98 				$response .= "</wp_error_data>";
    94 			}
    99 			}
    95 		} else {
   100 		} else {
    96 			$response = "<response_data><![CDATA[$data]]></response_data>";
   101 			$response = "<response_data><![CDATA[$data]]></response_data>";
    97 		}
   102 		}
    98 
   103 
    99 		$s = '';
   104 		$s = '';
   100 		if ( is_array($supplemental) ) {
   105 		if ( is_array( $r['supplemental'] ) ) {
   101 			foreach ( $supplemental as $k => $v )
   106 			foreach ( $r['supplemental'] as $k => $v ) {
   102 				$s .= "<$k><![CDATA[$v]]></$k>";
   107 				$s .= "<$k><![CDATA[$v]]></$k>";
       
   108 			}
   103 			$s = "<supplemental>$s</supplemental>";
   109 			$s = "<supplemental>$s</supplemental>";
   104 		}
   110 		}
   105 
   111 
   106 		if ( false === $action )
   112 		if ( false === $action ) {
   107 			$action = $_POST['action'];
   113 			$action = $_POST['action'];
   108 
   114 		}
   109 		$x = '';
   115 		$x = '';
   110 		$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
   116 		$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
   111 		$x .=	"<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
   117 		$x .=	"<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
   112 		$x .=		$response;
   118 		$x .=		$response;
   113 		$x .=		$s;
   119 		$x .=		$s;
   123 	 *
   129 	 *
   124 	 * Sets the content type header to text/xml.
   130 	 * Sets the content type header to text/xml.
   125 	 *
   131 	 *
   126 	 * @since 2.1.0
   132 	 * @since 2.1.0
   127 	 */
   133 	 */
   128 	function send() {
   134 	public function send() {
   129 		header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
   135 		header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
   130 		echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
   136 		echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
   131 		foreach ( (array) $this->responses as $response )
   137 		foreach ( (array) $this->responses as $response )
   132 			echo $response;
   138 			echo $response;
   133 		echo '</wp_ajax>';
   139 		echo '</wp_ajax>';