Make WordPress Core

Changeset 61789

Timestamp:
03/03/2026 05:53:52 AM (2 days ago)
Author:
westonruter
Message:

Code Quality: Normalize object as type being supplied as comment, post, site, term, and user.

When constructing these objects, the specific instance type is redundantly used in a union with a generic object. A generic object can be passed directly from database row result. This also hardens get_post() to account for passing an object that lacks an ID property. Similarly, sanitize_term() is hardened to account for an object lacking a term_id property. Comprehensive unit tests are added for get_post() and sanitize_term().

Developed in https://github.com/WordPress/wordpress-develop/pull/11096

Props westonruter, apermo.
See #64238, #64225.

Location:
trunk
Files:
2 added
7 edited

Legend:

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

    r61470 r61789  
    211211     * @since 4.4.0
    212212     *
    213      * @param WP_Comment $comment Comment object.
     213     * @param t $comment Comment object.
    214214     */
    215215    public function __construct( $comment ) {
  • trunk/src/wp-includes/class-wp-post.php

    r60654 r61789  
    261261     * @since 3.5.0
    262262     *
    263      * @param WP_Post|object $post Post object.
     263     * @param object $post Post object.
    264264     */
    265265    public function __construct( $post ) {
  • trunk/src/wp-includes/class-wp-site.php

    r60358 r61789  
    191191     * @since 4.5.0
    192192     *
    193      * @param WP_Site|object $site A site object.
     193     * @param object $site A site object.
    194194     */
    195195    public function __construct( $site ) {
  • trunk/src/wp-includes/class-wp-term.php

    r61445 r61789  
    193193     * @since 4.4.0
    194194     *
    195      * @param WP_Term|object $term Term object.
     195     * @param object $term Term object.
    196196     */
    197197    public function __construct( $term ) {
  • trunk/src/wp-includes/class-wp-user.php

    r61387 r61789  
    122122     * @global wpdb $wpdb WordPress database abstraction object.
    123123     *
    124      * @param int|string|stdClass|WP_User $id      User's ID, a WP_User object, or a user object from the DB.
    125      * @param string                      $name    Optional. User's username
    126      * @param int                         $site_id Optional Site ID, defaults to current site.
     124     * @param int|string| $id      User's ID, a WP_User object, or a user object from the DB.
     125     * @param string            $name    Optional. User's username
     126     * @param int               $site_id Optional Site ID, defaults to current site.
    127127     */
    128128    public function __construct( $id = 0, $name = '', $site_id = 0 ) {
  • trunk/src/wp-includes/post.php

    r61722 r61789  
    11361136 * @global WP_Post $post Global post object.
    11371137 *
    1138  * @param int|WP_Post|null $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values
     1138 * @param int| $post   Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values
    11391139 *                                 return the current global post inside the loop. A numerically valid post ID that
    11401140 *                                 points to a non-existent post returns `null`. Defaults to global $post.
     
    11601160        } elseif ( 'raw' === $post->filter ) {
    11611161            $_post = new WP_Post( $post );
     1162
     1163
    11621164        } else {
    1163             $_post = WP_Post::get_instance( $post->ID );
     1165            $_post = ;
    11641166        }
    11651167    } else {
  • trunk/src/wp-includes/taxonomy.php

    r61672 r61789  
    17241724    $do_object = is_object( $term );
    17251725
    1726     $term_id = $do_object ? $term->term_id : ( $term['term_id'] ?? 0 );
     1726    $term_id = $do_object ? : ( $term['term_id'] ?? 0 );
    17271727
    17281728    foreach ( (array) $fields as $field ) {
Note: See TracChangeset for help on using the changeset viewer.