Skip to content

Conversation

@malayladu
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2844608881/77895

Summary

By default, when we use GPPA to get a term, like category of a post, it returns the Term name. However, when this snippet which is used to update posts, is also active on the website, Populate Anything will return the Term ID when getting the category of a post instead of the name. The set up is what I showed in the video.

So the expected behaviour is that, even with the snippet active on the website, Populate Anything should still return the Term Name when populating Terms of a post.

Here's a video demo of the issue

https://www.loom.com/share/afe25a1453834809aa249fc342d52205?sid=dc46a634-5708-4627-b91c-789a6109a4ce

@malayladu malayladu self-assigned this Feb 19, 2025
Copy link
Contributor

@saifsultanc saifsultanc left a comment

Choose a reason for hiding this comment

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

Approved.

@malayladu malayladu force-pushed the malay/fix/77895-gw-update-post-returning-term-id-instead-of-value branch 2 times, most recently from 4d4a70f to be97d6f Compare February 26, 2025 15:16
@malayladu malayladu force-pushed the malay/fix/77895-gw-update-post-returning-term-id-instead-of-value branch from be97d6f to dcf64ac Compare March 11, 2025 15:31
@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2025

Walkthrough

The changes improve the handling of custom taxonomies in the update_post_by_entry method. The implementation now trims and splits term values, verifies term types (numeric or non-numeric), and uses term_exists to retrieve term IDs when necessary. New helper methods, is_taxonomy_hierarchical and get_term_names_by_ids, have been introduced to support improved taxonomy management. Additionally, the return_ids_instead_of_names method has been modified to include specific form ID checks.

Changes

File Change Summary
gravity-forms/gw-update-posts.php Updated update_post_by_entry to trim/split terms, check numeric values, and use term_exists. Added logic to retrieve term names via get_term_names_by_ids for non-hierarchical taxonomies. Modified return_ids_instead_of_names to handle specific form IDs. Added new methods is_taxonomy_hierarchical and get_term_names_by_ids to support improved taxonomy handling.

Suggested reviewers

  • veryspry

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b57f52d and 9208e14.

📒 Files selected for processing (1)
  • gravity-forms/gw-update-posts.php (3 hunks)
🔇 Additional comments (8)
gravity-forms/gw-update-posts.php (8)

149-189: Improved taxonomy handling with more robust term processing

The updated code significantly enhances how terms are handled when updating posts. The implementation now properly:

  1. Extracts terms from field values with appropriate trimming
  2. Detects taxonomy types from multiple potential sources
  3. Intelligently processes terms based on whether they're IDs or names
  4. Handles hierarchical vs non-hierarchical taxonomies differently

This addresses the core issue where GPPA was returning IDs instead of term names when populating post fields.


151-161: Good taxonomy detection from multiple sources

The code now intelligently determines the taxonomy from different field types and configurations, including special handling for when GravityView is activated. This makes the implementation more robust and flexible.


168-181: Thorough term validation and conversion

The term processing now properly checks if each term is numeric (ID) or a name, and handles the conversion appropriately using term_exists(). Removing non-existent terms is also a good defensive programming practice.


183-186: Smart handling of hierarchical vs non-hierarchical taxonomies

Different taxonomy types need different handling - for non-hierarchical taxonomies getting term names from term IDs is the right approach. This ensures consistent behavior across taxonomy types.


209-217: Well-implemented taxonomy helper method

This utility function properly checks if a taxonomy is hierarchical by retrieving the taxonomy object and checking its properties. The defensive check for a non-existent taxonomy is good practice.


227-239: Useful term name retrieval helper method

This helper function correctly retrieves term names from term IDs with proper error handling, ensuring no WP_Error objects are returned to the user. The type casting to array ensures consistent handling of both single IDs and arrays.


361-368: Fixed the core issue with GPPA term handling

This change addresses the core issue by:

  1. Checking if the field belongs to the target form
  2. Avoiding ID returns for post objects used in dynamically populated fields

The logic ensures that when GPPA is used to populate post fields, term names are returned instead of term IDs as expected.


422-423: Add newline at end of file

The file is missing a newline at the end, which is generally recommended for compatibility with various tools and to prevent issues if anything gets concatenated at the end of the file.

 }
 

+
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@malayladu malayladu force-pushed the malay/fix/77895-gw-update-post-returning-term-id-instead-of-value branch from dcf64ac to 1060730 Compare March 11, 2025 15:33
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: 0

🧹 Nitpick comments (2)
gravity-forms/gw-update-posts.php (2)

145-175: Refine non-hierarchical taxonomy handling for potential name collisions.

This block correctly differentiates between hierarchical and non-hierarchical taxonomies and updates term references accordingly. However, storing term names instead of slugs for non-hierarchical taxonomies can cause ambiguities if multiple terms share the same name. Consider switching to term slugs or verifying term uniqueness to mitigate potential collisions.

- $terms = $this->get_term_names_by_ids( $terms, $taxonomy );
+ $terms = $this->get_term_slugs_by_ids( $terms, $taxonomy );

As a follow-up, you could implement a helper method similar to “get_term_names_by_ids” that retrieves slugs instead of names. This ensures consistent handling even when term names are not unique.


205-225: Consider returning term slugs for non-hierarchical taxonomies.

While retrieving and returning $tag->name works, it may be safer to return $tag->slug as it’s guaranteed to be unique within the taxonomy. Multiple terms could share the same display name, causing unexpected collisions when setting post terms by name.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f35dd9 and dcf64ac.

📒 Files selected for processing (1)
  • gravity-forms/gw-update-posts.php (3 hunks)
🔇 Additional comments (5)
gravity-forms/gw-update-posts.php (5)

183-183: Comment is fine.

The additional inline comment clarifies the rationale for passing false for $wp_error and $fire_after_hooks in wp_update_post. No issues identified.


188-203: Hierarchical check is correct and efficient.

This new method accurately determines if a taxonomy is hierarchical by retrieving its object and examining the hierarchical property. It’s a straightforward and valid approach.


346-349: Form ID check is clean and straightforward.

This conditional correctly restricts the logic to the current form ID and avoids unintended side effects in other forms.


351-354: Early return for non-taxonomy object types.

Skipping ID transformation for posts ensures only taxonomy-based fields are affected. This aligns with the intended fix for returning name values in GPPA fields when dealing with post objects.


409-414: Verify the hard-coded post ID.

You are instantiating the class with 'post_id' => 1, which might be for testing or demo purposes. If this is intended for production usage, ensure post ID 1 is always valid and accessible.

Are you certain this is the correct post ID in all cases, or would a dynamic approach be preferable?

@malayladu malayladu force-pushed the malay/fix/77895-gw-update-post-returning-term-id-instead-of-value branch from 1060730 to b57f52d Compare March 14, 2025 15:46
Copy link
Contributor

@veryspry veryspry left a comment

Choose a reason for hiding this comment

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

@malayladu this is untested by me, but the code looks good. I did add a couple of suggestions and a question. I'm going to go ahead and approve so that you can move this along as soon as you decide what (if anything) to do with suggestions 🙂

…of value when GPPA dynamically populates post field.
@malayladu malayladu force-pushed the malay/fix/77895-gw-update-post-returning-term-id-instead-of-value branch from b57f52d to 9208e14 Compare March 27, 2025 17:21
@malayladu malayladu requested a review from veryspry March 27, 2025 17:22
@malayladu
Copy link
Contributor Author

@veryspry I've made changes based on the feedback. Ready for review and then S&M!

Copy link
Contributor

@veryspry veryspry left a comment

Choose a reason for hiding this comment

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

@malayladu thank you! And also approved

@malayladu malayladu merged commit 0363d01 into master Mar 31, 2025
4 of 5 checks passed
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.

4 participants