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