1
|
|
|
<?php
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
|
3
|
|
|
|
4
|
|
|
/**
|
5
|
|
|
* Sensei Course Component Widget
|
6
|
|
|
*
|
7
|
|
|
* A WooThemes standardized component widget.
|
8
|
|
|
*
|
9
|
|
|
* @package Views
|
10
|
|
|
* @subpackage Widgets
|
11
|
|
|
* @author Automattic
|
12
|
|
|
*
|
13
|
|
|
* @since 1.1.0
|
14
|
|
|
*/
|
15
|
|
|
class WooThemes_Sensei_Course_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
|
|
|
* Constructor function.
|
23
|
|
|
* @since 1.0.0
|
24
|
|
|
* @return void
|
|
|
|
|
25
|
|
|
*/
|
26
|
|
|
public function __construct() {
|
27
|
|
|
/* Widget variable settings. */
|
28
|
|
|
$this->woo_widget_cssclass = 'widget_sensei_course_component';
|
29
|
|
|
$this->woo_widget_description = __( 'This widget will output a list of Courses - New, Featured, Free, Paid, Active, Completed.', 'woothemes-sensei' );
|
30
|
|
|
$this->woo_widget_idbase = 'sensei_course_component';
|
31
|
|
|
$this->woo_widget_title = __( 'Sensei - Course Component', 'woothemes-sensei' );
|
32
|
|
|
|
33
|
|
|
$this->woo_widget_componentslist = array(
|
34
|
|
|
'usercourses' => __( 'New Courses', 'woothemes-sensei' ),
|
35
|
|
|
'featuredcourses' => __( 'Featured Courses', 'woothemes-sensei' ),
|
36
|
|
|
'activecourses' => __( 'My Active Courses', 'woothemes-sensei' ),
|
37
|
|
|
'completedcourses' => __( 'My Completed Courses', 'woothemes-sensei' ),
|
38
|
|
|
);
|
39
|
|
|
|
40
|
|
|
// Add support for the WooCommerce shelf.
|
41
|
|
|
if ( Sensei_WC::is_woocommerce_active() ) {
|
42
|
|
|
$this->woo_widget_componentslist['freecourses'] = __( 'Free Courses', 'woothemes-sensei' );
|
43
|
|
|
$this->woo_widget_componentslist['paidcourses'] = __( 'Paid Courses', 'woothemes-sensei' );
|
44
|
|
|
}
|
45
|
|
|
/* Widget settings. */
|
46
|
|
|
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
|
47
|
|
|
|
48
|
|
|
/* Widget control settings. */
|
49
|
|
|
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => $this->woo_widget_idbase );
|
50
|
|
|
|
51
|
|
|
/* Create the widget. */
|
52
|
|
|
parent::__construct( $this->woo_widget_idbase, $this->woo_widget_title, $widget_ops, $control_ops );
|
53
|
|
|
} // End __construct()
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Display the widget on the frontend.
|
57
|
|
|
* @since 1.0.0
|
58
|
|
|
* @param array $args Widget arguments.
|
59
|
|
|
* @param array $instance Widget settings for this instance.
|
60
|
|
|
* @return void
|
61
|
|
|
*/
|
62
|
|
|
public function widget( $args, $instance ) {
|
63
|
|
|
|
64
|
|
|
remove_filter( 'pre_get_posts', 'sensei_course_archive_filter', 10, 1 );
|
65
|
|
|
|
66
|
|
|
if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) )
|
67
|
|
|
&& ( 'activecourses' == $instance['component'] || 'completedcourses' == $instance['component'] )
|
68
|
|
|
&& !is_user_logged_in() ) {
|
69
|
|
|
|
70
|
|
|
// No Output
|
71
|
|
|
return;
|
72
|
|
|
|
73
|
|
|
} else {
|
74
|
|
|
/* Our variables from the widget settings. */
|
75
|
|
|
$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
|
76
|
|
|
|
77
|
|
|
/* Before widget (defined by themes). */
|
78
|
|
|
echo $args['before_widget'];
|
79
|
|
|
|
80
|
|
|
/* Display the widget title if one was input (before and after defined by themes). */
|
81
|
|
|
if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
|
82
|
|
|
|
83
|
|
|
/* Widget content. */
|
84
|
|
|
// Add actions for plugins/themes to hook onto.
|
85
|
|
|
do_action( $this->woo_widget_cssclass . '_top' );
|
86
|
|
|
|
87
|
|
|
if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) ) ) {
|
88
|
|
|
$this->load_component( $instance );
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
// Add actions for plugins/themes to hook onto.
|
92
|
|
|
do_action( $this->woo_widget_cssclass . '_bottom' );
|
93
|
|
|
|
94
|
|
|
/* After widget (defined by themes). */
|
95
|
|
|
echo $args['after_widget'];
|
96
|
|
|
|
97
|
|
|
} // End If Statement
|
98
|
|
|
|
99
|
|
|
add_filter( 'pre_get_posts', 'sensei_course_archive_filter', 10, 1 );
|
100
|
|
|
|
101
|
|
|
} // End widget()
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* Method to update the settings from the form() method.
|
105
|
|
|
* @since 1.0.0
|
106
|
|
|
* @param array $new_instance New settings.
|
107
|
|
|
* @param array $old_instance Previous settings.
|
108
|
|
|
* @return array Updated settings.
|
109
|
|
|
*/
|
110
|
|
View Code Duplication |
public function update ( $new_instance, $old_instance ) {
|
|
|
|
|
111
|
|
|
$instance = $old_instance;
|
112
|
|
|
|
113
|
|
|
/* Strip tags for title and name to remove HTML (important for text inputs). */
|
114
|
|
|
$instance['title'] = strip_tags( $new_instance['title'] );
|
115
|
|
|
|
116
|
|
|
/* The select box is returning a text value, so we escape it. */
|
117
|
|
|
$instance['component'] = esc_attr( $new_instance['component'] );
|
118
|
|
|
|
119
|
|
|
/* The select box is returning a text value, so we escape it. */
|
120
|
|
|
$instance['limit'] = esc_attr( $new_instance['limit'] );
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
return $instance;
|
124
|
|
|
} // End update()
|
125
|
|
|
|
126
|
|
|
/**
|
127
|
|
|
* The form on the widget control in the widget administration area.
|
128
|
|
|
* Make use of the get_field_id() and get_field_name() function when creating your form elements. This handles the confusing stuff.
|
129
|
|
|
* @since 1.0.0
|
130
|
|
|
* @param array $instance The settings for this instance.
|
131
|
|
|
* @return void
|
132
|
|
|
*/
|
133
|
|
View Code Duplication |
public function form( $instance ) {
|
|
|
|
|
134
|
|
|
|
135
|
|
|
/* Set up some default widget settings. */
|
136
|
|
|
/* Make sure all keys are added here, even with empty string values. */
|
137
|
|
|
$defaults = array(
|
138
|
|
|
'title' => '',
|
139
|
|
|
'component' => '',
|
140
|
|
|
'limit' => 3
|
141
|
|
|
);
|
142
|
|
|
|
143
|
|
|
$instance = wp_parse_args( (array) $instance, $defaults );
|
144
|
|
|
?>
|
145
|
|
|
<!-- Widget Title: Text Input -->
|
146
|
|
|
<p>
|
147
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title (optional):', 'woothemes-sensei' ); ?></label>
|
148
|
|
|
<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' ) ); ?>" />
|
149
|
|
|
</p>
|
150
|
|
|
<!-- Widget Component: Select Input -->
|
151
|
|
|
<p>
|
152
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>"><?php _e( 'Component:', 'woothemes-sensei' ); ?></label>
|
153
|
|
|
<select name="<?php echo esc_attr( $this->get_field_name( 'component' ) ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>">
|
154
|
|
|
<?php foreach ( $this->woo_widget_componentslist as $k => $v ) { ?>
|
155
|
|
|
<option value="<?php echo esc_attr( $k ); ?>"<?php selected( $instance['component'], $k ); ?>><?php echo $v; ?></option>
|
156
|
|
|
<?php } ?>
|
157
|
|
|
</select>
|
158
|
|
|
</p>
|
159
|
|
|
<!-- Widget Limit: Text Input -->
|
160
|
|
|
<p>
|
161
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Number of Courses (optional):', 'woothemes-sensei' ); ?></label>
|
162
|
|
|
<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' ) ); ?>" />
|
163
|
|
|
</p>
|
164
|
|
|
|
165
|
|
|
<?php
|
166
|
|
|
} // End form()
|
167
|
|
|
|
168
|
|
|
/**
|
169
|
|
|
* Load the desired component, if a method is available for it.
|
170
|
|
|
* @param string $component The component to potentially be loaded.
|
|
|
|
|
171
|
|
|
*
|
172
|
|
|
* @since 1.0.0
|
173
|
|
|
* @return void
|
174
|
|
|
*/
|
175
|
|
|
protected function load_component ( $instance ) {
|
176
|
|
|
global $current_user;
|
177
|
|
|
|
178
|
|
|
$course_ids = array();
|
179
|
|
|
if ( 'activecourses' == esc_attr( $instance['component'] ) ) {
|
180
|
|
|
$courses = Sensei_Utils::sensei_check_for_activity( array( 'user_id' => $current_user->ID, 'type' => 'sensei_course_status', 'status' => 'in-progress' ), true );
|
181
|
|
|
// Need to always return an array, even with only 1 item
|
182
|
|
|
if ( !is_array($courses) ) {
|
183
|
|
|
$courses = array( $courses );
|
184
|
|
|
}
|
185
|
|
|
foreach( $courses AS $course ) {
|
186
|
|
|
$course_ids[] = $course->comment_post_ID;
|
187
|
|
|
}
|
188
|
|
|
} elseif( 'completedcourses' == esc_attr( $instance['component'] ) ) {
|
189
|
|
|
$courses = Sensei_Utils::sensei_check_for_activity( array( 'user_id' => $current_user->ID, 'type' => 'sensei_course_status', 'status' => 'complete' ), true );
|
190
|
|
|
// Need to always return an array, even with only 1 item
|
191
|
|
|
if ( !is_array($courses) ) {
|
192
|
|
|
$courses = array( $courses );
|
193
|
|
|
}
|
194
|
|
|
foreach( $courses AS $course ) {
|
195
|
|
|
$course_ids[] = $course->comment_post_ID;
|
196
|
|
|
}
|
197
|
|
|
} // End If Statement
|
198
|
|
|
|
199
|
|
|
$posts_array = array();
|
|
|
|
|
200
|
|
|
|
201
|
|
|
// course_query() is buggy, it doesn't honour the 1st arg if includes are provided, so instead slice the includes
|
202
|
|
|
if ( !empty($instance['limit']) && intval( $instance['limit'] ) >= 1 && intval( $instance['limit'] ) < count($course_ids) ) {
|
203
|
|
|
|
204
|
|
|
$course_ids = array_slice( $course_ids, 0, intval( $instance['limit'] ) ); // This does mean the order by is effectively ignored
|
205
|
|
|
|
206
|
|
|
}
|
207
|
|
|
|
208
|
|
|
if ( ! empty( $course_ids ) ) {
|
209
|
|
|
|
210
|
|
|
$posts_array = Sensei()->course->course_query( intval( $instance['limit'] ), esc_attr( $instance['component'] ), $course_ids );
|
211
|
|
|
|
212
|
|
|
} else {
|
213
|
|
|
|
214
|
|
|
if ( 'activecourses' == esc_attr( $instance['component'] ) || 'completedcourses' == esc_attr( $instance['component'] ) ) {
|
215
|
|
|
$posts_array = array();
|
216
|
|
|
|
217
|
|
|
} else {
|
218
|
|
|
|
219
|
|
|
$course_args = array(
|
220
|
|
|
'post_type' => 'course',
|
221
|
|
|
'orderby' => 'date',
|
222
|
|
|
'order' => 'DESC',
|
223
|
|
|
'post_status' => 'publish',
|
224
|
|
|
'posts_per_page' => $instance['limit'],
|
225
|
|
|
);
|
226
|
|
|
|
227
|
|
|
$posts_array = get_posts( $course_args );
|
228
|
|
|
}
|
229
|
|
|
|
230
|
|
|
} // End If Statement
|
231
|
|
|
|
232
|
|
|
if ( count( $posts_array ) > 0 ) { ?>
|
233
|
|
|
<ul>
|
234
|
|
View Code Duplication |
<?php foreach ($posts_array as $post_item){
|
|
|
|
|
235
|
|
|
$post_id = absint( $post_item->ID );
|
236
|
|
|
$post_title = $post_item->post_title;
|
237
|
|
|
$user_info = get_userdata( absint( $post_item->post_author ) );
|
238
|
|
|
$author_link = get_author_posts_url( absint( $post_item->post_author ) );
|
239
|
|
|
$author_display_name = $user_info->display_name;
|
240
|
|
|
$author_id = $post_item->post_author;
|
|
|
|
|
241
|
|
|
?>
|
242
|
|
|
<li class="fix">
|
243
|
|
|
<?php do_action( 'sensei_course_image', $post_id ); ?>
|
244
|
|
|
<a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>" title="<?php echo esc_attr( $post_title ); ?>"><?php echo $post_title; ?></a>
|
245
|
|
|
<br />
|
246
|
|
|
<?php if ( isset( Sensei()->settings->settings[ 'course_author' ] ) && ( Sensei()->settings->settings[ 'course_author' ] ) ) { ?>
|
247
|
|
|
<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>
|
248
|
|
|
<br />
|
249
|
|
|
<?php } // End If Statement ?>
|
250
|
|
|
<span class="course-lesson-count"><?php echo Sensei()->course->course_lesson_count( $post_id ) . ' ' . __( 'Lessons', 'woothemes-sensei' ); ?></span>
|
251
|
|
|
<br />
|
252
|
|
|
<?php sensei_simple_course_price( $post_id ); ?>
|
253
|
|
|
</li>
|
254
|
|
|
<?php } // End For Loop ?>
|
255
|
|
|
<?php if ( 'activecourses' == esc_attr( $instance['component'] ) || 'completedcourses' == esc_attr( $instance['component'] ) ) {
|
256
|
|
|
$my_account_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] );
|
257
|
|
|
echo '<li class="my-account fix"><a href="'. esc_url( get_permalink( $my_account_page_id ) ) .'">'.__('My Courses', 'woothemes-sensei').' <span class="meta-nav"></span></a></li>';
|
258
|
|
|
} // End If Statement ?>
|
259
|
|
|
</ul>
|
260
|
|
|
<?php } else {
|
261
|
|
|
// No posts returned. This means the user either has no active or no completed courses.
|
262
|
|
|
if( isset( $instance['component'] ) ) {
|
263
|
|
|
|
264
|
|
|
if ( 'featuredcourses' == $instance['component'] ) {
|
265
|
|
|
|
266
|
|
|
_e( 'You have no featured courses.', 'woothemes-sensei' );
|
267
|
|
|
|
268
|
|
|
} elseif ( 'activecourses' == $instance['component'] ) {
|
269
|
|
|
|
270
|
|
|
_e( 'You have no active courses.', 'woothemes-sensei' );
|
271
|
|
|
|
272
|
|
|
} elseif ( 'completedcourses' == $instance['component'] ) {
|
273
|
|
|
_e( 'You have no completed courses.', 'woothemes-sensei' );
|
274
|
|
|
|
275
|
|
|
}else{
|
276
|
|
|
|
277
|
|
|
_e( 'You have no courses.', 'woothemes-sensei' );
|
278
|
|
|
|
279
|
|
|
}
|
280
|
|
|
|
281
|
|
|
} // end if compoenetn status instance
|
282
|
|
|
|
283
|
|
|
} // End If Statement
|
284
|
|
|
|
285
|
|
|
} // End load_component()
|
286
|
|
|
|
287
|
|
|
} // End Class |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.