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