-
Notifications
You must be signed in to change notification settings - Fork 368
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
Add methods to hide filled and expired posts #2449
base: trunk
Are you sure you want to change the base?
Changes from 13 commits
5afad7d
7a924f6
41f374d
2fccb36
eeb42e0
068a162
d1d04d4
cd94313
3fedb2b
fc8e201
2522485
27685ac
d3b5123
d23b3a3
ea63e66
3a210fb
1a7b7da
de746c4
82e75e6
27b3be7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -87,6 +87,7 @@ public function __construct() { | |||||||||||||||||||||||||||||
add_action( 'transition_post_status', [ $this, 'track_job_submission' ], 10, 3 ); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
add_action( 'parse_query', [ $this, 'add_feed_query_args' ] ); | ||||||||||||||||||||||||||||||
add_action( 'pre_get_posts', [ $this, 'maybe_hide_filled_job_listings_from_search' ] ); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// Single job content. | ||||||||||||||||||||||||||||||
$this->job_content_filter( true ); | ||||||||||||||||||||||||||||||
|
@@ -389,14 +390,29 @@ public function register_post_types() { | |||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
add_feed( self::get_job_feed_name(), [ $this, 'job_feed' ] ); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* This code checks if (1) we're on a search or (2) the option to hide expired job listings is enabled. | ||||||||||||||||||||||||||||||
* If either condition is false, we're going to set the register_post_status() 'public' arg to false, | ||||||||||||||||||||||||||||||
* otherwise it will be true. | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
* This will prevent single expired job listings from showing a '404', instead of an | ||||||||||||||||||||||||||||||
* 'Expired' notice, when viewing the single job listing and not logged in. | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
* This will also address historical issues stemming from addressing the issue raised here | ||||||||||||||||||||||||||||||
* https://github.com/Automattic/WP-Job-Manager/issues/1884. | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
$is_site_search = ( isset( $_GET['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||||||||||||||||||||||||||||||
$is_expired_searchable = (bool) get_option( 'job_manager_hide_expired' ); | ||||||||||||||||||||||||||||||
jom marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
$is_expired_public = ! $is_site_search || ! $is_expired_searchable; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Post status | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
register_post_status( | ||||||||||||||||||||||||||||||
'expired', | ||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||
'label' => _x( 'Expired', 'post status', 'wp-job-manager' ), | ||||||||||||||||||||||||||||||
'public' => true, | ||||||||||||||||||||||||||||||
'public' => $is_expired_public, | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jom Here's the refactored implementation for setting
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name threw me off a bit, but I think the ultimate logic is correct! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ended up reverting this due to issues checking conditionals like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main issue we were avoiding with the other strategy was I wonder if we should be setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, gotcha.
WP-Job-Manager/includes/class-wp-job-manager-post-types.php Lines 395 to 407 in 0568f04
This does not exclude expired jobs from WP Search. This seems to be getting overwritten by the
If this is set to When I was testing the I'm wondering if another option would be to keep the
IIUC, this is more to do with the issue report here https://core.trac.wordpress.org/ticket/44737, which is sitting stale. Making the suggested fix does fix the behaviour, but then that's a whole core-related thing. cc @yscik There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I saw https://core.trac.wordpress.org/ticket/44737, but then saw the lines get changed in https://core.trac.wordpress.org/ticket/48556 and thought that would fix it, but now I can see what it is doing in the
This is what I was going to suggest before I went down the 44737 rabbit hole because it seems like WP is doing this in a way that would work for us: I think if we did this we could just removed The only other thought I had is modifying the global that holds the post status objects here after our conditionals and then restore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||
'protected' => true, | ||||||||||||||||||||||||||||||
'exclude_from_search' => true, | ||||||||||||||||||||||||||||||
'show_in_admin_all_list' => true, | ||||||||||||||||||||||||||||||
|
@@ -659,6 +675,62 @@ public function job_feed() { | |||||||||||||||||||||||||||||
remove_filter( 'posts_search', 'get_job_listings_keyword_search' ); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Get filled jobs. | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
* @return array | ||||||||||||||||||||||||||||||
jom marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
public function get_filled_job_listings(): array { | ||||||||||||||||||||||||||||||
if ( ! get_option( 'job_manager_hide_filled_positions' ) ) { | ||||||||||||||||||||||||||||||
return []; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
jom marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$filled_jobs_transient = get_transient( 'hide_filled_jobs_transient' ); | ||||||||||||||||||||||||||||||
if ( false === $filled_jobs_transient ) { | ||||||||||||||||||||||||||||||
$filled_jobs_transient = get_posts( | ||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||
'post_status' => 'publish', | ||||||||||||||||||||||||||||||
'post_type' => 'job_listing', | ||||||||||||||||||||||||||||||
yscik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
'fields' => 'ids', | ||||||||||||||||||||||||||||||
'posts_per_page' => -1, | ||||||||||||||||||||||||||||||
'meta_query' => [ | ||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||
'key' => '_filled', | ||||||||||||||||||||||||||||||
'value' => '1', | ||||||||||||||||||||||||||||||
'compare' => '=', | ||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||
set_transient( 'hide_filled_jobs_transient', $filled_jobs_transient, DAY_IN_SECONDS ); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
return $filled_jobs_transient; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Maybe exclude expired and/or filled job listings from search and archive pages. | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
* @param $query WP_Query $query | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
* @return void | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
public function maybe_hide_filled_job_listings_from_search( WP_Query $query ): void { | ||||||||||||||||||||||||||||||
$hide_filled_positions = get_option( 'job_manager_hide_filled_positions' ); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ( ! $hide_filled_positions ) { | ||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||
! is_admin() | ||||||||||||||||||||||||||||||
&& $query->is_main_query() | ||||||||||||||||||||||||||||||
&& ( $query->is_search() || $query->is_archive() ) | ||||||||||||||||||||||||||||||
Comment on lines
+721
to
+723
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a comment here clarifying the reason for this logic: We want the filtering to apply only to search and archive public facing queries. We don't want it to apply in the admin panel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Committed 27b3be7 |
||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
$query->set( 'post__not_in', $this->get_filled_job_listings() ); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Adds query arguments in order to make sure that the feed properly queries the 'job_listing' type. | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
|
@@ -1898,4 +1970,5 @@ public function delete_user_add_job_listings_post_type( $types ) { | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return $types; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1712,3 +1712,21 @@ function job_manager_get_salary_unit_options( $include_empty = true ) { | |
*/ | ||
return apply_filters( 'job_manager_get_salary_unit_options', $options, $include_empty ); | ||
} | ||
|
||
/** | ||
* Delete filled_jobs_transient and expired_jobs_transient transients when a job listing is saved or updated. | ||
* | ||
* @param int $post_id The ID of the post being saved. | ||
* @param WP_Post $post The post object being saved. | ||
*/ | ||
function delete_filled_job_listing_transient_on_post_meta_update( int $post_id, WP_Post $post ): void { | ||
|
||
if ( 'job_listing' === $post->post_type ) { | ||
|
||
if ( '1' === get_post_meta( $post_id, '_filled', true ) ) { | ||
delete_transient( 'hide_filled_jobs_transient' ); | ||
} | ||
} | ||
|
||
} | ||
add_action( 'save_post', 'delete_filled_job_listing_transient_on_post_meta_update', 10, 2 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious about why this is separated from the other logic in this PR? Do we think this function is useful in the public API? There is also the [possibly edge] case where a job went from filled to not filled. Do you think that is worth flushing this transient after each job listing safe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jom Ah, I hadn't realised the functions file was dedicated to the API. I'll move this to post-types.php. One thing I'm having difficulty with is getting the method to trigger on
Clearing it on every post save is far too much and makes the transient pointless imo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I always get a little nervous about manipulating cached data (besides flushing it) because there are so many weird scenarios that could affect it. However, I suppose the weird ones would just happen for the life of the transient. In this case, one possible strangeness could happen in post status changes because to populate the transient you're querying published posts. While the query is inefficient generally, I think it isn't so bad for it to run it again when a possible change has been detected. What issues were you experiencing with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jom So I've learned that I was doing a check like:
But in debugging, I noticed that
But I don't imagine checking against |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this comment. Really helped with figuring out the logic.