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