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