You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

255 lines
6.8 KiB

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