From cbf665fb0f692d655248bb0860df47c2f8476adc Mon Sep 17 00:00:00 2001 From: Mikey Arce Date: Sat, 9 Dec 2023 12:01:57 -0800 Subject: [PATCH 1/4] Add featured first argument --- assets/js/ajax-filters.js | 2 ++ includes/class-wp-job-manager-ajax.php | 2 ++ includes/class-wp-job-manager-shortcodes.php | 9 +++++++++ wp-job-manager-functions.php | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/assets/js/ajax-filters.js b/assets/js/ajax-filters.js index 74cae42c4..ee7a13d75 100644 --- a/assets/js/ajax-filters.js +++ b/assets/js/ajax-filters.js @@ -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' ); @@ -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, diff --git a/includes/class-wp-job-manager-ajax.php b/includes/class-wp-job-manager-ajax.php index af1368a84..d89a16d1f 100644 --- a/includes/class-wp-job-manager-ajax.php +++ b/includes/class-wp-job-manager-ajax.php @@ -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 ) ) { @@ -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. ]; diff --git a/includes/class-wp-job-manager-shortcodes.php b/includes/class-wp-job-manager-shortcodes.php index cd0f4cc9d..6788c454e 100644 --- a/includes/class-wp-job-manager-shortcodes.php +++ b/includes/class-wp-job-manager-shortcodes.php @@ -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' => '', @@ -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 ); @@ -625,6 +627,9 @@ public function output_jobs( $atts ) { if ( ! is_null( $atts['remote_position'] ) ) { $atts['remote_position'] = ( is_bool( $atts['remote_position'] ) && $atts['remote_position'] ) || in_array( $atts['remote_position'], [ 1, '1', 'true', 'yes' ], true ); } + if ( ! is_null( $atts['featured_first'] ) ) { + $atts['featured_first'] = ( is_bool( $atts['featured_first'] ) && $atts['featured_first'] ) || in_array( $atts['featured_first'], [ 1, '1', 'true', 'yes' ], true ); + } // By default, use client-side state to populate form fields. $disable_client_state = false; @@ -679,6 +684,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'] ) { @@ -766,6 +772,9 @@ public function output_jobs( $atts ) { if ( ! empty( $atts['post_status'] ) ) { $data_attributes['post_status'] = implode( ',', $atts['post_status'] ); } + if ( ! empty( $atts['featured_first'] ) ) { + $data_attributes['featured_first'] = $atts['featured_first'] ? 'true' : 'false'; + } $data_attributes['post_id'] = isset( $GLOBALS['post'] ) ? $GLOBALS['post']->ID : 0; diff --git a/wp-job-manager-functions.php b/wp-job-manager-functions.php index c450976d8..c164b786f 100644 --- a/wp-job-manager-functions.php +++ b/wp-job-manager-functions.php @@ -34,6 +34,7 @@ function get_job_listings( $args = [] ) { 'filled' => null, 'remote_position' => null, 'fields' => 'all', + 'featured_first' => 0, ] ); @@ -180,6 +181,13 @@ function get_job_listings( $args = [] ) { ]; } + if ( 'true' === $args['featured_first'] ) { + $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 ) ) { From 7f3719b991c3953faae30108c1aee0a2e51ef56a Mon Sep 17 00:00:00 2001 From: Mikey Arce Date: Sat, 9 Dec 2023 13:46:39 -0800 Subject: [PATCH 2/4] Fix case where featured and rand_featured are set to the order --- wp-job-manager-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-job-manager-functions.php b/wp-job-manager-functions.php index c164b786f..d27aac350 100644 --- a/wp-job-manager-functions.php +++ b/wp-job-manager-functions.php @@ -181,7 +181,7 @@ function get_job_listings( $args = [] ) { ]; } - if ( 'true' === $args['featured_first'] ) { + if ( 'true' === $args['featured_first'] && 'featured' !== $args['orderby'] && 'rand_featured' !== $args['orderby'] ) { $query_args['orderby'] = [ 'menu_order' => 'ASC', $query_args['orderby'] => $query_args['order'], From d716a58bdd6dcb2742017797582c671869725f4b Mon Sep 17 00:00:00 2001 From: Mikey Arce Date: Mon, 11 Dec 2023 11:16:00 -0800 Subject: [PATCH 3/4] Remove extra string to bool conversion --- includes/class-wp-job-manager-shortcodes.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/class-wp-job-manager-shortcodes.php b/includes/class-wp-job-manager-shortcodes.php index 6788c454e..5261c62e5 100644 --- a/includes/class-wp-job-manager-shortcodes.php +++ b/includes/class-wp-job-manager-shortcodes.php @@ -627,9 +627,6 @@ public function output_jobs( $atts ) { if ( ! is_null( $atts['remote_position'] ) ) { $atts['remote_position'] = ( is_bool( $atts['remote_position'] ) && $atts['remote_position'] ) || in_array( $atts['remote_position'], [ 1, '1', 'true', 'yes' ], true ); } - if ( ! is_null( $atts['featured_first'] ) ) { - $atts['featured_first'] = ( is_bool( $atts['featured_first'] ) && $atts['featured_first'] ) || in_array( $atts['featured_first'], [ 1, '1', 'true', 'yes' ], true ); - } // By default, use client-side state to populate form fields. $disable_client_state = false; From c3b858a88193136c708f9508ab0ea779707195b0 Mon Sep 17 00:00:00 2001 From: Mikey Arce Date: Mon, 11 Dec 2023 11:18:49 -0800 Subject: [PATCH 4/4] Remove extra check --- includes/class-wp-job-manager-shortcodes.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/class-wp-job-manager-shortcodes.php b/includes/class-wp-job-manager-shortcodes.php index 5261c62e5..7f8123724 100644 --- a/includes/class-wp-job-manager-shortcodes.php +++ b/includes/class-wp-job-manager-shortcodes.php @@ -769,9 +769,6 @@ public function output_jobs( $atts ) { if ( ! empty( $atts['post_status'] ) ) { $data_attributes['post_status'] = implode( ',', $atts['post_status'] ); } - if ( ! empty( $atts['featured_first'] ) ) { - $data_attributes['featured_first'] = $atts['featured_first'] ? 'true' : 'false'; - } $data_attributes['post_id'] = isset( $GLOBALS['post'] ) ? $GLOBALS['post']->ID : 0;