Skip to content
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

Fix undefined array key #86

Closed
wants to merge 5 commits into from
Closed

Fix undefined array key #86

wants to merge 5 commits into from

Conversation

efuller
Copy link
Contributor

@efuller efuller commented Oct 27, 2023

Warning: Undefined array key "termRelations" in /code/wp-content/plugins/wp-curate/src/class-plugin-curated-posts.php

Add in a check to prevent this error.

Summary by CodeRabbit

Release Notes for WP Curate Version 1.4.0

  • Bug Fix: Enhanced the robustness of the WP Curate plugin by handling missing or incorrect data in the 'termRelations' attribute. This prevents potential errors and ensures smoother user experience.
  • General: Updated the plugin version from 1.3.0 to 1.4.0, marking the introduction of the above improvements.

These changes aim to improve the reliability of the WP Curate plugin, providing a more seamless and error-free experience for users.

@coderabbitai
Copy link

coderabbitai bot commented Oct 27, 2023

Warning

Rate Limit Exceeded

@efuller has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 15 minutes and 37 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, convert this PR to a draft and then mark it as ready for review again to re-trigger the review. Alternatively, you can push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per repository.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 75cd666 and 18eded4.

Walkthrough

The version 1.4.0 update for WP Curate introduces a robustness improvement in the handling of the termRelations attribute. The code now checks for the existence of the 'termRelations' key in the $attributes array before accessing its value, preventing potential errors. The plugin version has also been updated from 1.3.0 to 1.4.0.

Changes

File Summary
CHANGELOG.md Documented the bug fix in version 1.4.0 that prevents an error when the termRelations attribute is not set.
src/class-plugin-curated-posts.php Modified the code to check if the 'termRelations' key exists in the $attributes array before accessing its value. This change improves the code's robustness and prevents potential errors.
wp-curate.php Updated the plugin version from 1.3.0 to 1.4.0.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.json

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cdfe4e5 and cc18795.
Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • src/class-plugin-curated-posts.php (1 hunks)
  • wp-curate.php (1 hunks)
Files skipped from review due to trivial changes (2)
  • CHANGELOG.md
  • wp-curate.php
Additional comments: 1
src/class-plugin-curated-posts.php (1)
  • 62-62: The change here is a good one. It adds an additional check to ensure that the 'termRelations' key exists in the $attributes array before attempting to access its value. This will prevent potential "undefined array key" errors. The logic is sound and the code is more robust with this change.
- 'operator' => is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',
+ 'operator' => array_key_exists( 'termRelations', $attributes ) &&  is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cc18795 and 75cd666.
Files selected for processing (1)
  • src/class-plugin-curated-posts.php (1 hunks)

@@ -59,7 +59,7 @@ public function with_query_context( array $context, array $attributes, WP_Block_
$args['tax_query'][] = [
'taxonomy' => $taxonomy,
'terms' => array_column( $terms, 'id' ),
'operator' => is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',
'operator' => array_key_exists( 'termRelations', $attributes ) && is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',
Copy link

Choose a reason for hiding this comment

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

The new code adds an additional check to ensure that the 'termRelations' key exists in the $attributes array before attempting to access its value. This is a good practice as it prevents potential "undefined index" errors. However, the check for is_array( $attributes['termRelations'] ) is redundant because if 'termRelations' exists in $attributes and is not an array, it would not pass the array_key_exists check. Therefore, the is_array check can be removed to simplify the code.

- 'operator' => array_key_exists( 'termRelations', $attributes ) && is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',
+ 'operator' => array_key_exists( 'termRelations', $attributes ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',

Commitable suggestion (Beta)
Suggested change
'operator' => array_key_exists( 'termRelations', $attributes ) && is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',
'operator' => array_key_exists('termRelations', $attributes) ? $attributes['termRelations'][$taxonomy] ?? 'AND' : 'AND',

@efuller efuller closed this Oct 27, 2023
@efuller efuller deleted the fix/undefined-array-key branch October 27, 2023 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant