wp/wp-includes/class-wp-ajax-response.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 21 48c4eec2b7e6
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    16 
    16 
    17 	/**
    17 	/**
    18 	 * Constructor - Passes args to 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 	 *
    21 	 * @see WP_Ajax_Response::add()
    22 	 * @see WP_Ajax_Response::add()
    22 	 *
    23 	 *
    23 	 * @param string|array $args Optional. Will be passed to add() method.
    24 	 * @param string|array $args Optional. Will be passed to add() method.
    24 	 */
    25 	 */
    25 	public function __construct( $args = '' ) {
    26 	public function __construct( $args = '' ) {
    52 	 *     @type int|WP_Error    $id           The response ID, used as the response type `id` attribute. Also
    53 	 *     @type int|WP_Error    $id           The response ID, used as the response type `id` attribute. Also
    53 	 *                                         accepts a `WP_Error` object if the ID does not exist. Default 0.
    54 	 *                                         accepts a `WP_Error` object if the ID does not exist. Default 0.
    54 	 *     @type int|false       $old_id       The previous response ID. Used as the value for the response type
    55 	 *     @type int|false       $old_id       The previous response ID. Used as the value for the response type
    55 	 *                                         `old_id` attribute. False hides the attribute. Default false.
    56 	 *                                         `old_id` attribute. False hides the attribute. Default false.
    56 	 *     @type string          $position     Value of the response type `position` attribute. Accepts 1 (bottom),
    57 	 *     @type string          $position     Value of the response type `position` attribute. Accepts 1 (bottom),
    57 	 *                                         -1 (top), html ID (after), or -html ID (before). Default 1 (bottom).
    58 	 *                                         -1 (top), HTML ID (after), or -HTML ID (before). Default 1 (bottom).
    58 	 *     @type string|WP_Error $data         The response content/message. Also accepts a WP_Error object if the
    59 	 *     @type string|WP_Error $data         The response content/message. Also accepts a WP_Error object if the
    59 	 *                                         ID does not exist. Default empty.
    60 	 *                                         ID does not exist. Default empty.
    60 	 *     @type array           $supplemental An array of extra strings that will be output within a `<supplemental>`
    61 	 *     @type array           $supplemental An array of extra strings that will be output within a `<supplemental>`
    61 	 *                                         element as CDATA. Default empty array.
    62 	 *                                         element as CDATA. Default empty array.
    62 	 * }
    63 	 * }
    71 			'position'     => 1,
    72 			'position'     => 1,
    72 			'data'         => '',
    73 			'data'         => '',
    73 			'supplemental' => array(),
    74 			'supplemental' => array(),
    74 		);
    75 		);
    75 
    76 
    76 		$r = wp_parse_args( $args, $defaults );
    77 		$parsed_args = wp_parse_args( $args, $defaults );
    77 
    78 
    78 		$position = preg_replace( '/[^a-z0-9:_-]/i', '', $r['position'] );
    79 		$position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] );
    79 		$id       = $r['id'];
    80 		$id       = $parsed_args['id'];
    80 		$what     = $r['what'];
    81 		$what     = $parsed_args['what'];
    81 		$action   = $r['action'];
    82 		$action   = $parsed_args['action'];
    82 		$old_id   = $r['old_id'];
    83 		$old_id   = $parsed_args['old_id'];
    83 		$data     = $r['data'];
    84 		$data     = $parsed_args['data'];
    84 
    85 
    85 		if ( is_wp_error( $id ) ) {
    86 		if ( is_wp_error( $id ) ) {
    86 			$data = $id;
    87 			$data = $id;
    87 			$id   = 0;
    88 			$id   = 0;
    88 		}
    89 		}
    89 
    90 
    90 		$response = '';
    91 		$response = '';
    91 		if ( is_wp_error( $data ) ) {
    92 		if ( is_wp_error( $data ) ) {
    92 			foreach ( (array) $data->get_error_codes() as $code ) {
    93 			foreach ( (array) $data->get_error_codes() as $code ) {
    93 				$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>';
    94 				$response  .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>';
    94 				if ( ! $error_data = $data->get_error_data( $code ) ) {
    95 				$error_data = $data->get_error_data( $code );
       
    96 				if ( ! $error_data ) {
    95 					continue;
    97 					continue;
    96 				}
    98 				}
    97 				$class = '';
    99 				$class = '';
    98 				if ( is_object( $error_data ) ) {
   100 				if ( is_object( $error_data ) ) {
    99 					$class      = ' class="' . get_class( $error_data ) . '"';
   101 					$class      = ' class="' . get_class( $error_data ) . '"';
   115 		} else {
   117 		} else {
   116 			$response = "<response_data><![CDATA[$data]]></response_data>";
   118 			$response = "<response_data><![CDATA[$data]]></response_data>";
   117 		}
   119 		}
   118 
   120 
   119 		$s = '';
   121 		$s = '';
   120 		if ( is_array( $r['supplemental'] ) ) {
   122 		if ( is_array( $parsed_args['supplemental'] ) ) {
   121 			foreach ( $r['supplemental'] as $k => $v ) {
   123 			foreach ( $parsed_args['supplemental'] as $k => $v ) {
   122 				$s .= "<$k><![CDATA[$v]]></$k>";
   124 				$s .= "<$k><![CDATA[$v]]></$k>";
   123 			}
   125 			}
   124 			$s = "<supplemental>$s</supplemental>";
   126 			$s = "<supplemental>$s</supplemental>";
   125 		}
   127 		}
   126 
   128 
   127 		if ( false === $action ) {
   129 		if ( false === $action ) {
   128 			$action = $_POST['action'];
   130 			$action = $_POST['action'];
   129 		}
   131 		}
   130 		$x  = '';
   132 		$x  = '';
   131 		$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
   133 		$x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action.
   132 		$x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
   134 		$x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
   133 		$x .= $response;
   135 		$x .= $response;
   134 		$x .= $s;
   136 		$x .= $s;
   135 		$x .= "</$what>";
   137 		$x .= "</$what>";
   136 		$x .= '</response>';
   138 		$x .= '</response>';