GravityKit / GravityView

The best and easiest way to display Gravity Forms entries on your website.
https://www.gravitykit.com/products/gravityview/
245 stars 63 forks source link

Not possible to change the status of a post created with the Advanced Post Creation add-on integration #2111

Open rafaehlers opened 3 weeks ago

rafaehlers commented 3 weeks ago

A customer asked:

I have advanced post creation from gravityforms and use it in conjuction with gravityview to keep the posts up to date and manage their content. However, one challenge I have not been able to solve is how to unpublish the post from gravityview. I attempted to modify the snippet: Set Post Status by Product Field Value snippet, but couldn't get the results I was hoping. https://gravitywiz.com/snippet-library/gw-set-post-status-by-product-field-value/ Is there a way to tie these together?

Let's try to find a way to change this using the Edit Entry layout of GravityView instead of relying on custom coding.

I tried some custom coding without success:

// Gravity Wiz code: https://gravitywiz.com/snippet-library/gw-set-post-status-by-product-field-value/
// GravityView APC code: https://github.com/GravityKit/GravityView/commit/f7f44bc8a28c2a83458975de80e1ab510490f63b#diff-5669163db482120b66c8b5d814bc81e0721c3cafce66ec342f635e6029130df8R44
// Video: https://www.loom.com/share/a98f3a19232b4304b8aae96171ae7624
// 
add_action( 'gravityview/edit_entry/after_update', 'gk_update_post_status', 10, 3 );

function gk_update_post_status( $form, $entry_id, $object = null ) {

    if ( 63 !== (int) $form['id'] ) { // replace 100 with your form ID
        return;
    }

    if( ! class_exists('GF_Advanced_Post_Creation') ) {
        return;
    }

    $entry = GFAPI::get_entry( $entry_id );

    $apc = GF_Advanced_Post_Creation::get_instance();

    $created_posts = gform_get_meta( $entry_id, $apc->get_slug() . '_post_id' );

    if ( ! $created_posts ) {
        return;
    }

    $feeds = $apc->get_active_feeds( rgar( $form, 'id' ) );

    if ( ! $feeds ) {
        return;
    }

    // Map feeds on their id for easy access.
    $feeds = array_column( $feeds, null, 'id' );

    foreach ( $created_posts as $created_post ) {

        $feed_id = rgar( $created_post, 'feed_id' );
        $feed    = rgar( $feeds, $feed_id );
        if ( ! $feed ) {
            continue;
        }

        switch ( $entry[10] ) { // ID of the Status field on the form
            case 'Draft':
                $object->entry['post_status'] = 'draft';
                break;
            case 'Publish':
                $object->entry['post_status'] = 'publish';
                break;

        }

        $apc->update_post( $created_post['post_id'], $feed, $object->entry, $form );

        /** THIS BELOW DIDN'T WORK
        $update_post = array(
            'ID'           => $post_id,
            'post_status'  => $post_status,
            'post_title'   => 'This is the post title.',
        );

        wp_update_post( $update_post );
        if (is_wp_error($post_id)) {
            $errors = $post_id->get_error_messages();
            foreach ($errors as $error) {
                echo $error;
            }
        }
        */

    }

}
rafaehlers commented 3 weeks ago

https://secure.helpscout.net/conversation/2675629484/56471