WPCOM_JSON_API_Post_v1_1_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Post v1 1 Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-post-v1-1-endpoint.php  
  1. abstract class WPCOM_JSON_API_Post_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. public $post_object_format = array( 
  3. // explicitly document and cast all output 
  4. 'ID' => '(int) The post ID.',  
  5. 'site_ID' => '(int) The site ID.',  
  6. 'author' => '(object>author) The author of the post.',  
  7. 'date' => "(ISO 8601 datetime) The post's creation time.",  
  8. 'modified' => "(ISO 8601 datetime) The post's most recent update time.",  
  9. 'title' => '(HTML) <code>context</code> dependent.',  
  10. 'URL' => '(URL) The full permalink URL to the post.',  
  11. 'short_URL' => '(URL) The wp.me short URL.',  
  12. 'content' => '(HTML) <code>context</code> dependent.',  
  13. 'excerpt' => '(HTML) <code>context</code> dependent.',  
  14. 'slug' => '(string) The name (slug) for the post, used in URLs.',  
  15. 'guid' => '(string) The GUID for the post.',  
  16. 'status' => array( 
  17. 'publish' => 'The post is published.',  
  18. 'draft' => 'The post is saved as a draft.',  
  19. 'pending' => 'The post is pending editorial approval.',  
  20. 'private' => 'The post is published privately',  
  21. 'future' => 'The post is scheduled for future publishing.',  
  22. 'trash' => 'The post is in the trash.',  
  23. 'auto-draft' => 'The post is a placeholder for a new post.',  
  24. ),  
  25. 'sticky' => '(bool) Is the post sticky?',  
  26. 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',  
  27. 'parent' => "(object>post_reference|false) A reference to the post's parent, if it has one.",  
  28. '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.",  
  29. 'discussion' => '(object) Hash of discussion options for the post',  
  30. 'likes_enabled' => "(bool) Is the post open to likes?",  
  31. 'sharing_enabled' => "(bool) Should sharing buttons show on this post?",  
  32. 'like_count' => '(int) The number of likes for this post.',  
  33. 'i_like' => '(bool) Does the current user like this post?',  
  34. 'is_reblogged' => '(bool) Did the current user reblog this post?',  
  35. 'is_following' => '(bool) Is the current user following this blog?',  
  36. 'global_ID' => '(string) A unique WordPress.com-wide representation of a post.',  
  37. 'featured_image' => '(URL) The URL to the featured image for this post if it has one.',  
  38. 'post_thumbnail' => '(object>attachment) The attachment object for the featured image if it has one.',  
  39. 'format' => array(), // see constructor 
  40. 'geo' => '(object>geo|false)',  
  41. 'menu_order' => '(int) (Pages Only) The order pages should appear in.',  
  42. 'page_template' => '(string) (Pages Only) The page template this page is using.',  
  43. 'publicize_URLs' => '(array:URL) Array of Twitter and Facebook URLs published by this post.',  
  44. 'tags' => '(object:tag) Hash of tags (keyed by tag name) applied to the post.',  
  45. 'categories' => '(object:category) Hash of categories (keyed by category name) applied to the post.',  
  46. '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.',  
  47. '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.',  
  48. '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.',  
  49. 'meta' => '(object) API result meta data',  
  50. 'capabilities' => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post',  
  51. 'other_URLs' => '(object) List of URLs for this post. Permalink and slug suggestions.',  
  52. ); 
  53.  
  54. // public $response_format =& $this->post_object_format; 
  55.  
  56. function __construct( $args ) { 
  57. if ( is_array( $this->post_object_format ) && isset( $this->post_object_format['format'] ) ) { 
  58. $this->post_object_format['format'] = get_post_format_strings(); 
  59. if ( !$this->response_format ) { 
  60. $this->response_format =& $this->post_object_format; 
  61. parent::__construct( $args ); 
  62.  
  63. function is_metadata_public( $key ) { 
  64. if ( empty( $key ) ) 
  65. return false; 
  66.  
  67. // Default whitelisted meta keys. 
  68. $whitelisted_meta = array( '_thumbnail_id' ); 
  69.  
  70. // whitelist of metadata that can be accessed 
  71. /** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */ 
  72. if ( in_array( $key, apply_filters( 'rest_api_allowed_public_metadata', $whitelisted_meta ) ) ) 
  73. return true; 
  74.  
  75. if ( 0 === strpos( $key, 'geo_' ) ) 
  76. return true; 
  77.  
  78. if ( 0 === strpos( $key, '_wpas_' ) ) 
  79. return true; 
  80.  
  81. return false; 
  82.  
  83. function the_password_form() { 
  84. return __( 'This post is password protected.', 'jetpack' ); 
  85.  
  86. /** 
  87. * Get a post by a specified field and value 
  88. * @param string $field 
  89. * @param string $field_value 
  90. * @param string $context Post use context (e.g. 'display') 
  91. * @return array Post 
  92. **/ 
  93. function get_post_by( $field, $field_value, $context = 'display' ) { 
  94. global $blog_id; 
  95.  
  96. /** This filter is documented in class.json-api-endpoints.php */ 
  97. $is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id ); 
  98.  
  99. if ( defined( 'GEO_LOCATION__CLASS' ) && class_exists( GEO_LOCATION__CLASS ) ) { 
  100. $geo = call_user_func( array( GEO_LOCATION__CLASS, 'init' ) ); 
  101. } else { 
  102. $geo = false; 
  103.  
  104. if ( 'display' === $context ) { 
  105. $args = $this->query_args(); 
  106. if ( isset( $args['content_width'] ) && $args['content_width'] ) { 
  107. $GLOBALS['content_width'] = (int) $args['content_width']; 
  108.  
  109. if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'wp-windows8' ) ) { 
  110. remove_shortcode( 'gallery', 'gallery_shortcode' ); 
  111. add_shortcode( 'gallery', array( &$this, 'win8_gallery_shortcode' ) ); 
  112.  
  113. switch ( $field ) { 
  114. case 'name' : 
  115. $post_id = $this->get_post_id_by_name( $field_value ); 
  116. if ( is_wp_error( $post_id ) ) { 
  117. return $post_id; 
  118. break; 
  119. default : 
  120. $post_id = (int) $field_value; 
  121. break; 
  122.  
  123. $post = get_post( $post_id, OBJECT, $context ); 
  124.  
  125. if ( !$post || is_wp_error( $post ) ) { 
  126. return new WP_Error( 'unknown_post', 'Unknown post', 404 ); 
  127.  
  128. if ( ! $this->is_post_type_allowed( $post->post_type ) && ( ! function_exists( 'is_post_freshly_pressed' ) || ! is_post_freshly_pressed( $post->ID ) ) ) { 
  129. return new WP_Error( 'unknown_post', 'Unknown post', 404 ); 
  130.  
  131. // Permissions 
  132. $capabilities = $this->get_current_user_capabilities( $post ); 
  133.  
  134. switch ( $context ) { 
  135. case 'edit' : 
  136. if ( ! $capabilities['edit_post'] ) { 
  137. return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); 
  138. break; 
  139. case 'display' : 
  140. break; 
  141. default : 
  142. return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 ); 
  143.  
  144. $can_view = $this->user_can_view_post( $post->ID ); 
  145. if ( !$can_view || is_wp_error( $can_view ) ) { 
  146. return $can_view; 
  147.  
  148. $GLOBALS['post'] = $post; 
  149.  
  150. if ( 'display' === $context ) { 
  151. setup_postdata( $post ); 
  152.  
  153. $response = array(); 
  154. foreach ( array_keys( $this->post_object_format ) as $key ) { 
  155. switch ( $key ) { 
  156. case 'ID' : 
  157. // explicitly cast all output 
  158. $response[$key] = (int) $post->ID; 
  159. break; 
  160. case 'site_ID' : 
  161. $response[$key] = (int) $this->api->get_blog_id_for_output(); 
  162. break; 
  163. case 'author' : 
  164. $response[$key] = (object) $this->get_author( $post, 'edit' === $context && $capabilities['edit_post'] ); 
  165. break; 
  166. case 'date' : 
  167. $response[$key] = (string) $this->format_date( $post->post_date_gmt, $post->post_date ); 
  168. break; 
  169. case 'modified' : 
  170. $response[$key] = (string) $this->format_date( $post->post_modified_gmt, $post->post_modified ); 
  171. break; 
  172. case 'title' : 
  173. if ( 'display' === $context ) { 
  174. $response[$key] = (string) get_the_title( $post->ID ); 
  175. } else { 
  176. $response[$key] = (string) htmlspecialchars_decode( $post->post_title, ENT_QUOTES ); 
  177. break; 
  178. case 'URL' : 
  179. if ( 'revision' === $post->post_type ) { 
  180. $response[$key] = (string) esc_url_raw( get_permalink( $post->post_parent ) ); 
  181. } else { 
  182. $response[$key] = (string) esc_url_raw( get_permalink( $post->ID ) ); 
  183. break; 
  184. case 'short_URL' : 
  185. $response[$key] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) ); 
  186. break; 
  187. case 'content' : 
  188. if ( 'display' === $context ) { 
  189. add_filter( 'the_password_form', array( $this, 'the_password_form' ) ); 
  190. $response[$key] = (string) $this->get_the_post_content_for_display(); 
  191. remove_filter( 'the_password_form', array( $this, 'the_password_form' ) ); 
  192. } else { 
  193. $response[$key] = (string) $post->post_content; 
  194. break; 
  195. case 'excerpt' : 
  196. if ( 'display' === $context ) { 
  197. add_filter( 'the_password_form', array( $this, 'the_password_form' ) ); 
  198. ob_start(); 
  199. the_excerpt(); 
  200. $response[$key] = (string) ob_get_clean(); 
  201. remove_filter( 'the_password_form', array( $this, 'the_password_form' ) ); 
  202. } else { 
  203. $response[$key] = htmlspecialchars_decode( (string) $post->post_excerpt, ENT_QUOTES ); 
  204. break; 
  205. case 'status' : 
  206. $response[$key] = (string) get_post_status( $post->ID ); 
  207. break; 
  208. case 'sticky' : 
  209. $response[$key] = (bool) is_sticky( $post->ID ); 
  210. break; 
  211. case 'slug' : 
  212. $response[$key] = (string) $post->post_name; 
  213. break; 
  214. case 'guid' : 
  215. $response[$key] = (string) $post->guid; 
  216. break; 
  217. case 'password' : 
  218. $response[$key] = (string) $post->post_password; 
  219. if ( 'edit' === $context ) { 
  220. $response[$key] = htmlspecialchars_decode( (string) $response[$key], ENT_QUOTES ); 
  221. break; 
  222. case 'parent' : // (object|false) 
  223. if ( $post->post_parent ) { 
  224. $parent = get_post( $post->post_parent ); 
  225. if ( 'display' === $context ) { 
  226. $parent_title = (string) get_the_title( $parent->ID ); 
  227. } else { 
  228. $parent_title = (string) htmlspecialchars_decode( $post->post_title, ENT_QUOTES ); 
  229. $response[$key] = (object) array( 
  230. 'ID' => (int) $parent->ID,  
  231. 'type' => (string) $parent->post_type,  
  232. 'link' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $parent->ID ),  
  233. 'title' => $parent_title,  
  234. ); 
  235. } else { 
  236. $response[$key] = false; 
  237. break; 
  238. case 'type' : 
  239. $response[$key] = (string) $post->post_type; 
  240. break; 
  241. case 'discussion' : 
  242. $response[$key] = array( 
  243. 'comments_open' => (bool) comments_open( $post->ID ),  
  244. 'comment_status' => (string) $post->comment_status,  
  245. 'pings_open' => (bool) pings_open( $post->ID ),  
  246. 'ping_status' => (string) $post->ping_status,  
  247. 'comment_count' => (int) $post->comment_count,  
  248. ); 
  249. break; 
  250. case 'likes_enabled' : 
  251. /** This filter is documented in modules/likes.php */ 
  252. $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); 
  253. $post_likes_switched = (bool) get_post_meta( $post->ID, 'switch_like_status', true ); 
  254. $post_likes_enabled = $sitewide_likes_enabled; 
  255. if ( $post_likes_switched ) { 
  256. $post_likes_enabled = ! $post_likes_enabled; 
  257. $response[$key] = (bool) $post_likes_enabled; 
  258. break; 
  259. case 'sharing_enabled' : 
  260. $show = true; 
  261. /** This filter is documented in modules/sharedaddy/sharing-service.php */ 
  262. $show = apply_filters( 'sharing_show', $show, $post ); 
  263.  
  264. $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false ); 
  265.  
  266. if ( !empty( $switched_status ) ) 
  267. $show = false; 
  268. $response[$key] = (bool) $show; 
  269. break; 
  270. case 'like_count' : 
  271. $response[$key] = (int) $this->api->post_like_count( $blog_id, $post->ID ); 
  272. break; 
  273. case 'i_like' : 
  274. $response[$key] = (int) $this->api->is_liked( $blog_id, $post->ID ); 
  275. break; 
  276. case 'is_reblogged': 
  277. $response[$key] = (int) $this->api->is_reblogged( $blog_id, $post->ID ); 
  278. break; 
  279. case 'is_following': 
  280. $response[$key] = (int) $this->api->is_following( $blog_id ); 
  281. break; 
  282. case 'global_ID': 
  283. $response[$key] = (string) $this->api->add_global_ID( $blog_id, $post->ID ); 
  284. break; 
  285. case 'featured_image' : 
  286. if ( $is_jetpack && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { 
  287. $response[ $key ] = get_post_meta( $post->ID, '_jetpack_featured_image', true ); 
  288. } else { 
  289. $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); 
  290. if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { 
  291. $response[ $key ] = (string) $image_attributes[0]; 
  292. } else { 
  293. $response[ $key ] = ''; 
  294. break; 
  295. case 'post_thumbnail' : 
  296. $response[$key] = null; 
  297.  
  298. $thumb_id = get_post_thumbnail_id( $post->ID ); 
  299. if ( ! empty( $thumb_id ) ) { 
  300. $attachment = get_post( $thumb_id ); 
  301. if ( ! empty( $attachment ) ) 
  302. $featured_image_object = $this->get_attachment( $attachment ); 
  303.  
  304. if ( ! empty( $featured_image_object ) ) { 
  305. $response[$key] = (object) $featured_image_object; 
  306. break; 
  307. case 'format' : 
  308. $response[$key] = (string) get_post_format( $post->ID ); 
  309. if ( !$response[$key] ) { 
  310. $response[$key] = 'standard'; 
  311. break; 
  312. case 'geo' : // (object|false) 
  313. if ( !$geo ) { 
  314. $response[$key] = false; 
  315. } else { 
  316. $geo_data = $geo->get_geo( 'post', $post->ID ); 
  317. $response[$key] = false; 
  318. if ( $geo_data ) { 
  319. $geo_data = array_intersect_key( $geo_data, array( 'latitude' => true, 'longitude' => true, 'address' => true, 'public' => true ) ); 
  320. if ( $geo_data ) { 
  321. $response[$key] = (object) array( 
  322. 'latitude' => isset( $geo_data['latitude'] ) ? (float) $geo_data['latitude'] : 0,  
  323. 'longitude' => isset( $geo_data['longitude'] ) ? (float) $geo_data['longitude'] : 0,  
  324. 'address' => isset( $geo_data['address'] ) ? (string) $geo_data['address'] : '',  
  325. ); 
  326. } else { 
  327. $response[$key] = false; 
  328. // Private 
  329. if ( !isset( $geo_data['public'] ) || !$geo_data['public'] ) { 
  330. if ( 'edit' !== $context || ! $capabilities['edit_post'] ) { 
  331. // user can't access 
  332. $response[$key] = false; 
  333. break; 
  334. case 'menu_order': 
  335. $response[$key] = (int) $post->menu_order; 
  336. break; 
  337. case 'page_template': 
  338. $response[$key] = (string) get_post_meta( $post->ID, '_wp_page_template', true ); 
  339. break; 
  340. case 'publicize_URLs' : 
  341. $publicize_URLs = array(); 
  342. $publicize = get_post_meta( $post->ID, 'publicize_results', true ); 
  343. if ( $publicize ) { 
  344. foreach ( $publicize as $service => $data ) { 
  345. switch ( $service ) { 
  346. case 'twitter' : 
  347. foreach ( $data as $datum ) { 
  348. $publicize_URLs[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" ); 
  349. break; 
  350. case 'fb' : 
  351. foreach ( $data as $datum ) { 
  352. $publicize_URLs[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" ); 
  353. break; 
  354. $response[$key] = (array) $publicize_URLs; 
  355. break; 
  356. case 'tags' : 
  357. $response[$key] = array(); 
  358. $terms = wp_get_post_tags( $post->ID ); 
  359. foreach ( $terms as $term ) { 
  360. if ( !empty( $term->name ) ) { 
  361. $response[$key][$term->name] = $this->format_taxonomy( $term, 'post_tag', 'display' ); 
  362. $response[$key] = (object) $response[$key]; 
  363. break; 
  364. case 'categories': 
  365. $response[$key] = array(); 
  366. $terms = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'all' ) ); 
  367. foreach ( $terms as $term ) { 
  368. if ( !empty( $term->name ) ) { 
  369. $response[$key][$term->name] = $this->format_taxonomy( $term, 'category', 'display' ); 
  370. $response[$key] = (object) $response[$key]; 
  371. break; 
  372. case 'attachments': 
  373. $response[$key] = array(); 
  374. $_attachments = new WP_Query( array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => '20' ) ); 
  375. foreach ( $_attachments->posts as $attachment ) { 
  376. $response[$key][$attachment->ID] = $this->get_media_item_v1_1( $attachment->ID ); 
  377. $response['attachment_count'] = $_attachments->found_posts; 
  378. $response[$key] = (object) $response[$key]; 
  379. break; 
  380. case 'metadata' : // (array|false) 
  381. $metadata = array(); 
  382. foreach ( (array) has_meta( $post_id ) as $meta ) { 
  383. // Don't expose protected fields. 
  384. $show = false; 
  385. if ( $this->is_metadata_public( $meta['meta_key'] ) ) 
  386. $show = true; 
  387. if ( current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) ) 
  388. $show = true; 
  389.  
  390. if ( !$show ) 
  391. continue; 
  392.  
  393. $metadata[] = array( 
  394. 'id' => $meta['meta_id'],  
  395. 'key' => $meta['meta_key'],  
  396. 'value' => maybe_unserialize( $meta['meta_value'] ),  
  397. ); 
  398.  
  399. if ( ! empty( $metadata ) ) { 
  400. $response[$key] = $metadata; 
  401. } else { 
  402. $response[$key] = false; 
  403. break; 
  404. case 'meta' : 
  405. $response[$key] = (object) array( 
  406. 'links' => (object) array( 
  407. 'self' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),  
  408. 'help' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'help' ),  
  409. 'site' => (string) $this->get_site_link( $this->api->get_blog_id_for_output() ),  
  410. // 'author' => (string) $this->get_user_link( $post->post_author ),  
  411. // 'via' => (string) $this->get_post_link( $reblog_origin_blog_id, $reblog_origin_post_id ),  
  412. 'replies' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'replies/' ),  
  413. 'likes' => (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'likes/' ),  
  414. ),  
  415. ); 
  416.  
  417. // add autosave link if a more recent autosave exists 
  418. if ( 'edit' === $context ) { 
  419. $autosave = wp_get_post_autosave( $post_id ); 
  420. if ( $autosave && $autosave->post_modified > $post->post_modified ) 
  421. $response[$key]->links->autosave = (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ) . '/autosave'; 
  422.  
  423. break; 
  424. case 'capabilities' : 
  425. $response[$key] = $capabilities; 
  426. break; 
  427. case 'other_URLs' : 
  428. $other_urls = array(); 
  429.  
  430. if ( 'publish' !== $post->post_status ) { 
  431. $other_urls = $this->get_post_permalink_suggestions( $post->ID, $post->post_title ); 
  432.  
  433. $response[$key] = (object) $other_urls; 
  434. break; 
  435.  
  436. // WPCOM_JSON_API_Post_Endpoint::find_featured_worthy_media( $post ); 
  437. // $response['featured_media'] = self::find_featured_media( $response ); 
  438.  
  439. unset( $GLOBALS['post'] ); 
  440. return $response; 
  441.  
  442. // No Blog ID parameter. No Post ID parameter. Depends on globals. 
  443. // Expects setup_postdata() to already have been run 
  444. function get_the_post_content_for_display() { 
  445. global $pages, $page; 
  446.  
  447. $old_pages = $pages; 
  448. $old_page = $page; 
  449.  
  450. $content = join( "\n\n", $pages ); 
  451. $content = preg_replace( '/<!--more(.*?)?-->/', '', $content ); 
  452. $pages = array( $content ); 
  453. $page = 1; 
  454.  
  455. ob_start(); 
  456. the_content(); 
  457. $return = ob_get_clean(); 
  458.  
  459. $pages = $old_pages; 
  460. $page = $old_page; 
  461.  
  462. return $return; 
  463.  
  464. function get_blog_post( $blog_id, $post_id, $context = 'display' ) { 
  465. $blog_id = $this->api->get_blog_id( $blog_id ); 
  466. if ( !$blog_id || is_wp_error( $blog_id ) ) { 
  467. return $blog_id; 
  468. switch_to_blog( $blog_id ); 
  469. $post = $this->get_post_by( 'ID', $post_id, $context ); 
  470. restore_current_blog(); 
  471. return $post; 
  472.  
  473. /** 
  474. * Supporting featured media in post endpoints. Currently on for wpcom blogs 
  475. * since it's calling WPCOM_JSON_API_Read_Endpoint methods which presently 
  476. * rely on wpcom specific functionality. 
  477. * @param WP_Post $post 
  478. * @return object list of featured media 
  479. */ 
  480. public static function find_featured_media( &$post ) { 
  481.  
  482. if ( class_exists( 'WPCOM_JSON_API_Read_Endpoint' ) ) { 
  483. return WPCOM_JSON_API_Read_Endpoint::find_featured_worthy_media( (array) $post ); 
  484. } else { 
  485. return (object) array(); 
  486.  
  487.  
  488.  
  489.  
  490. function win8_gallery_shortcode( $attr ) { 
  491. global $post; 
  492.  
  493. static $instance = 0; 
  494. $instance++; 
  495.  
  496. $output = ''; 
  497.  
  498. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement 
  499. if ( isset( $attr['orderby'] ) ) { 
  500. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); 
  501. if ( !$attr['orderby'] ) 
  502. unset( $attr['orderby'] ); 
  503.  
  504. extract( shortcode_atts( array( 
  505. 'order' => 'ASC',  
  506. 'orderby' => 'menu_order ID',  
  507. 'id' => $post->ID,  
  508. 'include' => '',  
  509. 'exclude' => '',  
  510. 'slideshow' => false 
  511. ), $attr, 'gallery' ) ); 
  512.  
  513. // Custom image size and always use it 
  514. add_image_size( 'win8app-column', 480 ); 
  515. $size = 'win8app-column'; 
  516.  
  517. $id = intval( $id ); 
  518. if ( 'RAND' === $order ) 
  519. $orderby = 'none'; 
  520.  
  521. if ( !empty( $include ) ) { 
  522. $include = preg_replace( '/[^0-9, ]+/', '', $include ); 
  523. $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); 
  524. $attachments = array(); 
  525. foreach ( $_attachments as $key => $val ) { 
  526. $attachments[$val->ID] = $_attachments[$key]; 
  527. } elseif ( !empty( $exclude ) ) { 
  528. $exclude = preg_replace( '/[^0-9, ]+/', '', $exclude ); 
  529. $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); 
  530. } else { 
  531. $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); 
  532.  
  533. if ( ! empty( $attachments ) ) { 
  534. foreach ( $attachments as $id => $attachment ) { 
  535. $link = isset( $attr['link'] ) && 'file' === $attr['link'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false ); 
  536.  
  537. if ( $captiontag && trim($attachment->post_excerpt) ) { 
  538. $output .= "<div class='wp-caption aligncenter'>$link 
  539. <p class='wp-caption-text'>" . wptexturize($attachment->post_excerpt) . "</p> 
  540. </div>"; 
  541. } else { 
  542. $output .= $link . ' '; 
  543.  
  544. /** 
  545. * Returns attachment object. 
  546. * @param $attachment attachment row 
  547. * @return (object) 
  548. */ 
  549. function get_attachment( $attachment ) { 
  550. $metadata = wp_get_attachment_metadata( $attachment->ID ); 
  551.  
  552. $result = array( 
  553. 'ID' => (int) $attachment->ID,  
  554. 'URL' => (string) wp_get_attachment_url( $attachment->ID ),  
  555. 'guid' => (string) $attachment->guid,  
  556. 'mime_type' => (string) $attachment->post_mime_type,  
  557. 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,  
  558. 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,  
  559. ); 
  560.  
  561. if ( isset( $metadata['duration'] ) ) { 
  562. $result['duration'] = (int) $metadata['duration']; 
  563.  
  564. /** This filter is documented in class.jetpack-sync.php */ 
  565. return (object) apply_filters( 'get_attachment', $result ); 
  566.  
  567. /** 
  568. * Get post-specific user capabilities 
  569. * @param WP_Post $post post object 
  570. * @return array array of post-level permissions; 'publish_post', 'delete_post', 'edit_post' 
  571. */ 
  572. function get_current_user_capabilities( $post ) { 
  573. return array( 
  574. 'publish_post' => current_user_can( 'publish_post', $post ),  
  575. 'delete_post' => current_user_can( 'delete_post', $post ),  
  576. 'edit_post' => current_user_can( 'edit_post', $post ) 
  577. ); 
  578.  
  579. /** 
  580. * Get extra post permalink suggestions 
  581. * @param int $postID  
  582. * @param string $title  
  583. * @return array array of permalink suggestions: 'permalink_URL', 'suggested_slug' 
  584. */ 
  585. function get_post_permalink_suggestions( $postID, $title ) { 
  586. $suggestions = array(); 
  587. list( $suggestions['permalink_URL'], $suggestions['suggested_slug'] ) = get_sample_permalink( $postID, $title ); 
  588. return $suggestions; 
  589.  
  590. /** 
  591. * Get post ID by name 
  592. * Attempts to match name on post title and page path 
  593. * @param string $name 
  594. * @return int|object Post ID on success, WP_Error object on failure 
  595. **/ 
  596. protected function get_post_id_by_name( $name ) { 
  597. $name = sanitize_title( $name ); 
  598.  
  599. if ( ! $name ) { 
  600. return new WP_Error( 'invalid_post', 'Invalid post', 400 ); 
  601.  
  602. $posts = get_posts( array( 'name' => $name ) ); 
  603.  
  604. if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) { 
  605. $page = get_page_by_path( $name ); 
  606.  
  607. if ( ! $page ) { 
  608. return new WP_Error( 'unknown_post', 'Unknown post', 404 ); 
  609.  
  610. $post_id = $page->ID; 
  611. } else { 
  612. $post_id = (int) $posts[0]->ID; 
  613.  
  614. return $post_id; 
  615.