[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Send XML response back to Ajax request.
   4   *
   5   * @package WordPress
   6   * @since 2.1.0
   7   */
   8  class WP_Ajax_Response {
   9      /**
  10       * Store XML responses to send.
  11       *
  12       * @since 2.1.0
  13       * @var array
  14       */
  15      public $responses = array();
  16  
  17      /**
  18       * Constructor - Passes args to WP_Ajax_Response::add().
  19       *
  20       * @since 2.1.0
  21       * @see WP_Ajax_Response::add()
  22       *
  23       * @param string|array $args Optional. Will be passed to add() method.
  24       */
  25  	public function __construct( $args = '' ) {
  26          if ( ! empty( $args ) ) {
  27              $this->add( $args );
  28          }
  29      }
  30  
  31      /**
  32       * Appends data to an XML response based on given arguments.
  33       *
  34       * With `$args` defaults, extra data output would be:
  35       *
  36       *     <response action='{$action}_$id'>
  37       *      <$what id='$id' position='$position'>
  38       *          <response_data><![CDATA[$data]]></response_data>
  39       *      </$what>
  40       *     </response>
  41       *
  42       * @since 2.1.0
  43       *
  44       * @param string|array $args {
  45       *     Optional. An array or string of XML response arguments.
  46       *
  47       *     @type string          $what         XML-RPC response type. Used as a child element of `<response>`.
  48       *                                         Default 'object' (`<object>`).
  49       *     @type string|false    $action       Value to use for the `action` attribute in `<response>`. Will be
  50       *                                         appended with `_$id` on output. If false, `$action` will default to
  51       *                                         the value of `$_POST['action']`. Default false.
  52       *     @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       *     @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       *     @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       *     @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       *     @type array           $supplemental An array of extra strings that will be output within a `<supplemental>`
  61       *                                         element as CDATA. Default empty array.
  62       * }
  63       * @return string XML response.
  64       */
  65  	public function add( $args = '' ) {
  66          $defaults = array(
  67              'what'         => 'object',
  68              'action'       => false,
  69              'id'           => '0',
  70              'old_id'       => false,
  71              'position'     => 1,
  72              'data'         => '',
  73              'supplemental' => array(),
  74          );
  75  
  76          $parsed_args = wp_parse_args( $args, $defaults );
  77  
  78          $position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] );
  79          $id       = $parsed_args['id'];
  80          $what     = $parsed_args['what'];
  81          $action   = $parsed_args['action'];
  82          $old_id   = $parsed_args['old_id'];
  83          $data     = $parsed_args['data'];
  84  
  85          if ( is_wp_error( $id ) ) {
  86              $data = $id;
  87              $id   = 0;
  88          }
  89  
  90          $response = '';
  91          if ( is_wp_error( $data ) ) {
  92              foreach ( (array) $data->get_error_codes() as $code ) {
  93                  $response  .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>';
  94                  $error_data = $data->get_error_data( $code );
  95                  if ( ! $error_data ) {
  96                      continue;
  97                  }
  98                  $class = '';
  99                  if ( is_object( $error_data ) ) {
 100                      $class      = ' class="' . get_class( $error_data ) . '"';
 101                      $error_data = get_object_vars( $error_data );
 102                  }
 103  
 104                  $response .= "<wp_error_data code='$code'$class>";
 105  
 106                  if ( is_scalar( $error_data ) ) {
 107                      $response .= "<![CDATA[$error_data]]>";
 108                  } elseif ( is_array( $error_data ) ) {
 109                      foreach ( $error_data as $k => $v ) {
 110                          $response .= "<$k><![CDATA[$v]]></$k>";
 111                      }
 112                  }
 113  
 114                  $response .= '</wp_error_data>';
 115              }
 116          } else {
 117              $response = "<response_data><![CDATA[$data]]></response_data>";
 118          }
 119  
 120          $s = '';
 121          if ( is_array( $parsed_args['supplemental'] ) ) {
 122              foreach ( $parsed_args['supplemental'] as $k => $v ) {
 123                  $s .= "<$k><![CDATA[$v]]></$k>";
 124              }
 125              $s = "<supplemental>$s</supplemental>";
 126          }
 127  
 128          if ( false === $action ) {
 129              $action = $_POST['action'];
 130          }
 131          $x  = '';
 132          $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
 133          $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
 134          $x .= $response;
 135          $x .= $s;
 136          $x .= "</$what>";
 137          $x .= '</response>';
 138  
 139          $this->responses[] = $x;
 140          return $x;
 141      }
 142  
 143      /**
 144       * Display XML formatted responses.
 145       *
 146       * Sets the content type header to text/xml.
 147       *
 148       * @since 2.1.0
 149       */
 150  	public function send() {
 151          header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
 152          echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
 153          foreach ( (array) $this->responses as $response ) {
 154              echo $response;
 155          }
 156          echo '</wp_ajax>';
 157          if ( wp_doing_ajax() ) {
 158              wp_die();
 159          } else {
 160              die();
 161          }
 162      }
 163  }


Generated: Wed Oct 23 08:20:01 2019 Cross-referenced by PHPXref 0.7