Skip to content

Instantly share code, notes, and snippets.

@Pross
Last active August 20, 2024 16:36
Show Gist options
  • Save Pross/0b517612bb1d1dfb17083b9b32628b82 to your computer and use it in GitHub Desktop.
Save Pross/0b517612bb1d1dfb17083b9b32628b82 to your computer and use it in GitHub Desktop.
Publish BB draft data on certain date/time
**
* Convert BB drafted changes to live data on a certain date.
*/
class Publish_BB_Draft {
public $date = 'Aug 10 17:00'; // Day/time to convert
public $post_id = 474688; // the post/page/layout id
public function __construct() {
// Convert date to a timestamp
$this->date = strtotime( gmdate( 'Y-m-d H:i:s', strtotime( $this->date ) ) );
// if today is after date, bail
if ( time() > $this->date ) {
return false;
}
// Schedule the single event
wp_schedule_single_event( $this->date, 'publish_draft_changes' );
// Make the changes
add_action( 'publish_draft_changes', function () {
$draft_data = get_post_meta( $this->post_id, '_fl_builder_draft', true );
$draft_settings = get_post_meta( $this->post_id, '_fl_builder_draft_settings', true );
if ( ! empty( $draft_data ) && ! empty( $draft_settings ) ) {
update_post_meta( $this->post_id, '_fl_builder_data', $draft_data );
update_post_meta( $this->post_id, '_fl_builder_data_settings', $draft_settings );
}
} );
}
}
new Publish_BB_Draft;
@zackpyle
Copy link

Example to schedule multiple:

class Publish_BB_Draft {

    public $date;
    public $post_id;

    public function __construct( $post_id, $date ) {
        $this->post_id = $post_id;
        $this->date    = strtotime( gmdate( 'Y-m-d H:i:s', strtotime( $date ) ) );

        // if today is after date, bail
        if ( time() > $this->date ) {
            return false;
        }

        // Schedule the single event
        wp_schedule_single_event( $this->date, 'publish_draft_changes_' . $this->post_id );

        // Make the changes
        add_action( 'publish_draft_changes_' . $this->post_id, function () {
            $draft_data     = get_post_meta( $this->post_id, '_fl_builder_draft', true );
            $draft_settings = get_post_meta( $this->post_id, '_fl_builder_draft_settings', true );

            if ( ! empty( $draft_data ) && ! empty( $draft_settings ) ) {
                update_post_meta( $this->post_id, '_fl_builder_data', $draft_data );
                update_post_meta( $this->post_id, '_fl_builder_data_settings', $draft_settings );
            }
        } );
    }
}

$posts_to_schedule = [
    [ 'post_id' => 474688, 'date' => 'Aug 10 17:00' ],
    [ 'post_id' => 474689, 'date' => 'Aug 11 12:00' ],
    [ 'post_id' => 474690, 'date' => 'Aug 12 09:00' ],
];

foreach ( $posts_to_schedule as $post ) {
    new Publish_BB_Draft( $post['post_id'], $post['date'] );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment