1: <?php
2: if ( ! defined( 'ABSPATH' ) ) exit;
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class WooThemes_Sensei_Category_Courses_Widget extends WP_Widget {
16: protected $woo_widget_cssclass;
17: protected $woo_widget_description;
18: protected $woo_widget_idbase;
19: protected $woo_widget_title;
20:
21: 22: 23: 24: 25:
26: public function __construct() {
27:
28: $this->woo_widget_cssclass = 'widget_sensei_category_courses';
29: $this->woo_widget_description = __( 'This widget will output a list of Courses for a specific category.', 'woothemes-sensei' );
30: $this->woo_widget_idbase = 'sensei_category_courses';
31: $this->woo_widget_title = __( 'Sensei - Category Courses', 'woothemes-sensei' );
32:
33:
34: $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
35:
36:
37: $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => $this->woo_widget_idbase );
38:
39:
40: parent::__construct( $this->woo_widget_idbase, $this->woo_widget_title, $widget_ops, $control_ops );
41:
42: }
43:
44: 45: 46: 47: 48: 49: 50:
51: public function widget( $args, $instance ) {
52:
53: $before_widget = $args[ 'before_widget' ];
54: $before_title = $args[ 'before_title' ];
55: $after_title = $args[ 'after_title' ];
56: $after_widget = $args[ 'after_widget' ];
57:
58:
59: $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
60:
61:
62: echo $before_widget;
63:
64:
65: if ( $title ) { echo $before_title . $title . $after_title; }
66:
67:
68:
69: do_action( $this->woo_widget_cssclass . '_top' );
70:
71: if ( 0 < intval( $instance['course_category'] ) ) {
72: $this->load_component( $instance );
73: }
74:
75:
76: do_action( $this->woo_widget_cssclass . '_bottom' );
77:
78:
79: echo $after_widget;
80:
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public function update ( $new_instance, $old_instance ) {
91: $instance = $old_instance;
92:
93:
94: $instance['title'] = strip_tags( $new_instance['title'] );
95:
96:
97: $instance['course_category'] = esc_attr( $new_instance['course_category'] );
98:
99:
100: $instance['limit'] = strip_tags( $new_instance['limit'] );
101:
102:
103: return $instance;
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: public function form( $instance ) {
114:
115:
116:
117: $defaults = array(
118: 'title' => '',
119: 'course_category' => 0,
120: 'limit' => 3
121: );
122:
123: $instance = wp_parse_args( (array) $instance, $defaults );
124: ?>
125: <!-- Widget Title: Text Input -->
126: <p>
127: <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title (optional):', 'woothemes-sensei' ); ?></label>
128: <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" />
129: </p>
130: <!-- Widget Course Category: Select Input -->
131: <p>
132: <label for="<?php echo esc_attr( $this->get_field_id( 'course_category' ) ); ?>"><?php _e( 'Course Category:', 'woothemes-sensei' ); ?></label>
133: <?php
134: $cat_args = array( 'hierarchical' => true, 'show_option_none' => __( 'Select Category:', 'woothemes-sensei' ), 'taxonomy' => 'course-category', 'orderby' => 'name', 'selected' => intval( $instance['course_category'] ), 'id' => $this->get_field_id( 'course_category' ), 'name' => $this->get_field_name( 'course_category' ), 'class' => 'widefat' );
135: wp_dropdown_categories(apply_filters('widget_course_categories_dropdown_args', $cat_args));
136: ?>
137: </p>
138: <!-- Widget Limit: Text Input -->
139: <p>
140: <label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Number of Courses (optional):', 'woothemes-sensei' ); ?></label>
141: <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>" value="<?php echo esc_attr( $instance['limit'] ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>" />
142: </p>
143:
144: <?php
145: }
146:
147: 148: 149: 150: 151: 152:
153: protected function load_component ( $instance ) {
154:
155:
156: $posts_array = array();
157: $post_args = array( 'post_type' => 'course',
158: 'posts_per_page' => intval( $instance[ 'limit' ] ),
159: 'orderby' => 'menu_order date',
160: 'order' => 'ASC',
161: 'post_status' => 'publish',
162: 'suppress_filters' => 0,
163: );
164:
165: $post_args[ 'tax_query' ] = array(
166: array(
167: 'taxonomy' => 'course-category',
168: 'field' => 'id',
169: 'terms' => intval( $instance['course_category'] )
170: )
171: );
172:
173: $posts_array = get_posts( $post_args );
174:
175: if ( count( $posts_array ) > 0 ) { ?>
176: <ul>
177: <?php foreach ($posts_array as $post_item){
178: $post_id = absint( $post_item->ID );
179: $post_title = $post_item->post_title;
180: $user_info = get_userdata( absint( $post_item->post_author ) );
181: $author_link = get_author_posts_url( absint( $post_item->post_author ) );
182: $author_display_name = $user_info->display_name;
183: $author_id = $post_item->post_author;
184: ?>
185: <li class="fix">
186: <?php do_action( 'sensei_course_image', $post_id ); ?>
187: <a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>" title="<?php echo esc_attr( $post_title ); ?>"><?php echo $post_title; ?></a>
188: <br />
189: <?php if ( isset( Sensei()->settings->settings[ 'course_author' ] ) && ( Sensei()->settings->settings[ 'course_author' ] ) ) { ?>
190: <span class="course-author"><?php _e( 'by ', 'woothemes-sensei' ); ?><a href="<?php echo esc_url( $author_link ); ?>" title="<?php echo esc_attr( $author_display_name ); ?>"><?php echo esc_html( $author_display_name ); ?></a></span>
191: <br />
192: <?php }
193: <span class="course-lesson-count"><?php echo Sensei()->course->course_lesson_count( $post_id ) . ' ' . __( 'Lessons', 'woothemes-sensei' ); ?></span>
194: <br />
195: <?php sensei_simple_course_price( $post_id ); ?>
196: </li>
197: <?php }
198: </ul>
199: <?php }
200: }
201: }