Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/7456709f30f3ab92dcd87e5e72a85bbf to your computer and use it in GitHub Desktop.
Save mlbd/7456709f30f3ab92dcd87e5e72a85bbf to your computer and use it in GitHub Desktop.
public function api_get_step_details( $step_details_criteria ) {
if ( ! wp_verify_nonce( $step_details_criteria->get_header( 'x_wp_nonce' ), 'wp_rest' ) ) {
wp_die( esc_html__( 'Unauthorized access.', 'oasisworkflow' ) );
}
if ( ! current_user_can( 'ow_submit_to_workflow' ) && ! current_user_can( 'ow_sign_off_step' ) ) {
wp_die( esc_html__( 'You are not allowed to get workflow step details.', 'oasisworkflow' ) );
}
// sanitize incoming data
$history_id = intval( $step_details_criteria['action_history_id'] );
$step_id = intval( $step_details_criteria['step_id'] );
$post_id = intval( $step_details_criteria['post_id'] );
// create an array of all the inputs
$step_details_params = array(
"step_id" => $step_id,
"post_id" => $post_id,
"history_id" => $history_id
);
// initialize the return array
$step_details = array(
"users" => "",
"process" => "",
"assign_to_all" => 0,
"due_date" => ""
);
// get step users
$users_and_process_info = $this->get_users_in_step( $step_id, $post_id );
if ( $users_and_process_info != null ) {
$step_details["users"] = $users_and_process_info["users"];
$step_details["process"] = $users_and_process_info["process"];
$step_details["assign_to_all"] = $users_and_process_info["assign_to_all"];
}
// get the due date for the step
$default_due_days = get_option( 'oasiswf_default_due_days' ) ? get_option( 'oasiswf_default_due_days' ) : 1;
$due_date = date_i18n( OASISWF_EDIT_DATE_FORMAT, current_time( 'timestamp' ) + DAY_IN_SECONDS * $default_due_days );
$start = '-';
$end = ' ';
$replace_string = '';
$formatted_date = preg_replace( '#(' . preg_quote( $start ) . ')(.*?)(' . preg_quote( $end ) . ')#si', '$1' . $replace_string . '$3', $due_date );
$formatted_date = str_replace( "-", "", $formatted_date );
$final_due_date = '';
// Try normal parsing first
try {
$date_obj = new DateTime($formatted_date);
if ($date_obj) {
$final_due_date = $date_obj->format('Y-m-d');
}
} catch (Exception $e) {
$final_due_date = '';
}
// If still empty, it's mean formatting failed due to invalid date Or irregular date format Or contain special characters
// In that case, do a "digits only" parse
if (!$final_due_date) {
$clean_date = preg_replace('/[^\d ]/', ' ', $formatted_date);
$clean_date = preg_replace('/\s+/', ' ', trim($clean_date));
$parts = explode(' ', $clean_date);
if (count($parts) >= 3) {
list($month, $day, $year) = $parts;
if (checkdate((int)$month, (int)$day, (int)$year)) {
$final_due_date = sprintf('%04d-%02d-%02d', $year, $month, $day);
}
}
}
$step_details["due_date"] = $final_due_date;
return $step_details;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment