wp/wp-includes/post-formats.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    24 	$_format = get_the_terms( $post->ID, 'post_format' );
    24 	$_format = get_the_terms( $post->ID, 'post_format' );
    25 
    25 
    26 	if ( empty( $_format ) )
    26 	if ( empty( $_format ) )
    27 		return false;
    27 		return false;
    28 
    28 
    29 	$format = array_shift( $_format );
    29 	$format = reset( $_format );
    30 
    30 
    31 	return str_replace('post-format-', '', $format->slug );
    31 	return str_replace('post-format-', '', $format->slug );
    32 }
    32 }
    33 
    33 
    34 /**
    34 /**
    35  * Check if a post has a particular format
    35  * Check if a post has any of the given formats, or any format.
    36  *
    36  *
    37  * @since 3.1.0
    37  * @since 3.1.0
    38  *
    38  *
    39  * @uses has_term()
    39  * @param string|array $format Optional. The format or formats to check.
    40  *
    40  * @param object|int $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop.
    41  * @param string|array $format The format or formats to check.
    41  * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise.
    42  * @param object|int   $post   The post to check. If not supplied, defaults to the current post if used in the loop.
    42  */
    43  * @return bool True if the post has the format, false otherwise.
    43 function has_post_format( $format = array(), $post = null ) {
    44  */
       
    45 function has_post_format( $format, $post = null ) {
       
    46 	if ( ! is_array( $format ) )
       
    47 		$format = array( $format );
       
    48 
       
    49 	$prefixed = array();
    44 	$prefixed = array();
    50 	foreach( $format as $single ) {
    45 
    51 		$prefixed[] = 'post-format-' . sanitize_key( $single );
    46 	if ( $format ) {
       
    47 		foreach ( (array) $format as $single ) {
       
    48 			$prefixed[] = 'post-format-' . sanitize_key( $single );
       
    49 		}
    52 	}
    50 	}
    53 
    51 
    54 	return has_term( $prefixed, 'post_format', $post );
    52 	return has_term( $prefixed, 'post_format', $post );
    55 }
    53 }
    56 
    54 
   106 /**
   104 /**
   107  * Retrieves an array of post format slugs.
   105  * Retrieves an array of post format slugs.
   108  *
   106  *
   109  * @since 3.1.0
   107  * @since 3.1.0
   110  *
   108  *
   111  * @uses get_post_format_strings()
       
   112  *
       
   113  * @return array The array of post format slugs.
   109  * @return array The array of post format slugs.
   114  */
   110  */
   115 function get_post_format_slugs() {
   111 function get_post_format_slugs() {
   116 	$slugs = array_keys( get_post_format_strings() );
   112 	$slugs = array_keys( get_post_format_strings() );
   117 	return array_combine( $slugs, $slugs );
   113 	return array_combine( $slugs, $slugs );
   119 
   115 
   120 /**
   116 /**
   121  * Returns a pretty, translated version of a post format slug
   117  * Returns a pretty, translated version of a post format slug
   122  *
   118  *
   123  * @since 3.1.0
   119  * @since 3.1.0
   124  *
       
   125  * @uses get_post_format_strings()
       
   126  *
   120  *
   127  * @param string $slug A post format slug.
   121  * @param string $slug A post format slug.
   128  * @return string The translated post format name.
   122  * @return string The translated post format name.
   129  */
   123  */
   130 function get_post_format_string( $slug ) {
   124 function get_post_format_string( $slug ) {
   165 	$tax = get_taxonomy( 'post_format' );
   159 	$tax = get_taxonomy( 'post_format' );
   166 	if ( ! is_admin() )
   160 	if ( ! is_admin() )
   167 		$qvs['post_type'] = $tax->object_type;
   161 		$qvs['post_type'] = $tax->object_type;
   168 	return $qvs;
   162 	return $qvs;
   169 }
   163 }
   170 add_filter( 'request', '_post_format_request' );
       
   171 
   164 
   172 /**
   165 /**
   173  * Filters the post format term link to remove the format prefix.
   166  * Filters the post format term link to remove the format prefix.
   174  *
   167  *
   175  * @access private
   168  * @access private
   184 	} else {
   177 	} else {
   185 		$link = remove_query_arg( 'post_format', $link );
   178 		$link = remove_query_arg( 'post_format', $link );
   186 		return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
   179 		return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
   187 	}
   180 	}
   188 }
   181 }
   189 add_filter( 'term_link', '_post_format_link', 10, 3 );
       
   190 
   182 
   191 /**
   183 /**
   192  * Remove the post format prefix from the name property of the term object created by get_term().
   184  * Remove the post format prefix from the name property of the term object created by get_term().
   193  *
   185  *
   194  * @access private
   186  * @access private
   198 	if ( isset( $term->slug ) ) {
   190 	if ( isset( $term->slug ) ) {
   199 		$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   191 		$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   200 	}
   192 	}
   201 	return $term;
   193 	return $term;
   202 }
   194 }
   203 add_filter( 'get_post_format', '_post_format_get_term' );
       
   204 
   195 
   205 /**
   196 /**
   206  * Remove the post format prefix from the name property of the term objects created by get_terms().
   197  * Remove the post format prefix from the name property of the term objects created by get_terms().
   207  *
   198  *
   208  * @access private
   199  * @access private
   222 			}
   213 			}
   223 		}
   214 		}
   224 	}
   215 	}
   225 	return $terms;
   216 	return $terms;
   226 }
   217 }
   227 add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
       
   228 
   218 
   229 /**
   219 /**
   230  * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
   220  * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
   231  *
   221  *
   232  * @access private
   222  * @access private
   238 			$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   228 			$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   239 		}
   229 		}
   240 	}
   230 	}
   241 	return $terms;
   231 	return $terms;
   242 }
   232 }
   243 add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );