bu-ist / bu-versions

Make and save edits to published posts/pages in WordPress without immediately replacing the public content.
https://developer.bu.edu/bu-versions/
19 stars 5 forks source link

Advanced custom fields #7

Open defunctl opened 11 years ago

defunctl commented 11 years ago

Is there some way to get this to work with Advanced custom fields?

http://www.advancedcustomfields.com/

unwrittendevin commented 11 years ago

I've gotten it to work with using Advanced Custom Fields

    // Adding Advanced Custom Field Data
    function create( $post_id, $alt_post_type, $meta_keys = null ) {
    $this->get_version( $post_id );
    if ( $this->has_version() ) {
        return new WP_Error( 'alternate_already_exists', __( 'An alternate version already exists for this post.',   SUV_TEXTDOMAIN ) );
    }

    $this->original = get_post( $post_id );
    if ( ! isset( $this->original ) ) {
        return new WP_Error( 'alternate_no_original', sprintf( __( 'The post ID: %s could not be found.', SUV_TEXTDOMAIN ), $post_id ) );
    }
    $new_version['post_type'] = $alt_post_type;
    $new_version['post_parent'] = $this->original->ID;
    $new_version['ID'] = null;
    $new_version['post_status'] = 'draft';
    $new_version['post_content'] = $this->original->post_content;
    $new_version['post_name'] = $this->original->post_name;
    $new_version['post_title'] = $this->original->post_title;
    $new_version['post_excerpt'] = $this->original->post_excerpt;

    // Loop through Post Custom Meta and add to array 
    $custom_field_keys = get_post_custom_keys($new_version['post_parent']);
    foreach ( $custom_field_keys as $key => $value ) {
        $new_version[$value] = $this->original->$value;
    }
    //
    $result = wp_insert_post($new_version);
    if ( ! is_wp_error( $result ) ) {
        $this->post = get_post( $result );
        $this->copy_original_meta( $new_version );
        update_post_meta( $this->original->ID, self::tracking_meta_key, $this->post->ID );
    }
    return $result;
}
/* Because of sanization and serialization, it may be better to use SQL, but for now we are using the API
 **/
private function copy_original_meta( $new_version ) {   
    foreach ( $new_version as $key ) {
        $values = get_post_meta( $this->original->ID, $key );
        foreach ( $values as $v ) { 
            update_post_meta( $this->post->ID, $key, $v );
        }
        $taxonomies = get_object_taxonomies( $new_version['post_type'] );
            foreach( $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $this->original->ID, $taxonomy, array('fields' => 'names') );
            wp_set_object_terms( $this->post->ID, $terms, $taxonomy );
        }
        $custom_fields = get_post_custom( $this->original->ID );
            foreach ( $custom_fields as $key => $value ) {
            add_post_meta( $this->post->ID, $key, maybe_unserialize($value[0]) );
        }
    }
    update_post_meta( $this->post->ID, '_bu_version_copied_keys', $meta_keys);
}
defunctl commented 10 years ago

Where did you add this code?

defunctl commented 10 years ago

Ahh I see you've manually edited bu-versions.php. Is it possible for the authors to fork this into the core?

unwrittendevin commented 10 years ago

Sounds good to me. I have no issues with you guys adjusting my code to work better with the overall plugin!

On Wed, Jan 22, 2014 at 5:22 PM, defunctl notifications@github.com wrote:

Ahh I see you've manually edited bu-versions.php. Is it possible for the authors to fork this into the core?

— Reply to this email directly or view it on GitHubhttps://github.com/bu-ist/bu-versions/issues/7#issuecomment-33075565 .

mgburns commented 10 years ago

That particular approach to handling cloned meta data isn't something we're ready to merge in to this plugin, but we are actively investigating alternatives.

ChongoShaun commented 9 years ago

When previewing the page using the ACF code above, the changes made are not shown in preview mode. Just an FYI

tomfinitely commented 8 years ago

Has any progress been made with custom post meta copying?