| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Widget class |
| 5 |
* |
| 6 |
* @author: themeum |
| 7 |
* @author_uri: https://themeum.com |
| 8 |
* @package Tutor |
| 9 |
* @since v.1.3.1 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace TUTOR; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) |
| 15 |
exit; |
| 16 |
|
| 17 |
class Course_Widget extends \WP_Widget { |
| 18 |
|
| 19 |
function __construct() { |
| 20 |
parent::__construct( |
| 21 |
'tutor_course_widget', // Base ID |
| 22 |
esc_html__( 'Tutor Course', 'tutor' ), // Name |
| 23 |
array( 'description' => esc_html__( 'Get the courses and show it anywhere widget support available', 'tutor' ), ) // Args |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Front-end display of widget. |
| 29 |
* |
| 30 |
* @see WP_Widget::widget() |
| 31 |
* |
| 32 |
* @param array $args Widget arguments. |
| 33 |
* @param array $instance Saved values from database. |
| 34 |
*/ |
| 35 |
public function widget( $args, $instance ) { |
| 36 |
echo $args['before_widget']; |
| 37 |
if ( ! empty( $instance['title'] ) ) { |
| 38 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 39 |
} |
| 40 |
|
| 41 |
$course_post_type = tutor()->course_post_type; |
| 42 |
|
| 43 |
$form_args = $instance; |
| 44 |
unset($form_args['title']); |
| 45 |
|
| 46 |
$default_args = array( |
| 47 |
'post_type' => $course_post_type, |
| 48 |
'post_status' => 'publish', |
| 49 |
|
| 50 |
'id' => '', |
| 51 |
'exclude_ids' => '', |
| 52 |
'category' => '', |
| 53 |
|
| 54 |
'orderby' => 'ID', |
| 55 |
'order' => 'DESC', |
| 56 |
'count' => '6', |
| 57 |
); |
| 58 |
|
| 59 |
$a = array_merge($default_args, $form_args); |
| 60 |
|
| 61 |
if ( ! empty($a['id'])){ |
| 62 |
$ids = (array) explode(',', $a['id']); |
| 63 |
$a['post__in'] = $ids; |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! empty($a['exclude_ids'])){ |
| 67 |
$exclude_ids = (array) explode(',', $a['exclude_ids']); |
| 68 |
$a['post__not_in'] = $exclude_ids; |
| 69 |
} |
| 70 |
if ( ! empty($a['category'])){ |
| 71 |
$category = (array) explode(',', $a['category']); |
| 72 |
|
| 73 |
$a['tax_query'] = array( |
| 74 |
array( |
| 75 |
'taxonomy' => 'course-category', |
| 76 |
'field' => 'term_id', |
| 77 |
'terms' => $category, |
| 78 |
'operator' => 'IN', |
| 79 |
), |
| 80 |
); |
| 81 |
} |
| 82 |
$a['posts_per_page'] = (int) $a['count']; |
| 83 |
|
| 84 |
wp_reset_query(); |
| 85 |
query_posts($a); |
| 86 |
ob_start(); |
| 87 |
tutor_load_template('widget.courses'); |
| 88 |
$output = ob_get_clean(); |
| 89 |
wp_reset_query(); |
| 90 |
|
| 91 |
|
| 92 |
echo $output; |
| 93 |
|
| 94 |
echo $args['after_widget']; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Back-end widget form. |
| 99 |
* |
| 100 |
* @see WP_Widget::form() |
| 101 |
* |
| 102 |
* @param array $instance Previously saved values from database. |
| 103 |
*/ |
| 104 |
public function form( $instance ) { |
| 105 |
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'tutor' ); |
| 106 |
$id = ! empty( $instance['id'] ) ? $instance['id'] : ''; |
| 107 |
$exclude_ids = ! empty( $instance['exclude_ids'] ) ? $instance['exclude_ids'] : ''; |
| 108 |
$category = ! empty( $instance['category'] ) ? $instance['category'] : ''; |
| 109 |
$orderby = ! empty( $instance['orderby'] ) ? $instance['orderby'] : ''; |
| 110 |
$order = ! empty( $instance['order'] ) ? $instance['order'] : ''; |
| 111 |
$count = ! empty( $instance['count'] ) ? $instance['count'] : '6'; |
| 112 |
?> |
| 113 |
<p> |
| 114 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'tutor' ); ?></label> |
| 115 |
<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 ); ?>"> |
| 116 |
</p> |
| 117 |
|
| 118 |
<p> |
| 119 |
<label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php esc_attr_e( 'ID:', 'tutor' ); ?></label> |
| 120 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>" type="text" value="<?php echo esc_attr( $id ); ?>"> <br /> |
| 121 |
<span style="color: #AAAAAA"><?php _e('Place single course id or comma (,) separated course ids', 'tutor'); ?></span> |
| 122 |
</p> |
| 123 |
|
| 124 |
<p> |
| 125 |
<label for="<?php echo esc_attr( $this->get_field_id( 'exclude_ids' ) ); ?>"><?php esc_attr_e( 'Exclude IDS:', 'tutor' ); ?></label> |
| 126 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'exclude_ids' ) ); ?>" name="<?php echo esc_attr( |
| 127 |
$this->get_field_name( 'exclude_ids' ) ); ?>" type="text" value="<?php echo esc_attr( $exclude_ids ); ?>"> <br /> |
| 128 |
<span style="color: #AAAAAA"><?php _e('Place comma (,) separated coruses ids which you like to exclude from the query', 'tutor'); |
| 129 |
?></span> |
| 130 |
</p> |
| 131 |
|
| 132 |
<p> |
| 133 |
<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php esc_attr_e( 'Category:', 'tutor' ); ?></label> |
| 134 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" name="<?php echo esc_attr( |
| 135 |
$this->get_field_name( 'category' ) ); ?>" type="text" value="<?php echo esc_attr( $category ); ?>"> <br /> |
| 136 |
<span style="color: #AAAAAA"><?php _e('Place comma (,) separated category ids', 'tutor'); |
| 137 |
?></span> |
| 138 |
</p> |
| 139 |
|
| 140 |
<p> |
| 141 |
<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php esc_attr_e( 'OrderBy', 'tutor' ); ?></label> |
| 142 |
|
| 143 |
<select class="widefat" name="<?php echo esc_attr($this->get_field_name( 'orderby' ) ); ?>" > |
| 144 |
<option value="ID" <?php selected('ID', $orderby) ?> >ID</option> |
| 145 |
<option value="title" <?php selected('title', $orderby) ?> >title</option> |
| 146 |
<option value="rand" <?php selected('rand', $orderby) ?> >rand</option> |
| 147 |
<option value="date" <?php selected('date', $orderby) ?> >date</option> |
| 148 |
<option value="menu_order" <?php selected('menu_order', $orderby) ?> >menu_order</option> |
| 149 |
<option value="post__in" <?php selected('post__in', $orderby) ?> >post__in</option> |
| 150 |
</select> <br /> |
| 151 |
</p> |
| 152 |
|
| 153 |
<p> |
| 154 |
<label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php esc_attr_e( 'order', 'tutor' ); ?></label> |
| 155 |
|
| 156 |
<select class="widefat" name="<?php echo esc_attr($this->get_field_name( 'order' ) ); ?>" > |
| 157 |
<option value="DESC" <?php selected('DESC', $order) ?> >DESC</option> |
| 158 |
<option value="ASC" <?php selected('ASC', $order) ?> >ASC</option> |
| 159 |
</select> <br /> |
| 160 |
</p> |
| 161 |
|
| 162 |
<p> |
| 163 |
<label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php esc_attr_e( 'Count:', 'tutor' ); ?></label> |
| 164 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( |
| 165 |
$this->get_field_name( 'count' ) ); ?>" type="number" value="<?php echo esc_attr( $count ); ?>"> <br /> |
| 166 |
<span style="color: #AAAAAA"><?php _e('Total results you like to show', 'tutor'); ?></span> |
| 167 |
</p> |
| 168 |
|
| 169 |
<?php |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Sanitize widget form values as they are saved. |
| 174 |
* |
| 175 |
* @see WP_Widget::update() |
| 176 |
* |
| 177 |
* @param array $new_instance Values just sent to be saved. |
| 178 |
* @param array $old_instance Previously saved values from database. |
| 179 |
* |
| 180 |
* @return array Updated safe values to be saved. |
| 181 |
*/ |
| 182 |
public function update( $new_instance, $old_instance ) { |
| 183 |
$instance = array(); |
| 184 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : ''; |
| 185 |
$instance['id'] = ( ! empty( $new_instance['id'] ) ) ? sanitize_text_field( $new_instance['id'] ) : ''; |
| 186 |
$instance['exclude_ids'] = ( ! empty( $new_instance['exclude_ids'] ) ) ? sanitize_text_field( $new_instance['exclude_ids'] ) : ''; |
| 187 |
$instance['category'] = ( ! empty( $new_instance['category'] ) ) ? sanitize_text_field( $new_instance['category'] ) : ''; |
| 188 |
$instance['orderby'] = ( ! empty( $new_instance['orderby'] ) ) ? sanitize_text_field( $new_instance['orderby'] ) : ''; |
| 189 |
$instance['order'] = ( ! empty( $new_instance['order'] ) ) ? sanitize_text_field( $new_instance['order'] ) : ''; |
| 190 |
$instance['count'] = ( ! empty( $new_instance['count'] ) ) ? sanitize_text_field( $new_instance['count'] ) : ''; |
| 191 |
|
| 192 |
return $instance; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
} |