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/featured first argument #2675

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions assets/js/ajax-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ jQuery( document ).ready( function( $ ) {
var $results = $target.find( '.job_listings' );
var per_page = $target.data( 'per_page' );
var orderby = $target.data( 'orderby' );
var featured_first = $target.data( 'featured_first' );
var order = $target.data( 'order' );
var featured = $target.data( 'featured' );
var filled = $target.data( 'filled' );
Expand Down Expand Up @@ -380,6 +381,7 @@ jQuery( document ).ready( function( $ ) {
filter_post_status: post_status,
per_page: per_page,
orderby: orderby,
featured_first: featured_first,
order: order,
page: page,
featured: featured,
Expand Down
2 changes: 2 additions & 0 deletions includes/class-wp-job-manager-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function get_listings() {
$featured = isset( $_REQUEST['featured'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['featured'] ) ) : null;
$remote_position = isset( $_REQUEST['remote_position'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_position'] ) ) : null;
$show_pagination = isset( $_REQUEST['show_pagination'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['show_pagination'] ) ) : null;
$featured_first = isset( $_REQUEST['featured_first'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['featured_first'] ) ) : null;
// phpcs:enable WordPress.Security.NonceVerification.Recommended

if ( is_array( $search_categories ) ) {
Expand All @@ -154,6 +155,7 @@ public function get_listings() {
'post_status' => $filter_post_status,
'orderby' => $orderby,
'order' => $order,
'featured_first' => $featured_first,
'offset' => ( $page - 1 ) * $per_page,
'posts_per_page' => max( 1, $per_page ), // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- Known slow query.
];
Expand Down
3 changes: 3 additions & 0 deletions includes/class-wp-job-manager-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ public function output_jobs( $atts ) {
'featured' => null, // True to show only featured, false to hide featured, leave null to show both.
'filled' => null, // True to show only filled, false to hide filled, leave null to show both/use the settings.
'remote_position' => null, // True to show only remote, false to hide remote, leave null to show both.
'featured_first' => false, // True to show featured first, false to show in default order.

// Default values for filters.
'location' => '',
Expand All @@ -613,6 +614,7 @@ public function output_jobs( $atts ) {
$atts['show_category_multiselect'] = $this->string_to_bool( $atts['show_category_multiselect'] );
$atts['show_more'] = $this->string_to_bool( $atts['show_more'] );
$atts['show_pagination'] = $this->string_to_bool( $atts['show_pagination'] );
$atts['featured_first'] = $this->string_to_bool( $atts['featured_first'] );

if ( ! is_null( $atts['featured'] ) ) {
$atts['featured'] = ( is_bool( $atts['featured'] ) && $atts['featured'] ) || in_array( $atts['featured'], [ 1, '1', 'true', 'yes' ], true );
Expand Down Expand Up @@ -679,6 +681,7 @@ public function output_jobs( $atts ) {
'order' => $atts['order'],
'categories' => implode( ',', $atts['categories'] ),
'disable-form-state-storage' => $disable_client_state,
'featured_first' => $atts['featured_first'] ? 'true' : 'false',
];

if ( $atts['show_filters'] ) {
Expand Down
8 changes: 8 additions & 0 deletions wp-job-manager-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function get_job_listings( $args = [] ) {
'filled' => null,
'remote_position' => null,
'fields' => 'all',
'featured_first' => 0,
]
);

Expand Down Expand Up @@ -180,6 +181,13 @@ function get_job_listings( $args = [] ) {
];
}

if ( 'true' === $args['featured_first'] && 'featured' !== $args['orderby'] && 'rand_featured' !== $args['orderby'] ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does $args['featured_first'] (and all added data_attributes) have to be a string ?
If we want to test it against a bool, it's way better to test it ... well ... against a bool, but maybe there's a reason for that ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to also account for people who might use featured_first="0" or featured_first="1" so checking the string worked for all cases. What do you think about something like this:

if ( true === filter_var( $args['featured_first'], FILTER_VALIDATE_BOOLEAN ) && 'featured' !== $args['orderby'] && 'rand_featured' !== $args['orderby'] ) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the data here were populated by https://github.com/Automattic/WP-Job-Manager/pull/2675/files#diff-a0e82a74ed6b687d4ee6efce7bb4c78d6fa157212afa751c5c40f88306c489ddR684 , hence my question about making it boolean directly instead of string, but, if it's supposed to do more than what I thought, that's ok as-is

$query_args['orderby'] = [
'menu_order' => 'ASC',
$query_args['orderby'] => $query_args['order'],
];
}

$job_manager_keyword = sanitize_text_field( $args['search_keywords'] );

if ( ! empty( $job_manager_keyword ) && strlen( $job_manager_keyword ) >= apply_filters( 'job_manager_get_listings_keyword_length_threshold', 2 ) ) {
Expand Down
Loading