wp/wp-includes/post-formats.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     9 /**
     9 /**
    10  * Retrieve the format slug for a post
    10  * Retrieve the format slug for a post
    11  *
    11  *
    12  * @since 3.1.0
    12  * @since 3.1.0
    13  *
    13  *
    14  * @param int|object|null $post Post ID or post object. Optional, default is the current post from the loop.
    14  * @param int|object|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.
    15  * @return string|false The format if successful. False otherwise.
    16  */
    16  */
    17 function get_post_format( $post = null ) {
    17 function get_post_format( $post = null ) {
    18 	$post = get_post( $post );
    18 	$post = get_post( $post );
    19 
    19 
    39 /**
    39 /**
    40  * Check if a post has any of the given formats, or any format.
    40  * Check if a post has any of the given formats, or any format.
    41  *
    41  *
    42  * @since 3.1.0
    42  * @since 3.1.0
    43  *
    43  *
    44  * @param string|array    $format Optional. The format or formats to check.
    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.
    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), false otherwise.
    46  * @return bool True if the post has any of the given formats (or any format, if no format specified),
       
    47  *              false otherwise.
    47  */
    48  */
    48 function has_post_format( $format = array(), $post = null ) {
    49 function has_post_format( $format = array(), $post = null ) {
    49 	$prefixed = array();
    50 	$prefixed = array();
    50 
    51 
    51 	if ( $format ) {
    52 	if ( $format ) {
    62  *
    63  *
    63  * @since 3.1.0
    64  * @since 3.1.0
    64  *
    65  *
    65  * @param int|object $post   The post for which to assign a format.
    66  * @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  * @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  * @return array|WP_Error|false Array of affected term IDs on success. WP_Error on error.
    68  */
    69  */
    69 function set_post_format( $post, $format ) {
    70 function set_post_format( $post, $format ) {
    70 	$post = get_post( $post );
    71 	$post = get_post( $post );
    71 
    72 
    72 	if ( ! $post ) {
    73 	if ( ! $post ) {
    73 		return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
    74 		return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
    74 	}
    75 	}
    75 
    76 
    76 	if ( ! empty( $format ) ) {
    77 	if ( ! empty( $format ) ) {
    77 		$format = sanitize_key( $format );
    78 		$format = sanitize_key( $format );
    78 		if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) ) {
    79 		if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs(), true ) ) {
    79 			$format = '';
    80 			$format = '';
    80 		} else {
    81 		} else {
    81 			$format = 'post-format-' . $format;
    82 			$format = 'post-format-' . $format;
    82 		}
    83 		}
    83 	}
    84 	}
    88 /**
    89 /**
    89  * Returns an array of post format slugs to their translated and pretty display versions
    90  * Returns an array of post format slugs to their translated and pretty display versions
    90  *
    91  *
    91  * @since 3.1.0
    92  * @since 3.1.0
    92  *
    93  *
    93  * @return array The array of translated post format names.
    94  * @return string[] Array of post format labels keyed by format slug.
    94  */
    95  */
    95 function get_post_format_strings() {
    96 function get_post_format_strings() {
    96 	$strings = array(
    97 	$strings = array(
    97 		'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
    98 		'standard' => _x( 'Standard', 'Post format' ), // Special case. Any value that evals to false will be considered standard.
    98 		'aside'    => _x( 'Aside', 'Post format' ),
    99 		'aside'    => _x( 'Aside', 'Post format' ),
    99 		'chat'     => _x( 'Chat', 'Post format' ),
   100 		'chat'     => _x( 'Chat', 'Post format' ),
   100 		'gallery'  => _x( 'Gallery', 'Post format' ),
   101 		'gallery'  => _x( 'Gallery', 'Post format' ),
   101 		'link'     => _x( 'Link', 'Post format' ),
   102 		'link'     => _x( 'Link', 'Post format' ),
   102 		'image'    => _x( 'Image', 'Post format' ),
   103 		'image'    => _x( 'Image', 'Post format' ),
   111 /**
   112 /**
   112  * Retrieves the array of post format slugs.
   113  * Retrieves the array of post format slugs.
   113  *
   114  *
   114  * @since 3.1.0
   115  * @since 3.1.0
   115  *
   116  *
   116  * @return array The array of post format slugs as both keys and values.
   117  * @return string[] The array of post format slugs as both keys and values.
   117  */
   118  */
   118 function get_post_format_slugs() {
   119 function get_post_format_slugs() {
   119 	$slugs = array_keys( get_post_format_strings() );
   120 	$slugs = array_keys( get_post_format_strings() );
   120 	return array_combine( $slugs, $slugs );
   121 	return array_combine( $slugs, $slugs );
   121 }
   122 }
   181  * Filters the post format term link to remove the format prefix.
   182  * Filters the post format term link to remove the format prefix.
   182  *
   183  *
   183  * @access private
   184  * @access private
   184  * @since 3.1.0
   185  * @since 3.1.0
   185  *
   186  *
   186  * @global WP_Rewrite $wp_rewrite
   187  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   187  *
   188  *
   188  * @param string $link
   189  * @param string $link
   189  * @param object $term
   190  * @param object $term
   190  * @param string $taxonomy
   191  * @param string $taxonomy
   191  * @return string
   192  * @return string
   192  */
   193  */
   193 function _post_format_link( $link, $term, $taxonomy ) {
   194 function _post_format_link( $link, $term, $taxonomy ) {
   194 	global $wp_rewrite;
   195 	global $wp_rewrite;
   195 	if ( 'post_format' != $taxonomy ) {
   196 	if ( 'post_format' !== $taxonomy ) {
   196 		return $link;
   197 		return $link;
   197 	}
   198 	}
   198 	if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
   199 	if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
   199 		return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
   200 		return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
   200 	} else {
   201 	} else {
   229  * @param string|array $taxonomies
   230  * @param string|array $taxonomies
   230  * @param array        $args
   231  * @param array        $args
   231  * @return array
   232  * @return array
   232  */
   233  */
   233 function _post_format_get_terms( $terms, $taxonomies, $args ) {
   234 function _post_format_get_terms( $terms, $taxonomies, $args ) {
   234 	if ( in_array( 'post_format', (array) $taxonomies ) ) {
   235 	if ( in_array( 'post_format', (array) $taxonomies, true ) ) {
   235 		if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
   236 		if ( isset( $args['fields'] ) && 'names' === $args['fields'] ) {
   236 			foreach ( $terms as $order => $name ) {
   237 			foreach ( $terms as $order => $name ) {
   237 				$terms[ $order ] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
   238 				$terms[ $order ] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
   238 			}
   239 			}
   239 		} else {
   240 		} else {
   240 			foreach ( (array) $terms as $order => $term ) {
   241 			foreach ( (array) $terms as $order => $term ) {
   241 				if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   242 				if ( isset( $term->taxonomy ) && 'post_format' === $term->taxonomy ) {
   242 					$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   243 					$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   243 				}
   244 				}
   244 			}
   245 			}
   245 		}
   246 		}
   246 	}
   247 	}
   256  * @param array $terms
   257  * @param array $terms
   257  * @return array
   258  * @return array
   258  */
   259  */
   259 function _post_format_wp_get_object_terms( $terms ) {
   260 function _post_format_wp_get_object_terms( $terms ) {
   260 	foreach ( (array) $terms as $order => $term ) {
   261 	foreach ( (array) $terms as $order => $term ) {
   261 		if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   262 		if ( isset( $term->taxonomy ) && 'post_format' === $term->taxonomy ) {
   262 			$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   263 			$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   263 		}
   264 		}
   264 	}
   265 	}
   265 	return $terms;
   266 	return $terms;
   266 }
   267 }