WordPress.org

Make WordPress Core

Ticket #20564: 20564.4.patch

File 20564.4.patch, 3.5 KB (added by WraithKenny, 5 years ago)
  • wp-includes/post.php

     
    17551755}
    17561756
    17571757/**
     1758 * Add versioned meta data field to a post or revision.
     1759 *
     1760 * Post meta data is called "Custom Fields" on the Administration Screen.
     1761 *
     1762 * @since 3.6.0
     1763 * @uses $wpdb
     1764 * @link http://codex.wordpress.org/Function_Reference/add_versioned_meta
     1765 *
     1766 * @param int $post_id Post or Revision ID.
     1767 * @param string $meta_key Metadata name.
     1768 * @param mixed $meta_value Metadata value.
     1769 * @param bool $unique Optional, default is false. Whether the same key should not be added.
     1770 * @return bool False for failure. True for success.
     1771 */
     1772function add_versioned_meta($post_id, $meta_key, $meta_value, $unique = false) {
     1773        return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
     1774}
     1775
     1776/**
     1777 * Remove versioned metadata matching criteria from a post or revision.
     1778 *
     1779 * You can match based on the key, or key and value. Removing based on key and
     1780 * value, will keep from removing duplicate metadata with the same key. It also
     1781 * allows removing all metadata matching key, if needed.
     1782 *
     1783 * @since 3.6.0
     1784 * @uses $wpdb
     1785 * @link http://codex.wordpress.org/Function_Reference/delete_versioned_meta
     1786 *
     1787 * @param int $post_id Post or Revision ID
     1788 * @param string $meta_key Metadata name.
     1789 * @param mixed $meta_value Optional. Metadata value.
     1790 * @return bool False for failure. True for success.
     1791 */
     1792function delete_versioned_meta($post_id, $meta_key, $meta_value = '') {
     1793        return delete_metadata('post', $post_id, $meta_key, $meta_value);
     1794}
     1795
     1796/**
     1797 * Update versioned post meta field based on post or revision.
     1798 *
     1799 * Use the $prev_value parameter to differentiate between meta fields with the
     1800 * same key and post revision ID.
     1801 *
     1802 * If the meta field for the post or revision does not exist, it will be added.
     1803 *
     1804 * @since 3.6.0
     1805 * @uses $wpdb
     1806 * @link http://codex.wordpress.org/Function_Reference/update_versioned_meta
     1807 *
     1808 * @param int $post_id Post or Revision ID.
     1809 * @param string $meta_key Metadata key.
     1810 * @param mixed $meta_value Metadata value.
     1811 * @param mixed $prev_value Optional. Previous value to check before removing.
     1812 * @return bool False on failure, true if success.
     1813 */
     1814function update_versioned_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
     1815        return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
     1816}
     1817
     1818/**
    17581819 * Retrieve post meta fields, based on post ID.
    17591820 *
    17601821 * The post meta fields are retrieved from the cache where possible,
  • wp-includes/revision.php

     
    335335        if ( is_wp_error( $post_id ) )
    336336                return $post_id;
    337337
     338        // Handle versioned meta data
     339        $versioned_meta = array(); // WordPress Core's versioned meta
     340        $versioned_meta = apply_filters( 'versioned_meta', $versioned_meta );
     341
     342        foreach ( $versioned_meta as $key ) {
     343                $meta = get_post_meta( $revision_id, $key );
     344                delete_post_meta( $post_id, $key );
     345                if ( ! empty( $meta ) ) {
     346                        $unique = 1 == count( $meta ) ? true : false;
     347                        foreach ( $meta as $value )
     348                                add_post_meta( $post_id, $key, $value, $unique );
     349                }
     350        }
     351
    338352        if ( $post_id )
    339353                do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
    340354
     
    706720        $r .= "</table>";
    707721
    708722        return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
    709         }
     723}