WordPress.org

Make WordPress Core

Ticket #20564: 20564.5.patch

File 20564.5.patch, 4.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

     
    298298 * Restores a post to the specified revision.
    299299 *
    300300 * Can restore a past revision using all fields of the post revision, or only selected fields.
     301 * Since 3.6 also uses versioned meta data.
    301302 *
    302303 * @package WordPress
    303304 * @subpackage Post_Revisions
     
    305306 *
    306307 * @uses wp_get_post_revision()
    307308 * @uses wp_update_post()
     309 * @uses get_versioned_meta_keys()
    308310 * @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
    309311 *  is successful.
    310312 *
     
    335337        if ( is_wp_error( $post_id ) )
    336338                return $post_id;
    337339
     340        // Handle versioned meta data
     341        foreach ( get_versioned_meta_keys() as $key ) {
     342                $meta = get_post_meta( $revision_id, $key );
     343                delete_post_meta( $post_id, $key );
     344                if ( ! empty( $meta ) ) {
     345                        $unique = 1 == count( $meta ) ? true : false;
     346                        foreach ( $meta as $value )
     347                                add_post_meta( $post_id, $key, $value, $unique );
     348                }
     349        }
     350
    338351        if ( $post_id )
    339352                do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
    340353
     
    349362}
    350363
    351364/**
     365 * Gets array of meta keys that should be versioned.
     366 *
     367 * @package WordPress
     368 * @subpackage Post_Revisions
     369 * @since 3.6.0
     370 *
     371 * @uses apply_filters() Calls 'versioned_meta' on Core's list of versioned meta.
     372 *
     373 * @return array Versioned meta keys.
     374 */
     375function get_versioned_meta_keys() {
     376        $versioned_meta = array(); // WordPress Core's versioned meta
     377        return apply_filters( 'versioned_meta', $versioned_meta );
     378}
     379
     380/**
    352381 * Deletes a revision.
    353382 *
    354383 * Deletes the row from the posts table corresponding to the specified revision.
     
    706735        $r .= "</table>";
    707736
    708737        return array( 'html' => $r, 'linesadded' => $linesadded, 'linesdeleted' => $linesdeleted );
    709         }
     738}