Skip to content

Conversation

@veryspry
Copy link
Contributor

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.

@coderabbitai
Copy link

coderabbitai bot commented May 26, 2025

Walkthrough

A 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

File(s) Change Summary
gc-notion/gcn-create-relation.php Added a script with a function to create Notion relation properties from Gravity Forms data.

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
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link

github-actions bot commented May 26, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against f3379c0

Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 96c1d2e and b6ea665.

📒 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 php

Length 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
fi

Length 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-notion

Length of output: 138


Verify dynamic filter hook naming

Ensure the constructed filter names—

  • gcn_notion_page_data_add
  • gcn_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_filters or add_filter calls 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)

Comment on lines 15 to 18
$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;
Copy link

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.

Suggested change
$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.

Comment on lines 35 to 40
$page_id = rgar( $entry, $dropdown_field_id );
$prop_type = 'relation';

if ( empty( $page_id ) ) {
return $page_data;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
$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.

Comment on lines 42 to 48
$page_data['properties'][ $property_name ] = array(
$prop_type => array(
array(
'id' => $page_id,
),
),
);
Copy link

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.

Copy link
Contributor

@wizlizard wizlizard left a comment

Choose a reason for hiding this comment

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

Suggestions around clarity.

@veryspry veryspry force-pushed the matt/gcn-relation branch from b6ea665 to f3379c0 Compare May 28, 2025 10:50
Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between b6ea665 and f3379c0.

📒 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.

Comment on lines +21 to +26
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;

Copy link

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.

Suggested change
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.

@veryspry veryspry merged commit dc5b10d into master Jun 3, 2025
3 of 4 checks passed
@veryspry veryspry deleted the matt/gcn-relation branch June 3, 2025 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants