post-formats.php 6.9 KB

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