[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Error API.
   4   *
   5   * Contains the WP_Error class and the is_wp_error() function.
   6   *
   7   * @package WordPress
   8   */
   9  
  10  /**
  11   * WordPress Error class.
  12   *
  13   * Container for checking for WordPress errors and error messages. Return
  14   * WP_Error and use is_wp_error() to check if this class is returned. Many
  15   * core WordPress functions pass this class in the event of an error and
  16   * if not handled properly will result in code errors.
  17   *
  18   * @since 2.1.0
  19   */
  20  class WP_Error {
  21      /**
  22       * Stores the list of errors.
  23       *
  24       * @since 2.1.0
  25       * @var array
  26       */
  27      public $errors = array();
  28  
  29      /**
  30       * Stores the list of data for error codes.
  31       *
  32       * @since 2.1.0
  33       * @var array
  34       */
  35      public $error_data = array();
  36  
  37      /**
  38       * Initialize the error.
  39       *
  40       * If `$code` is empty, the other parameters will be ignored.
  41       * When `$code` is not empty, `$message` will be used even if
  42       * it is empty. The `$data` parameter will be used only if it
  43       * is not empty.
  44       *
  45       * Though the class is constructed with a single error code and
  46       * message, multiple codes can be added using the `add()` method.
  47       *
  48       * @since 2.1.0
  49       *
  50       * @param string|int $code Error code
  51       * @param string $message Error message
  52       * @param mixed $data Optional. Error data.
  53       */
  54  	public function __construct( $code = '', $message = '', $data = '' ) {
  55          if ( empty( $code ) ) {
  56              return;
  57          }
  58  
  59          $this->errors[ $code ][] = $message;
  60  
  61          if ( ! empty( $data ) ) {
  62              $this->error_data[ $code ] = $data;
  63          }
  64      }
  65  
  66      /**
  67       * Retrieve all error codes.
  68       *
  69       * @since 2.1.0
  70       *
  71       * @return array List of error codes, if available.
  72       */
  73  	public function get_error_codes() {
  74          if ( ! $this->has_errors() ) {
  75              return array();
  76          }
  77  
  78          return array_keys( $this->errors );
  79      }
  80  
  81      /**
  82       * Retrieve first error code available.
  83       *
  84       * @since 2.1.0
  85       *
  86       * @return string|int Empty string, if no error codes.
  87       */
  88  	public function get_error_code() {
  89          $codes = $this->get_error_codes();
  90  
  91          if ( empty( $codes ) ) {
  92              return '';
  93          }
  94  
  95          return $codes[0];
  96      }
  97  
  98      /**
  99       * Retrieve all error messages or error messages matching code.
 100       *
 101       * @since 2.1.0
 102       *
 103       * @param string|int $code Optional. Retrieve messages matching code, if exists.
 104       * @return array Error strings on success, or empty array on failure (if using code parameter).
 105       */
 106  	public function get_error_messages( $code = '' ) {
 107          // Return all messages if no code specified.
 108          if ( empty( $code ) ) {
 109              $all_messages = array();
 110              foreach ( (array) $this->errors as $code => $messages ) {
 111                  $all_messages = array_merge( $all_messages, $messages );
 112              }
 113  
 114              return $all_messages;
 115          }
 116  
 117          if ( isset( $this->errors[ $code ] ) ) {
 118              return $this->errors[ $code ];
 119          } else {
 120              return array();
 121          }
 122      }
 123  
 124      /**
 125       * Get single error message.
 126       *
 127       * This will get the first message available for the code. If no code is
 128       * given then the first code available will be used.
 129       *
 130       * @since 2.1.0
 131       *
 132       * @param string|int $code Optional. Error code to retrieve message.
 133       * @return string
 134       */
 135  	public function get_error_message( $code = '' ) {
 136          if ( empty( $code ) ) {
 137              $code = $this->get_error_code();
 138          }
 139          $messages = $this->get_error_messages( $code );
 140          if ( empty( $messages ) ) {
 141              return '';
 142          }
 143          return $messages[0];
 144      }
 145  
 146      /**
 147       * Retrieve error data for error code.
 148       *
 149       * @since 2.1.0
 150       *
 151       * @param string|int $code Optional. Error code.
 152       * @return mixed Error data, if it exists.
 153       */
 154  	public function get_error_data( $code = '' ) {
 155          if ( empty( $code ) ) {
 156              $code = $this->get_error_code();
 157          }
 158  
 159          if ( isset( $this->error_data[ $code ] ) ) {
 160              return $this->error_data[ $code ];
 161          }
 162      }
 163  
 164      /**
 165       * Verify if the instance contains errors.
 166       *
 167       * @since 5.1.0
 168       *
 169       * @return bool
 170       */
 171  	public function has_errors() {
 172          if ( ! empty( $this->errors ) ) {
 173              return true;
 174          }
 175          return false;
 176      }
 177  
 178      /**
 179       * Add an error or append additional message to an existing error.
 180       *
 181       * @since 2.1.0
 182       *
 183       * @param string|int $code Error code.
 184       * @param string $message Error message.
 185       * @param mixed $data Optional. Error data.
 186       */
 187  	public function add( $code, $message, $data = '' ) {
 188          $this->errors[ $code ][] = $message;
 189          if ( ! empty( $data ) ) {
 190              $this->error_data[ $code ] = $data;
 191          }
 192      }
 193  
 194      /**
 195       * Add data for error code.
 196       *
 197       * The error code can only contain one error data.
 198       *
 199       * @since 2.1.0
 200       *
 201       * @param mixed $data Error data.
 202       * @param string|int $code Error code.
 203       */
 204  	public function add_data( $data, $code = '' ) {
 205          if ( empty( $code ) ) {
 206              $code = $this->get_error_code();
 207          }
 208  
 209          $this->error_data[ $code ] = $data;
 210      }
 211  
 212      /**
 213       * Removes the specified error.
 214       *
 215       * This function removes all error messages associated with the specified
 216       * error code, along with any error data for that code.
 217       *
 218       * @since 4.1.0
 219       *
 220       * @param string|int $code Error code.
 221       */
 222  	public function remove( $code ) {
 223          unset( $this->errors[ $code ] );
 224          unset( $this->error_data[ $code ] );
 225      }
 226  }


Generated: Fri Oct 25 08:20:01 2019 Cross-referenced by PHPXref 0.7