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

Add methods to hide filled and expired posts #2449

Draft
wants to merge 20 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5afad7d
Add methods to hide filled and expired posts
anaemnesis Jun 6, 2023
7a924f6
Remove unused method
anaemnesis Jun 7, 2023
41f374d
Hide expired and filled jobs
anaemnesis Jun 13, 2023
2fccb36
Remove an 's' from 'jobs' in method name
anaemnesis Jun 13, 2023
eeb42e0
Update maybe_hide_filled_expired_job_listings_from_search logic
anaemnesis Jun 13, 2023
068a162
Account for 'trash' post status
anaemnesis Jun 13, 2023
d1d04d4
Add additional query checks
anaemnesis Jun 21, 2023
cd94313
Remove post_type check, as search page is querying "any"
anaemnesis Jun 21, 2023
3fedb2b
Refactor hide/filled
anaemnesis Jun 21, 2023
fc8e201
Explicitly query 'publish' post status
anaemnesis Jun 21, 2023
2522485
Refactor how $expired posts are hidden from search
anaemnesis Jun 22, 2023
27685ac
Nonce verification for $_GET['s'] isn't necessary here, so let's try …
anaemnesis Jun 22, 2023
d3b5123
Update issue reference
anaemnesis Jun 23, 2023
d23b3a3
Rename $is_expired_hidden variable to be more readable
anaemnesis Jun 28, 2023
ea63e66
Update PHP doc for the get_filled_job_listings() method to state the …
anaemnesis Jun 28, 2023
3a210fb
Early return on get_filled_job_listings() method isn't necessary, as …
anaemnesis Jun 28, 2023
1a7b7da
Revert how expired posts are hidden to previous implementation
anaemnesis Jul 2, 2023
de746c4
Delete 'hide_filled_jobs_transient' on 'updated_post_meta' action.
anaemnesis Jul 2, 2023
82e75e6
Refactor maybe_hide_expired_posts()
anaemnesis Jul 4, 2023
27b3be7
Add comments explaining conditional logic in the maybe_hide_* methods.
anaemnesis Jul 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions includes/class-wp-job-manager-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_expired_job_listings_from_search' ] );

// Single job content.
$this->job_content_filter( true );
Expand Down Expand Up @@ -659,6 +660,83 @@ 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_type' => 'job_listing',
'fields' => 'ids',
'meta_query' => [
[
'key' => '_filled',
'value' => '1',
'compare' => '=',
],
],
]
);
set_transient( 'hide_filled_jobs_transient', $filled_jobs_transient, DAY_IN_SECONDS );
}
return $filled_jobs_transient;
}

/**
* Get expired jobs.
*
* @return array
*/
public function get_expired_job_listings(): array {
if ( ! get_option( 'job_manager_hide_expired' ) ) {
return [];
}

$expired_jobs_transient = get_transient( 'hide_expired_jobs_transient' );
if ( false === $expired_jobs_transient ) {
$expired_jobs_transient = get_posts(
[
'post_type' => 'job_listing',
'post_status' => 'expired',
'fields' => 'ids',
]
);
set_transient( 'hide_expired_jobs_transient', $expired_jobs_transient, DAY_IN_SECONDS );
}
return $expired_jobs_transient;
}

/**
* Maybe exclude expired and/or filled job listings from search.
*
* @param $query WP_Query $query
*
* @return void
*/
public function maybe_hide_filled_expired_job_listings_from_search( WP_Query $query ): void {
$hide_filled_positions = get_option( 'job_manager_hide_filled_positions' );
$hide_expired = get_option( 'job_manager_hide_expired' );

if ( ! $hide_filled_positions && ! $hide_expired ) {
return;
}

if ( ! is_admin() && $query->is_search() ) {
yscik marked this conversation as resolved.
Show resolved Hide resolved
$jobs_to_exclude = array_merge( $this->get_filled_job_listings(), $this->get_expired_job_listings() );
if ( ! empty( $jobs_to_exclude ) ) {
$query->set( 'post__not_in', $jobs_to_exclude );
}
}
}

/**
* Adds query arguments in order to make sure that the feed properly queries the 'job_listing' type.
*
Expand Down Expand Up @@ -1898,4 +1976,5 @@ public function delete_user_add_job_listings_post_type( $types ) {

return $types;
}

}
23 changes: 23 additions & 0 deletions wp-job-manager-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,3 +1712,26 @@ 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_job_listings_transients_on_save( 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' );
}

$post_status = [ 'publish', 'expired', 'trash' ];
if ( in_array( $post->post_status, $post_status, true ) ) {
delete_transient( 'hide_expired_jobs_transient' );
}
}

}
add_action( 'save_post', 'delete_job_listings_transients_on_save', 10, 2 );
yscik marked this conversation as resolved.
Show resolved Hide resolved