1: <?php
2: if ( ! defined( 'ABSPATH' ) ) exit;
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class WooThemes_Sensei_Lesson_Component_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_lesson_component';
29: $this->woo_widget_description = __( 'This widget will output a list of the latest Lessons.', 'woothemes-sensei' );
30: $this->woo_widget_idbase = 'sensei_lesson_component';
31: $this->woo_widget_title = __( 'Sensei - Lesson Component', 'woothemes-sensei' );
32:
33: $this->woo_widget_componentslist = array(
34: 'newlessons' => __( 'New Lessons', 'woothemes-sensei' ),
35: );
36:
37:
38: $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
39:
40:
41: $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => $this->woo_widget_idbase );
42:
43:
44: parent::__construct( $this->woo_widget_idbase, $this->woo_widget_title, $widget_ops, $control_ops );
45: }
46:
47: 48: 49: 50: 51: 52: 53:
54: public function widget( $args, $instance ) {
55:
56: $before_widget = $args[ 'before_widget' ];
57: $before_title = $args[ 'before_title' ];
58: $after_title = $args[ 'after_title' ];
59: $after_widget = $args[ 'after_widget' ];
60:
61: if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) ) && ( 'activecourses' == $instance['component'] || 'completedcourses' == $instance['component'] ) && !is_user_logged_in() ) {
62:
63: } else {
64:
65: $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
66:
67:
68: echo $before_widget;
69:
70:
71: if ( $title ) { echo $before_title . $title . $after_title; }
72:
73:
74:
75: do_action( $this->woo_widget_cssclass . '_top' );
76:
77: if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) ) ) {
78: $this->load_component( $instance );
79: }
80:
81:
82: do_action( $this->woo_widget_cssclass . '_bottom' );
83:
84:
85: echo $after_widget;
86: }
87:
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: public function update ( $new_instance, $old_instance ) {
98: $instance = $old_instance;
99:
100:
101: $instance['title'] = strip_tags( $new_instance['title'] );
102:
103:
104: $instance['component'] = esc_attr( $new_instance['component'] );
105:
106:
107: $instance['limit'] = esc_attr( $new_instance['limit'] );
108:
109:
110: return $instance;
111: }
112:
113: 114: 115: 116: 117: 118: 119:
120: public function form( $instance ) {
121:
122:
123:
124: $defaults = array(
125: 'title' => '',
126: 'component' => '',
127: 'limit' => 3
128: );
129:
130: $instance = wp_parse_args( (array) $instance, $defaults );
131: ?>
132: <!-- Widget Title: Text Input -->
133: <p>
134: <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title (optional):', 'woothemes-sensei' ); ?></label>
135: <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' ) ); ?>" />
136: </p>
137: <!-- Widget Component: Select Input -->
138: <p>
139: <label for="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>"><?php _e( 'Component:', 'woothemes-sensei' ); ?></label>
140: <select name="<?php echo esc_attr( $this->get_field_name( 'component' ) ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>">
141: <?php foreach ( $this->woo_widget_componentslist as $k => $v ) { ?>
142: <option value="<?php echo esc_attr( $k ); ?>"<?php selected( $instance['component'], $k ); ?>><?php echo $v; ?></option>
143: <?php } ?>
144: </select>
145: </p>
146: <!-- Widget Limit: Text Input -->
147: <p>
148: <label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Number of Lessons (optional):', 'woothemes-sensei' ); ?></label>
149: <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' ) ); ?>" />
150: </p>
151:
152: <?php
153: }
154:
155: 156: 157: 158: 159: 160:
161: protected function load_component ( $instance ) {
162:
163: global $current_user;
164:
165: 166: 167:
168: $posts_array = array();
169:
170: $post_args = array( 'post_type' => 'lesson',
171: 'posts_per_page' => intval( $instance[ 'limit' ] ),
172: 'orderby' => 'menu_order date',
173: 'order' => 'DESC',
174: 'post_status' => 'publish',
175: 'suppress_filters' => 0
176: );
177: $posts_array = get_posts( $post_args );
178:
179: if ( count( $posts_array ) > 0 ) { ?>
180: <ul>
181: <?php foreach ($posts_array as $post_item){
182: $post_id = absint( $post_item->ID );
183: $post_title = $post_item->post_title;
184: $user_info = get_userdata( absint( $post_item->post_author ) );
185: $author_link = get_author_posts_url( absint( $post_item->post_author ) );
186: $author_display_name = $user_info->display_name;
187: $author_id = $post_item->post_author;
188: $lesson_course_id = get_post_meta( $post_id, '_lesson_course', true );
189: ?>
190: <li class="fix">
191: <?php do_action( 'sensei_lesson_image', $post_id, '100', '100', false, true ); ?>
192: <a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>" title="<?php echo esc_attr( $post_title ); ?>"><?php echo $post_title; ?></a>
193: <br />
194: <?php if ( isset( Sensei()->settings->settings[ 'lesson_author' ] ) && ( Sensei()->settings->settings[ 'lesson_author' ] ) ) { ?>
195: <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>
196: <br />
197: <?php }
198: <?php if ( 0 < $lesson_course_id ) { ?>
199: <span class="lesson-course"><?php echo ' ' . sprintf( __( 'Part of: %s', 'woothemes-sensei' ), '<a href="' . esc_url( get_permalink( $lesson_course_id ) ) . '" title="' . esc_attr( __( 'View course', 'woothemes-sensei' ) ) . '"><em>' . get_the_title( $lesson_course_id ) . '</em></a>' ); ?></span>
200: <?php } ?>
201: <br />
202: </li>
203: <?php }
204: <?php echo '<li class="my-account fix"><a class="button" href="'. esc_url( get_post_type_archive_link( 'lesson' ) ) .'">'.__('More Lessons', 'woothemes-sensei').'</a></li>'; ?>
205: </ul>
206: <?php }
207: }
208: }