WordPress.org

Make WordPress Core

Ticket #14302: object_property_functions.14302.diff

File object_property_functions.14302.diff, 2.8 KB (added by filosofo, 8 years ago)
  • wp-includes/post.php

     
    539539}
    540540
    541541/**
     542 * Get a post object's property while being agnostic about its database location.
     543 *
     544 * @since 3.1.0
     545 *
     546 * @param int $object_id The ID of the object for which to get the property.
     547 * @param string $prop_name The name of the property to get.
     548 * @param bool $force_single Optional, default is false.  If true, return only the first value of the
     549 *              specified property even if multiples of that property exist.
     550 * @return mixed The object's property if set; null otherwise.
     551 */
     552function get_object_property( $object_id = 0, $prop_name = '', $force_single = false ) {
     553        global $wpdb;
     554       
     555        $object_id = (int) $object_id;
     556
     557        if ( empty( $object_id ) || empty( $prop_name ) )
     558                return null;
     559
     560        $post_object = get_post( $object_id );
     561
     562        if ( isset( $post_object->$prop_name ) ) {
     563                return $post_object->$prop_name;
     564        } else {
     565                $metadata = get_post_meta( $object_id, $prop_name, $force_single );
     566                if ( ! $force_single && is_array( $metadata ) && 1 == count( $metadata ) )
     567                        return array_shift( $metadata );
     568                else
     569                        return $metadata;
     570        }
     571
     572}
     573
     574/**
     575 * Set a post object's property while being agnostic about its database location.
     576 *
     577 * @since 3.1.0
     578 *
     579 * @param int $object_id The ID of the object for which to set the property.
     580 * @param string $prop_name The name of the property to set.
     581 * @param mixed $prop_value The value of the property to set.
     582 *      If $prop_value is NULL and is not a posts table field value,
     583 *      it will be deleted from the posts meta table.
     584 * @return bool Whether the setting actually made a change.
     585 */
     586function set_object_property( $object_id = 0, $prop_name = '', $prop_value = false ) {
     587        global $wpdb;
     588
     589        $object_id = (int) $object_id;
     590
     591        if ( empty( $object_id ) || empty( $prop_name ) )
     592                return false;
     593
     594        $posts_db_fields = array(       
     595                'comment_count',
     596                'comment_status',
     597                'guid',
     598                'menu_order',
     599                'pinged',
     600                'ping_status',
     601                'post_author',
     602                'post_category',
     603                'post_content',
     604                'post_content_filtered',
     605                'post_date',
     606                'post_date_gmt',
     607                'post_excerpt',
     608                'post_mime_type',
     609                'post_modified',
     610                'post_modified_gmt',
     611                'post_name',
     612                'post_parent',
     613                'post_password',
     614                'post_status',
     615                'post_title',
     616                'post_type',
     617                'to_ping',
     618        );
     619
     620        if ( in_array( $prop_name, $posts_db_fields ) ) {
     621                return $wpdb->update( $wpdb->posts, array( $prop_name => $prop_value ), array( 'ID' => $object_id ) );
     622        } elseif ( null === $prop_value ) {
     623                return delete_post_meta( $object_id, $prop_name );
     624        } else {
     625                return update_post_meta( $object_id, $prop_name, $prop_value );
     626        }
     627}
     628
     629/**
    542630 * Register a post type. Do not use before init.
    543631 *
    544632 * A simple function for creating or modifying a post status based on the