Skip to content

Commit

Permalink
Add functionality for filtering job listings on Filled and Featured
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsanford committed Apr 13, 2018
1 parent 341fb77 commit 7ff287c
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
119 changes: 119 additions & 0 deletions includes/admin/class-wp-job-manager-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct() {
add_filter( 'manage_edit-job_listing_sortable_columns', array( $this, 'sortable_columns' ) );
add_filter( 'request', array( $this, 'sort_columns' ) );
add_action( 'parse_query', array( $this, 'search_meta' ) );
add_action( 'parse_query', array( $this, 'filter_meta' ) );
add_filter( 'get_search_query', array( $this, 'search_meta_label' ) );
add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
add_action( 'bulk_actions-edit-job_listing', array( $this, 'add_bulk_actions' ) );
Expand All @@ -55,6 +56,7 @@ public function __construct() {
if ( get_option( 'job_manager_enable_categories' ) ) {
add_action( "restrict_manage_posts", array( $this, "jobs_by_category" ) );
}
add_action( "restrict_manage_posts", array( $this, "jobs_meta_filters" ) );

foreach ( array( 'post', 'post-new' ) as $hook ) {
add_action( "admin_footer-{$hook}.php", array( $this,'extend_submitdiv_post_status' ) );
Expand Down Expand Up @@ -303,6 +305,84 @@ public function jobs_by_category() {
echo $output;
}

/**
* Output dropdowns for filters based on post meta.
*
* @since 1.31.0
*/
public function jobs_meta_filters() {
global $typenow;

// Only add the filters for job_listings
if ( 'job_listing' !== $typenow ) {
return;
}

// Filter by Filled.
$this->jobs_filter_dropdown( 'job_listing_filled', array(
array(
'value' => '',
'text' => 'Select Filled',
),
array(
'value' => '1',
'text' => 'Filled',
),
array(
'value' => '0',
'text' => 'Not Filled',
),
) );

// Filter by Featured.
$this->jobs_filter_dropdown( 'job_listing_featured', array(
array(
'value' => '',
'text' => 'Select Featured',
),
array(
'value' => '1',
'text' => 'Featured',
),
array(
'value' => '0',
'text' => 'Not Featured',
),
) );
}

/**
* Shows dropdown to filter by the given URL parameter. The dropdown will
* have three options: "Select $name", "$name", and "Not $name".
*
* The $options element should be an array of arrays, each with the
* attributes needed to create an <option> HTML element. The attributes are
* as follows:
*
* $options[i]['value'] The value for the <option> HTML element.
* $options[i]['text'] The text for the <option> HTML element.
*
* @since 1.31.0
*
* @param string $param The URL parameter.
* @param array $options The options for the dropdown. See the
* description above.
*/
private function jobs_filter_dropdown( $param, $options ) {
$selected = isset( $_GET[ $param ] ) ? $_GET[ $param ] : '';

$output = "<select name=\"$param\" id=\"dropdown_$param\">";

foreach ( $options as $option ) {
$output .= '<option value="' . esc_attr( $option['value'] ) . '"'
. ( $selected === $option['value'] ? ' selected' : '' )
. '>' . esc_html( $option['text'] ) . '</option>';
}
$output .= '</select>';

echo $output;
}

/**
* Filters page title placeholder text to show custom label.
*
Expand Down Expand Up @@ -597,6 +677,45 @@ public function search_meta( $wp ) {
$wp->query_vars['post__in'] = $post_ids;
}

/**
* Filters by meta fields.
*
* @param WP_Query $wp
*/
public function filter_meta( $wp ) {
global $pagenow;

if ( 'edit.php' !== $pagenow || 'job_listing' !== $wp->query_vars['post_type'] ) {
return;
}

$meta_query = $wp->get( 'meta_query' );
if ( ! is_array( $meta_query ) ) {
$meta_query = array();
}

// Filter on _filled meta.
if ( isset( $_GET['job_listing_filled'] ) && '' !== $_GET['job_listing_filled'] ) {
$meta_query[] = array(
'key' => '_filled',
'value' => $_GET['job_listing_filled'],
);
}

// Filter on _featured meta.
if ( isset( $_GET['job_listing_featured'] ) && '' !== $_GET['job_listing_featured'] ) {
$meta_query[] = array(
'key' => '_featured',
'value' => $_GET['job_listing_featured'],
);
}

// Set new meta query.
if ( ! empty( $meta_query ) ) {
$wp->set( 'meta_query', $meta_query );
}
}

/**
* Changes the label when searching meta.
*
Expand Down
103 changes: 103 additions & 0 deletions tests/php/tests/includes/admin/test_class.wp-job-manager-cpt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

require 'includes/admin/class-wp-job-manager-cpt.php';

class WP_Test_WP_Job_Manager_CPT extends WPJM_BaseTest {
public function setUp() {
parent::setUp();

// Ensure the hooks are set up.
$this->job_manager_cpt = new WP_Job_Manager_CPT();
}

/**
* Ensure that filter_meta adds the correct filters to the query based on
* the URL parameters.
*
* @since 1.31.0
* @covers WP_Job_Manager_CPT::filter_meta
*/
public function test_filter_meta() {
global $pagenow;

// Create some listings.
$listing_notfilled_notfeatured_id = $this->create_listing_with_meta(
array( '_filled' => '0', '_featured' => '0' )
);
$listing_notfilled_featured_id = $this->create_listing_with_meta(
array( '_filled' => '0', '_featured' => '1' )
);
$listing_filled_notfeatured_id = $this->create_listing_with_meta(
array( '_filled' => '1', '_featured' => '0' )
);
$listing_filled_featured_id = $this->create_listing_with_meta(
array( '_filled' => '1', '_featured' => '1' )
);

// Simulate viewing the edit.php page.
$pagenow = 'edit.php';

// When no filters are given.
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) );
$this->assertContains( $listing_notfilled_notfeatured_id, $query->posts );
$this->assertContains( $listing_notfilled_featured_id, $query->posts );
$this->assertContains( $listing_filled_notfeatured_id, $query->posts );
$this->assertContains( $listing_filled_featured_id, $query->posts );

// Filtering on Filled.
$_GET['job_listing_filled'] = '1';
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) );
$this->assertNotContains( $listing_notfilled_notfeatured_id, $query->posts );
$this->assertNotContains( $listing_notfilled_featured_id, $query->posts );
$this->assertContains( $listing_filled_notfeatured_id, $query->posts );
$this->assertContains( $listing_filled_featured_id, $query->posts );

// Filtering on Featured.
$_GET['job_listing_filled'] = '';
$_GET['job_listing_featured'] = '0';
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) );
$this->assertContains( $listing_notfilled_notfeatured_id, $query->posts );
$this->assertNotContains( $listing_notfilled_featured_id, $query->posts );
$this->assertContains( $listing_filled_notfeatured_id, $query->posts );
$this->assertNotContains( $listing_filled_featured_id, $query->posts );
}

/**
* Ensure that filter_meta adds the correct filters to the query only on
* edit.php page.
*
* @since 1.31.0
* @covers WP_Job_Manager_CPT::filter_meta
*/
public function test_filter_meta_only_on_edit() {
global $pagenow;

// Create some listings.
$listing_id = $this->factory->post->create(
array( 'post_type' => 'job_listing' )
);

// Simulate viewing some other page.
$pagenow = 'index.php';

// Filter should do nothing
$_GET['job_listing_filled'] = '1';
$_GET['job_listing_featured'] = '1';
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) );
$this->assertContains( $listing_id, $query->posts );
}

/* Helper methods. */

private function create_listing_with_meta( $meta ) {
$id = $this->factory->post->create(
array( 'post_type' => 'job_listing' )
);

foreach ( $meta as $meta_key => $meta_value ) {
update_post_meta( $id, $meta_key, $meta_value );
}

return $id;
}
}

0 comments on commit 7ff287c

Please sign in to comment.