Last active
March 29, 2018 06:33
-
-
Save Idealien/63b313ca66e458950f18 to your computer and use it in GitHub Desktop.
Gravity Flow - Different ways to have workflow step(s) trigger post status update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Update after each step completion - probably executes too often for most use cases | |
add_action( 'gravityflow_step_complete', 'gravityflow_step_complete_post_publish', 10, 4 ); | |
function gravityflow_step_complete_post_publish( $step_id, $entry_id, $form_id, $status ) { | |
$entry = GFAPI::get_entry( $entry_id ); | |
$currentPost = get_post( rgar( $entry, 'post_id' ) ); | |
if("draft" == $currentPost->post_status) { | |
wp_update_post( | |
array( | |
'ID' => $currentPost->ID, | |
'post_status' => 'publish', | |
) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Custom Step Type that lets you pick status to move between as a specific step. | |
add_action( 'gravityflow_loaded', function(){ | |
class Gravity_Flow_Step_Update_Post_Status_Process extends Gravity_Flow_Step { | |
// Make this unique | |
public $_step_type = 'update_post_status'; | |
/** | |
* Returns the label for the step type. | |
* | |
* @return string | |
*/ | |
public function get_label() { | |
return 'Update Post Status'; | |
} | |
public function get_icon_url() { | |
//Replace with your own path / image | |
return IDEALIEN_TRAINING_PATH . '/images/post_status.png'; | |
} | |
public function get_settings() { | |
$postStatuses = get_post_statuses(); | |
$outputStatus = array(); | |
foreach($postStatuses as $key => $value) { | |
$outputStatus[] = array( 'value' => $key, 'label' => $value ); | |
} | |
return array( | |
'title' => 'Update Post Status Step', | |
'fields' => array( | |
array( | |
'name' => 'new_post_status', | |
'required' => true, | |
'label' => 'New Post Status', | |
'type' => 'select', | |
'choices' => $outputStatus, | |
), | |
), | |
); | |
} | |
/** | |
* Retrieve the associated entry / post object and update the status to selected value | |
* | |
* @return bool Is the step complete? | |
*/ | |
public function process(){ | |
$currentPost = get_post( rgar( $this->get_entry(), 'post_id' ) ); | |
if("draft" == $currentPost->post_status) { | |
wp_update_post( | |
array( | |
'ID' => $currentPost->ID, | |
'post_status' => $this->new_post_status, | |
) | |
); | |
} | |
return true; | |
} | |
} | |
// Register the step | |
Gravity_Flow_Steps::register( new Gravity_Flow_Step_Update_Post_Status_Process() ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment