[ Index ]

PHP Cross Reference of WordPress

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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1