Skip to content

Instantly share code, notes, and snippets.

@kish2011
Last active February 20, 2017 12:02
Show Gist options
  • Save kish2011/bd417504e14dcc66806ad8d250c699f2 to your computer and use it in GitHub Desktop.
Save kish2011/bd417504e14dcc66806ad8d250c699f2 to your computer and use it in GitHub Desktop.
Featured Companies Widget
function featured_company_register_widgets() {
if ( ! class_exists( 'WP_Job_Manager_Widget' ) ) {
return;
}
/**
* Featured Companies Widget
*/
class WP_Job_Manager_Widget_Featured_Company extends WP_Job_Manager_Widget {
/**
* Constructor
*/
public function __construct() {
global $wp_post_types;
$this->widget_cssclass = 'job_manager widget_featured_companies';
$this->widget_description = __( 'Display a list of featured company listings on your site.', 'wp-job-manager-company-listings' );
$this->widget_id = 'widget_featured_companies';
$this->widget_name = sprintf( __( 'Featured %s', 'wp-job-manager-company-listings' ), __( 'Company', 'wp-job-manager-company-listings' ) );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => sprintf( __( 'Featured %s', 'wp-job-manager-company-listings' ), __( 'Company', 'wp-job-manager-company-listings' ) ),
'label' => __( 'Title', 'wp-job-manager-company-listings' )
),
'number' => array(
'type' => 'number',
'step' => 1,
'min' => 1,
'max' => '',
'std' => 10,
'label' => __( 'Number of listings to show', 'wp-job-manager-company-listings' )
)
);
$this->register();
add_filter( 'job_manager_get_listings', array( $this, 'job_manager_get_listings_filters' ), 10, 2 );
}
/**
* Queries job listings with certain criteria and returns them
* @see $query_args = apply_filters( 'job_manager_get_listings', $query_args, $args );
* @access public
* @return $query_args
*/
public function job_manager_get_listings_filters( $query_args, $args ) {
$args = wp_parse_args( $args, array('company_featured' => null));
if ( ! is_null( $args['company_featured'] ) ) {
$query_args['meta_query'][] = array(
'key' => '_company_featured',
'value' => '1',
'compare' => $args['company_featured'] ? '=' : '!='
);
}
return $query_args;
}
/**
* widget function.
*
* @see WP_Widget
* @access public
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
if ( $this->get_cached_widget( $args ) ) {
return;
}
ob_start();
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
$number = absint( $instance['number'] );
$companies = get_job_listings( array(
'posts_per_page' => $number,
'orderby' => 'date',
'order' => 'DESC',
'company_featured' => true
) );
if ( $companies->have_posts() ) : ?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul class="companies">
<?php while ( $companies->have_posts() ) : $companies->the_post(); ?>
<?php
get_job_manager_template_part( 'content-single', 'job_listing-company' );
// Please refer content-single-job_listing-company.php for more option.
?>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php else : ?>
<?php get_job_manager_template_part( 'content-widget', 'no-jobs-found' ); ?>
<?php endif;
wp_reset_postdata();
$content = ob_get_clean();
echo $content;
$this->cache_widget( $args, $content );
}
}
register_widget( 'WP_Job_Manager_Widget_Featured_Company' );
}
add_action( 'widgets_init', 'featured_company_register_widgets' );
/* Add the Featured Company fields to the admin */
add_filter( 'job_manager_job_listing_data_fields', 'admin_add_featured_company_field' );
function admin_add_featured_company_field( $fields ) {
global $post;
$current_user = wp_get_current_user();
if ( $current_user->has_cap( 'manage_job_listings' ) ) {
$fields['_company_featured'] = array(
'label' => __( 'Featured Company', 'wp-job-manager' ),
'type' => 'checkbox',
'description' => __( 'Featured Companies will be set, and can be styled differently.', 'wp-job-manager' ),
'priority' => 8
);
}
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment