WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-result.php
Go to the documentation of this file.
1 <?php
13  const ERROR = 'error';
14  const WARNING = 'warning';
15  const STATUS = 'status';
16 }
17 
18 
31 class WPDKResult extends WP_Error {
32 
45  public function __construct( $code, $type = WPDKResultType::STATUS, $message = '', $data = '' )
46  {
47  $new_code = sprintf( '%s-%s', $type, $code );
48  $sanitize_code = $this->sanitizeCode( $new_code );
49  parent::__construct( $sanitize_code, $message, $data );
50  }
51 
63  public function display( $echo = true, $log = null )
64  {
65  $message = '<div class="wpdk-watchdog-wp-error">';
66 
67  foreach ( $this->errors as $code => $single ) {
68  $message .= sprintf( '<code>Code: 0x%x, Description: %s</code>', $code, $single[0] );
69  $error_data = $this->get_error_data( $code );
70  if ( !empty( $error_data ) ) {
71  if ( is_array( $error_data ) ) {
72  foreach ( $error_data as $key => $data ) {
73  $message .= sprintf( '<code>Key: %s, Data: %s</code>', $key, urldecode( $data ) );
74  }
75  }
76  else {
77  $message .= sprintf( '<code>Data: %s</code>', urldecode( $error_data ) );
78  }
79  }
80  }
81 
82  /* log to file if enabled */
83  if ( !is_null( $log ) ) {
84  $log->log( esc_attr( wp_strip_all_tags( $message ) ) );
85  }
86 
87  $message .= '</div>';
88 
89  if ( $echo ) {
90  echo $message;
91  return true;
92  }
93  return $message;
94  }
95 
105  public function getVarDump( $content )
106  {
107  ob_start();
108  ?>
109  <pre><?php var_dump( $content ) ?></pre><?php
110  $content = ob_get_contents();
111  ob_end_clean();
112  return $content;
113  }
114 
124  protected function sanitizeCode( $code )
125  {
126  $replace = array(
127  ' ' => '_',
128  '-' => '_'
129  );
130  $code = strtr( $code, $replace );
131  return strtolower( $code );
132  }
133 
134 
145  protected static function _is( $thing, $class = 'WPDKError' )
146  {
147  if ( is_object( $thing ) && is_a( $thing, $class ) ) {
148  return true;
149  }
150  return false;
151  }
152 
164  public static function isError( $thing )
165  {
166  return self::_is( $thing );
167  }
168 
180  public static function isWarning( $thing )
181  {
182  return self::_is( $thing, 'WPDKWarning' );
183  }
184 
196  public static function isStatus( $thing )
197  {
198  return self::_is( $thing, 'WPDKStatus' );
199  }
200 
201 }
202 
203 
214 class WPDKError extends WPDKResult {
215 
227  public function __construct( $code, $message = '', $data = '' )
228  {
229  parent::__construct( $code, WPDKResultType::ERROR, $message, $data );
230  }
231 
232 }
233 
245 class WPDKWarning extends WPDKResult {
246 
258  public function __construct( $code, $message = '', $data = '' )
259  {
260  parent::__construct( $code, WPDKResultType::WARNING, $message, $data );
261  }
262 
263 }
264 
275 class WPDKStatus extends WPDKResult {
276 
288  public function __construct( $code, $message = '', $data = '' )
289  {
290  parent::__construct( $code, WPDKResultType::STATUS, $message, $data );
291  }
292 }