*
* @since 3.1.0
*
- * @param int|object $post Post ID or post object. Optional, default is the current post from the loop.
- * @return mixed The format if successful. False otherwise.
+ * @param int|object|null $post Post ID or post object. Optional, default is the current post from the loop.
+ * @return string|false The format if successful. False otherwise.
*/
function get_post_format( $post = null ) {
if ( ! $post = get_post( $post ) )
if ( empty( $_format ) )
return false;
- $format = array_shift( $_format );
+ $format = reset( $_format );
return str_replace('post-format-', '', $format->slug );
}
/**
- * Check if a post has a particular format
+ * Check if a post has any of the given formats, or any format.
*
* @since 3.1.0
*
- * @uses has_term()
- *
- * @param string|array $format The format or formats to check.
- * @param object|int $post The post to check. If not supplied, defaults to the current post if used in the loop.
- * @return bool True if the post has the format, false otherwise.
+ * @param string|array $format Optional. The format or formats to check.
+ * @param object|int|null $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop.
+ * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise.
*/
-function has_post_format( $format, $post = null ) {
- if ( ! is_array( $format ) )
- $format = array( $format );
-
+function has_post_format( $format = array(), $post = null ) {
$prefixed = array();
- foreach( $format as $single ) {
- $prefixed[] = 'post-format-' . sanitize_key( $single );
+
+ if ( $format ) {
+ foreach ( (array) $format as $single ) {
+ $prefixed[] = 'post-format-' . sanitize_key( $single );
+ }
}
return has_term( $prefixed, 'post_format', $post );
*
* @since 3.1.0
*
- * @param int|object $post The post for which to assign a format.
- * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
- * @return mixed WP_Error on error. Array of affected term IDs on success.
+ * @param int|object $post The post for which to assign a format.
+ * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
+ * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success.
*/
function set_post_format( $post, $format ) {
$post = get_post( $post );
if ( empty( $post ) )
- return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
+ return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
if ( ! empty( $format ) ) {
$format = sanitize_key( $format );
*
* @since 3.1.0
*
- * @uses get_post_format_strings()
- *
* @return array The array of post format slugs.
*/
function get_post_format_slugs() {
*
* @since 3.1.0
*
- * @uses get_post_format_strings()
- *
* @param string $slug A post format slug.
* @return string The translated post format name.
*/
* @since 3.1.0
*
* @param string $format The post format slug.
- * @return string The post format term link.
+ * @return string|WP_Error|false The post format term link.
*/
function get_post_format_link( $format ) {
$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
*
* @access private
* @since 3.1.0
+ *
+ * @param array $qvs
+ * @return array
*/
function _post_format_request( $qvs ) {
if ( ! isset( $qvs['post_format'] ) )
$qvs['post_type'] = $tax->object_type;
return $qvs;
}
-add_filter( 'request', '_post_format_request' );
/**
* Filters the post format term link to remove the format prefix.
*
* @access private
* @since 3.1.0
+ *
+ * @global WP_Rewrite $wp_rewrite
+ *
+ * @param string $link
+ * @param object $term
+ * @param string $taxonomy
+ * @return string
*/
function _post_format_link( $link, $term, $taxonomy ) {
global $wp_rewrite;
- if ( 'post_format' != $taxonomy )
+ if ( 'post_format' != $taxonomy ) {
return $link;
+ }
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
} else {
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
}
}
-add_filter( 'term_link', '_post_format_link', 10, 3 );
/**
* Remove the post format prefix from the name property of the term object created by get_term().
*
* @access private
* @since 3.1.0
+ *
+ * @param object $term
+ * @return object
*/
function _post_format_get_term( $term ) {
if ( isset( $term->slug ) ) {
}
return $term;
}
-add_filter( 'get_post_format', '_post_format_get_term' );
/**
* Remove the post format prefix from the name property of the term objects created by get_terms().
*
* @access private
* @since 3.1.0
+ *
+ * @param array $terms
+ * @param string|array $taxonomies
+ * @param array $args
+ * @return array
*/
function _post_format_get_terms( $terms, $taxonomies, $args ) {
if ( in_array( 'post_format', (array) $taxonomies ) ) {
}
return $terms;
}
-add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
/**
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
*
* @access private
* @since 3.1.0
+ *
+ * @param array $terms
+ * @return array
*/
function _post_format_wp_get_object_terms( $terms ) {
foreach ( (array) $terms as $order => $term ) {
}
return $terms;
}
-add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );