-
Notifications
You must be signed in to change notification settings - Fork 92
gc-notion/gcn-add-relation-from-dropdown.php: Added snippet to create relations in Notion from GPPA populated Dropdowns.
#1110
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
Conversation
WalkthroughA new PHP script is added to enable integration between Gravity Connect and Notion. It introduces a function that allows the creation of a Notion relation property on a page using data from Gravity Forms submissions. The script includes a configurable function and usage instructions. Changes
Sequence Diagram(s)sequenceDiagram
participant GravityForms
participant gcn_create_relation
participant NotionAPI
GravityForms->>gcn_create_relation: Form submission triggers hook
gcn_create_relation->>gcn_create_relation: Extract Notion page ID from entry
gcn_create_relation->>gcn_create_relation: Add relation property to Notion data
gcn_create_relation->>NotionAPI: Update Notion page with relation property
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
🧹 Nitpick comments (2)
gc-notion/gcn-add-relation-from-dropdown.php (2)
11-11: Fix typo in documentation.The documentation contains a grammatical error.
- * 3. Configure the Usage Example below to match your form, feed, dropdown field id and relation property name (this much match the relation property name in Notion exactly). + * 3. Configure the Usage Example below to match your form, feed, dropdown field id and relation property name (this must match the relation property name in Notion exactly).
59-66: Consider adding error handling to the usage example.While the example is clear, it would be helpful to demonstrate best practices by including error handling or validation.
Consider wrapping the function call in a conditional check:
-gcn_add_relation_property_from_dropdown( - array( - 'form_id' => 5, // Change this to your form ID. - 'feed_id' => 2, // Change this to the ID of the feed you want to use. - 'dropdown_field_id' => 3, // Change this to the ID of the field you want to populate. - 'property_name' => 'Tasks', // Change this to the name of the relation property in your Notion database. - ) -); +// Only initialize if Gravity Connect Notion is active +if ( function_exists( 'gcn_add_relation_property_from_dropdown' ) ) { + gcn_add_relation_property_from_dropdown( + array( + 'form_id' => 5, // Change this to your form ID. + 'feed_id' => 2, // Change this to the ID of the feed you want to use. + 'dropdown_field_id' => 3, // Change this to the ID of the field you want to populate. + 'property_name' => 'Tasks', // Change this to the name of the relation property in your Notion database. + ) + ); +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gc-notion/gcn-add-relation-from-dropdown.php(1 hunks)
🔇 Additional comments (1)
gc-notion/gcn-add-relation-from-dropdown.php (1)
20-31:❓ Verification inconclusive
Verify the filter naming pattern matches the plugin's expectations.
The dynamic filter name construction assumes a specific pattern. Please ensure this matches the actual filter names used by the Gravity Connect Notion plugin.
Run the following script to search for similar filter patterns in the codebase:
🏁 Script executed:
#!/bin/bash # Description: Search for similar filter patterns in the Gravity Connect Notion plugin # Search for gcn_notion_page_data_add filter usage rg -A 5 "gcn_notion_page_data_add" --type php # Search for apply_filters calls that might use this pattern rg -A 3 "apply_filters.*gcn.*notion.*page.*data" --type phpLength of output: 497
🏁 Script executed:
#!/bin/bash # Inspect apply_filters usage in gcn-add-relation-from-dropdown.php file="gc-notion/gcn-add-relation-from-dropdown.php" # Find apply_filters occurrences and show context apply_lines=$(rg -n "apply_filters" --type php "$file" | cut -d: -f1) if [ -z "$apply_lines" ]; then echo "No apply_filters calls found in $file" else for line in $apply_lines; do echo "---- Context around apply_filters at line $line ----" sed -n "$((line-3)),$((line+3))p" "$file" echo done fiLength of output: 339
🏁 Script executed:
#!/bin/bash # Search the gc-notion plugin directory for apply_filters and add_filter calls using the gcn_notion_page_data_add prefix # Apply_filters usage rg -n "apply_filters.*gcn_notion_page_data_add" --type php gc-notion # Add_filter usage rg -n "add_filter.*gcn_notion_page_data_add" --type php gc-notionLength of output: 138
Verify dynamic filter hook naming
Ensure the constructed filter names—
gcn_notion_page_data_addgcn_notion_page_data_add_{form_id}gcn_notion_page_data_add_{form_id}_{feed_id}—actually match the hook names used elsewhere in the Gravity Connect Notion plugin. We didn’t find any corresponding
apply_filtersoradd_filtercalls for these patterns in the codebase. Please confirm that these hooks are registered or consumed correctly.Location to review:
gc-notion/gcn-add-relation-from-dropdown.php(lines 20–31)
| $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; | ||
| $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; | ||
| $dropdown_field_id = isset( $args['dropdown_field_id'] ) ? $args['dropdown_field_id'] : null; | ||
| $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; |
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.
🛠️ Refactor suggestion
Add input validation for function parameters.
The function parameters are checked for existence but not validated. This could lead to unexpected behavior if invalid values are passed.
Consider adding validation:
$form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
$feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
$dropdown_field_id = isset( $args['dropdown_field_id'] ) ? $args['dropdown_field_id'] : null;
$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;
+
+// Validate required parameters
+if ( ! $form_id || ! is_numeric( $form_id ) ) {
+ return;
+}
+if ( ! $dropdown_field_id || ! is_numeric( $dropdown_field_id ) ) {
+ return;
+}
+if ( empty( $property_name ) || ! is_string( $property_name ) ) {
+ return;
+}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; | |
| $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; | |
| $dropdown_field_id = isset( $args['dropdown_field_id'] ) ? $args['dropdown_field_id'] : null; | |
| $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; | |
| $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; | |
| $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; | |
| $dropdown_field_id = isset( $args['dropdown_field_id'] ) ? $args['dropdown_field_id'] : null; | |
| $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; | |
| // Validate required parameters | |
| if ( ! $form_id || ! is_numeric( $form_id ) ) { | |
| return; | |
| } | |
| if ( ! $dropdown_field_id || ! is_numeric( $dropdown_field_id ) ) { | |
| return; | |
| } | |
| if ( empty( $property_name ) || ! is_string( $property_name ) ) { | |
| return; | |
| } |
🤖 Prompt for AI Agents
In gc-notion/gcn-add-relation-from-dropdown.php lines 15 to 18, the code assigns
values from the $args array to variables without validating their types or
presence beyond existence checks. Add validation to ensure each parameter is not
only set but also of the expected type or format, and handle invalid cases
appropriately, such as by setting defaults or throwing errors. This will improve
robustness and prevent potential issues caused by invalid input values.
| $page_id = rgar( $entry, $dropdown_field_id ); | ||
| $prop_type = 'relation'; | ||
|
|
||
| if ( empty( $page_id ) ) { | ||
| return $page_data; | ||
| } |
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.
Validate and sanitize the page ID.
The page ID retrieved from the form entry is used without validation. This could potentially cause issues if the dropdown contains invalid data.
Add validation for the page ID:
$page_id = rgar( $entry, $dropdown_field_id );
$prop_type = 'relation';
-if ( empty( $page_id ) ) {
+// Validate page ID format (Notion page IDs are UUIDs)
+if ( empty( $page_id ) || ! preg_match( '/^[a-f0-9]{8}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[a-f0-9]{12}$/i', $page_id ) ) {
return $page_data;
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $page_id = rgar( $entry, $dropdown_field_id ); | |
| $prop_type = 'relation'; | |
| if ( empty( $page_id ) ) { | |
| return $page_data; | |
| } | |
| $page_id = rgar( $entry, $dropdown_field_id ); | |
| $prop_type = 'relation'; | |
| // Validate page ID format (Notion page IDs are UUIDs) | |
| if ( empty( $page_id ) || ! preg_match( '/^[a-f0-9]{8}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[a-f0-9]{4}-?[a-f0-9]{12}$/i', $page_id ) ) { | |
| return $page_data; | |
| } |
🤖 Prompt for AI Agents
In gc-notion/gcn-add-relation-from-dropdown.php around lines 35 to 40, the code
retrieves the page ID from the form entry without validation or sanitization,
which could lead to issues if the data is invalid. Validate and sanitize the
$page_id variable before using it, for example by checking its format or type,
to ensure it is a valid ID before proceeding with further processing.
| $page_data['properties'][ $property_name ] = array( | ||
| $prop_type => array( | ||
| array( | ||
| 'id' => $page_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.
🛠️ Refactor suggestion
Preserve existing relation data instead of overwriting.
The current implementation overwrites any existing relation data for the property. This could lead to unintended data loss.
Modify the code to append to existing relations:
+// Preserve existing relations if any
+$existing_relations = isset( $page_data['properties'][ $property_name ]['relation'] )
+ ? $page_data['properties'][ $property_name ]['relation']
+ : array();
+
+// Add new relation
+$existing_relations[] = array( 'id' => $page_id );
+
$page_data['properties'][ $property_name ] = array(
- $prop_type => array(
- array(
- 'id' => $page_id,
- ),
- ),
+ $prop_type => $existing_relations,
);🤖 Prompt for AI Agents
In gc-notion/gcn-add-relation-from-dropdown.php around line 42, the code
overwrites existing relation data for the property instead of preserving it. To
fix this, retrieve the current property data, append the new relation to the
existing array, and then assign it back, ensuring existing relations are
preserved and new ones are added without overwriting.
wizlizard
left a comment
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.
Suggestions around clarity.
…to an entry's Notion page.
b6ea665 to
f3379c0
Compare
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
gc-notion/gcn-create-relation.php (2)
39-60: Consider handling multiple page IDs and improve error handling.The current implementation only handles a single page ID, but the documentation mentions "one or more Notion Page ID's". Additionally, error handling could be enhanced.
add_filter( $filter_name, function ( $page_data, $form, $entry, $feed ) use ( $property_name, $field_id ) { - $page_id = rgar( $entry, $field_id ); + $page_ids = rgar( $entry, $field_id ); $prop_type = 'relation'; - if ( empty( $page_id ) ) { + if ( empty( $page_ids ) ) { return $page_data; } + // Handle both single and multiple page IDs + $page_ids = is_array( $page_ids ) ? $page_ids : array( $page_ids ); + $relations = array(); + + foreach ( $page_ids as $page_id ) { + if ( ! empty( trim( $page_id ) ) ) { + $relations[] = array( 'id' => trim( $page_id ) ); + } + } + + if ( empty( $relations ) ) { + return $page_data; + } $page_data['properties'][ $property_name ] = array( - $prop_type => array( - array( - 'id' => $page_id, - ), - ), + $prop_type => $relations, ); return $page_data; }, 10, 4 );
63-87: Consider wrapping the usage example in a conditional check.The usage example executes immediately when the file is included, which could cause issues if the file is loaded multiple times or in contexts where the function shouldn't run.
/** * Usage Example: */ -gcn_create_relation( - array( - /** - * Change this to your form ID. - */ - 'form_id' => 1, - /** - * Change this to the ID of the feed you want to use. - * You can technically omit this to apply to all feeds - * for the form, but it's recommended to specify it for clarity. - */ - 'feed_id' => 2, - /** - * Change this to the ID of the field which holds the Notion Page ID. - */ - 'field_id' => 3, - /** - * Change this to the name of the relation property in your Notion database. - */ - 'property_name' => 'Tasks', - ) -); +if ( ! function_exists( 'gcn_create_relation_example' ) ) { + function gcn_create_relation_example() { + gcn_create_relation( + array( + /** + * Change this to your form ID. + */ + 'form_id' => 1, + /** + * Change this to the ID of the feed you want to use. + * You can technically omit this to apply to all feeds + * for the form, but it's recommended to specify it for clarity. + */ + 'feed_id' => 2, + /** + * Change this to the ID of the field which holds the Notion Page ID. + */ + 'field_id' => 3, + /** + * Change this to the name of the relation property in your Notion database. + */ + 'property_name' => 'Tasks', + ) + ); + } + + // Uncomment the line below to activate the example + // gcn_create_relation_example(); +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gc-notion/gcn-create-relation.php(1 hunks)
🔇 Additional comments (2)
gc-notion/gcn-create-relation.php (2)
1-19: LGTM! Excellent documentation.The documentation is comprehensive and provides clear step-by-step instructions for implementation. The comments effectively guide users through the setup process.
27-37: LGTM! Smart filter name construction.The dynamic filter name construction logic is well-implemented, creating specific filters based on form and feed combinations while maintaining backward compatibility.
| function gcn_create_relation( $args = array() ) { | ||
| $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; | ||
| $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; | ||
| $field_id = isset( $args['field_id'] ) ? $args['field_id'] : null; | ||
| $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; | ||
|
|
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.
🛠️ Refactor suggestion
Add parameter validation for required arguments.
The function should validate that essential parameters are provided to prevent silent failures or unexpected behavior.
function gcn_create_relation( $args = array() ) {
$form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
$feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null;
$field_id = isset( $args['field_id'] ) ? $args['field_id'] : null;
$property_name = isset( $args['property_name'] ) ? $args['property_name'] : null;
+
+ // Validate required parameters
+ if ( empty( $field_id ) || empty( $property_name ) ) {
+ return;
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| function gcn_create_relation( $args = array() ) { | |
| $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; | |
| $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; | |
| $field_id = isset( $args['field_id'] ) ? $args['field_id'] : null; | |
| $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; | |
| function gcn_create_relation( $args = array() ) { | |
| $form_id = isset( $args['form_id'] ) ? $args['form_id'] : null; | |
| $feed_id = isset( $args['feed_id'] ) ? $args['feed_id'] : null; | |
| $field_id = isset( $args['field_id'] ) ? $args['field_id'] : null; | |
| $property_name = isset( $args['property_name'] ) ? $args['property_name'] : null; | |
| // Validate required parameters | |
| if ( empty( $field_id ) || empty( $property_name ) ) { | |
| return; | |
| } |
🤖 Prompt for AI Agents
In gc-notion/gcn-create-relation.php around lines 21 to 26, the function
gcn_create_relation does not validate that required parameters like form_id,
feed_id, field_id, and property_name are provided. Add checks after extracting
these parameters to verify they are not null or empty, and handle missing
required arguments appropriately, such as returning an error or throwing an
exception, to prevent silent failures or unexpected behavior.
Context
💬 Slack: https://gravitywiz.slack.com/archives/GMP0ZMNSE/p1747312064858409
Summary
Adds a snippet to add relations to Notion page entries from a GPPA populated dropdown.