Make WordPress Core


Ignore:
Timestamp:
07/14/2026 12:27:51 AM (7 weeks ago)
Author:
westonruter
Message:

Code Quality: Improve WP_Post type coverage.

Add a Data_Array array-shape type describing every key returned by WP_Post::to_array(), and tighten each WP_Post property to the narrowest type the schema and code guarantee:

  • ID and post_parent become non-negative-int.
  • comment_count becomes numeric-string, and post_author becomes numeric-string|'' (a user ID, or an empty string for a default post that has not yet been assigned an author).
  • The always-populated post_status, comment_status, ping_status, and post_type slugs become non-empty-string, left open rather than enumerated so custom statuses and types remain valid.
  • The magic ancestors, post_category, and tags_input accessors become precise lists (list<non-negative-int> and list<non-empty-string>).

The shape stays open because WP_Post permits dynamic properties. Fields that can legitimately be empty, including the datetime fields (which get_default_post_to_edit() may leave empty), stay string, and menu_order stays int since it can be negative.

Correct WP_Post::$filter, which was previously typed without null or the 'sample' context: an unsanitized post has no filter (checked in core via isset() and empty()), and get_sample_permalink() has assigned 'sample' since [8526]. Widen sanitize_post_field()'s $context to accept 'sample' to match, which it already treats as a 'display' context.

Also type get_post_ancestors() as returning list<non-negative-int>, read the post in trackback_url_list() via to_array() while guarding against a missing post, and cast post_author to a string in WP_Customize_Nav_Menu_Item_Setting and inject_ignored_hooked_blocks_metadata_attributes(), which each populate an object before passing it to new WP_Post().

Developed in https://github.com/WordPress/wordpress-develop/pull/12491.
Follow-up to r62648, r62694.

See #64898, #64896.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r62699 r62717  
    11861186 * @param int|WP_Post $post Post ID or post object.
    11871187 * @return int[] Array of ancestor IDs or empty array if there are none.
     1188 * @phpstan-return list<non-negative-int>
    11881189 */
    11891190function get_post_ancestors( $post ) {
     
    30023003 * Possible context values are:  'raw', 'edit', 'db', 'display', 'attribute' and
    30033004 * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
    3004  * are treated like 'display' when calling filters.
     3005 * are treated like 'display' when calling filters. The 'sample' value is used
     3006 * for permalink previewing.
    30053007 *
    30063008 * @since 2.3.0
     
    30103012 * @param mixed  $value   The Post Object value.
    30113013 * @param int    $post_id Post ID.
    3012  * @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit',
    3013  *                        'db', 'display', 'attribute' and 'js'. Default 'display'.
     3014 * @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit', 'db', 'display',
     3015 *                        'attribute' and 'js'. The 'sample' value is used for permalink previewing. Default 'display'.
    30143016 * @return mixed Sanitized value.
    30153017 *
    3016  * @phpstan-param 'raw'|'edit'|'db'|'display'|'attribute'|'js' $context
     3018 * @phpstan-param 'raw'|'edit'|'db'|'display'|'attribute'|'js'|'sample' $context
    30173019 * @phpstan-return (
    30183020 *     $field is 'ID'|'post_parent'|'menu_order' ? int : (
     
    32873289                         * @param string $context Context for how to sanitize the field.
    32883290                         *                        Accepts 'raw', 'edit', 'db', 'display',
    3289                          *                        'attribute', or 'js'. Default 'display'.
     3291                         *                        'attribute', or 'js'. The 'sample' value is
     3292                         *                        used for permalink previewing. Default 'display'.
    32903293                         */
    32913294                        $value = apply_filters( "{$field}", $value, $post_id, $context );
     
    33143317                         * @param string $context Context for how to sanitize the field.
    33153318                         *                        Accepts 'raw', 'edit', 'db', 'display',
    3316                          *                        'attribute', or 'js'. Default 'display'.
     3319                         *                        'attribute', or 'js'. The 'sample' value is
     3320                         *                        used for permalink previewing. Default 'display'.
    33173321                         */
    33183322                        $value = apply_filters( "post_{$field}", $value, $post_id, $context );
     
    61506154        if ( ! empty( $tb_list ) ) {
    61516155                // Get post data.
    6152                 $postdata = get_post( $post_id, ARRAY_A );
    6153 
    6154                 if ( ! $postdata ) {
     6156                $post = get_post( $post_id );
     6157                if ( ! $post ) {
    61556158                        return;
    61566159                }
     6160                $postdata = $post->to_array();
    61576161
    61586162                // Form an excerpt.
Note: See TracChangeset for help on using the changeset viewer.