widgets.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. add_action( 'widgets_init', 'booked_register_widgets' );
  3. function booked_register_widgets(){
  4. register_widget( 'Booked_Calendar_Widget' );
  5. }
  6. class Booked_Calendar_Widget extends WP_Widget {
  7. public function __construct() {
  8. $widget_ops = array(
  9. 'classname' => 'booked_calendar',
  10. 'description' => 'The Booked Calendar Widget',
  11. );
  12. parent::__construct( 'booked_calendar', esc_html__('Booked Calendar','booked'), $widget_ops );
  13. }
  14. function form($instance) {
  15. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  16. $calendar = isset($instance['booked_calendar_chooser']) ? $instance['booked_calendar_chooser'] : 0;
  17. $month = isset($instance['booked_calendar_month']) ? $instance['booked_calendar_month'] : 0;
  18. $year = isset($instance['booked_calendar_year']) ? $instance['booked_calendar_year'] : 0;
  19. $args = array(
  20. 'taxonomy' => 'booked_custom_calendars',
  21. 'show_option_none' => 'Default',
  22. 'option_none_value' => 0,
  23. 'hide_empty' => 0,
  24. 'echo' => 0,
  25. 'orderby' => 'name',
  26. 'id' => $this->get_field_id('booked_calendar_chooser'),
  27. 'name' => $this->get_field_name('booked_calendar_chooser'),
  28. 'selected' => $calendar
  29. );
  30. if (!get_option('booked_hide_default_calendar')): $args['show_option_all'] = esc_html__('Default Calendar','booked'); endif;
  31. ?>
  32. <p>
  33. <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Widget Title','booked'); ?>:</label>
  34. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  35. </p>
  36. <p class="booked-widget-col-13">
  37. <label><?php esc_html_e('Calendar to Display','booked'); ?>:</label><br>
  38. <?php echo str_replace( "\n", '', wp_dropdown_categories( $args ) ); ?>
  39. </p>
  40. <?php $current_month = 0; ?>
  41. <p class="booked-widget-col-13">
  42. <label><?php esc_html_e('Month','booked'); ?>:</label><br>
  43. <select name="<?php echo $this->get_field_name('booked_calendar_month'); ?>">
  44. <?php do {
  45. echo '<option value="'.$current_month.'"'.($month == $current_month ? ' selected' : '').'>'.(!$current_month ? esc_html__('Current month') : date_i18n('F',strtotime('2016-'.$current_month.'-01'))).'</option>';
  46. $current_month++;
  47. } while ($current_month <= 12); ?>
  48. </select>
  49. </p>
  50. <?php $current_year = date_i18n('Y'); $highest_year = $current_year + 25; ?>
  51. <p class="booked-widget-col-13">
  52. <label><?php esc_html_e('Year','booked'); ?>:</label><br>
  53. <select name="<?php echo $this->get_field_name('booked_calendar_year'); ?>">
  54. <option value="0"<?php if (!$year): ?> selected<?php endif; ?>><?php esc_html_e('Current year'); ?></option>
  55. <?php do {
  56. echo '<option value="'.$current_year.'"'.($year == $current_year ? ' selected' : '').'>'.$current_year.'</option>';
  57. $current_year++;
  58. } while ($current_year <= $highest_year); ?>
  59. </select>
  60. </p>
  61. <?php
  62. }
  63. function widget($args, $instance) {
  64. extract( $args );
  65. // these are our widget options
  66. $widget_title = isset($instance['title']) ? $instance['title'] : false;
  67. $title = apply_filters('widget_title', $widget_title);
  68. $calendar = isset($instance['booked_calendar_chooser']) ? $instance['booked_calendar_chooser'] : false;
  69. $month = isset($instance['booked_calendar_month']) ? $instance['booked_calendar_month'] : false;
  70. $year = isset($instance['booked_calendar_year']) ? $instance['booked_calendar_year'] : false;
  71. echo $before_widget;
  72. if ( $title ) {
  73. echo $before_title . $title . $after_title;
  74. }
  75. echo do_shortcode('[booked-calendar size="small"'.($calendar ? ' calendar="'.$calendar.'"' : '').($month ? ' month="'.$month.'"' : '').($year ? ' year="'.$year.'"' : '').']');
  76. echo $after_widget;
  77. }
  78. function update($new_instance, $old_instance) {
  79. $instance = $old_instance;
  80. $instance['title'] = strip_tags($new_instance['title']);
  81. $instance['booked_calendar_month'] = $new_instance['booked_calendar_month'];
  82. $instance['booked_calendar_year'] = $new_instance['booked_calendar_year'];
  83. $instance['booked_calendar_chooser'] = $new_instance['booked_calendar_chooser'];
  84. return $instance;
  85. }
  86. }