[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> class.wp-error.php (source)

   1  <?php
   2  // Last sync [WP11537] - split out from wp-includes/classes.php
   3  
   4  /**
   5   * WordPress Error class.
   6   *
   7   * Container for checking for WordPress errors and error messages. Return
   8   * WP_Error and use {@link is_wp_error()} to check if this class is returned.
   9   * Many core WordPress functions pass this class in the event of an error and
  10   * if not handled properly will result in code errors.
  11   *
  12   * @package WordPress
  13   * @since 2.1.0
  14   */
  15  class WP_Error {
  16      /**
  17       * Stores the list of errors.
  18       *
  19       * @since 2.1.0
  20       * @var array
  21       * @access private
  22       */
  23      var $errors = array();
  24  
  25      /**
  26       * Stores the list of data for error codes.
  27       *
  28       * @since 2.1.0
  29       * @var array
  30       * @access private
  31       */
  32      var $error_data = array();
  33  
  34      /**
  35       * PHP4 Constructor - Sets up error message.
  36       *
  37       * If code parameter is empty then nothing will be done. It is possible to
  38       * add multiple messages to the same code, but with other methods in the
  39       * class.
  40       *
  41       * All parameters are optional, but if the code parameter is set, then the
  42       * data parameter is optional.
  43       *
  44       * @since 2.1.0
  45       *
  46       * @param string|int $code Error code
  47       * @param string $message Error message
  48       * @param mixed $data Optional. Error data.
  49       * @return WP_Error
  50       */
  51  	function __construct($code = '', $message = '', $data = '') {
  52          if ( empty($code) )
  53              return;
  54  
  55          $this->errors[$code][] = $message;
  56  
  57          if ( ! empty($data) )
  58              $this->error_data[$code] = $data;
  59      }
  60  
  61  	function WP_Error($code = '', $message = '', $data = '') {
  62          self::__construct($code, $message, $data);
  63      }
  64  
  65      /**
  66       * Retrieve all error codes.
  67       *
  68       * @since 2.1.0
  69       * @access public
  70       *
  71       * @return array List of error codes, if avaiable.
  72       */
  73  	function get_error_codes() {
  74          if ( empty($this->errors) )
  75              return array();
  76  
  77          return array_keys($this->errors);
  78      }
  79  
  80      /**
  81       * Retrieve first error code available.
  82       *
  83       * @since 2.1.0
  84       * @access public
  85       *
  86       * @return string|int Empty string, if no error codes.
  87       */
  88  	function get_error_code() {
  89          $codes = $this->get_error_codes();
  90  
  91          if ( empty($codes) )
  92              return '';
  93  
  94          return $codes[0];
  95      }
  96  
  97      /**
  98       * Retrieve all error messages or error messages matching code.
  99       *
 100       * @since 2.1.0
 101       *
 102       * @param string|int $code Optional. Retrieve messages matching code, if exists.
 103       * @return array Error strings on success, or empty array on failure (if using codee parameter).
 104       */
 105  	function get_error_messages($code = '') {
 106          // Return all messages if no code specified.
 107          if ( empty($code) ) {
 108              $all_messages = array();
 109              foreach ( (array) $this->errors as $code => $messages )
 110                  $all_messages = array_merge($all_messages, $messages);
 111  
 112              return $all_messages;
 113          }
 114  
 115          if ( isset($this->errors[$code]) )
 116              return $this->errors[$code];
 117          else
 118              return array();
 119      }
 120  
 121      /**
 122       * Get single error message.
 123       *
 124       * This will get the first message available for the code. If no code is
 125       * given then the first code available will be used.
 126       *
 127       * @since 2.1.0
 128       *
 129       * @param string|int $code Optional. Error code to retrieve message.
 130       * @return string
 131       */
 132  	function get_error_message($code = '') {
 133          if ( empty($code) )
 134              $code = $this->get_error_code();
 135          $messages = $this->get_error_messages($code);
 136          if ( empty($messages) )
 137              return '';
 138          return $messages[0];
 139      }
 140  
 141      /**
 142       * Retrieve error data for error code.
 143       *
 144       * @since 2.1.0
 145       *
 146       * @param string|int $code Optional. Error code.
 147       * @return mixed Null, if no errors.
 148       */
 149  	function get_error_data($code = '') {
 150          if ( empty($code) )
 151              $code = $this->get_error_code();
 152  
 153          if ( isset($this->error_data[$code]) )
 154              return $this->error_data[$code];
 155          return null;
 156      }
 157  
 158      /**
 159       * Append more error messages to list of error messages.
 160       *
 161       * @since 2.1.0
 162       * @access public
 163       *
 164       * @param string|int $code Error code.
 165       * @param string $message Error message.
 166       * @param mixed $data Optional. Error data.
 167       */
 168  	function add($code, $message, $data = '') {
 169          $this->errors[$code][] = $message;
 170          if ( ! empty($data) )
 171              $this->error_data[$code] = $data;
 172      }
 173  
 174      /**
 175       * Add data for error code.
 176       *
 177       * The error code can only contain one error data.
 178       *
 179       * @since 2.1.0
 180       *
 181       * @param mixed $data Error data.
 182       * @param string|int $code Error code.
 183       */
 184  	function add_data($data, $code = '') {
 185          if ( empty($code) )
 186              $code = $this->get_error_code();
 187  
 188          $this->error_data[$code] = $data;
 189      }
 190  }
 191  
 192  /**
 193   * Check whether variable is a WordPress Error.
 194   *
 195   * Looks at the object and if a WP_Error class. Does not check to see if the
 196   * parent is also WP_Error, so can't inherit WP_Error and still use this
 197   * function.
 198   *
 199   * @since 2.1.0
 200   *
 201   * @param mixed $thing Check if unknown variable is WordPress Error object.
 202   * @return bool True, if WP_Error. False, if not WP_Error.
 203   */
 204  function is_wp_error($thing) {
 205      if ( is_object($thing) && is_a($thing, 'WP_Error') )
 206          return true;
 207      return false;
 208  }


Generated: Tue Oct 15 01:00:51 2024 Cross-referenced by PHPXref 0.7.1