/wp-includes/post-formats.php

  1. <?php 
  2. /** 
  3. * Post format functions. 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Post 
  7. */ 
  8.  
  9. /** 
  10. * Retrieve the format slug for a post 
  11. * 
  12. * @since 3.1.0 
  13. * 
  14. * @param int|object|null $post Post ID or post object. Optional, default is the current post from the loop. 
  15. * @return string|false The format if successful. False otherwise. 
  16. */ 
  17. function get_post_format( $post = null ) { 
  18. if ( ! $post = get_post( $post ) ) 
  19. return false; 
  20.  
  21. if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) 
  22. return false; 
  23.  
  24. $_format = get_the_terms( $post->ID, 'post_format' ); 
  25.  
  26. if ( empty( $_format ) ) 
  27. return false; 
  28.  
  29. $format = reset( $_format ); 
  30.  
  31. return str_replace('post-format-', '', $format->slug ); 
  32.  
  33. /** 
  34. * Check if a post has any of the given formats, or any format. 
  35. * 
  36. * @since 3.1.0 
  37. * 
  38. * @param string|array $format Optional. The format or formats to check. 
  39. * @param object|int|null $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop. 
  40. * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise. 
  41. */ 
  42. function has_post_format( $format = array(), $post = null ) { 
  43. $prefixed = array(); 
  44.  
  45. if ( $format ) { 
  46. foreach ( (array) $format as $single ) { 
  47. $prefixed[] = 'post-format-' . sanitize_key( $single ); 
  48.  
  49. return has_term( $prefixed, 'post_format', $post ); 
  50.  
  51. /** 
  52. * Assign a format to a post 
  53. * 
  54. * @since 3.1.0 
  55. * 
  56. * @param int|object $post The post for which to assign a format. 
  57. * @param string $format A format to assign. Use an empty string or array to remove all formats from the post. 
  58. * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success. 
  59. */ 
  60. function set_post_format( $post, $format ) { 
  61. $post = get_post( $post ); 
  62.  
  63. if ( empty( $post ) ) 
  64. return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); 
  65.  
  66. if ( ! empty( $format ) ) { 
  67. $format = sanitize_key( $format ); 
  68. if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) ) 
  69. $format = ''; 
  70. else 
  71. $format = 'post-format-' . $format; 
  72.  
  73. return wp_set_post_terms( $post->ID, $format, 'post_format' ); 
  74.  
  75. /** 
  76. * Returns an array of post format slugs to their translated and pretty display versions 
  77. * 
  78. * @since 3.1.0 
  79. * 
  80. * @return array The array of translated post format names. 
  81. */ 
  82. function get_post_format_strings() { 
  83. $strings = array( 
  84. 'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard 
  85. 'aside' => _x( 'Aside', 'Post format' ),  
  86. 'chat' => _x( 'Chat', 'Post format' ),  
  87. 'gallery' => _x( 'Gallery', 'Post format' ),  
  88. 'link' => _x( 'Link', 'Post format' ),  
  89. 'image' => _x( 'Image', 'Post format' ),  
  90. 'quote' => _x( 'Quote', 'Post format' ),  
  91. 'status' => _x( 'Status', 'Post format' ),  
  92. 'video' => _x( 'Video', 'Post format' ),  
  93. 'audio' => _x( 'Audio', 'Post format' ),  
  94. ); 
  95. return $strings; 
  96.  
  97. /** 
  98. * Retrieves an array of post format slugs. 
  99. * 
  100. * @since 3.1.0 
  101. * 
  102. * @return array The array of post format slugs. 
  103. */ 
  104. function get_post_format_slugs() { 
  105. $slugs = array_keys( get_post_format_strings() ); 
  106. return array_combine( $slugs, $slugs ); 
  107.  
  108. /** 
  109. * Returns a pretty, translated version of a post format slug 
  110. * 
  111. * @since 3.1.0 
  112. * 
  113. * @param string $slug A post format slug. 
  114. * @return string The translated post format name. 
  115. */ 
  116. function get_post_format_string( $slug ) { 
  117. $strings = get_post_format_strings(); 
  118. if ( !$slug ) 
  119. return $strings['standard']; 
  120. else 
  121. return ( isset( $strings[$slug] ) ) ? $strings[$slug] : ''; 
  122.  
  123. /** 
  124. * Returns a link to a post format index. 
  125. * 
  126. * @since 3.1.0 
  127. * 
  128. * @param string $format The post format slug. 
  129. * @return string|WP_Error|false The post format term link. 
  130. */ 
  131. function get_post_format_link( $format ) { 
  132. $term = get_term_by('slug', 'post-format-' . $format, 'post_format' ); 
  133. if ( ! $term || is_wp_error( $term ) ) 
  134. return false; 
  135. return get_term_link( $term ); 
  136.  
  137. /** 
  138. * Filters the request to allow for the format prefix. 
  139. * 
  140. * @access private 
  141. * @since 3.1.0 
  142. * 
  143. * @param array $qvs 
  144. * @return array 
  145. */ 
  146. function _post_format_request( $qvs ) { 
  147. if ( ! isset( $qvs['post_format'] ) ) 
  148. return $qvs; 
  149. $slugs = get_post_format_slugs(); 
  150. if ( isset( $slugs[ $qvs['post_format'] ] ) ) 
  151. $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ]; 
  152. $tax = get_taxonomy( 'post_format' ); 
  153. if ( ! is_admin() ) 
  154. $qvs['post_type'] = $tax->object_type; 
  155. return $qvs; 
  156.  
  157. /** 
  158. * Filters the post format term link to remove the format prefix. 
  159. * 
  160. * @access private 
  161. * @since 3.1.0 
  162. * 
  163. * @global WP_Rewrite $wp_rewrite 
  164. * 
  165. * @param string $link 
  166. * @param object $term 
  167. * @param string $taxonomy 
  168. * @return string 
  169. */ 
  170. function _post_format_link( $link, $term, $taxonomy ) { 
  171. global $wp_rewrite; 
  172. if ( 'post_format' != $taxonomy ) { 
  173. return $link; 
  174. if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) { 
  175. return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link ); 
  176. } else { 
  177. $link = remove_query_arg( 'post_format', $link ); 
  178. return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link ); 
  179.  
  180. /** 
  181. * Remove the post format prefix from the name property of the term object created by get_term(). 
  182. * 
  183. * @access private 
  184. * @since 3.1.0 
  185. * 
  186. * @param object $term 
  187. * @return object 
  188. */ 
  189. function _post_format_get_term( $term ) { 
  190. if ( isset( $term->slug ) ) { 
  191. $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); 
  192. return $term; 
  193.  
  194. /** 
  195. * Remove the post format prefix from the name property of the term objects created by get_terms(). 
  196. * 
  197. * @access private 
  198. * @since 3.1.0 
  199. * 
  200. * @param array $terms 
  201. * @param string|array $taxonomies 
  202. * @param array $args 
  203. * @return array 
  204. */ 
  205. function _post_format_get_terms( $terms, $taxonomies, $args ) { 
  206. if ( in_array( 'post_format', (array) $taxonomies ) ) { 
  207. if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) { 
  208. foreach ( $terms as $order => $name ) { 
  209. $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) ); 
  210. } else { 
  211. foreach ( (array) $terms as $order => $term ) { 
  212. if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) { 
  213. $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); 
  214. return $terms; 
  215.  
  216. /** 
  217. * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms(). 
  218. * 
  219. * @access private 
  220. * @since 3.1.0 
  221. * 
  222. * @param array $terms 
  223. * @return array 
  224. */ 
  225. function _post_format_wp_get_object_terms( $terms ) { 
  226. foreach ( (array) $terms as $order => $term ) { 
  227. if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) { 
  228. $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); 
  229. return $terms; 
.