PluginProbe
Friends / 4.3.1
Friends v4.3.1
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / feed-parsers / class-feed-item.php

class-feed-item.php in Friends 4.3.1, at feed-parsers/class-feed-item.php

345 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 case 'meta':
63 $meta = $this->data['meta'];
64 if ( isset( $meta['enclosure'] ) ) {
65 if ( isset( $meta['enclosure']['url'] ) ) {
66 $url = $meta['enclosure']['url'];
67 $len = '';
68 if ( isset( $meta['enclosure']['length'] ) ) {
69 $len = $meta['enclosure']['length'];
70 }
71 $mime = '';
72 if ( isset( $meta['enclosure']['mime'] ) ) {
73 $mime = $meta['enclosure']['mime'];
74 }
75
76 $meta['enclosure'] = "$url\n$len\n$mime\n";
77 } else {
78 // An enclosure only makes sense if it has a URL.
79 unset( $meta['enclosure'] );
80 }
81 }
82
83 return $meta;
84 }
85
86 return $this->data[ $key ];
87 }
88
89 /**
90 * Whether the specified key is set
91 *
92 * @param string $key The key.
93 *
94 * @return bool Whether the key is set.
95 */
96 public function __isset( $key ) {
97 if ( 'content' === $key ) {
98 $key = 'post_content';
99 } elseif ( 'title' === $key ) {
100 $key = 'post_title';
101 }
102
103 return isset( $this->data[ $key ] );
104 }
105
106 /**
107 * Sets the value for the key and validates it.
108 *
109 * @param string $key The key.
110 * @param mixed $value The value.
111 *
112 * @return mixed|\WP_Error The value or a wp error.
113 */
114 public function __set( $key, $value ) {
115 if ( apply_filters( 'feed_item_allow_set_metadata', false, $key, $value, $this ) ) {
116 $this->data['meta'][ $key ] = $value;
117 return $value;
118 }
119
120 switch ( $key ) {
121 case 'permalink':
122 $value = $this->validate_url( $value, 'invalid-permalink' );
123 break;
124
125 case 'gravatar':
126 $value = $this->validate_url( $value, 'invalid-gravatar' );
127 break;
128
129 case 'title':
130 case 'post_title':
131 $key = 'post_title';
132 $value = $this->validate_string( $value, 300, 'invalid-title' );
133 break;
134
135 case 'author':
136 $value = $this->validate_string( $value, 300, 'invalid-author' );
137 break;
138
139 case 'post_content':
140 case 'content':
141 $key = 'post_content';
142 $value = $this->validate_string( $value, 50000, 'invalid-content' );
143 break;
144
145 case 'comment_count':
146 $value = $this->validate_integer( $value, 0, 10000, 'invalid-comments-count' );
147 break;
148
149 case 'comments_feed':
150 $value = $this->validate_url( $value, 'invalid-comments-feed' );
151 break;
152
153 case 'post_id':
154 $value = $this->validate_integer( $value, 0, PHP_INT_MAX, 'invalid-post-id' );
155 break;
156
157 case 'post_format':
158 $value = $this->validate_post_format( $value );
159 break;
160
161 case 'post_status':
162 $value = $this->validate_post_status( $value );
163 break;
164
165 case 'date':
166 $value = $this->validate_date( $value, 'invalid-date' );
167 break;
168
169 case 'updated_date':
170 $value = $this->validate_date( $value, 'invalid-updated-date' );
171 break;
172
173 case 'enclosure':
174 if ( ! is_array( $value ) ) {
175 return new \WP_Error( 'invalid-enclosure', 'This value cannot be stored in a enclosure.' );
176 }
177
178 if ( ! isset( $value['url'] ) || ! $this->validate_url( $value['url'], 'invalid-enclosure-url' ) ) {
179 return new \WP_Error( 'invalid-enclosure-url', 'The enclosure URL is invalid.' );
180 }
181
182 $this->data['meta']['enclosure'] = $value;
183 break;
184
185 // Internal.
186 case '_feed_rule_delete':
187 $value = boolval( $value );
188 break;
189 case '_feed_rule_transform':
190 if ( ! is_array( $value ) ) {
191 $value = new \WP_Error( 'invalid-key', 'This value cannot be stored in a _feed_rule_transform.' );
192 }
193 break;
194 case '_is_new':
195 $value = boolval( $value );
196 break;
197 case '_full_content_fetched':
198 $value = boolval( $value );
199 $this->data['meta']['full-content-fetched'] = $value;
200 return $value;
201 case '_external_id':
202 $value = strval( $value );
203 $this->data['meta']['external-id'] = $value;
204 return $value;
205 case 'friend_tags':
206 case 'friend_mention_tags':
207 if ( ! is_array( $value ) ) {
208 return new \WP_Error( 'invalid-tags', 'Tags must be an array.' );
209 }
210 break;
211
212 default:
213 return new \WP_Error( 'invalid-key', 'This value cannot be stored in a feed item.' );
214 }
215
216 if ( ! is_wp_error( $value ) ) {
217 $this->data[ $key ] = $value;
218 }
219
220 return $value;
221 }
222
223 /**
224 * Validate a given string.
225 *
226 * @param string $str The string.
227 * @param int $max_length The maximum length of the string.
228 * @param string $error_code The error code.
229 *
230 * @return string|\WP_Error The validated string.
231 */
232 public function validate_string( $str, $max_length, $error_code ) {
233 if ( ! is_string( $str ) ) {
234 return new \WP_Error( $error_code, 'No string was supplied.' );
235 }
236
237 return rtrim( substr( trim( $str ), 0, $max_length ) );
238 }
239
240 /**
241 * Validate a given integer.
242 *
243 * @param int|string $integer The integer.
244 * @param int $min The minimum acceptable value.
245 * @param int $max The maximum acceptable value.
246 * @param string $error_code The error code.
247 *
248 * @return int|\WP_Error The validated integer.
249 */
250 public function validate_integer( $integer, $min, $max, $error_code ) {
251 if ( ! is_numeric( $integer ) ) {
252 return new \WP_Error( $error_code, 'No number was supplied.' );
253 }
254 $int = intval( $integer );
255 if ( $int > $max ) {
256 return new \WP_Error( $error_code, 'Number exceeds the maximum value.' );
257 }
258 if ( $int < $min ) {
259 return new \WP_Error( $error_code, 'Number is below the minimum value.' );
260 }
261 return $int;
262 }
263
264 /**
265 * Validate a given url.
266 *
267 * @param string $url The url.
268 * @param string $error_code The error code.
269 *
270 * @return string|\WP_Error The validated url.
271 */
272 public function validate_url( $url, $error_code ) {
273 $url = filter_var( $url, FILTER_VALIDATE_URL );
274 if ( false === $url ) {
275 return new \WP_Error( $error_code, 'An invalid URL was supplied.' );
276 }
277
278 return $url;
279 }
280
281 /**
282 * Validate a given date.
283 *
284 * @param string|int $date The date.
285 * @param string $error_code The error code.
286 *
287 * @return string|\WP_Error The validated date.
288 */
289 public function validate_date( $date, $error_code ) {
290 if ( is_string( $date ) && ! is_numeric( $date ) ) {
291 $date = strtotime( $date );
292 }
293
294 if ( is_null( $date ) || 0 > $date ) {
295 return new \WP_Error( $error_code, 'An invalid timestamp was supplied.' );
296 }
297
298 if ( false === $date ) {
299 return new \WP_Error( $error_code, 'The date could not be convered to a timestamp.' );
300 }
301 return $date;
302 }
303
304 /**
305 * Validate a given post format.
306 *
307 * @param string $format The post format.
308 *
309 * @return string|\WP_Error The validated format.
310 */
311 public function validate_post_format( $format ) {
312 $post_formats = get_post_format_strings();
313 if ( ! isset( $post_formats[ $format ] ) ) {
314 return new \WP_Error( 'invalid-post-format', 'The format needs to be one of get_post_format_strings().' );
315
316 }
317 return $format;
318 }
319
320 /**
321 * Validate a given post status.
322 *
323 * @param string $status The post status.
324 *
325 * @return string|\WP_Error The validated status.
326 */
327 public function validate_post_status( $status ) {
328 $valid_post_statuses = array( 'draft', 'publish', 'private' );
329 if ( in_array( $status, $valid_post_statuses ) ) {
330 return new \WP_Error( 'invalid-post-status', 'The status needs to be one of draft, publish, or private.' );
331
332 }
333 return $status;
334 }
335
336 /**
337 * Determines if the item is new.
338 *
339 * @return bool True if new, False otherwise.
340 */
341 public function is_new() {
342 return isset( $this->data['_is_new'] ) && $this->data['_is_new'];
343 }
344 }
345