-
Notifications
You must be signed in to change notification settings - Fork 92
gpuid-generate-post-workflow.php: Added snippet to delay generation of Unique ID until a workflow step is complete.
#1102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Gravity Perks // Unique ID // Conditional Unique ID for Gravity Flow | ||||||||||||||||||||||||||||||||||||||||||
| * https://gravitywiz.com/documentation/gravity-forms-unique-id/ | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * Instruction Video: https://www.loom.com/share/e799e8b4b0984d99a66da79faa34ffee | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * Prevent a Unique ID field from generating its value until a specific Gravity Flow Workflow step is completed. | ||||||||||||||||||||||||||||||||||||||||||
| * Useful when you want to generate the Unique ID only after approval or other workflow steps. | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * Plugin Name: Conditional Unique ID for Gravity Flow | ||||||||||||||||||||||||||||||||||||||||||
| * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-unique-id/ | ||||||||||||||||||||||||||||||||||||||||||
| * Description: Prevent a Unique ID field from generating its value until a specific Gravity Flow step is completed. | ||||||||||||||||||||||||||||||||||||||||||
| * Author: Gravity Wiz | ||||||||||||||||||||||||||||||||||||||||||
| * Version: 0.1 | ||||||||||||||||||||||||||||||||||||||||||
| * Author URI: https://gravitywiz.com | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| class GPUID_Generate_Post_Workflow { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private $_args = array(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public function __construct( $args = array() ) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| $this->_args = wp_parse_args( $args, array( | ||||||||||||||||||||||||||||||||||||||||||
| 'form_id' => false, | ||||||||||||||||||||||||||||||||||||||||||
| 'field_id' => false, | ||||||||||||||||||||||||||||||||||||||||||
| 'step_id' => false, | ||||||||||||||||||||||||||||||||||||||||||
| ) ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if ( ! $this->_args['form_id'] || ! $this->_args['field_id'] || ! $this->_args['step_id'] ) { | ||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| add_action( 'init', array( $this, 'init' ) ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public function init() { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| add_filter( 'gpui_unique_id', array( $this, 'prevent_unique_id_generation' ), 10, 3 ); | ||||||||||||||||||||||||||||||||||||||||||
| add_action( 'gravityflow_step_complete', array( $this, 'maybe_generate_unique_id' ), 10, 4 ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public function prevent_unique_id_generation( $unique, $form_id, $field_id ) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if ( $form_id == $this->_args['form_id'] && $field_id == $this->_args['field_id'] && ! gravity_flow()->is_workflow_detail_page() ) { | ||||||||||||||||||||||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty character is how we do it on GPUID as well. |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return $unique; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public function maybe_generate_unique_id( $step_id, $entry_id, $form_id, $status ) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if ( $step_id == $this->_args['step_id'] && $form_id == $this->_args['form_id'] ) { | ||||||||||||||||||||||||||||||||||||||||||
| $entry = GFAPI::get_entry( $entry_id ); | ||||||||||||||||||||||||||||||||||||||||||
| if ( ! is_wp_error( $entry ) && empty( $entry[ $this->_args['field_id'] ] ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| $uid_field = GFAPI::get_field( $form_id, $this->_args['field_id'] ); | ||||||||||||||||||||||||||||||||||||||||||
| $uid_field->save_value( GFAPI::get_entry( $entry_id ), $uid_field, false ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant API call in the maybe_generate_unique_id method. There's an unnecessary duplicate call to GFAPI::get_entry() at line 60 when you've already retrieved the entry at line 57. if ( $step_id == $this->_args['step_id'] && $form_id == $this->_args['form_id'] ) {
$entry = GFAPI::get_entry( $entry_id );
if ( ! is_wp_error( $entry ) && empty( $entry[ $this->_args['field_id'] ] ) ) {
$uid_field = GFAPI::get_field( $form_id, $this->_args['field_id'] );
- $uid_field->save_value( GFAPI::get_entry( $entry_id ), $uid_field, false );
+ $uid_field->save_value( $entry, $uid_field, false );
}
}Also, consider using strict comparison operators here: -if ( $step_id == $this->_args['step_id'] && $form_id == $this->_args['form_id'] ) {
+if ( (int) $step_id === $this->_args['step_id'] && (int) $form_id === $this->_args['form_id'] ) {📝 Committable suggestion
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a good suggestion 🧐 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @veryspry Thanks for confirming! The changes will make the code more efficient by eliminating the redundant API call and more robust with strict type comparisons. Would you like me to help with anything else on this snippet? If you found this review helpful, would you consider giving us a shout-out on X? Thank you for using CodeRabbit! |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Configuration | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| new GPUID_Generate_Post_Workflow( array( | ||||||||||||||||||||||||||||||||||||||||||
| 'form_id' => 5, // Replace with your form ID. | ||||||||||||||||||||||||||||||||||||||||||
| 'field_id' => 3, // Replace with your Unique ID field ID. | ||||||||||||||||||||||||||||||||||||||||||
| 'step_id' => 3 // Replace with your Gravity Flow step ID. | ||||||||||||||||||||||||||||||||||||||||||
| ) ); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on throwing individual exceptions here including a message with the exact missing args?
For example: