Edit File by line
/home/barbar84/www/wp-inclu...
File: post-formats.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Post format functions.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Post
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Retrieve the format slug for a post
[9] Fix | Delete
*
[10] Fix | Delete
* @since 3.1.0
[11] Fix | Delete
*
[12] Fix | Delete
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to the current post in the loop.
[13] Fix | Delete
* @return string|false The format if successful. False otherwise.
[14] Fix | Delete
*/
[15] Fix | Delete
function get_post_format( $post = null ) {
[16] Fix | Delete
$post = get_post( $post );
[17] Fix | Delete
[18] Fix | Delete
if ( ! $post ) {
[19] Fix | Delete
return false;
[20] Fix | Delete
}
[21] Fix | Delete
[22] Fix | Delete
if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) {
[23] Fix | Delete
return false;
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
$_format = get_the_terms( $post->ID, 'post_format' );
[27] Fix | Delete
[28] Fix | Delete
if ( empty( $_format ) ) {
[29] Fix | Delete
return false;
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
$format = reset( $_format );
[33] Fix | Delete
[34] Fix | Delete
return str_replace( 'post-format-', '', $format->slug );
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Check if a post has any of the given formats, or any format.
[39] Fix | Delete
*
[40] Fix | Delete
* @since 3.1.0
[41] Fix | Delete
*
[42] Fix | Delete
* @param string|string[] $format Optional. The format or formats to check.
[43] Fix | Delete
* @param WP_Post|int|null $post Optional. The post to check. Defaults to the current post in the loop.
[44] Fix | Delete
* @return bool True if the post has any of the given formats (or any format, if no format specified),
[45] Fix | Delete
* false otherwise.
[46] Fix | Delete
*/
[47] Fix | Delete
function has_post_format( $format = array(), $post = null ) {
[48] Fix | Delete
$prefixed = array();
[49] Fix | Delete
[50] Fix | Delete
if ( $format ) {
[51] Fix | Delete
foreach ( (array) $format as $single ) {
[52] Fix | Delete
$prefixed[] = 'post-format-' . sanitize_key( $single );
[53] Fix | Delete
}
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
return has_term( $prefixed, 'post_format', $post );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Assign a format to a post
[61] Fix | Delete
*
[62] Fix | Delete
* @since 3.1.0
[63] Fix | Delete
*
[64] Fix | Delete
* @param int|object $post The post for which to assign a format.
[65] Fix | Delete
* @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
[66] Fix | Delete
* @return array|WP_Error|false Array of affected term IDs on success. WP_Error on error.
[67] Fix | Delete
*/
[68] Fix | Delete
function set_post_format( $post, $format ) {
[69] Fix | Delete
$post = get_post( $post );
[70] Fix | Delete
[71] Fix | Delete
if ( ! $post ) {
[72] Fix | Delete
return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
if ( ! empty( $format ) ) {
[76] Fix | Delete
$format = sanitize_key( $format );
[77] Fix | Delete
if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs(), true ) ) {
[78] Fix | Delete
$format = '';
[79] Fix | Delete
} else {
[80] Fix | Delete
$format = 'post-format-' . $format;
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
return wp_set_post_terms( $post->ID, $format, 'post_format' );
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Returns an array of post format slugs to their translated and pretty display versions
[89] Fix | Delete
*
[90] Fix | Delete
* @since 3.1.0
[91] Fix | Delete
*
[92] Fix | Delete
* @return string[] Array of post format labels keyed by format slug.
[93] Fix | Delete
*/
[94] Fix | Delete
function get_post_format_strings() {
[95] Fix | Delete
$strings = array(
[96] Fix | Delete
'standard' => _x( 'Standard', 'Post format' ), // Special case. Any value that evals to false will be considered standard.
[97] Fix | Delete
'aside' => _x( 'Aside', 'Post format' ),
[98] Fix | Delete
'chat' => _x( 'Chat', 'Post format' ),
[99] Fix | Delete
'gallery' => _x( 'Gallery', 'Post format' ),
[100] Fix | Delete
'link' => _x( 'Link', 'Post format' ),
[101] Fix | Delete
'image' => _x( 'Image', 'Post format' ),
[102] Fix | Delete
'quote' => _x( 'Quote', 'Post format' ),
[103] Fix | Delete
'status' => _x( 'Status', 'Post format' ),
[104] Fix | Delete
'video' => _x( 'Video', 'Post format' ),
[105] Fix | Delete
'audio' => _x( 'Audio', 'Post format' ),
[106] Fix | Delete
);
[107] Fix | Delete
return $strings;
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Retrieves the array of post format slugs.
[112] Fix | Delete
*
[113] Fix | Delete
* @since 3.1.0
[114] Fix | Delete
*
[115] Fix | Delete
* @return string[] The array of post format slugs as both keys and values.
[116] Fix | Delete
*/
[117] Fix | Delete
function get_post_format_slugs() {
[118] Fix | Delete
$slugs = array_keys( get_post_format_strings() );
[119] Fix | Delete
return array_combine( $slugs, $slugs );
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Returns a pretty, translated version of a post format slug
[124] Fix | Delete
*
[125] Fix | Delete
* @since 3.1.0
[126] Fix | Delete
*
[127] Fix | Delete
* @param string $slug A post format slug.
[128] Fix | Delete
* @return string The translated post format name.
[129] Fix | Delete
*/
[130] Fix | Delete
function get_post_format_string( $slug ) {
[131] Fix | Delete
$strings = get_post_format_strings();
[132] Fix | Delete
if ( ! $slug ) {
[133] Fix | Delete
return $strings['standard'];
[134] Fix | Delete
} else {
[135] Fix | Delete
return ( isset( $strings[ $slug ] ) ) ? $strings[ $slug ] : '';
[136] Fix | Delete
}
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Returns a link to a post format index.
[141] Fix | Delete
*
[142] Fix | Delete
* @since 3.1.0
[143] Fix | Delete
*
[144] Fix | Delete
* @param string $format The post format slug.
[145] Fix | Delete
* @return string|WP_Error|false The post format term link.
[146] Fix | Delete
*/
[147] Fix | Delete
function get_post_format_link( $format ) {
[148] Fix | Delete
$term = get_term_by( 'slug', 'post-format-' . $format, 'post_format' );
[149] Fix | Delete
if ( ! $term || is_wp_error( $term ) ) {
[150] Fix | Delete
return false;
[151] Fix | Delete
}
[152] Fix | Delete
return get_term_link( $term );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
/**
[156] Fix | Delete
* Filters the request to allow for the format prefix.
[157] Fix | Delete
*
[158] Fix | Delete
* @access private
[159] Fix | Delete
* @since 3.1.0
[160] Fix | Delete
*
[161] Fix | Delete
* @param array $qvs
[162] Fix | Delete
* @return array
[163] Fix | Delete
*/
[164] Fix | Delete
function _post_format_request( $qvs ) {
[165] Fix | Delete
if ( ! isset( $qvs['post_format'] ) ) {
[166] Fix | Delete
return $qvs;
[167] Fix | Delete
}
[168] Fix | Delete
$slugs = get_post_format_slugs();
[169] Fix | Delete
if ( isset( $slugs[ $qvs['post_format'] ] ) ) {
[170] Fix | Delete
$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
[171] Fix | Delete
}
[172] Fix | Delete
$tax = get_taxonomy( 'post_format' );
[173] Fix | Delete
if ( ! is_admin() ) {
[174] Fix | Delete
$qvs['post_type'] = $tax->object_type;
[175] Fix | Delete
}
[176] Fix | Delete
return $qvs;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Filters the post format term link to remove the format prefix.
[181] Fix | Delete
*
[182] Fix | Delete
* @access private
[183] Fix | Delete
* @since 3.1.0
[184] Fix | Delete
*
[185] Fix | Delete
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
[186] Fix | Delete
*
[187] Fix | Delete
* @param string $link
[188] Fix | Delete
* @param WP_Term $term
[189] Fix | Delete
* @param string $taxonomy
[190] Fix | Delete
* @return string
[191] Fix | Delete
*/
[192] Fix | Delete
function _post_format_link( $link, $term, $taxonomy ) {
[193] Fix | Delete
global $wp_rewrite;
[194] Fix | Delete
if ( 'post_format' !== $taxonomy ) {
[195] Fix | Delete
return $link;
[196] Fix | Delete
}
[197] Fix | Delete
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
[198] Fix | Delete
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
[199] Fix | Delete
} else {
[200] Fix | Delete
$link = remove_query_arg( 'post_format', $link );
[201] Fix | Delete
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
[202] Fix | Delete
}
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
/**
[206] Fix | Delete
* Remove the post format prefix from the name property of the term object created by get_term().
[207] Fix | Delete
*
[208] Fix | Delete
* @access private
[209] Fix | Delete
* @since 3.1.0
[210] Fix | Delete
*
[211] Fix | Delete
* @param object $term
[212] Fix | Delete
* @return object
[213] Fix | Delete
*/
[214] Fix | Delete
function _post_format_get_term( $term ) {
[215] Fix | Delete
if ( isset( $term->slug ) ) {
[216] Fix | Delete
$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
[217] Fix | Delete
}
[218] Fix | Delete
return $term;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
/**
[222] Fix | Delete
* Remove the post format prefix from the name property of the term objects created by get_terms().
[223] Fix | Delete
*
[224] Fix | Delete
* @access private
[225] Fix | Delete
* @since 3.1.0
[226] Fix | Delete
*
[227] Fix | Delete
* @param array $terms
[228] Fix | Delete
* @param string|array $taxonomies
[229] Fix | Delete
* @param array $args
[230] Fix | Delete
* @return array
[231] Fix | Delete
*/
[232] Fix | Delete
function _post_format_get_terms( $terms, $taxonomies, $args ) {
[233] Fix | Delete
if ( in_array( 'post_format', (array) $taxonomies, true ) ) {
[234] Fix | Delete
if ( isset( $args['fields'] ) && 'names' === $args['fields'] ) {
[235] Fix | Delete
foreach ( $terms as $order => $name ) {
[236] Fix | Delete
$terms[ $order ] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
[237] Fix | Delete
}
[238] Fix | Delete
} else {
[239] Fix | Delete
foreach ( (array) $terms as $order => $term ) {
[240] Fix | Delete
if ( isset( $term->taxonomy ) && 'post_format' === $term->taxonomy ) {
[241] Fix | Delete
$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
[242] Fix | Delete
}
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
}
[246] Fix | Delete
return $terms;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
[251] Fix | Delete
*
[252] Fix | Delete
* @access private
[253] Fix | Delete
* @since 3.1.0
[254] Fix | Delete
*
[255] Fix | Delete
* @param array $terms
[256] Fix | Delete
* @return array
[257] Fix | Delete
*/
[258] Fix | Delete
function _post_format_wp_get_object_terms( $terms ) {
[259] Fix | Delete
foreach ( (array) $terms as $order => $term ) {
[260] Fix | Delete
if ( isset( $term->taxonomy ) && 'post_format' === $term->taxonomy ) {
[261] Fix | Delete
$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
[262] Fix | Delete
}
[263] Fix | Delete
}
[264] Fix | Delete
return $terms;
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function