Skip to content

Commit 7bf995e

Browse files
authored
gpuid-generate-post-workflow.php: Added snippet to delay generation of Unique ID until a workflow step is complete.
1 parent 54c5969 commit 7bf995e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Unique ID // Conditional Unique ID for Gravity Flow
4+
* https://gravitywiz.com/documentation/gravity-forms-unique-id/
5+
*
6+
* Instruction Video: https://www.loom.com/share/e799e8b4b0984d99a66da79faa34ffee
7+
*
8+
* Prevent a Unique ID field from generating its value until a specific Gravity Flow Workflow step is completed.
9+
* Useful when you want to generate the Unique ID only after approval or other workflow steps.
10+
*
11+
* Plugin Name: Conditional Unique ID for Gravity Flow
12+
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-unique-id/
13+
* Description: Prevent a Unique ID field from generating its value until a specific Gravity Flow step is completed.
14+
* Author: Gravity Wiz
15+
* Version: 0.1
16+
* Author URI: https://gravitywiz.com
17+
*/
18+
class GPUID_Generate_Post_Workflow {
19+
20+
private $_args = array();
21+
22+
public function __construct( $args = array() ) {
23+
24+
$this->_args = wp_parse_args( $args, array(
25+
'form_id' => false,
26+
'field_id' => false,
27+
'step_id' => false,
28+
) );
29+
30+
if ( ! $this->_args['form_id'] || ! $this->_args['field_id'] || ! $this->_args['step_id'] ) {
31+
return;
32+
}
33+
34+
add_action( 'init', array( $this, 'init' ) );
35+
36+
}
37+
38+
public function init() {
39+
40+
add_filter( 'gpui_unique_id', array( $this, 'prevent_unique_id_generation' ), 10, 3 );
41+
add_action( 'gravityflow_step_complete', array( $this, 'maybe_generate_unique_id' ), 10, 4 );
42+
43+
}
44+
45+
public function prevent_unique_id_generation( $unique, $form_id, $field_id ) {
46+
47+
if ( $form_id == $this->_args['form_id'] && $field_id == $this->_args['field_id'] && ! gravity_flow()->is_workflow_detail_page() ) {
48+
return '';
49+
}
50+
51+
return $unique;
52+
}
53+
54+
public function maybe_generate_unique_id( $step_id, $entry_id, $form_id, $status ) {
55+
56+
if ( (int) $step_id === $this->_args['step_id'] && (int) $form_id === $this->_args['form_id'] ) {
57+
$entry = GFAPI::get_entry( $entry_id );
58+
if ( ! is_wp_error( $entry ) && empty( $entry[ $this->_args['field_id'] ] ) ) {
59+
$uid_field = GFAPI::get_field( $form_id, $this->_args['field_id'] );
60+
$uid_field->save_value( GFAPI::get_entry( $entry_id ), $uid_field, false );
61+
}
62+
}
63+
}
64+
65+
}
66+
67+
# Configuration
68+
69+
new GPUID_Generate_Post_Workflow( array(
70+
'form_id' => 5, // Replace with your form ID.
71+
'field_id' => 3, // Replace with your Unique ID field ID.
72+
'step_id' => 3 // Replace with your Gravity Flow step ID.
73+
) );

0 commit comments

Comments
 (0)