Skip to content

Instantly share code, notes, and snippets.

@PaulGwamanda
Created February 13, 2017 08:51
Show Gist options
  • Select an option

  • Save PaulGwamanda/7f1348d22bcc094997a1c4fcf485ddf3 to your computer and use it in GitHub Desktop.

Select an option

Save PaulGwamanda/7f1348d22bcc094997a1c4fcf485ddf3 to your computer and use it in GitHub Desktop.
Export Jobs to HTML - Custom Bulk Action job manager wordpress theme -- modyfying custom bulk action
<?php
/*
Plugin Name: Export Jobs to HTML - Custom Bulk Action job manager wordpress theme -- modyfying custom bulk action
Description: Modifying Custom Bulk Action
Version: 1.0
*/
class THEME_Custom_Bulk_Action {
public function __construct()
{
if (is_admin()) {
add_action('admin_footer-edit.php', array(&$this, 'custom_bulk_admin_footer'));
add_action('load-edit.php', array(&$this, 'custom_bulk_action'));
//add in a hook to display the region filter
add_action( "restrict_manage_posts", array($this, "jobs_by_region" ));
add_action( "restrict_manage_posts", array($this, "jobs_by_job_type" ));
}
}
/**
* Step 1: add the custom Bulk Action to the select menus
*/
function custom_bulk_admin_footer() {
global $post_type;
if($post_type == 'job_listing') {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('<option>').val('exportToHtml').text('<?php echo _e('Export To HTML')?>').appendTo("select[name='action']");
jQuery('<option>').val('exportToHtml').text('<?php echo _e('Export To HTML')?>').appendTo("select[name='action2']");
});
</script>
<?php
}
}
/**
* 2. Handle the custom Bulk Action
*
*/
function custom_bulk_action() {
global $typenow;
$post_type = $typenow;
if($post_type == 'job_listing') {
/**
* get the action
*/
$wp_list_table = _get_list_table('WP_Posts_List_Table');
$action = $wp_list_table->current_action();
$allowed_actions = array("exportToHtml");
if(!in_array($action, $allowed_actions)) return;
/**
* security check
*/
check_admin_referer('bulk-posts');
if(isset($_REQUEST['post'])) {
$post_ids = array_map('intval', $_REQUEST['post']);
}
// this is based on wp-admin/edit.php
if(empty($post_ids)) return;
//call a function that will query to get the post data using the ids
//render the jobs in standard html format, using the /opportunities/ as an example (table structure only)
//ensure a link to the job is displayed as it is currently on the site
switch($action) {
case 'exportToHtml':
//get the jobs for the given ids
$jobs = new WP_Query(
array(
'post_type' => 'job_listing',
'post__in' => $post_ids
)
);
echo $this->renderHtml($jobs);
wp_reset_postdata();
exit;
break;
}
}
}
/**
* Render the jobs in html like the /opportunities page
*
* @param array $jobs The list of jobs and their data
* @return string The html formatted table with the jobs
*/
public function renderHtml($jobs)
{
echo '
<html>
<body>
<table class="job-listings" border="1" cellpadding="5" cellspacing="0" style="
border-collapse: collapse;
" width="100%">
<thead>
<tr>
<th class="ref" width="100">Ref</th>
<th class="position">Title/Industry/Location</th>
</tr>
</thead>
<tbody class="job_listings">';
while ( $jobs->have_posts() ) {
$jobs->the_post();
?>
<tr <?php job_listing_class(); ?>>
<th valign="top">
#<?php echo get_the_ID();?><?php the_author_meta('THEME_initials'); //$job_ref = get_post_meta( get_the_ID(), '_THEME_job_reference', true );?><?php //echo esc_html( $job_ref ) ;?>
</th>
<?php /** <td class="job-type <?php echo get_the_job_type() ? sanitize_title( get_the_job_type()->slug ) :
* ''; ?>" valign="top"><span><?php the_job_type(); ?></span></td>
* **/?>
<td class="position" valign="top">
<a href="<?php the_job_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
<?php do_action( 'job_listing_meta_start' ); ?>
<?php
$terms = get_the_terms (get_the_ID(), 'job_listing_category');
if ( !is_wp_error($terms)) {
$terms = wp_list_pluck($terms, 'name');
$terms = implode(", ", $terms);
}
?>
<?php if ($terms) :?><div><?php echo $terms;?></div><?php endif;?>
<div class="location"><?php echo THEME_get_category_parents_list( get_the_ID(), 'job-region', ', '); ?></div>
</td>
<?php /** <td class="salary" valign="top">
<?php $job_salary_value = get_post_meta( get_the_ID(), '_THEME_job_salary', true );?>
<?php if ($job_salary_value):?><span class="salary"><?php echo esc_html( $job_salary_value ) ;?></span><?php endif;?>
</td> **/?>
</tr>
<?php
}
echo '</tbody></table></body></html>';
}
/**
* Adds in a Region filter to filter jobs by regions
*
* @see jobs_by_category in job_listing plugin as an example
*
* Should return a select html element with the regions (similar to current categories
*
* @return string Select dropdown with a list of regions (hierarchical)
*/
public function jobs_by_region() {
global $typenow, $wp_query;
if ( $typenow != 'job_listing' || ! taxonomy_exists( 'job-region' ) ) {
return;
}
include_once( JOB_MANAGER_PLUGIN_DIR . '/includes/class-wp-job-manager-category-walker.php' );
$r = array();
$r['pad_counts'] = 1;
$r['hierarchical'] = 1;
$r['hide_empty'] = 0;
$r['show_count'] = 1;
$r['selected'] = ( isset( $wp_query->query['job-region'] ) ) ? $wp_query->query['job-region'] : '';
$r['menu_order'] = false;
$terms = get_terms( 'job-region', $r );
$walker = new WP_Job_Manager_Category_Walker;
if ( ! $terms ) {
return;
}
$output = "<select name='job-region' id='dropdown_job-region'>";
$output .= '<option value="" ' . selected( isset( $_GET['job-region'] ) ?
$_GET['job-region'] : '', '', false ) . '>' . __( 'Select region', 'wp-job-manager' ) . '</option>';
$output .= $walker->walk( $terms, 0, $r );
$output .= "</select>";
echo $output;
}
/**
* Adds in a Text filter to filter out jobs by title and description
*
* @return string Text input
*/
// public function jobs_by_keyword() <-- This is handled by the native search function
// {
// }
/**
* Adds in a Job Type Filter to filter out jobs by the type
*
* @return string Select dropdown with the ability to multiselect Job Types
*/
public function jobs_by_job_type()
{
global $typenow, $wp_query;
if ( $typenow != 'job_listing' || ! taxonomy_exists( 'job_listing_type' ) ) {
return;
}
include_once( JOB_MANAGER_PLUGIN_DIR . '/includes/class-wp-job-manager-category-walker.php' );
$r = array();
$r['hide_empty'] = 1;
$r['show_count'] = 1;
$r['selected'] = ( isset( $wp_query->query['job_listing_type'] ) ) ? $wp_query->query['job_listing_type'] : '';
$terms = get_terms( 'job_listing_type', $r );
$walker = new WP_Job_Manager_Category_Walker;
if ( ! $terms ) {
return;
}
$output = "<select name='job_listing_type' id='dropdown_job_listing_type'>";
$output .= '<option value="" ' . selected( isset( $_GET['job_listing_type'] ) ? $_GET['job_listing_type'] : '', '', false ) . '>' . __( 'Select job type', 'wp-job-manager' ) . '</option>';
$output .= $walker->walk( $terms, 0, $r );
$output .= "</select>";
echo $output;
}
}
new THEME_Custom_Bulk_Action();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment