summaryrefslogtreecommitdiff
blob: 418cfb16a69c9689faafbb168edee1d6f7d10250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php

abstract class WPCOM_JSON_API_Post_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
	public $post_object_format = array(
		// explicitly document and cast all output
		'ID'                => '(int) The post ID.',
		'site_ID'           => '(int) The site ID.',
		'author'            => '(object>author) The author of the post.',
		'date'              => "(ISO 8601 datetime) The post's creation time.",
		'modified'          => "(ISO 8601 datetime) The post's most recent update time.",
		'title'             => '(HTML) <code>context</code> dependent.',
		'URL'               => '(URL) The full permalink URL to the post.',
		'short_URL'         => '(URL) The wp.me short URL.',
		'content'           => '(HTML) <code>context</code> dependent.',
		'excerpt'           => '(HTML) <code>context</code> dependent.',
		'slug'              => '(string) The name (slug) for the post, used in URLs.',
		'guid'              => '(string) The GUID for the post.',
		'status'            => array(
			'publish'           => 'The post is published.',
			'draft'             => 'The post is saved as a draft.',
			'pending'           => 'The post is pending editorial approval.',
			'private'           => 'The post is published privately',
			'future'            => 'The post is scheduled for future publishing.',
			'trash'             => 'The post is in the trash.',
			'auto-draft'        => 'The post is a placeholder for a new post.',
		),
		'sticky'            => '(bool) Is the post sticky?',
		'password'          => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',
		'parent'            => "(object>post_reference|false) A reference to the post's parent, if it has one.",
		'type'              => "(string) The post's post_type. Post types besides post, page and revision need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.",
		'discussion'        => '(object) Hash of discussion options for the post',
		'likes_enabled'     => "(bool) Is the post open to likes?",
		'sharing_enabled'   => "(bool) Should sharing buttons show on this post?",
		'like_count'        => '(int) The number of likes for this post.',
		'i_like'            => '(bool) Does the current user like this post?',
		'is_reblogged'      => '(bool) Did the current user reblog this post?',
		'is_following'      => '(bool) Is the current user following this blog?',
		'global_ID'         => '(string) A unique WordPress.com-wide representation of a post.',
		'featured_image'    => '(URL) The URL to the featured image for this post if it has one.',
		'post_thumbnail'    => '(object>attachment) The attachment object for the featured image if it has one.',
		'format'            => array(), // see constructor
		'geo'               => '(object>geo|false)',
		'menu_order'        => '(int) (Pages Only) The order pages should appear in.',
		'page_template'     => '(string) (Pages Only) The page template this page is using.',
		'publicize_URLs'    => '(array:URL) Array of Twitter and Facebook URLs published by this post.',
		'terms'             => '(object) Hash of taxonomy names mapping to a hash of terms keyed by term name.',
		'tags'              => '(object:tag) Hash of tags (keyed by tag name) applied to the post.',
		'categories'        => '(object:category) Hash of categories (keyed by category name) applied to the post.',
		'attachments'       => '(object:attachment) Hash of post attachments (keyed by attachment ID). Returns the most recent 20 attachments. Use the `/sites/$site/media` endpoint to query the attachments beyond the default of 20 that are returned here.',
		'attachment_count'  => '(int) The total number of attachments for this post. Use the `/sites/$site/media` endpoint to query the attachments beyond the default of 20 that are returned here.',
		'metadata'          => '(array) Array of post metadata keys and values. All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are available for authenticated requests with access. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.',
		'meta'              => '(object) API result meta data',
		'capabilities'      => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post',
		'revisions'         => '(array) List of post revision IDs. Only available for posts retrieved with context=edit.',
		'other_URLs'        => '(object) List of URLs for this post. Permalink and slug suggestions.',
	);

	// public $response_format =& $this->post_object_format;

	function __construct( $args ) {
		if ( is_array( $this->post_object_format ) && isset( $this->post_object_format['format'] ) ) {
			$this->post_object_format['format'] = get_post_format_strings();
		}
		if ( !$this->response_format ) {
			$this->response_format =& $this->post_object_format;
		}
		parent::__construct( $args );
	}

	/**
	 * Get a post by a specified field and value
	 *
	 * @param string $field
	 * @param string $field_value
	 * @param string $context Post use context (e.g. 'display')
	 * @return array Post
	 **/
	function get_post_by( $field, $field_value, $context = 'display' ) {

		// validate input
		if ( ! in_array( $field, array( 'ID', 'name' ) ) ) {
			return new WP_Error( 'invalid_field', 'Invalid API FIELD', 400 );
		}

		if ( ! in_array( $context, array( 'display', 'edit' ) ) ) {
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
		}

		if ( 'display' === $context ) {
			$args = $this->query_args();
			if ( isset( $args['content_width'] ) && $args['content_width'] ) {
				$GLOBALS['content_width'] = (int) $args['content_width'];
			}
		}

		if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'wp-windows8' ) ) {
			remove_shortcode( 'gallery', 'gallery_shortcode' );
			add_shortcode( 'gallery', array( &$this, 'win8_gallery_shortcode' ) );
		}

		// fetch SAL post
		$post = $this->get_sal_post_by( $field, $field_value, $context );

		if ( is_wp_error( $post ) ) {
			return $post;
		}

		$GLOBALS['post'] = $post;

		// TODO: not sure where this one should go
		if ( 'display' === $context ) {
			setup_postdata( $post );
		}

		$keys_to_render = array_keys( $this->post_object_format );
		if ( isset( $this->api->query[ 'fields' ] ) ) {
			$limit_to_fields = array_map( 'trim', explode( ',', $this->api->query['fields'] ) );
			$keys_to_render = array_intersect( $keys_to_render, $limit_to_fields );
		}

		// always include some keys because processors require it to validate access
		$keys_to_render = array_unique( array_merge( $keys_to_render, array( 'type', 'status', 'password' ) ) );

		$response = $this->render_response_keys( $post, $context, $keys_to_render );

		unset( $GLOBALS['post'] );

		return $response;
	}

	protected function get_sal_post_by( $field, $field_value, $context ) {
		global $blog_id;

		$site = $this->get_platform()->get_site( $blog_id );

		$post = ( $field === 'name' ) ?
			$site->get_post_by_name( $field_value, $context ) : 
			$site->get_post_by_id( $field_value, $context );

		return $post;
	}

	private function render_response_keys( $post, $context, $keys ) {
		foreach ( $keys as $key ) {
			switch ( $key ) {
			case 'ID' :
				// explicitly cast all output
				$response[$key] = (int) $post->ID;
				break;
			case 'site_ID' :
				$response[$key] = $post->site->get_id();
				break;
			case 'author' :
				$response[$key] = $post->get_author();
				break;
			case 'date' :
				$response[$key] = $post->get_date();
				break;
			case 'modified' :
				$response[$key] = $post->get_modified_date();
				break;
			case 'title' :
				$response[$key] = $post->get_title();
				break;
			case 'URL' :
				$response[$key] = $post->get_url();
				break;
			case 'short_URL' :
				$response[$key] = $post->get_shortlink();
				break;
			case 'content' :
				$response[$key] = $post->get_content();
				break;
			case 'excerpt' :
				$response[$key] = $post->get_excerpt();
				break;
			case 'status' :
				$response[$key] = $post->get_status();
				break;
			case 'sticky' :
				$response[$key] = $post->is_sticky();
				break;
			case 'slug' :
				$response[$key] = $post->get_slug();
				break;
			case 'guid' :
				$response[$key] = $post->get_guid();
				break;
			case 'password' :
				$response[$key] = $post->get_password();
				break;
			case 'parent' : // (object|false)
				$response[$key] = $post->get_parent();
				break;
			case 'type' :
				$response[$key] = $post->get_type();
				break;
			case 'discussion' :
				$response[$key] = $post->get_discussion();
				break;
			case 'likes_enabled' :
				$response[$key] = $post->is_likes_enabled();
				break;
			case 'sharing_enabled' :
				$response[$key] = $post->is_sharing_enabled();
				break;
			case 'like_count' :
				$response[$key] = $post->get_like_count();
				break;
			case 'i_like'     :
				$response[$key] = $post->is_liked();
				break;
			case 'is_reblogged':
				$response[$key] = $post->is_reblogged();
				break;
			case 'is_following':
				$response[$key] = $post->is_following();
				break;
			case 'global_ID':
				$response[$key] = $post->get_global_id();
				break;
			case 'featured_image' :
				$response[$key] = $post->get_featured_image();
				break;
			case 'post_thumbnail' :
				$response[$key] = $post->get_post_thumbnail();
				break;
			case 'format' :
				$response[$key] = $post->get_format();
				break;
			case 'geo' : // (object|false)
				$response[$key] = $post->get_geo();
				break;
			case 'menu_order':
				$response[$key] = $post->get_menu_order();
				break;
			case 'page_template':
				$response[$key] = $post->get_page_template();
				break;
			case 'publicize_URLs' :
				$response[$key] = $post->get_publicize_urls();
				break;
			case 'terms':
				$response[$key] = $post->get_terms();
				break;
			case 'tags' :
				$response[$key] = $post->get_tags();
				break;
			case 'categories':
				$response[$key] = $post->get_categories();
				break;
			case 'attachments':
				list( $attachments, $attachment_count ) = $post->get_attachments_and_count();
				$response[$key] = $attachments;
				$response['attachment_count'] = $attachment_count;
				break;
			case 'metadata' : // (array|false)
				$response[$key] = $post->get_metadata();
				break;
			case 'meta' :
				$response[$key] = $post->get_meta();
				break;
			case 'capabilities' :
				$response[$key] = $post->get_current_user_capabilities();
				break;
			case 'revisions' :
				$revisions = $post->get_revisions();
				if ( $revisions ) {
					$response[$key] = $revisions;
				}
				break;
			case 'other_URLs' :
				$response[$key] = $post->get_other_urls();
				break;
			}
		}

		return $response;
	}

	// TODO: factor this out
	function get_blog_post( $blog_id, $post_id, $context = 'display' ) {
		$blog_id = $this->api->get_blog_id( $blog_id );
		if ( !$blog_id || is_wp_error( $blog_id ) ) {
			return $blog_id;
		}
		switch_to_blog( $blog_id );
		$post = $this->get_post_by( 'ID', $post_id, $context );
		restore_current_blog();
		return $post;
	}

	function win8_gallery_shortcode( $attr ) {
		global $post;

		static $instance = 0;
		$instance++;

		$output = '';

		// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
		if ( isset( $attr['orderby'] ) ) {
			$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
			if ( !$attr['orderby'] )
				unset( $attr['orderby'] );
		}

		extract( shortcode_atts( array(
			'order'     => 'ASC',
			'orderby'   => 'menu_order ID',
			'id'        => $post->ID,
			'include'   => '',
			'exclude'   => '',
			'slideshow' => false
		), $attr, 'gallery' ) );

		// Custom image size and always use it
		add_image_size( 'win8app-column', 480 );
		$size = 'win8app-column';

		$id = intval( $id );
		if ( 'RAND' === $order )
			$orderby = 'none';

		if ( !empty( $include ) ) {
			$include      = preg_replace( '/[^0-9,]+/', '', $include );
			$_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
			$attachments  = array();
			foreach ( $_attachments as $key => $val ) {
				$attachments[$val->ID] = $_attachments[$key];
			}
		} elseif ( !empty( $exclude ) ) {
			$exclude     = preg_replace( '/[^0-9,]+/', '', $exclude );
			$attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
		} else {
			$attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
		}

		if ( ! empty( $attachments ) ) {
			foreach ( $attachments as $id => $attachment ) {
				$link = isset( $attr['link'] ) && 'file' === $attr['link'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false );

				if ( $captiontag && trim($attachment->post_excerpt) ) {
					$output .= "<div class='wp-caption aligncenter'>$link
						<p class='wp-caption-text'>" . wptexturize($attachment->post_excerpt) . "</p>
						</div>";
				} else {
					$output .= $link . ' ';
				}
			}
		}
	}
}