Skip to content

Instantly share code, notes, and snippets.

@mlbd
Created April 17, 2025 16:45
Show Gist options
  • Save mlbd/85b78cd16426cd961fd0f853c5f8c6e7 to your computer and use it in GitHub Desktop.
Save mlbd/85b78cd16426cd961fd0f853c5f8c6e7 to your computer and use it in GitHub Desktop.
/**
* Validate the editorial checklist condition.
*
* @param array $validation_result The validation result array passed by the filter.
* @param array $workflow_action_params The workflow action parameters array passed by the filter.
*
* @return array The validation result array with the error message and type.
*/
function owf_ediotrial_checklist_validate_condition( $validation_result, $workflow_action_params ) {
if ( get_option( 'oasiswf_checklist_action' ) === 'by_pass_the_checklist' ) {
return $validation_result;
}
$post_id = intval( $workflow_action_params["post_id"] );
$step_id = "";
if ( isset( $workflow_action_params["step_id"] ) ) {
$step_id = intval( $workflow_action_params["step_id"] );
}
$step_decision = '';
if ( isset( $workflow_action_params["step_decision"] ) ) {
$step_decision = sanitize_text_field( $workflow_action_params["step_decision"] );
}
// if the history_id is not null, it indicates that the post is in some workflow.
// So, the step_id from the $workflow_action_params is essentially the next step_id and NOT current step_id
// Use history to get the current step_id
$ow_history_service = new OW_History_Service();
$history_id = $workflow_action_params['history_id'];
if ( ! empty( $history_id ) ) {
// get the current step_id from the history
$history_details = $ow_history_service->get_action_history_by_id( $history_id );
$step_id = $history_details->step_id;
}
$associated_condition_group_id = $this->get_condition_group_for_step( $post_id, $step_id );
if ( empty( $associated_condition_group_id ) ) {
return $validation_result;
}
// don't evaluate the conditions if the user signed off as "rejected" or "unable to complete"
// return an empty validation message
if ( ! empty( $step_decision ) && $step_decision == "unable" ) {
return $validation_result;
}
// Get workflow action post parameters by post id
$post_parameters = $this->get_post_parameters( $post_id );
$checklist_parameters = array_merge( $workflow_action_params, $post_parameters );
// validate the context condition as you want to validate use $post_id for post id and $associated_condition_group_id for checklist group id
// after validation or running your condition make sure to set the error message in the validation result
// and set the error type as "error" or "warning" like below
$context_messages = "your custom error message";
// Make sure to merge the error message with the existing error message in the validation result
$validation_result['error_message'] = array_merge( $validation_result['error_message'], $context_messages );
$validation_result['error_type'] = "error";
// Check if checklist action is warning
if ( get_option( 'oasiswf_checklist_action' ) === 'show_as_warning' &&
count( $validation_result['error_message'] ) > 0 ) {
$validation_result['error_type'] = "warning";
}
return $validation_result;
}
add_filter( 'owf_api_submit_to_workflow_pre', 'owf_ediotrial_checklist_validate_condition', 10, 2 );
add_filter( 'owf_api_sign_off_workflow_pre', 'owf_ediotrial_checklist_validate_condition', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment