-
Notifications
You must be signed in to change notification settings - Fork 93
gpr-randomize-fields-to-its-own-page.php
: Added snippet to randomize fields to its own page.
#1130
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 all commits
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,100 @@ | ||
<?php | ||
/** | ||
* Gravity Perks // Randomizer // Randomize Fields to Its Own Page | ||
* https://gravitywiz.com/documentation/gravity-forms-randomizer/ | ||
* | ||
* Use this snippet to randomize fields within their respective pages. | ||
* | ||
* See video: https://www.loom.com/share/2dd4f3c6995349ae91d4b88f3990294b | ||
*/ | ||
add_filter( 'gpr_filtered_fields', function( $filtered_fields, $form ) { | ||
|
||
$feed_id = gp_randomizer()->get_default_feed_id( $form['id'] ); | ||
$settings = rgar( gp_randomizer()->get_feed( $feed_id ), 'meta', array() ); | ||
|
||
if ( ! $settings['display_count'] ) { | ||
$filtered_fields = randomize_fields_within_pages( $form['fields'] ); | ||
} else { | ||
$filtered_fields = arrange_fields_by_page( $form['fields'], $filtered_fields ); | ||
} | ||
return $filtered_fields; | ||
}, 10, 2 ); | ||
|
||
function arrange_fields_by_page( $fields_backup, $filtered_fields ) { | ||
$filtered_map = array(); | ||
foreach ( $filtered_fields as $field ) { | ||
$filtered_map[ $field->id ] = $field; | ||
} | ||
|
||
$result = array(); | ||
$used_ids = array(); | ||
|
||
foreach ( $fields_backup as $field ) { | ||
if ( $field->type === 'page' ) { | ||
$result[] = $field; | ||
} elseif ( isset( $filtered_map[ $field->id ] ) ) { | ||
$result[] = $filtered_map[ $field->id ]; | ||
|
||
$used_ids[] = $field->id; | ||
} | ||
} | ||
|
||
foreach ( $filtered_fields as $field ) { | ||
if ( ! in_array( $field->id, $used_ids, true ) && $field->type !== 'page' ) { | ||
$result[] = $field; | ||
} | ||
} | ||
|
||
// Clean up redundant pages | ||
$cleaned_result = array(); | ||
$prev_is_page = false; | ||
|
||
foreach ( $result as $field ) { | ||
if ( $field->type === 'page' ) { | ||
if ( $prev_is_page ) { | ||
continue; // Skip consecutive page | ||
} | ||
$prev_is_page = true; | ||
} else { | ||
$prev_is_page = false; | ||
} | ||
$cleaned_result[] = $field; | ||
} | ||
|
||
// Remove leading page | ||
if ( isset( $cleaned_result[0] ) && $cleaned_result[0]->type === 'page' ) { | ||
array_shift( $cleaned_result ); | ||
} | ||
|
||
// Remove trailing page | ||
if ( ! empty( $cleaned_result ) && end( $cleaned_result )->type === 'page' ) { | ||
array_pop( $cleaned_result ); | ||
} | ||
|
||
return $cleaned_result; | ||
} | ||
|
||
function randomize_fields_within_pages( $fields ) { | ||
$result = array(); | ||
$current_group = array(); | ||
|
||
foreach ( $fields as $field ) { | ||
if ( $field->type === 'page' ) { | ||
// Shuffle current group and add to result. | ||
shuffle( $current_group ); | ||
$result = array_merge( $result, $current_group ); | ||
$result[] = $field; // Add the page marker. | ||
$current_group = array(); // Start new group. | ||
} else { | ||
$current_group[] = $field; | ||
} | ||
} | ||
|
||
// Add any remaining fields in the last group. | ||
if ( ! empty( $current_group ) ) { | ||
shuffle( $current_group ); | ||
$result = array_merge( $result, $current_group ); | ||
} | ||
|
||
return $result; | ||
} | ||
Comment on lines
+23
to
+100
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. 🛠️ Refactor suggestion Consider function naming conflicts in global scope. The helper functions are defined in global scope which could cause naming conflicts with other plugins or themes. Wrap the functions in a namespace or use unique prefixes: -function arrange_fields_by_page( $fields_backup, $filtered_fields ) {
+function gpr_arrange_fields_by_page( $fields_backup, $filtered_fields ) {
// ... function body ...
}
-function randomize_fields_within_pages( $fields ) {
+function gpr_randomize_fields_within_pages( $fields ) {
// ... function body ...
} And update the function calls in the filter hook: if ( ! $settings['display_count'] ) {
- $filtered_fields = randomize_fields_within_pages( $form['fields'] );
+ $filtered_fields = gpr_randomize_fields_within_pages( $form['fields'] );
} else {
- $filtered_fields = arrange_fields_by_page( $form['fields'], $filtered_fields );
+ $filtered_fields = gpr_arrange_fields_by_page( $form['fields'], $filtered_fields );
}
🤖 Prompt for AI Agents
|
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.
Add error handling for plugin dependency.
The filter hook logic is sound, but there's no validation that the required
gp_randomizer()
plugin function exists, which could cause fatal errors if the plugin is deactivated.Apply this diff to add proper error handling:
🤖 Prompt for AI Agents