wp/wp-includes/post-formats.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    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 Post ID or post object. Optional, default is the current post from 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 	if ( ! $post = get_post( $post ) )
    18 	$post = get_post( $post );
    19 		return false;
    19 
    20 
    20 	if ( ! $post ) {
    21 	if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
    21 		return false;
    22 		return false;
    22 	}
       
    23 
       
    24 	if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) {
       
    25 		return false;
       
    26 	}
    23 
    27 
    24 	$_format = get_the_terms( $post->ID, 'post_format' );
    28 	$_format = get_the_terms( $post->ID, 'post_format' );
    25 
    29 
    26 	if ( empty( $_format ) )
    30 	if ( empty( $_format ) ) {
    27 		return false;
    31 		return false;
       
    32 	}
    28 
    33 
    29 	$format = reset( $_format );
    34 	$format = reset( $_format );
    30 
    35 
    31 	return str_replace('post-format-', '', $format->slug );
    36 	return str_replace( 'post-format-', '', $format->slug );
    32 }
    37 }
    33 
    38 
    34 /**
    39 /**
    35  * 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.
    36  *
    41  *
    62  * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success.
    67  * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success.
    63  */
    68  */
    64 function set_post_format( $post, $format ) {
    69 function set_post_format( $post, $format ) {
    65 	$post = get_post( $post );
    70 	$post = get_post( $post );
    66 
    71 
    67 	if ( empty( $post ) )
    72 	if ( ! $post ) {
    68 		return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
    73 		return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
       
    74 	}
    69 
    75 
    70 	if ( ! empty( $format ) ) {
    76 	if ( ! empty( $format ) ) {
    71 		$format = sanitize_key( $format );
    77 		$format = sanitize_key( $format );
    72 		if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) )
    78 		if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) ) {
    73 			$format = '';
    79 			$format = '';
    74 		else
    80 		} else {
    75 			$format = 'post-format-' . $format;
    81 			$format = 'post-format-' . $format;
       
    82 		}
    76 	}
    83 	}
    77 
    84 
    78 	return wp_set_post_terms( $post->ID, $format, 'post_format' );
    85 	return wp_set_post_terms( $post->ID, $format, 'post_format' );
    79 }
    86 }
    80 
    87 
    86  * @return array The array of translated post format names.
    93  * @return array The array of translated post format names.
    87  */
    94  */
    88 function get_post_format_strings() {
    95 function get_post_format_strings() {
    89 	$strings = array(
    96 	$strings = array(
    90 		'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
    97 		'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
    91 		'aside'    => _x( 'Aside',    'Post format' ),
    98 		'aside'    => _x( 'Aside', 'Post format' ),
    92 		'chat'     => _x( 'Chat',     'Post format' ),
    99 		'chat'     => _x( 'Chat', 'Post format' ),
    93 		'gallery'  => _x( 'Gallery',  'Post format' ),
   100 		'gallery'  => _x( 'Gallery', 'Post format' ),
    94 		'link'     => _x( 'Link',     'Post format' ),
   101 		'link'     => _x( 'Link', 'Post format' ),
    95 		'image'    => _x( 'Image',    'Post format' ),
   102 		'image'    => _x( 'Image', 'Post format' ),
    96 		'quote'    => _x( 'Quote',    'Post format' ),
   103 		'quote'    => _x( 'Quote', 'Post format' ),
    97 		'status'   => _x( 'Status',   'Post format' ),
   104 		'status'   => _x( 'Status', 'Post format' ),
    98 		'video'    => _x( 'Video',    'Post format' ),
   105 		'video'    => _x( 'Video', 'Post format' ),
    99 		'audio'    => _x( 'Audio',    'Post format' ),
   106 		'audio'    => _x( 'Audio', 'Post format' ),
   100 	);
   107 	);
   101 	return $strings;
   108 	return $strings;
   102 }
   109 }
   103 
   110 
   104 /**
   111 /**
   121  * @param string $slug A post format slug.
   128  * @param string $slug A post format slug.
   122  * @return string The translated post format name.
   129  * @return string The translated post format name.
   123  */
   130  */
   124 function get_post_format_string( $slug ) {
   131 function get_post_format_string( $slug ) {
   125 	$strings = get_post_format_strings();
   132 	$strings = get_post_format_strings();
   126 	if ( !$slug )
   133 	if ( ! $slug ) {
   127 		return $strings['standard'];
   134 		return $strings['standard'];
   128 	else
   135 	} else {
   129 		return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
   136 		return ( isset( $strings[ $slug ] ) ) ? $strings[ $slug ] : '';
       
   137 	}
   130 }
   138 }
   131 
   139 
   132 /**
   140 /**
   133  * Returns a link to a post format index.
   141  * Returns a link to a post format index.
   134  *
   142  *
   136  *
   144  *
   137  * @param string $format The post format slug.
   145  * @param string $format The post format slug.
   138  * @return string|WP_Error|false The post format term link.
   146  * @return string|WP_Error|false The post format term link.
   139  */
   147  */
   140 function get_post_format_link( $format ) {
   148 function get_post_format_link( $format ) {
   141 	$term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
   149 	$term = get_term_by( 'slug', 'post-format-' . $format, 'post_format' );
   142 	if ( ! $term || is_wp_error( $term ) )
   150 	if ( ! $term || is_wp_error( $term ) ) {
   143 		return false;
   151 		return false;
       
   152 	}
   144 	return get_term_link( $term );
   153 	return get_term_link( $term );
   145 }
   154 }
   146 
   155 
   147 /**
   156 /**
   148  * Filters the request to allow for the format prefix.
   157  * Filters the request to allow for the format prefix.
   152  *
   161  *
   153  * @param array $qvs
   162  * @param array $qvs
   154  * @return array
   163  * @return array
   155  */
   164  */
   156 function _post_format_request( $qvs ) {
   165 function _post_format_request( $qvs ) {
   157 	if ( ! isset( $qvs['post_format'] ) )
   166 	if ( ! isset( $qvs['post_format'] ) ) {
   158 		return $qvs;
   167 		return $qvs;
       
   168 	}
   159 	$slugs = get_post_format_slugs();
   169 	$slugs = get_post_format_slugs();
   160 	if ( isset( $slugs[ $qvs['post_format'] ] ) )
   170 	if ( isset( $slugs[ $qvs['post_format'] ] ) ) {
   161 		$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
   171 		$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
       
   172 	}
   162 	$tax = get_taxonomy( 'post_format' );
   173 	$tax = get_taxonomy( 'post_format' );
   163 	if ( ! is_admin() )
   174 	if ( ! is_admin() ) {
   164 		$qvs['post_type'] = $tax->object_type;
   175 		$qvs['post_type'] = $tax->object_type;
       
   176 	}
   165 	return $qvs;
   177 	return $qvs;
   166 }
   178 }
   167 
   179 
   168 /**
   180 /**
   169  * Filters the post format term link to remove the format prefix.
   181  * Filters the post format term link to remove the format prefix.
   220  */
   232  */
   221 function _post_format_get_terms( $terms, $taxonomies, $args ) {
   233 function _post_format_get_terms( $terms, $taxonomies, $args ) {
   222 	if ( in_array( 'post_format', (array) $taxonomies ) ) {
   234 	if ( in_array( 'post_format', (array) $taxonomies ) ) {
   223 		if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
   235 		if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
   224 			foreach ( $terms as $order => $name ) {
   236 			foreach ( $terms as $order => $name ) {
   225 				$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
   237 				$terms[ $order ] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
   226 			}
   238 			}
   227 		} else {
   239 		} else {
   228 			foreach ( (array) $terms as $order => $term ) {
   240 			foreach ( (array) $terms as $order => $term ) {
   229 				if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   241 				if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   230 					$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   242 					$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   231 				}
   243 				}
   232 			}
   244 			}
   233 		}
   245 		}
   234 	}
   246 	}
   235 	return $terms;
   247 	return $terms;
   245  * @return array
   257  * @return array
   246  */
   258  */
   247 function _post_format_wp_get_object_terms( $terms ) {
   259 function _post_format_wp_get_object_terms( $terms ) {
   248 	foreach ( (array) $terms as $order => $term ) {
   260 	foreach ( (array) $terms as $order => $term ) {
   249 		if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   261 		if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
   250 			$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   262 			$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
   251 		}
   263 		}
   252 	}
   264 	}
   253 	return $terms;
   265 	return $terms;
   254 }
   266 }