| 1 |
<?php |
| 2 |
// Need 'class-IXR' for the 'IXR_Error' cass |
| 3 |
require_once ABSPATH . WPINC . '/class-IXR.php'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Error Object |
| 7 |
* TODO I want a generic response object class, not limited to errors. |
| 8 |
*/ |
| 9 |
class Meow_WPLR_Sync_Error extends IXR_Error { |
| 10 |
|
| 11 |
/** |
| 12 |
* Variables to be encoded to JSON. |
| 13 |
* Must be public. |
| 14 |
* Note: JsonSerializable is not available for PHP 5.3 |
| 15 |
*/ |
| 16 |
public |
| 17 |
$context = null, |
| 18 |
$reason = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* Instantiates with a specific context |
| 22 |
* @param string|Meow_WPLR_Sync_Error_Context $context |
| 23 |
* @param string $reason |
| 24 |
* @param string $msg Alternative message |
| 25 |
* @return Meow_WPLR_Sync_Error New instance |
| 26 |
*/ |
| 27 |
public static function createByContext( $context, $reason = '', $msg = '' ) { |
| 28 |
$code = 400; // Generic client-side error (Bad Request) |
| 29 |
$msg = $msg ?: __( 'Unknown Error', 'wplr-sync' ); |
| 30 |
|
| 31 |
$context = $context instanceof Meow_WPLR_Sync_Error_Context ? |
| 32 |
$context : new Meow_WPLR_Sync_Error_Context( $context ); |
| 33 |
|
| 34 |
switch ( $context->getName() ) { |
| 35 |
case 'wp_authenticate': // Attempted to login WordPress |
| 36 |
$code = 401; // Unauthorized |
| 37 |
switch ( $reason ) { |
| 38 |
case 'empty_username': // Empty username supplied |
| 39 |
$msg = __( 'Please input your WP username', 'wplr-sync' ); |
| 40 |
break; |
| 41 |
case 'empty_password': // Empty password supplied |
| 42 |
$msg = __( 'Please input your WP password', 'wplr-sync' ); |
| 43 |
break; |
| 44 |
case 'invalid_username': // Specified user doesn't exist |
| 45 |
$msg = __( 'Invalid Username', 'wplr-sync' ); |
| 46 |
break; |
| 47 |
case 'incorrect_password': // Password is incorrect |
| 48 |
$msg = __( 'Incorrect Password', 'wplr-sync' ); |
| 49 |
break; |
| 50 |
} |
| 51 |
break; |
| 52 |
case 'wp_check_user_caps': // Checking user capability |
| 53 |
$code = 403; // Forbidden |
| 54 |
switch ( $reason ) { |
| 55 |
case 'cannot_upload': // User doesn't have permission to upload |
| 56 |
$msg = __( 'You do not have permission to upload files.' ); |
| 57 |
break; |
| 58 |
} |
| 59 |
break; |
| 60 |
} |
| 61 |
$r = new static( $code, $msg ); |
| 62 |
return $r |
| 63 |
->setReason( $reason ) |
| 64 |
->setContext( $context ); |
| 65 |
} |
| 66 |
|
| 67 |
public function __toString() { |
| 68 |
return $this->message; |
| 69 |
} |
| 70 |
|
| 71 |
public function getCode() { |
| 72 |
return $this->code; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
public function getMessage() { |
| 79 |
return $this->message; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return Meow_WPLR_Sync_Error_Context |
| 84 |
*/ |
| 85 |
public function getContext() { |
| 86 |
return $this->context; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function getReason() { |
| 93 |
return $this->reason; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param Meow_WPLR_Sync_Error_Context $X |
| 98 |
* @return Meow_WPLR_Sync_Error This |
| 99 |
*/ |
| 100 |
public function setContext( $X ) { |
| 101 |
$this->context = $X; |
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param string $X |
| 107 |
* @return Meow_WPLR_Sync_Error This |
| 108 |
*/ |
| 109 |
public function setReason( $X ) { |
| 110 |
$this->reason = $X; |
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function toArray() { |
| 118 |
return array ( |
| 119 |
'code' => $this->code, |
| 120 |
'message' => $this->message, |
| 121 |
'reason' => $this->reason, |
| 122 |
'context' => $this->context->toArray() |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Error Context |
| 129 |
*/ |
| 130 |
class Meow_WPLR_Sync_Error_Context { |
| 131 |
private |
| 132 |
$name, |
| 133 |
$data; |
| 134 |
|
| 135 |
/** |
| 136 |
* @param string $name |
| 137 |
*/ |
| 138 |
public function __construct( $name ) { |
| 139 |
$this->name = $name; |
| 140 |
$this->data = array (); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns the name of the context |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function getName() { |
| 148 |
return $this->name; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Returns the data associated with the specified key |
| 153 |
* @param string $key |
| 154 |
* @return mixed |
| 155 |
*/ |
| 156 |
public function get( $key ) { |
| 157 |
return $this->data[$key]; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Sets a data |
| 162 |
* @param string $key |
| 163 |
* @param mixed $value |
| 164 |
* @return Meow_WPLR_Sync_Error_Context This |
| 165 |
*/ |
| 166 |
public function set( $key, $value ) { |
| 167 |
$this->data[$key] = $value; |
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public function toArray() { |
| 175 |
return array ( |
| 176 |
'name' => $this->name, |
| 177 |
'data' => $this->data |
| 178 |
); |
| 179 |
} |
| 180 |
} |
| 181 |
|