Skip to content

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

Merged
merged 2 commits into from
Jul 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions gp-randomizer/gpr-randomize-fields-to-its-own-page.php
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 );
Comment on lines +10 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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:

 add_filter( 'gpr_filtered_fields', function( $filtered_fields, $form ) {
+	if ( ! function_exists( 'gp_randomizer' ) || ! gp_randomizer() ) {
+		return $filtered_fields;
+	}
 
 	$feed_id = gp_randomizer()->get_default_feed_id( $form['id'] );
 	$settings = rgar( gp_randomizer()->get_feed( $feed_id ), 'meta', array() );
🤖 Prompt for AI Agents
In gp-randomizer/gpr-randomize-fields-to-its-own-page.php around lines 10 to 21,
the code calls the gp_randomizer() function without checking if it exists, which
can cause fatal errors if the plugin is deactivated. Add a conditional check at
the start of the filter callback to verify if the gp_randomizer() function
exists; if it does not, return the original $filtered_fields immediately to
prevent errors. This ensures safe execution even when the plugin dependency is
missing.


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
Copy link

Choose a reason for hiding this comment

The 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 );
 	}

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In gp-randomizer/gpr-randomize-fields-to-its-own-page.php from lines 23 to 100,
the functions arrange_fields_by_page and randomize_fields_within_pages are
defined in the global scope, risking naming conflicts with other plugins or
themes. To fix this, wrap these functions inside a unique namespace or add a
unique prefix to their names to avoid collisions. Also, update any calls to
these functions accordingly, such as in filter hooks, to use the new namespaced
or prefixed versions.

Loading