Edit File by line
/home/barbar84/www/wp-inclu...
File: post.php
}
[2000] Fix | Delete
[2001] Fix | Delete
/**
[2002] Fix | Delete
* Determines whether a post type is considered "viewable".
[2003] Fix | Delete
*
[2004] Fix | Delete
* For built-in post types such as posts and pages, the 'public' value will be evaluated.
[2005] Fix | Delete
* For all others, the 'publicly_queryable' value will be used.
[2006] Fix | Delete
*
[2007] Fix | Delete
* @since 4.4.0
[2008] Fix | Delete
* @since 4.5.0 Added the ability to pass a post type name in addition to object.
[2009] Fix | Delete
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
[2010] Fix | Delete
*
[2011] Fix | Delete
* @param string|WP_Post_Type $post_type Post type name or object.
[2012] Fix | Delete
* @return bool Whether the post type should be considered viewable.
[2013] Fix | Delete
*/
[2014] Fix | Delete
function is_post_type_viewable( $post_type ) {
[2015] Fix | Delete
if ( is_scalar( $post_type ) ) {
[2016] Fix | Delete
$post_type = get_post_type_object( $post_type );
[2017] Fix | Delete
if ( ! $post_type ) {
[2018] Fix | Delete
return false;
[2019] Fix | Delete
}
[2020] Fix | Delete
}
[2021] Fix | Delete
[2022] Fix | Delete
if ( ! is_object( $post_type ) ) {
[2023] Fix | Delete
return false;
[2024] Fix | Delete
}
[2025] Fix | Delete
[2026] Fix | Delete
return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
[2027] Fix | Delete
}
[2028] Fix | Delete
[2029] Fix | Delete
/**
[2030] Fix | Delete
* Determine whether a post status is considered "viewable".
[2031] Fix | Delete
*
[2032] Fix | Delete
* For built-in post statuses such as publish and private, the 'public' value will be evaluted.
[2033] Fix | Delete
* For all others, the 'publicly_queryable' value will be used.
[2034] Fix | Delete
*
[2035] Fix | Delete
* @since 5.7.0
[2036] Fix | Delete
*
[2037] Fix | Delete
* @param string|stdClass $post_status Post status name or object.
[2038] Fix | Delete
* @return bool Whether the post status should be considered viewable.
[2039] Fix | Delete
*/
[2040] Fix | Delete
function is_post_status_viewable( $post_status ) {
[2041] Fix | Delete
if ( is_scalar( $post_status ) ) {
[2042] Fix | Delete
$post_status = get_post_status_object( $post_status );
[2043] Fix | Delete
if ( ! $post_status ) {
[2044] Fix | Delete
return false;
[2045] Fix | Delete
}
[2046] Fix | Delete
}
[2047] Fix | Delete
[2048] Fix | Delete
if (
[2049] Fix | Delete
! is_object( $post_status ) ||
[2050] Fix | Delete
$post_status->internal ||
[2051] Fix | Delete
$post_status->protected
[2052] Fix | Delete
) {
[2053] Fix | Delete
return false;
[2054] Fix | Delete
}
[2055] Fix | Delete
[2056] Fix | Delete
return $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
[2057] Fix | Delete
}
[2058] Fix | Delete
[2059] Fix | Delete
/**
[2060] Fix | Delete
* Determine whether a post is publicly viewable.
[2061] Fix | Delete
*
[2062] Fix | Delete
* Posts are considered publicly viewable if both the post status and post type
[2063] Fix | Delete
* are viewable.
[2064] Fix | Delete
*
[2065] Fix | Delete
* @since 5.7.0
[2066] Fix | Delete
*
[2067] Fix | Delete
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
[2068] Fix | Delete
* @return bool Whether the post is publicly viewable.
[2069] Fix | Delete
*/
[2070] Fix | Delete
function is_post_publicly_viewable( $post = null ) {
[2071] Fix | Delete
$post = get_post( $post );
[2072] Fix | Delete
[2073] Fix | Delete
if ( ! $post ) {
[2074] Fix | Delete
return false;
[2075] Fix | Delete
}
[2076] Fix | Delete
[2077] Fix | Delete
$post_type = get_post_type( $post );
[2078] Fix | Delete
$post_status = get_post_status( $post );
[2079] Fix | Delete
[2080] Fix | Delete
return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
[2081] Fix | Delete
}
[2082] Fix | Delete
[2083] Fix | Delete
/**
[2084] Fix | Delete
* Retrieves an array of the latest posts, or posts matching the given criteria.
[2085] Fix | Delete
*
[2086] Fix | Delete
* For more information on the accepted arguments, see the
[2087] Fix | Delete
* {@link https://developer.wordpress.org/reference/classes/wp_query/
[2088] Fix | Delete
* WP_Query} documentation in the Developer Handbook.
[2089] Fix | Delete
*
[2090] Fix | Delete
* The `$ignore_sticky_posts` and `$no_found_rows` arguments are ignored by
[2091] Fix | Delete
* this function and both are set to `true`.
[2092] Fix | Delete
*
[2093] Fix | Delete
* The defaults are as follows:
[2094] Fix | Delete
*
[2095] Fix | Delete
* @since 1.2.0
[2096] Fix | Delete
*
[2097] Fix | Delete
* @see WP_Query
[2098] Fix | Delete
* @see WP_Query::parse_query()
[2099] Fix | Delete
*
[2100] Fix | Delete
* @param array $args {
[2101] Fix | Delete
* Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all
[2102] Fix | Delete
* available arguments.
[2103] Fix | Delete
*
[2104] Fix | Delete
* @type int $numberposts Total number of posts to retrieve. Is an alias of `$posts_per_page`
[2105] Fix | Delete
* in WP_Query. Accepts -1 for all. Default 5.
[2106] Fix | Delete
* @type int|string $category Category ID or comma-separated list of IDs (this or any children).
[2107] Fix | Delete
* Is an alias of `$cat` in WP_Query. Default 0.
[2108] Fix | Delete
* @type int[] $include An array of post IDs to retrieve, sticky posts will be included.
[2109] Fix | Delete
* Is an alias of `$post__in` in WP_Query. Default empty array.
[2110] Fix | Delete
* @type int[] $exclude An array of post IDs not to retrieve. Default empty array.
[2111] Fix | Delete
* @type bool $suppress_filters Whether to suppress filters. Default true.
[2112] Fix | Delete
* }
[2113] Fix | Delete
* @return WP_Post[]|int[] Array of post objects or post IDs.
[2114] Fix | Delete
*/
[2115] Fix | Delete
function get_posts( $args = null ) {
[2116] Fix | Delete
$defaults = array(
[2117] Fix | Delete
'numberposts' => 5,
[2118] Fix | Delete
'category' => 0,
[2119] Fix | Delete
'orderby' => 'date',
[2120] Fix | Delete
'order' => 'DESC',
[2121] Fix | Delete
'include' => array(),
[2122] Fix | Delete
'exclude' => array(),
[2123] Fix | Delete
'meta_key' => '',
[2124] Fix | Delete
'meta_value' => '',
[2125] Fix | Delete
'post_type' => 'post',
[2126] Fix | Delete
'suppress_filters' => true,
[2127] Fix | Delete
);
[2128] Fix | Delete
[2129] Fix | Delete
$parsed_args = wp_parse_args( $args, $defaults );
[2130] Fix | Delete
if ( empty( $parsed_args['post_status'] ) ) {
[2131] Fix | Delete
$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
[2132] Fix | Delete
}
[2133] Fix | Delete
if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
[2134] Fix | Delete
$parsed_args['posts_per_page'] = $parsed_args['numberposts'];
[2135] Fix | Delete
}
[2136] Fix | Delete
if ( ! empty( $parsed_args['category'] ) ) {
[2137] Fix | Delete
$parsed_args['cat'] = $parsed_args['category'];
[2138] Fix | Delete
}
[2139] Fix | Delete
if ( ! empty( $parsed_args['include'] ) ) {
[2140] Fix | Delete
$incposts = wp_parse_id_list( $parsed_args['include'] );
[2141] Fix | Delete
$parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included.
[2142] Fix | Delete
$parsed_args['post__in'] = $incposts;
[2143] Fix | Delete
} elseif ( ! empty( $parsed_args['exclude'] ) ) {
[2144] Fix | Delete
$parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
[2145] Fix | Delete
}
[2146] Fix | Delete
[2147] Fix | Delete
$parsed_args['ignore_sticky_posts'] = true;
[2148] Fix | Delete
$parsed_args['no_found_rows'] = true;
[2149] Fix | Delete
[2150] Fix | Delete
$get_posts = new WP_Query;
[2151] Fix | Delete
return $get_posts->query( $parsed_args );
[2152] Fix | Delete
[2153] Fix | Delete
}
[2154] Fix | Delete
[2155] Fix | Delete
//
[2156] Fix | Delete
// Post meta functions.
[2157] Fix | Delete
//
[2158] Fix | Delete
[2159] Fix | Delete
/**
[2160] Fix | Delete
* Adds a meta field to the given post.
[2161] Fix | Delete
*
[2162] Fix | Delete
* Post meta data is called "Custom Fields" on the Administration Screen.
[2163] Fix | Delete
*
[2164] Fix | Delete
* @since 1.5.0
[2165] Fix | Delete
*
[2166] Fix | Delete
* @param int $post_id Post ID.
[2167] Fix | Delete
* @param string $meta_key Metadata name.
[2168] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[2169] Fix | Delete
* @param bool $unique Optional. Whether the same key should not be added.
[2170] Fix | Delete
* Default false.
[2171] Fix | Delete
* @return int|false Meta ID on success, false on failure.
[2172] Fix | Delete
*/
[2173] Fix | Delete
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
[2174] Fix | Delete
// Make sure meta is added to the post, not a revision.
[2175] Fix | Delete
$the_post = wp_is_post_revision( $post_id );
[2176] Fix | Delete
if ( $the_post ) {
[2177] Fix | Delete
$post_id = $the_post;
[2178] Fix | Delete
}
[2179] Fix | Delete
[2180] Fix | Delete
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
[2181] Fix | Delete
}
[2182] Fix | Delete
[2183] Fix | Delete
/**
[2184] Fix | Delete
* Deletes a post meta field for the given post ID.
[2185] Fix | Delete
*
[2186] Fix | Delete
* You can match based on the key, or key and value. Removing based on key and
[2187] Fix | Delete
* value, will keep from removing duplicate metadata with the same key. It also
[2188] Fix | Delete
* allows removing all metadata matching the key, if needed.
[2189] Fix | Delete
*
[2190] Fix | Delete
* @since 1.5.0
[2191] Fix | Delete
*
[2192] Fix | Delete
* @param int $post_id Post ID.
[2193] Fix | Delete
* @param string $meta_key Metadata name.
[2194] Fix | Delete
* @param mixed $meta_value Optional. Metadata value. If provided,
[2195] Fix | Delete
* rows will only be removed that match the value.
[2196] Fix | Delete
* Must be serializable if non-scalar. Default empty.
[2197] Fix | Delete
* @return bool True on success, false on failure.
[2198] Fix | Delete
*/
[2199] Fix | Delete
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
[2200] Fix | Delete
// Make sure meta is added to the post, not a revision.
[2201] Fix | Delete
$the_post = wp_is_post_revision( $post_id );
[2202] Fix | Delete
if ( $the_post ) {
[2203] Fix | Delete
$post_id = $the_post;
[2204] Fix | Delete
}
[2205] Fix | Delete
[2206] Fix | Delete
return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
[2207] Fix | Delete
}
[2208] Fix | Delete
[2209] Fix | Delete
/**
[2210] Fix | Delete
* Retrieves a post meta field for the given post ID.
[2211] Fix | Delete
*
[2212] Fix | Delete
* @since 1.5.0
[2213] Fix | Delete
*
[2214] Fix | Delete
* @param int $post_id Post ID.
[2215] Fix | Delete
* @param string $key Optional. The meta key to retrieve. By default,
[2216] Fix | Delete
* returns data for all keys. Default empty.
[2217] Fix | Delete
* @param bool $single Optional. Whether to return a single value.
[2218] Fix | Delete
* This parameter has no effect if $key is not specified.
[2219] Fix | Delete
* Default false.
[2220] Fix | Delete
* @return mixed An array if $single is false. The value of the meta field
[2221] Fix | Delete
* if $single is true. False for an invalid $post_id.
[2222] Fix | Delete
*/
[2223] Fix | Delete
function get_post_meta( $post_id, $key = '', $single = false ) {
[2224] Fix | Delete
return get_metadata( 'post', $post_id, $key, $single );
[2225] Fix | Delete
}
[2226] Fix | Delete
[2227] Fix | Delete
/**
[2228] Fix | Delete
* Updates a post meta field based on the given post ID.
[2229] Fix | Delete
*
[2230] Fix | Delete
* Use the `$prev_value` parameter to differentiate between meta fields with the
[2231] Fix | Delete
* same key and post ID.
[2232] Fix | Delete
*
[2233] Fix | Delete
* If the meta field for the post does not exist, it will be added and its ID returned.
[2234] Fix | Delete
*
[2235] Fix | Delete
* Can be used in place of add_post_meta().
[2236] Fix | Delete
*
[2237] Fix | Delete
* @since 1.5.0
[2238] Fix | Delete
*
[2239] Fix | Delete
* @param int $post_id Post ID.
[2240] Fix | Delete
* @param string $meta_key Metadata key.
[2241] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[2242] Fix | Delete
* @param mixed $prev_value Optional. Previous value to check before updating.
[2243] Fix | Delete
* If specified, only update existing metadata entries with
[2244] Fix | Delete
* this value. Otherwise, update all entries. Default empty.
[2245] Fix | Delete
* @return int|bool Meta ID if the key didn't exist, true on successful update,
[2246] Fix | Delete
* false on failure or if the value passed to the function
[2247] Fix | Delete
* is the same as the one that is already in the database.
[2248] Fix | Delete
*/
[2249] Fix | Delete
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
[2250] Fix | Delete
// Make sure meta is added to the post, not a revision.
[2251] Fix | Delete
$the_post = wp_is_post_revision( $post_id );
[2252] Fix | Delete
if ( $the_post ) {
[2253] Fix | Delete
$post_id = $the_post;
[2254] Fix | Delete
}
[2255] Fix | Delete
[2256] Fix | Delete
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
[2257] Fix | Delete
}
[2258] Fix | Delete
[2259] Fix | Delete
/**
[2260] Fix | Delete
* Deletes everything from post meta matching the given meta key.
[2261] Fix | Delete
*
[2262] Fix | Delete
* @since 2.3.0
[2263] Fix | Delete
*
[2264] Fix | Delete
* @param string $post_meta_key Key to search for when deleting.
[2265] Fix | Delete
* @return bool Whether the post meta key was deleted from the database.
[2266] Fix | Delete
*/
[2267] Fix | Delete
function delete_post_meta_by_key( $post_meta_key ) {
[2268] Fix | Delete
return delete_metadata( 'post', null, $post_meta_key, '', true );
[2269] Fix | Delete
}
[2270] Fix | Delete
[2271] Fix | Delete
/**
[2272] Fix | Delete
* Registers a meta key for posts.
[2273] Fix | Delete
*
[2274] Fix | Delete
* @since 4.9.8
[2275] Fix | Delete
*
[2276] Fix | Delete
* @param string $post_type Post type to register a meta key for. Pass an empty string
[2277] Fix | Delete
* to register the meta key across all existing post types.
[2278] Fix | Delete
* @param string $meta_key The meta key to register.
[2279] Fix | Delete
* @param array $args Data used to describe the meta key when registered. See
[2280] Fix | Delete
* {@see register_meta()} for a list of supported arguments.
[2281] Fix | Delete
* @return bool True if the meta key was successfully registered, false if not.
[2282] Fix | Delete
*/
[2283] Fix | Delete
function register_post_meta( $post_type, $meta_key, array $args ) {
[2284] Fix | Delete
$args['object_subtype'] = $post_type;
[2285] Fix | Delete
[2286] Fix | Delete
return register_meta( 'post', $meta_key, $args );
[2287] Fix | Delete
}
[2288] Fix | Delete
[2289] Fix | Delete
/**
[2290] Fix | Delete
* Unregisters a meta key for posts.
[2291] Fix | Delete
*
[2292] Fix | Delete
* @since 4.9.8
[2293] Fix | Delete
*
[2294] Fix | Delete
* @param string $post_type Post type the meta key is currently registered for. Pass
[2295] Fix | Delete
* an empty string if the meta key is registered across all
[2296] Fix | Delete
* existing post types.
[2297] Fix | Delete
* @param string $meta_key The meta key to unregister.
[2298] Fix | Delete
* @return bool True on success, false if the meta key was not previously registered.
[2299] Fix | Delete
*/
[2300] Fix | Delete
function unregister_post_meta( $post_type, $meta_key ) {
[2301] Fix | Delete
return unregister_meta_key( 'post', $meta_key, $post_type );
[2302] Fix | Delete
}
[2303] Fix | Delete
[2304] Fix | Delete
/**
[2305] Fix | Delete
* Retrieve post meta fields, based on post ID.
[2306] Fix | Delete
*
[2307] Fix | Delete
* The post meta fields are retrieved from the cache where possible,
[2308] Fix | Delete
* so the function is optimized to be called more than once.
[2309] Fix | Delete
*
[2310] Fix | Delete
* @since 1.2.0
[2311] Fix | Delete
*
[2312] Fix | Delete
* @param int $post_id Optional. Post ID. Default is ID of the global $post.
[2313] Fix | Delete
* @return array Post meta for the given post.
[2314] Fix | Delete
*/
[2315] Fix | Delete
function get_post_custom( $post_id = 0 ) {
[2316] Fix | Delete
$post_id = absint( $post_id );
[2317] Fix | Delete
if ( ! $post_id ) {
[2318] Fix | Delete
$post_id = get_the_ID();
[2319] Fix | Delete
}
[2320] Fix | Delete
[2321] Fix | Delete
return get_post_meta( $post_id );
[2322] Fix | Delete
}
[2323] Fix | Delete
[2324] Fix | Delete
/**
[2325] Fix | Delete
* Retrieve meta field names for a post.
[2326] Fix | Delete
*
[2327] Fix | Delete
* If there are no meta fields, then nothing (null) will be returned.
[2328] Fix | Delete
*
[2329] Fix | Delete
* @since 1.2.0
[2330] Fix | Delete
*
[2331] Fix | Delete
* @param int $post_id Optional. Post ID. Default is ID of the global $post.
[2332] Fix | Delete
* @return array|void Array of the keys, if retrieved.
[2333] Fix | Delete
*/
[2334] Fix | Delete
function get_post_custom_keys( $post_id = 0 ) {
[2335] Fix | Delete
$custom = get_post_custom( $post_id );
[2336] Fix | Delete
[2337] Fix | Delete
if ( ! is_array( $custom ) ) {
[2338] Fix | Delete
return;
[2339] Fix | Delete
}
[2340] Fix | Delete
[2341] Fix | Delete
$keys = array_keys( $custom );
[2342] Fix | Delete
if ( $keys ) {
[2343] Fix | Delete
return $keys;
[2344] Fix | Delete
}
[2345] Fix | Delete
}
[2346] Fix | Delete
[2347] Fix | Delete
/**
[2348] Fix | Delete
* Retrieve values for a custom post field.
[2349] Fix | Delete
*
[2350] Fix | Delete
* The parameters must not be considered optional. All of the post meta fields
[2351] Fix | Delete
* will be retrieved and only the meta field key values returned.
[2352] Fix | Delete
*
[2353] Fix | Delete
* @since 1.2.0
[2354] Fix | Delete
*
[2355] Fix | Delete
* @param string $key Optional. Meta field key. Default empty.
[2356] Fix | Delete
* @param int $post_id Optional. Post ID. Default is ID of the global $post.
[2357] Fix | Delete
* @return array|null Meta field values.
[2358] Fix | Delete
*/
[2359] Fix | Delete
function get_post_custom_values( $key = '', $post_id = 0 ) {
[2360] Fix | Delete
if ( ! $key ) {
[2361] Fix | Delete
return null;
[2362] Fix | Delete
}
[2363] Fix | Delete
[2364] Fix | Delete
$custom = get_post_custom( $post_id );
[2365] Fix | Delete
[2366] Fix | Delete
return isset( $custom[ $key ] ) ? $custom[ $key ] : null;
[2367] Fix | Delete
}
[2368] Fix | Delete
[2369] Fix | Delete
/**
[2370] Fix | Delete
* Determines whether a post is sticky.
[2371] Fix | Delete
*
[2372] Fix | Delete
* Sticky posts should remain at the top of The Loop. If the post ID is not
[2373] Fix | Delete
* given, then The Loop ID for the current post will be used.
[2374] Fix | Delete
*
[2375] Fix | Delete
* For more information on this and similar theme functions, check out
[2376] Fix | Delete
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
[2377] Fix | Delete
* Conditional Tags} article in the Theme Developer Handbook.
[2378] Fix | Delete
*
[2379] Fix | Delete
* @since 2.7.0
[2380] Fix | Delete
*
[2381] Fix | Delete
* @param int $post_id Optional. Post ID. Default is ID of the global $post.
[2382] Fix | Delete
* @return bool Whether post is sticky.
[2383] Fix | Delete
*/
[2384] Fix | Delete
function is_sticky( $post_id = 0 ) {
[2385] Fix | Delete
$post_id = absint( $post_id );
[2386] Fix | Delete
[2387] Fix | Delete
if ( ! $post_id ) {
[2388] Fix | Delete
$post_id = get_the_ID();
[2389] Fix | Delete
}
[2390] Fix | Delete
[2391] Fix | Delete
$stickies = get_option( 'sticky_posts' );
[2392] Fix | Delete
[2393] Fix | Delete
if ( is_array( $stickies ) ) {
[2394] Fix | Delete
$stickies = array_map( 'intval', $stickies );
[2395] Fix | Delete
$is_sticky = in_array( $post_id, $stickies, true );
[2396] Fix | Delete
} else {
[2397] Fix | Delete
$is_sticky = false;
[2398] Fix | Delete
}
[2399] Fix | Delete
[2400] Fix | Delete
/**
[2401] Fix | Delete
* Filters whether a post is sticky.
[2402] Fix | Delete
*
[2403] Fix | Delete
* @since 5.3.0
[2404] Fix | Delete
*
[2405] Fix | Delete
* @param bool $is_sticky Whether a post is sticky.
[2406] Fix | Delete
* @param int $post_id Post ID.
[2407] Fix | Delete
*/
[2408] Fix | Delete
return apply_filters( 'is_sticky', $is_sticky, $post_id );
[2409] Fix | Delete
}
[2410] Fix | Delete
[2411] Fix | Delete
/**
[2412] Fix | Delete
* Sanitizes every post field.
[2413] Fix | Delete
*
[2414] Fix | Delete
* If the context is 'raw', then the post object or array will get minimal
[2415] Fix | Delete
* sanitization of the integer fields.
[2416] Fix | Delete
*
[2417] Fix | Delete
* @since 2.3.0
[2418] Fix | Delete
*
[2419] Fix | Delete
* @see sanitize_post_field()
[2420] Fix | Delete
*
[2421] Fix | Delete
* @param object|WP_Post|array $post The post object or array
[2422] Fix | Delete
* @param string $context Optional. How to sanitize post fields.
[2423] Fix | Delete
* Accepts 'raw', 'edit', 'db', 'display',
[2424] Fix | Delete
* 'attribute', or 'js'. Default 'display'.
[2425] Fix | Delete
* @return object|WP_Post|array The now sanitized post object or array (will be the
[2426] Fix | Delete
* same type as `$post`).
[2427] Fix | Delete
*/
[2428] Fix | Delete
function sanitize_post( $post, $context = 'display' ) {
[2429] Fix | Delete
if ( is_object( $post ) ) {
[2430] Fix | Delete
// Check if post already filtered for this context.
[2431] Fix | Delete
if ( isset( $post->filter ) && $context == $post->filter ) {
[2432] Fix | Delete
return $post;
[2433] Fix | Delete
}
[2434] Fix | Delete
if ( ! isset( $post->ID ) ) {
[2435] Fix | Delete
$post->ID = 0;
[2436] Fix | Delete
}
[2437] Fix | Delete
foreach ( array_keys( get_object_vars( $post ) ) as $field ) {
[2438] Fix | Delete
$post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context );
[2439] Fix | Delete
}
[2440] Fix | Delete
$post->filter = $context;
[2441] Fix | Delete
} elseif ( is_array( $post ) ) {
[2442] Fix | Delete
// Check if post already filtered for this context.
[2443] Fix | Delete
if ( isset( $post['filter'] ) && $context == $post['filter'] ) {
[2444] Fix | Delete
return $post;
[2445] Fix | Delete
}
[2446] Fix | Delete
if ( ! isset( $post['ID'] ) ) {
[2447] Fix | Delete
$post['ID'] = 0;
[2448] Fix | Delete
}
[2449] Fix | Delete
foreach ( array_keys( $post ) as $field ) {
[2450] Fix | Delete
$post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context );
[2451] Fix | Delete
}
[2452] Fix | Delete
$post['filter'] = $context;
[2453] Fix | Delete
}
[2454] Fix | Delete
return $post;
[2455] Fix | Delete
}
[2456] Fix | Delete
[2457] Fix | Delete
/**
[2458] Fix | Delete
* Sanitizes a post field based on context.
[2459] Fix | Delete
*
[2460] Fix | Delete
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and
[2461] Fix | Delete
* 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
[2462] Fix | Delete
* are treated like 'display' when calling filters.
[2463] Fix | Delete
*
[2464] Fix | Delete
* @since 2.3.0
[2465] Fix | Delete
* @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'.
[2466] Fix | Delete
*
[2467] Fix | Delete
* @param string $field The Post Object field name.
[2468] Fix | Delete
* @param mixed $value The Post Object value.
[2469] Fix | Delete
* @param int $post_id Post ID.
[2470] Fix | Delete
* @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit',
[2471] Fix | Delete
* 'db', 'display', 'attribute' and 'js'. Default 'display'.
[2472] Fix | Delete
* @return mixed Sanitized value.
[2473] Fix | Delete
*/
[2474] Fix | Delete
function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
[2475] Fix | Delete
$int_fields = array( 'ID', 'post_parent', 'menu_order' );
[2476] Fix | Delete
if ( in_array( $field, $int_fields, true ) ) {
[2477] Fix | Delete
$value = (int) $value;
[2478] Fix | Delete
}
[2479] Fix | Delete
[2480] Fix | Delete
// Fields which contain arrays of integers.
[2481] Fix | Delete
$array_int_fields = array( 'ancestors' );
[2482] Fix | Delete
if ( in_array( $field, $array_int_fields, true ) ) {
[2483] Fix | Delete
$value = array_map( 'absint', $value );
[2484] Fix | Delete
return $value;
[2485] Fix | Delete
}
[2486] Fix | Delete
[2487] Fix | Delete
if ( 'raw' === $context ) {
[2488] Fix | Delete
return $value;
[2489] Fix | Delete
}
[2490] Fix | Delete
[2491] Fix | Delete
$prefixed = false;
[2492] Fix | Delete
if ( false !== strpos( $field, 'post_' ) ) {
[2493] Fix | Delete
$prefixed = true;
[2494] Fix | Delete
$field_no_prefix = str_replace( 'post_', '', $field );
[2495] Fix | Delete
}
[2496] Fix | Delete
[2497] Fix | Delete
if ( 'edit' === $context ) {
[2498] Fix | Delete
$format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' );
[2499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function