[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> class.wp-ajax-response.php (source)

   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 WP_Ajax_Response( $args = '' ) {
  30          if ( !empty($args) )
  31              $this->add($args);
  32      }
  33  
  34      /**
  35       * Append to XML response based on given arguments.
  36       *
  37       * The arguments that can be passed in the $args parameter are below. It is
  38       * also possible to pass a WP_Error object in either the 'id' or 'data'
  39       * argument. The parameter isn't actually optional, content should be given
  40       * in order to send the correct response.
  41       *
  42       * 'what' argument is a string that is the XMLRPC response type.
  43       * 'action' argument is a boolean or string that acts like a nonce.
  44       * 'id' argument can be WP_Error or an integer.
  45       * 'old_id' argument is false by default or an integer of the previous ID.
  46       * 'position' argument is an integer or a string with -1 = top, 1 = bottom,
  47       * html ID = after, -html ID = before.
  48       * 'data' argument is a string with the content or message.
  49       * 'supplemental' argument is an array of strings that will be children of
  50       * the supplemental element.
  51       *
  52       * @since 2.1.0
  53       *
  54       * @param string|array $args Override defaults.
  55       * @return string XML response.
  56       */
  57  	function add( $args = '' ) {
  58          $defaults = array(
  59              'what' => 'object', 'action' => false,
  60              'id' => '0', 'old_id' => false,
  61              'position' => 1,
  62              'data' => '', 'supplemental' => array()
  63          );
  64  
  65          $r = wp_parse_args( $args, $defaults );
  66          extract( $r, EXTR_SKIP );
  67          $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position );
  68  
  69          if ( is_wp_error($id) ) {
  70              $data = $id;
  71              $id = 0;
  72          }
  73  
  74          $response = '';
  75          if ( is_wp_error($data) ) {
  76              foreach ( (array) $data->get_error_codes() as $code ) {
  77                  $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
  78                  if ( !$error_data = $data->get_error_data($code) )
  79                      continue;
  80                  $class = '';
  81                  if ( is_object($error_data) ) {
  82                      $class = ' class="' . get_class($error_data) . '"';
  83                      $error_data = get_object_vars($error_data);
  84                  }
  85  
  86                  $response .= "<wp_error_data code='$code'$class>";
  87  
  88                  if ( is_scalar($error_data) ) {
  89                      $response .= "<![CDATA[$error_data]]>";
  90                  } elseif ( is_array($error_data) ) {
  91                      foreach ( $error_data as $k => $v )
  92                          $response .= "<$k><![CDATA[$v]]></$k>";
  93                  }
  94  
  95                  $response .= "</wp_error_data>";
  96              }
  97          } else {
  98              $response = "<response_data><![CDATA[$data]]></response_data>";
  99          }
 100  
 101          $s = '';
 102          if ( is_array($supplemental) ) {
 103              foreach ( $supplemental as $k => $v )
 104                  $s .= "<$k><![CDATA[$v]]></$k>";
 105              $s = "<supplemental>$s</supplemental>";
 106          }
 107  
 108          if ( false === $action )
 109              $action = $_POST['action'];
 110  
 111          $x = '';
 112          $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
 113          $x .=    "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
 114          $x .=        $response;
 115          $x .=        $s;
 116          $x .=    "</$what>";
 117          $x .= "</response>";
 118  
 119          $this->responses[] = $x;
 120          return $x;
 121      }
 122  
 123      /**
 124       * Display XML formatted responses.
 125       *
 126       * Sets the content type header to text/xml.
 127       *
 128       * @since 2.1.0
 129       */
 130  	function send() {
 131          header('Content-Type: text/xml');
 132          echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
 133          foreach ( (array) $this->responses as $response )
 134              echo $response;
 135          echo '</wp_ajax>';
 136          die();
 137      }
 138  }


Generated: Wed Feb 8 03:57:40 2012 Hosted by follow the white rabbit.