WPCOM_JSON_API_Post_Endpoint

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

Defined (1)

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

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