Skip to content

Commit

Permalink
Refactor maybe_hide_expired_posts()
Browse files Browse the repository at this point in the history
Refactors the maybe_hide_expired_posts() method to set the 'public' static to 'false', then back to 'true', during/after query generation.
  • Loading branch information
anaemnesis committed Jul 4, 2023
1 parent de746c4 commit 82e75e6
Showing 1 changed file with 62 additions and 9 deletions.
71 changes: 62 additions & 9 deletions includes/class-wp-job-manager-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ 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' ] );
add_action( 'pre_get_posts', [ $this, 'maybe_hide_filled_job_listings' ] );
add_action( 'pre_get_posts', [ $this, 'maybe_hide_expired_job_listings' ] );

// Single job content.
$this->job_content_filter( true );
Expand Down Expand Up @@ -691,17 +692,16 @@ public function get_filled_job_listings(): array {
}

/**
* Maybe exclude expired and/or filled job listings from search and archive pages.
* Maybe exclude filled job listings from search and archive pages.
*
* @param $query WP_Query $query
*
* @return void
*/
public function maybe_hide_filled_expired_job_listings_from_search( WP_Query $query ): void {
public function maybe_hide_filled_job_listings( 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 ) {
if ( ! $hide_filled_positions ) {
return;
}

Expand All @@ -711,14 +711,67 @@ public function maybe_hide_filled_expired_job_listings_from_search( WP_Query $qu
&& ( $query->is_search() || $query->is_archive() )
) {

if ( $hide_expired ) {
$query->set( 'post_status', 'publish' );
}

$query->set( 'post__not_in', $this->get_filled_job_listings() );
}
}

/**
* Maybe exclude expired job listings from search and archive pages.
*
* @param $query WP_Query $query
*
* @return void
*/
public function maybe_hide_expired_job_listings( WP_Query $query ): void {
$hide_expired = get_option( 'job_manager_hide_expired' );

if ( ! $hide_expired ) {
return;
}

if (
! is_admin()
&& $query->is_main_query()
&& ( $query->is_search() || $query->is_archive() )
) {

$this->make_expired_private();

add_action( 'posts_selection', [ $this, 'make_expired_public' ] );
}
}

/**
* Make the expired post status public.
*
* @return void
* @internal
*/
public function make_expired_public(): void {

global $wp_post_statuses;

if ( isset( $wp_post_statuses['expired'] ) ) {
$wp_post_statuses['expired']->public = true;
}

}

/**
* Make the expired post status private.
*
* @return void
* @internal
*/
public function make_expired_private() {

global $wp_post_statuses;

if ( isset( $wp_post_statuses['expired'] ) ) {
$wp_post_statuses['expired']->public = false;
}
}

/**
* Adds query arguments in order to make sure that the feed properly queries the 'job_listing' type.
*
Expand Down

0 comments on commit 82e75e6

Please sign in to comment.