| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends Feed Item |
| 4 |
* |
| 5 |
* This contains the reference implementation for a item. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Friends; |
| 11 |
|
| 12 |
/** |
| 13 |
/** |
| 14 |
* This class describes a friends feed item. |
| 15 |
*/ |
| 16 |
class Feed_Item { |
| 17 |
/** |
| 18 |
* Holds the feed item data. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $data = array( |
| 23 |
'post_format' => 'standard', |
| 24 |
'post_status' => 'publish', |
| 25 |
'is_new' => false, |
| 26 |
'meta' => array(), |
| 27 |
); |
| 28 |
|
| 29 |
/** |
| 30 |
* Initialize a new feed item with the supplied data. |
| 31 |
* |
| 32 |
* @param array $data The data. |
| 33 |
*/ |
| 34 |
public function __construct( array $data = array() ) { |
| 35 |
foreach ( $data as $key => $value ) { |
| 36 |
$this->__set( $key, $value ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Gets the specified key. |
| 42 |
* |
| 43 |
* @param string $key The key. |
| 44 |
* |
| 45 |
* @return mixed The value. |
| 46 |
*/ |
| 47 |
public function __get( $key ) { |
| 48 |
if ( 'content' === $key ) { |
| 49 |
$key = 'post_content'; |
| 50 |
} elseif ( 'title' === $key ) { |
| 51 |
$key = 'post_title'; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! $this->__isset( $key ) ) { |
| 55 |
return null; |
| 56 |
} |
| 57 |
|
| 58 |
switch ( $key ) { |
| 59 |
case 'updated_date': |
| 60 |
case 'date': |
| 61 |
return gmdate( 'Y-m-d H:i:s', $this->data[ $key ] ); |
| 62 |
} |
| 63 |
|
| 64 |
return $this->data[ $key ]; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Whether the specified key is set |
| 69 |
* |
| 70 |
* @param string $key The key. |
| 71 |
* |
| 72 |
* @return bool Whether the key is set. |
| 73 |
*/ |
| 74 |
public function __isset( $key ) { |
| 75 |
if ( 'content' === $key ) { |
| 76 |
$key = 'post_content'; |
| 77 |
} elseif ( 'title' === $key ) { |
| 78 |
$key = 'post_title'; |
| 79 |
} |
| 80 |
|
| 81 |
return isset( $this->data[ $key ] ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Sets the value for the key and validates it. |
| 86 |
* |
| 87 |
* @param string $key The key. |
| 88 |
* @param mixed $value The value. |
| 89 |
* |
| 90 |
* @return mixed|\WP_Error The value or a wp error. |
| 91 |
*/ |
| 92 |
public function __set( $key, $value ) { |
| 93 |
if ( apply_filters( 'feed_item_allow_set_metadata', false, $key, $value, $this ) ) { |
| 94 |
$this->data['meta'][ $key ] = $value; |
| 95 |
return $value; |
| 96 |
} |
| 97 |
|
| 98 |
switch ( $key ) { |
| 99 |
case 'permalink': |
| 100 |
$value = $this->validate_url( $value, 'invalid-permalink' ); |
| 101 |
break; |
| 102 |
|
| 103 |
case 'gravatar': |
| 104 |
$value = $this->validate_url( $value, 'invalid-gravatar' ); |
| 105 |
break; |
| 106 |
|
| 107 |
case 'title': |
| 108 |
case 'post_title': |
| 109 |
$key = 'post_title'; |
| 110 |
$value = $this->validate_string( $value, 300, 'invalid-title' ); |
| 111 |
break; |
| 112 |
|
| 113 |
case 'author': |
| 114 |
$value = $this->validate_string( $value, 300, 'invalid-author' ); |
| 115 |
break; |
| 116 |
|
| 117 |
case 'post_content': |
| 118 |
case 'content': |
| 119 |
$key = 'post_content'; |
| 120 |
$value = $this->validate_string( $value, 50000, 'invalid-content' ); |
| 121 |
break; |
| 122 |
|
| 123 |
case 'comment_count': |
| 124 |
$value = $this->validate_integer( $value, 0, 10000, 'invalid-comments-count' ); |
| 125 |
break; |
| 126 |
|
| 127 |
case 'comments_feed': |
| 128 |
$value = $this->validate_url( $value, 'invalid-comments-feed' ); |
| 129 |
break; |
| 130 |
|
| 131 |
case 'post_id': |
| 132 |
$value = $this->validate_integer( $value, 0, PHP_INT_MAX, 'invalid-post-id' ); |
| 133 |
break; |
| 134 |
|
| 135 |
case 'post_format': |
| 136 |
$value = $this->validate_post_format( $value ); |
| 137 |
break; |
| 138 |
|
| 139 |
case 'post_status': |
| 140 |
$value = $this->validate_post_status( $value ); |
| 141 |
break; |
| 142 |
|
| 143 |
case 'date': |
| 144 |
$value = $this->validate_date( $value, 'invalid-date' ); |
| 145 |
break; |
| 146 |
|
| 147 |
case 'updated_date': |
| 148 |
$value = $this->validate_date( $value, 'invalid-updated-date' ); |
| 149 |
break; |
| 150 |
|
| 151 |
// Internal. |
| 152 |
case '_feed_rule_delete': |
| 153 |
$value = boolval( $value ); |
| 154 |
break; |
| 155 |
case '_feed_rule_transform': |
| 156 |
if ( ! is_array( $value ) ) { |
| 157 |
$value = new \WP_Error( 'invalid-key', 'This value cannot be stored in a _feed_rule_transform.' ); |
| 158 |
} |
| 159 |
break; |
| 160 |
case '_is_new': |
| 161 |
$value = boolval( $value ); |
| 162 |
break; |
| 163 |
case '_full_content_fetched': |
| 164 |
$value = boolval( $value ); |
| 165 |
$this->data['meta']['full-content-fetched'] = $value; |
| 166 |
return $value; |
| 167 |
case '_external_id': |
| 168 |
$value = strval( $value ); |
| 169 |
$this->data['meta']['external-id'] = $value; |
| 170 |
return $value; |
| 171 |
|
| 172 |
default: |
| 173 |
return new \WP_Error( 'invalid-key', 'This value cannot be stored in a feed item.' ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! is_wp_error( $value ) ) { |
| 177 |
$this->data[ $key ] = $value; |
| 178 |
} |
| 179 |
|
| 180 |
return $value; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Validate a given string. |
| 185 |
* |
| 186 |
* @param string $str The string. |
| 187 |
* @param int $max_length The maximum length of the string. |
| 188 |
* @param string $error_code The error code. |
| 189 |
* |
| 190 |
* @return string|\WP_Error The validated string. |
| 191 |
*/ |
| 192 |
public function validate_string( $str, $max_length, $error_code ) { |
| 193 |
if ( ! is_string( $str ) ) { |
| 194 |
return new \WP_Error( $error_code, 'No string was supplied.' ); |
| 195 |
} |
| 196 |
|
| 197 |
return rtrim( substr( trim( $str ), 0, $max_length ) ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Validate a given integer. |
| 202 |
* |
| 203 |
* @param int|string $integer The integer. |
| 204 |
* @param int $min The minimum acceptable value. |
| 205 |
* @param int $max The maximum acceptable value. |
| 206 |
* @param string $error_code The error code. |
| 207 |
* |
| 208 |
* @return int|\WP_Error The validated integer. |
| 209 |
*/ |
| 210 |
public function validate_integer( $integer, $min, $max, $error_code ) { |
| 211 |
if ( ! is_numeric( $integer ) ) { |
| 212 |
return new \WP_Error( $error_code, 'No number was supplied.' ); |
| 213 |
} |
| 214 |
$int = intval( $integer ); |
| 215 |
if ( $int > $max ) { |
| 216 |
return new \WP_Error( $error_code, 'Number exceeds the maximum value.' ); |
| 217 |
} |
| 218 |
if ( $int < $min ) { |
| 219 |
return new \WP_Error( $error_code, 'Number is below the minimum value.' ); |
| 220 |
} |
| 221 |
return $int; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Validate a given url. |
| 226 |
* |
| 227 |
* @param string $url The url. |
| 228 |
* @param string $error_code The error code. |
| 229 |
* |
| 230 |
* @return string|\WP_Error The validated url. |
| 231 |
*/ |
| 232 |
public function validate_url( $url, $error_code ) { |
| 233 |
$url = filter_var( $url, FILTER_VALIDATE_URL ); |
| 234 |
if ( false === $url ) { |
| 235 |
return new \WP_Error( $error_code, 'An invalid URL was supplied.' ); |
| 236 |
} |
| 237 |
|
| 238 |
return $url; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Validate a given date. |
| 243 |
* |
| 244 |
* @param string|int $date The date. |
| 245 |
* @param string $error_code The error code. |
| 246 |
* |
| 247 |
* @return string|\WP_Error The validated date. |
| 248 |
*/ |
| 249 |
public function validate_date( $date, $error_code ) { |
| 250 |
if ( is_string( $date ) && ! is_numeric( $date ) ) { |
| 251 |
$date = strtotime( $date ); |
| 252 |
} |
| 253 |
|
| 254 |
if ( is_null( $date ) || 0 > $date ) { |
| 255 |
return new \WP_Error( $error_code, 'An invalid timestamp was supplied.' ); |
| 256 |
} |
| 257 |
|
| 258 |
if ( false === $date ) { |
| 259 |
return new \WP_Error( $error_code, 'The date could not be convered to a timestamp.' ); |
| 260 |
} |
| 261 |
return $date; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Validate a given post format. |
| 266 |
* |
| 267 |
* @param string $format The post format. |
| 268 |
* |
| 269 |
* @return string|\WP_Error The validated format. |
| 270 |
*/ |
| 271 |
public function validate_post_format( $format ) { |
| 272 |
$post_formats = get_post_format_strings(); |
| 273 |
if ( ! isset( $post_formats[ $format ] ) ) { |
| 274 |
return new \WP_Error( 'invalid-post-format', 'The format needs to be one of get_post_format_strings().' ); |
| 275 |
|
| 276 |
} |
| 277 |
return $format; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Validate a given post status. |
| 282 |
* |
| 283 |
* @param string $status The post status. |
| 284 |
* |
| 285 |
* @return string|\WP_Error The validated status. |
| 286 |
*/ |
| 287 |
public function validate_post_status( $status ) { |
| 288 |
$valid_post_statuses = array( 'draft', 'publish', 'private' ); |
| 289 |
if ( in_array( $status, $valid_post_statuses ) ) { |
| 290 |
return new \WP_Error( 'invalid-post-status', 'The status needs to be one of draft, publish, or private.' ); |
| 291 |
|
| 292 |
} |
| 293 |
return $status; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Determines if the item is new. |
| 298 |
* |
| 299 |
* @return bool True if new, False otherwise. |
| 300 |
*/ |
| 301 |
public function is_new() { |
| 302 |
return isset( $this->data['_is_new'] ) && $this->data['_is_new']; |
| 303 |
} |
| 304 |
} |
| 305 |
|