wp/wp-includes/post-formats.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     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 $post Post ID or post object. Optional, default is the current post from the loop.
    14  * @param int|object|null $post Post ID or post object. Optional, default is the current post from the loop.
    15  * @return mixed 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 	if ( ! $post = get_post( $post ) )
    18 	if ( ! $post = get_post( $post ) )
    19 		return false;
    19 		return false;
    20 
    20 
    34 /**
    34 /**
    35  * Check if a post has any of the given formats, or any 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  * @param string|array $format Optional. The format or formats to check.
    39  * @param string|array    $format Optional. The format or formats to check.
    40  * @param object|int $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop.
    40  * @param object|int|null $post   Optional. The post to check. If not supplied, defaults to the current post if used in the loop.
    41  * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise.
    41  * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise.
    42  */
    42  */
    43 function has_post_format( $format = array(), $post = null ) {
    43 function has_post_format( $format = array(), $post = null ) {
    44 	$prefixed = array();
    44 	$prefixed = array();
    45 
    45 
    55 /**
    55 /**
    56  * Assign a format to a post
    56  * Assign a format to a post
    57  *
    57  *
    58  * @since 3.1.0
    58  * @since 3.1.0
    59  *
    59  *
    60  * @param int|object $post The post for which to assign a format.
    60  * @param int|object $post   The post for which to assign a format.
    61  * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
    61  * @param string     $format A format to assign. Use an empty string or array to remove all formats from the post.
    62  * @return mixed WP_Error on error. Array of affected term IDs on success.
    62  * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success.
    63  */
    63  */
    64 function set_post_format( $post, $format ) {
    64 function set_post_format( $post, $format ) {
    65 	$post = get_post( $post );
    65 	$post = get_post( $post );
    66 
    66 
    67 	if ( empty( $post ) )
    67 	if ( empty( $post ) )
    68 		return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
    68 		return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
    69 
    69 
    70 	if ( ! empty( $format ) ) {
    70 	if ( ! empty( $format ) ) {
    71 		$format = sanitize_key( $format );
    71 		$format = sanitize_key( $format );
    72 		if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) )
    72 		if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) )
    73 			$format = '';
    73 			$format = '';
   100 	);
   100 	);
   101 	return $strings;
   101 	return $strings;
   102 }
   102 }
   103 
   103 
   104 /**
   104 /**
   105  * Retrieves an array of post format slugs.
   105  * Retrieves the array of post format slugs.
   106  *
   106  *
   107  * @since 3.1.0
   107  * @since 3.1.0
   108  *
   108  *
   109  * @return array The array of post format slugs.
   109  * @return array The array of post format slugs as both keys and values.
   110  */
   110  */
   111 function get_post_format_slugs() {
   111 function get_post_format_slugs() {
   112 	$slugs = array_keys( get_post_format_strings() );
   112 	$slugs = array_keys( get_post_format_strings() );
   113 	return array_combine( $slugs, $slugs );
   113 	return array_combine( $slugs, $slugs );
   114 }
   114 }
   133  * Returns a link to a post format index.
   133  * Returns a link to a post format index.
   134  *
   134  *
   135  * @since 3.1.0
   135  * @since 3.1.0
   136  *
   136  *
   137  * @param string $format The post format slug.
   137  * @param string $format The post format slug.
   138  * @return string The post format term link.
   138  * @return string|WP_Error|false The post format term link.
   139  */
   139  */
   140 function get_post_format_link( $format ) {
   140 function get_post_format_link( $format ) {
   141 	$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
   141 	$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
   142 	if ( ! $term || is_wp_error( $term ) )
   142 	if ( ! $term || is_wp_error( $term ) )
   143 		return false;
   143 		return false;
   147 /**
   147 /**
   148  * Filters the request to allow for the format prefix.
   148  * Filters the request to allow for the format prefix.
   149  *
   149  *
   150  * @access private
   150  * @access private
   151  * @since 3.1.0
   151  * @since 3.1.0
       
   152  *
       
   153  * @param array $qvs
       
   154  * @return array
   152  */
   155  */
   153 function _post_format_request( $qvs ) {
   156 function _post_format_request( $qvs ) {
   154 	if ( ! isset( $qvs['post_format'] ) )
   157 	if ( ! isset( $qvs['post_format'] ) )
   155 		return $qvs;
   158 		return $qvs;
   156 	$slugs = get_post_format_slugs();
   159 	$slugs = get_post_format_slugs();
   165 /**
   168 /**
   166  * Filters the post format term link to remove the format prefix.
   169  * Filters the post format term link to remove the format prefix.
   167  *
   170  *
   168  * @access private
   171  * @access private
   169  * @since 3.1.0
   172  * @since 3.1.0
       
   173  *
       
   174  * @global WP_Rewrite $wp_rewrite
       
   175  *
       
   176  * @param string $link
       
   177  * @param object $term
       
   178  * @param string $taxonomy
       
   179  * @return string
   170  */
   180  */
   171 function _post_format_link( $link, $term, $taxonomy ) {
   181 function _post_format_link( $link, $term, $taxonomy ) {
   172 	global $wp_rewrite;
   182 	global $wp_rewrite;
   173 	if ( 'post_format' != $taxonomy )
   183 	if ( 'post_format' != $taxonomy ) {
   174 		return $link;
   184 		return $link;
       
   185 	}
   175 	if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
   186 	if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
   176 		return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
   187 		return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
   177 	} else {
   188 	} else {
   178 		$link = remove_query_arg( 'post_format', $link );
   189 		$link = remove_query_arg( 'post_format', $link );
   179 		return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
   190 		return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
   183 /**
   194 /**
   184  * Remove the post format prefix from the name property of the term object created by get_term().
   195  * Remove the post format prefix from the name property of the term object created by get_term().
   185  *
   196  *
   186  * @access private
   197  * @access private
   187  * @since 3.1.0
   198  * @since 3.1.0
       
   199  *
       
   200  * @param object $term
       
   201  * @return object
   188  */
   202  */
   189 function _post_format_get_term( $term ) {
   203 function _post_format_get_term( $term ) {
   190 	if ( isset( $term->slug ) ) {
   204 	if ( isset( $term->slug ) ) {
   191 		$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   205 		$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   192 	}
   206 	}
   196 /**
   210 /**
   197  * Remove the post format prefix from the name property of the term objects created by get_terms().
   211  * Remove the post format prefix from the name property of the term objects created by get_terms().
   198  *
   212  *
   199  * @access private
   213  * @access private
   200  * @since 3.1.0
   214  * @since 3.1.0
       
   215  *
       
   216  * @param array        $terms
       
   217  * @param string|array $taxonomies
       
   218  * @param array        $args
       
   219  * @return array
   201  */
   220  */
   202 function _post_format_get_terms( $terms, $taxonomies, $args ) {
   221 function _post_format_get_terms( $terms, $taxonomies, $args ) {
   203 	if ( in_array( 'post_format', (array) $taxonomies ) ) {
   222 	if ( in_array( 'post_format', (array) $taxonomies ) ) {
   204 		if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
   223 		if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
   205 			foreach( $terms as $order => $name ) {
   224 			foreach ( $terms as $order => $name ) {
   206 				$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
   225 				$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
   207 			}
   226 			}
   208 		} else {
   227 		} else {
   209 			foreach ( (array) $terms as $order => $term ) {
   228 			foreach ( (array) $terms as $order => $term ) {
   210 				if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   229 				if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   219 /**
   238 /**
   220  * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
   239  * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
   221  *
   240  *
   222  * @access private
   241  * @access private
   223  * @since 3.1.0
   242  * @since 3.1.0
       
   243  *
       
   244  * @param array $terms
       
   245  * @return array
   224  */
   246  */
   225 function _post_format_wp_get_object_terms( $terms ) {
   247 function _post_format_wp_get_object_terms( $terms ) {
   226 	foreach ( (array) $terms as $order => $term ) {
   248 	foreach ( (array) $terms as $order => $term ) {
   227 		if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   249 		if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   228 			$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   250 			$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );