[jobs] shortcode: add author attribute to filter listings by user ID#2935
[jobs] shortcode: add author attribute to filter listings by user ID#2935masteradhoc wants to merge 16 commits intoAutomattic:trunkfrom
Conversation
donnchawp
left a comment
There was a problem hiding this comment.
Looks good, but if I modify the jobs shortcode on my page to show
[jobs author=1]
The RSS feed link on the page doesn't have the author in it.
|
Thanks @donnchawp! Now its possible to use http://wpjm-pr.local/job-autor-1/?feed=job_feed&author=1,2 to get the requested authors :) |
donnchawp
left a comment
There was a problem hiding this comment.
Only one thing I'm worried about below.
|
|
||
| $should_cache = 'rand_featured' !== $args['orderby'] && 'rand' !== $args['orderby']; | ||
|
|
||
| // Bypass cache when author filter is active to avoid stale results. |
There was a problem hiding this comment.
Are you sure this is required? The cache key is derived from $to_hash = wp_json_encode( $query_args ); so having the author field in there will create a new cache key. It shouldn't be stale.
There was a problem hiding this comment.
I had issues while applying the PR that the change didnt show. but when i retest now all seems fine even without this part. so i went ahead and removed it now @donnchawp - thanks!
donnchawp
left a comment
There was a problem hiding this comment.
Only a tiny change this time, but there are no tests for author in tests/php/tests/test_class.wp-job-manager-functions.php either which would be great to have.
Co-authored-by: Donncha O Caoimh <5656673+donnchawp@users.noreply.github.com>
|
@donnchawp i've just pushed the tests. Can you give it a check? :) |
donnchawp
left a comment
There was a problem hiding this comment.
Still some nagging issues, the first one really should be fixed and the rest cleared up too:
[MEDIUM] AJAX response RSS link drops the active author filter
includes/class-wp-job-manager-ajax.php:242-249
get_listings() reads $author from the request and passes it to get_job_listings(), but the immediately-following call to job_manager_get_filtered_links() omits it. Reproduced live with [jobs author="3"] on my test site: the response
payload's rss_link href is …&search_keywords&author (empty value) — the "Subscribe via RSS" link points to an unscoped feed and leaks listings the page intentionally hid.
The PR's own change in wp-job-manager-functions.php:642 (adding 'author' to the feed URL when $args['author'] is present) is dead code because no caller supplies it.
Fix: Add 'author' => $author to the args array passed to job_manager_get_filtered_links().
[MEDIUM] Fails open on invalid author input
wp-job-manager-functions.php:195-202, includes/class-wp-job-manager-ajax.php:139, includes/class-wp-job-manager-post-types.php:730-735
Invalid input silently shows all listings instead of returning zero results. Two failure modes:
- Non-numeric like
[jobs author="alice,bob"]→absintproduces[0,0], thenarray_filterproduces[], then! empty( $author_ids )is false → noauthor__inis set → all listings render. - Array input like
?author[]=1(AJAX or feed) →sanitize_text_field()flattens the array to''→ filter silently disappears.
For a publicly-rendered shortcode, "user typed invalid filter, sees everything" is the wrong default. Reproduced: [jobs author="x"] returns all listings.
Fix: When author was provided but parsing yielded no valid IDs, force an empty result ('author__in' => [0]). Reject array input early at the AJAX/feed entry points.
[LOW] Negative IDs become valid via absint
wp-job-manager-functions.php:197-198
absint(-5) is 5, so [jobs author="-5"] filters by user 5's listings rather than returning nothing. Low real impact, but the filter behaves non-monotonically — a typo'd minus sign returns a stranger's data instead of empty.
Fix: After absint, drop values <= 0 rather than relying on array_filter.
[LOW] Missing PHPDoc for new author arg
wp-job-manager-functions.php:11-17
The PHPDoc on get_job_listings() is not updated for the new public arg.
Fix: Add @param int|string|int[] $author Optional. User ID, comma-separated user IDs, or array of user IDs.
[LOW] PR description claims a transient-cache bypass that isn't implemented
The description says "Bypasses the transient cache when an author filter is active to prevent stale results." No such code is in the diff, and inspection confirms the cache key already hashes $query_args (and therefore
author__in), so the bypass would be unnecessary anyway. Just update the PR description.
Test coverage gaps
The two new tests only cover the happy path (single ID, multi ID). Add cases for: empty string, non-numeric ("abc"), negative ("-5"), "0", mixed valid+invalid ("3,abc"), and array input (['author' => [3, 5]]).
Reviewer note: tester friction
The dist bundle (assets/dist/js/) is gitignored, so the JS changes only take effect after npm run build:assets. I initially saw "all listings shown" until I rebuilt. Worth either calling out in the PR description or considering
whether dist should be a build artifact attached to the release flow.
Fixes #1892
Changes Proposed in this Pull Request
authorattribute to the[jobs]shortcode, allowing job listings to be filtered by one or more WordPress user IDs[jobs author="1"]) or comma-separated multiple IDs ([jobs author="1,3,5"])author__insupport toget_job_listings()(available to all callers, not just the shortcode)Testing Instructions
[jobs author="X"]to a page (where X is a user ID) — verify only that user's listings appear[jobs author="X,Y"]— verify listings from both users appearshow_filters="true"(default), interact with the filter form and verify the author restriction is preserved across AJAX requests[jobs]without theauthorattribute continues to show all listings as beforeRelease Notes
[jobs]shortcode now supports anauthorattribute to filter listings by user ID (e.g.[jobs author="42"]or[jobs author="1,2,3"])New or Updated Hooks and Templates
authorargument is now accepted byget_job_listings()and will be respected by any code that calls it directly.Important
Make sure to update WPJM Documentation for Shortcode as well and include the new feature!
Deprecated Code
Screenshot / Video