post-formats.php 6.9 KB

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