Automattic / Automattic/crowdsignal-plugin

Rating shortcode: stable identifier fallback when no current post

Open
#155 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
15
Forks
11
Avg merge
14m
Merged PRs (30d)
1

Description

Follow-up to #104.

In the rating-embed branch of `polldaddy-shortcode.php`, `$unique_id` and `$item_id` are derived from the current post:

```php
if ( empty( $unique_id ) && $post ) {
$unique_id = is_page() ? 'wp-page-' . $post->ID : 'wp-post-' . $post->ID;
}
if ( empty( $item_id ) && $post ) {
$item_id = is_page() ? '_page_' . $post->ID : '_post_' . $post->ID;
}
```

When there is no current post (taxonomy archives, etc.) and the attributes aren't supplied in the shortcode, both stay empty. Downstream that yields `id="pd_rating_holder_{$rating}"` and `PDRTJS_settings_{$rating}` with no post component, so multiple ratings sharing the same `rating` id on the same post-less page collide on identical element IDs, and `unique_id` is empty in the settings JSON.

This collision predates #104 (the previous code produced a constant `_post_` suffix in the same situation) — #104 just removed the warning. Tracking the underlying behaviour separately.

### Proposed change
Fall back to a deterministic value when `$post` is absent, e.g. `get_queried_object_id()` or the `rating` id, so element IDs stay unique. If an empty `unique_id` is acceptable per the rating JS contract, add a short comment saying so instead.

### Tests
The shortcode currently has no coverage. Add PHPUnit tests:
- No current post: `[polldaddy rating=5]` renders markup without notices.
- Current post set: identifiers still resolve to `wp-post-{ID}` / `_post_{ID}`.
- Two ratings with the same `rating` id on a post-less page produce distinct element IDs.

Contributor guide

Open the contributing guide

Research direction

Start in the rating-embed branch of polldaddy-shortcode.php and trace how $unique_id and $item_id are derived when no current post exists. Check the rating JavaScript contract, then add PHPUnit coverage for post-less and current-post rendering, including duplicate-rating identifiers; done means the tests pass without notices and verify stable, distinct markup IDs.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.