[ Index ] |
PHP Cross Reference of BackPress |
[Summary view] [Print] [Text view]
1 <?php 2 // Last sync [WP11537] - split out from wp-includes/classes.php 3 4 /** 5 * Send XML response back to AJAX request. 6 * 7 * @package WordPress 8 * @since 2.1.0 9 */ 10 class WP_Ajax_Response { 11 /** 12 * Store XML responses to send. 13 * 14 * @since 2.1.0 15 * @var array 16 * @access private 17 */ 18 var $responses = array(); 19 20 /** 21 * PHP4 Constructor - Passes args to {@link WP_Ajax_Response::add()}. 22 * 23 * @since 2.1.0 24 * @see WP_Ajax_Response::add() 25 * 26 * @param string|array $args Optional. Will be passed to add() method. 27 * @return WP_Ajax_Response 28 */ 29 function __construct( $args = '' ) { 30 if ( !empty($args) ) 31 $this->add($args); 32 } 33 34 function WP_Ajax_Response( $args = '' ) { 35 self::__construct( $args ); 36 } 37 38 /** 39 * Append to XML response based on given arguments. 40 * 41 * The arguments that can be passed in the $args parameter are below. It is 42 * also possible to pass a WP_Error object in either the 'id' or 'data' 43 * argument. The parameter isn't actually optional, content should be given 44 * in order to send the correct response. 45 * 46 * 'what' argument is a string that is the XMLRPC response type. 47 * 'action' argument is a boolean or string that acts like a nonce. 48 * 'id' argument can be WP_Error or an integer. 49 * 'old_id' argument is false by default or an integer of the previous ID. 50 * 'position' argument is an integer or a string with -1 = top, 1 = bottom, 51 * html ID = after, -html ID = before. 52 * 'data' argument is a string with the content or message. 53 * 'supplemental' argument is an array of strings that will be children of 54 * the supplemental element. 55 * 56 * @since 2.1.0 57 * 58 * @param string|array $args Override defaults. 59 * @return string XML response. 60 */ 61 function add( $args = '' ) { 62 $defaults = array( 63 'what' => 'object', 'action' => false, 64 'id' => '0', 'old_id' => false, 65 'position' => 1, 66 'data' => '', 'supplemental' => array() 67 ); 68 69 $r = wp_parse_args( $args, $defaults ); 70 extract( $r, EXTR_SKIP ); 71 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position ); 72 73 if ( is_wp_error($id) ) { 74 $data = $id; 75 $id = 0; 76 } 77 78 $response = ''; 79 if ( is_wp_error($data) ) { 80 foreach ( (array) $data->get_error_codes() as $code ) { 81 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>"; 82 if ( !$error_data = $data->get_error_data($code) ) 83 continue; 84 $class = ''; 85 if ( is_object($error_data) ) { 86 $class = ' class="' . get_class($error_data) . '"'; 87 $error_data = get_object_vars($error_data); 88 } 89 90 $response .= "<wp_error_data code='$code'$class>"; 91 92 if ( is_scalar($error_data) ) { 93 $response .= "<![CDATA[$error_data]]>"; 94 } elseif ( is_array($error_data) ) { 95 foreach ( $error_data as $k => $v ) 96 $response .= "<$k><![CDATA[$v]]></$k>"; 97 } 98 99 $response .= "</wp_error_data>"; 100 } 101 } else { 102 $response = "<response_data><![CDATA[$data]]></response_data>"; 103 } 104 105 $s = ''; 106 if ( is_array($supplemental) ) { 107 foreach ( $supplemental as $k => $v ) 108 $s .= "<$k><![CDATA[$v]]></$k>"; 109 $s = "<supplemental>$s</supplemental>"; 110 } 111 112 if ( false === $action ) 113 $action = $_POST['action']; 114 115 $x = ''; 116 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action 117 $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; 118 $x .= $response; 119 $x .= $s; 120 $x .= "</$what>"; 121 $x .= "</response>"; 122 123 $this->responses[] = $x; 124 return $x; 125 } 126 127 /** 128 * Display XML formatted responses. 129 * 130 * Sets the content type header to text/xml. 131 * 132 * @since 2.1.0 133 */ 134 function send() { 135 header('Content-Type: text/xml'); 136 echo "<?xml version='1.0' standalone='yes'?><wp_ajax>"; 137 foreach ( (array) $this->responses as $response ) 138 echo $response; 139 echo '</wp_ajax>'; 140 die(); 141 } 142 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 5 01:00:55 2024 | Cross-referenced by PHPXref 0.7.1 |