class-simple-job-board-widgets-recent-jobs.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. exit;
  4. } // Exit if accessed directly
  5. /**
  6. * Simple_Job_Board_Widgets_Recent_Jobs class
  7. *
  8. * @link http://presstigers.com
  9. * @since 2.4.3
  10. *
  11. * @package Simple_Job_Board
  12. * @subpackage Simple_Job_Board/admin
  13. * @author PressTigers <support@presstigers.com>
  14. */
  15. class Simple_Job_Board_Widgets_Recent_Jobs extends WP_Widget {
  16. /**
  17. * Register widget with WordPress
  18. *
  19. * @since 2.4.3
  20. *
  21. * @param void
  22. * @param return void
  23. */
  24. public function __construct() {
  25. $widget_opts = array(
  26. 'classname' => 'sjb-recent-jobs-widget',
  27. 'description' => __('Your site\'s most recent Jobs.', 'simple-job-board')
  28. );
  29. parent::__construct('Simple_Job_Board_Widgets_Recent_Jobs', __('SJB Recent Jobs', 'simple-job-board'), $widget_opts);
  30. }
  31. /**
  32. * @SJB Core: Widgets Recent Jobs form Back-end.
  33. *
  34. * @since 2.4.3
  35. *
  36. * @param array $instance Previously saved values from database.
  37. * return void
  38. */
  39. public function form($instance) {
  40. $instance = wp_parse_args((array) $instance, array('title' => ''));
  41. // Widgets Form Default Parameters
  42. $title = $instance['title'];
  43. $showcount = isset($instance['showcount']) ? esc_attr($instance['showcount']) : '5';
  44. $job_category = isset($instance['job_category']) ? $instance['job_category'] : '0';
  45. // Job Categories
  46. $cat_arg = array(
  47. 'type' => 'jobpost',
  48. 'child_of' => 0,
  49. 'taxonomy' => 'jobpost_category',
  50. 'hide_empty' => FALSE,
  51. );
  52. $categories = get_categories($cat_arg);
  53. ?>
  54. <!-- Widget Title -->
  55. <p>
  56. <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"> <?php _e('Title:', 'simple-job-board'); ?>
  57. <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
  58. </label>
  59. </p>
  60. <!-- Job Categories -->
  61. <p>
  62. <label for="<?php echo $this->get_field_id('job_category'); ?>"> <?php _e('Select Category:', 'simple-job-board'); ?>
  63. <select class="widefat" id="<?php echo esc_attr($this->get_field_id('job_category')); ?>" name="<?php echo esc_attr($this->get_field_name('job_category')); ?>">
  64. <option value="0"><?php echo __('All', 'simple-job-board'); ?></option>
  65. <?php
  66. if (isset($categories) && $categories) {
  67. foreach ($categories as $category) {
  68. ?>
  69. <option <?php selected($job_category, $category->slug); ?> value="<?php echo esc_attr($category->slug); ?>" ><?php echo esc_attr($category->name); ?></option>
  70. <?php
  71. }
  72. }
  73. ?>
  74. </select>
  75. </label>
  76. </p>
  77. <!-- Number of Job Posts -->
  78. <p>
  79. <label for="<?php echo $this->get_field_id('showcount'); ?>"> <?php _e('Number of posts to display:', 'simple-job-board'); ?>
  80. <input type="text" value="<?php echo $showcount; ?>" id="<?php echo esc_attr($this->get_field_id('showcount')); ?>" size='2' name="<?php echo esc_attr($this->get_field_name('showcount')); ?>" />
  81. </label>
  82. </p>
  83. <?php
  84. }
  85. /**
  86. * @SJB Core: Widgets Recent Jobs Sanitize widget form values as they are saved.
  87. *
  88. * @since 2.4.3
  89. *
  90. * @param array $new_instance Values just sent to be saved,
  91. * @param array $old_instance Previously saved values from database.
  92. *
  93. * @return array Updated safe values to be saved.
  94. */
  95. public function update($new_instance, $old_instance) {
  96. // Updated Widget Data
  97. $instance = $old_instance;
  98. $instance['title'] = $new_instance['title'];
  99. $instance['showcount'] = $new_instance['showcount'];
  100. $instance['job_category'] = $new_instance['job_category'];
  101. return $instance;
  102. }
  103. /**
  104. * @SJB Core: Widgets Recent Jobs Front-end display of widget.
  105. *
  106. * @since 2.4.3
  107. *
  108. * @param array $args Widget arguments
  109. * @param array $instance Saved values from database.
  110. * @return void
  111. */
  112. public function widget($args, $instance) {
  113. extract($args, EXTR_SKIP);
  114. $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
  115. $showcount = empty($instance['showcount']) ? '5' : $instance['showcount'];
  116. $jobpost_category = empty($instance['job_category']) ? '' : $instance['job_category'];
  117. $title = htmlspecialchars_decode(stripslashes($title));
  118. $html = '';
  119. $arguments = array(
  120. 'posts_per_page' => $showcount,
  121. 'post_type' => 'jobpost',
  122. );
  123. if (isset($jobpost_category) && $jobpost_category != '0') {
  124. $arguments['jobpost_category'] = $jobpost_category;
  125. }
  126. // Job Query
  127. $wp_recent_jobs = new WP_Query($arguments);
  128. // List Jobs
  129. if ($wp_recent_jobs->have_posts()) {
  130. // Display Widget Before HTML
  131. echo $before_widget;
  132. // Widget Title
  133. if (!empty($title) && $title != ' ') {
  134. echo $before_title . $title . $after_title;
  135. }
  136. /**
  137. * Template -> Widget Start Wrapper
  138. *
  139. * @since 2.4.3
  140. */
  141. get_simple_job_board_template('widget/job-widget-start.php');
  142. while ( $wp_recent_jobs->have_posts() ) {
  143. $wp_recent_jobs->the_post();
  144. /**
  145. * Template -> Recent Job Widget Content
  146. *
  147. * @since 2.4.3
  148. */
  149. get_simple_job_board_template('widget/content-recent-jobs-widget.php');
  150. }
  151. /**
  152. * Template -> Widget End Wrapper
  153. *
  154. * @since 2.4.3
  155. */
  156. get_simple_job_board_template('widget/job-widget-end.php');
  157. wp_reset_query();
  158. // Display Widget After HTML
  159. echo $after_widget;
  160. }
  161. }
  162. }