Completed
Push — master ( efb4fd...b89b53 )
by Dwain
05:05
created

display_courses()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 60
Code Lines 39

Duplication

Lines 21
Ratio 35 %
Metric Value
dl 21
loc 60
rs 8.6961
cc 6
eloc 39
nc 6
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	protected $instance;
21
22
	/**
23
	 * Constructor function.
24
	 * @since  1.0.0
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
46
		/* Widget settings. */
47
		$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
48
49
		/* Widget control settings. */
50
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => $this->woo_widget_idbase );
51
52
		/* Create the widget. */
53
		parent::__construct( $this->woo_widget_idbase, $this->woo_widget_title, $widget_ops, $control_ops );
54
	} // End __construct()
55
56
	/**
57
	 * Display the widget on the frontend.
58
	 * @since  1.0.0
59
	 * @param  array $args     Widget arguments.
60
	 * @param  array $instance Widget settings for this instance.
61
	 * @return void
62
	 */
63
	public function widget( $args, $instance ) {
64
65
		remove_filter( 'pre_get_posts', 'sensei_course_archive_filter', 10, 1 );
66
67
		//don't show active or completed course if a user is not logged in
68
		if ( ! in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) )
69
		     || ( ! is_user_logged_in() && ( 'activecourses' == $instance['component'] || 'completedcourses' == $instance['component'] ) )  ) {
70
			// No Output
71
            return;
72
73
		}
74
75
		$this->instance = $instance;
76
77
		/* Our variables from the widget settings. */
78
		$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
79
80
		/* Before widget (defined by themes). */
81
		echo $args['before_widget'];
82
83
		/* Display the widget title if one was input (before and after defined by themes). */
84
		if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
85
86
		/* Widget content. */
87
		// Add actions for plugins/themes to hook onto.
88
		do_action( $this->woo_widget_cssclass . '_top' );
89
90
		if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) ) ) {
91
			$this->load_component( $instance );
92
		}
93
94
		// Add actions for plugins/themes to hook onto.
95
		do_action( $this->woo_widget_cssclass . '_bottom' );
96
97
		/* After widget (defined by themes). */
98
		echo $args['after_widget'];
99
100
101
		add_filter( 'pre_get_posts', 'sensei_course_archive_filter', 10, 1 );
102
103
	} // End widget()
104
105
	/**
106
	 * Method to update the settings from the form() method.
107
	 * @since  1.0.0
108
	 * @param  array $new_instance New settings.
109
	 * @param  array $old_instance Previous settings.
110
	 * @return array               Updated settings.
111
	 */
112 View Code Duplication
	public function update ( $new_instance, $old_instance ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
		$instance = $old_instance;
114
115
		/* Strip tags for title and name to remove HTML (important for text inputs). */
116
		$instance['title'] = strip_tags( $new_instance['title'] );
117
118
		/* The select box is returning a text value, so we escape it. */
119
		$instance['component'] = esc_attr( $new_instance['component'] );
120
121
		/* The select box is returning a text value, so we escape it. */
122
		$instance['limit'] = esc_attr( $new_instance['limit'] );
123
124
125
		return $instance;
126
	} // End update()
127
128
	/**
129
	 * The form on the widget control in the widget administration area.
130
	 * Make use of the get_field_id() and get_field_name() function when creating your form elements. This handles the confusing stuff.
131
	 * @since  1.0.0
132
	 * @param  array $instance The settings for this instance.
133
	 * @return void
134
	 */
135 View Code Duplication
    public function form( $instance ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
137
		/* Set up some default widget settings. */
138
		/* Make sure all keys are added here, even with empty string values. */
139
		$defaults = array(
140
						'title' => '',
141
						'component' => '',
142
						'limit' => 3
143
					);
144
145
		$instance = wp_parse_args( (array) $instance, $defaults );
146
?>
147
		<!-- Widget Title: Text Input -->
148
		<p>
149
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title (optional):', 'woothemes-sensei' ); ?></label>
150
			<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' ) ); ?>" />
151
		</p>
152
		<!-- Widget Component: Select Input -->
153
		<p>
154
			<label for="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>"><?php _e( 'Component:', 'woothemes-sensei' ); ?></label>
155
			<select name="<?php echo esc_attr( $this->get_field_name( 'component' ) ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>">
156
			<?php foreach ( $this->woo_widget_componentslist as $k => $v ) { ?>
157
				<option value="<?php echo esc_attr( $k ); ?>"<?php selected( $instance['component'], $k ); ?>><?php echo $v; ?></option>
158
			<?php } ?>
159
			</select>
160
		</p>
161
		<!-- Widget Limit: Text Input -->
162
		<p>
163
			<label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Number of Courses (optional):', 'woothemes-sensei' ); ?></label>
164
			<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' ) ); ?>" />
165
		</p>
166
167
<?php
168
	} // End form()
169
170
	/**
171
	 * Load the desired component, if a method is available for it.
172
	 *
173
	 * @param array $instance The component to potentially be loaded.
174
     *
175
	 * @since  1.0.0
176
	 * @return void
177
	 */
178
	protected function load_component ( $instance ) {
179
180
		$courses = array();
0 ignored issues
show
Unused Code introduced by
$courses is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
181
182
		if ( 'usercourses' == esc_attr( $instance['component'] ) ) {
183
			// usercourses == new courses
184
			$courses =  $this->get_new_courses( );
185
186
		} elseif ( 'activecourses' == esc_attr( $instance['component'] ) ) {
187
188
			$courses =  $this->get_active_courses( );
189
190
191
		} elseif ( 'completedcourses' == esc_attr( $instance['component'] ) ) {
192
193
			$courses =  $this->get_completed_courses();
194
195
		} elseif ( 'featuredcourses' == esc_attr( $instance['component'] ) ) {
196
197
			$courses =  $this->get_featured_courses();
198
199
		} elseif ( 'paidcourses' == esc_attr( $instance['component'] ) ) {
200
201
			$args = array( 'posts_per_page' => $this->instance['limit'] );
202
			$courses =  Sensei_WC::get_paid_courses( $args );
203
204
		} elseif ( 'freecourses' == esc_attr( $instance['component'] ) ) {
205
206
			$args = array( 'posts_per_page' => $this->instance['limit'] );
207
			$courses =  Sensei_WC::get_free_courses( $args );
208
209
		} else {
210
211
			return;
212
213
		}
214
215
		// course_query() is buggy, it doesn't honour the 1st arg if includes are provided, so instead slice the includes
216
		if ( !empty($instance['limit']) && intval( $instance['limit'] ) >= 1 && intval( $instance['limit'] ) < count($courses) ) {
217
218
			$courses = array_slice( $courses, 0, intval( $instance['limit'] ) );
219
220
		}
221
222
		if ( empty( $courses ) && $instance['limit'] != 0 ) {
223
224
			$this->display_no_courses_message();
225
			return;
226
227
		}
228
229
		$this->display_courses( $courses );
230
231
	} // End load_component()
232
233
234
	/**
235
	 * Output the message telling the user that
236
	 * there are no course for their desired settings
237
	 *
238
	 * @since 1.9.2
239
	 */
240
	public function display_no_courses_message ( ) {
241
242
		if ( 'featuredcourses' == $this->instance['component'] ) {
243
244
			_e( 'You have no featured courses.', 'woothemes-sensei' );
245
246
		} elseif ( 'activecourses' == $this->instance['component'] ) {
247
248
			_e( 'You have no active courses.', 'woothemes-sensei' );
249
250
		} elseif ( 'completedcourses' == $this->instance['component'] ) {
251
252
			_e( 'You have no completed courses.', 'woothemes-sensei' );
253
254
		}else{
255
256
			_e( 'You have no courses.', 'woothemes-sensei' );
257
258
		}
259
	}
260
261
	/**
262
	 * Output the widget courses
263
	 *
264
	 * @since 1.9.2
265
	 * @param array $courses
266
	 */
267
	public function display_courses( $courses = array() ){ ?>
268
		<ul>
269
			<?php
270
271 View Code Duplication
			foreach ($courses as $course) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
273
				$post_id = absint( $course->ID );
274
				$post_title = $course->post_title;
275
				$user_info = get_userdata( absint( $course->post_author ) );
276
				$author_link = get_author_posts_url( absint( $course->post_author ) );
277
				$author_display_name = $user_info->display_name;
278
				$author_id = $course->post_author;
0 ignored issues
show
Unused Code introduced by
$author_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
279
				?>
280
281
				<li class="fix">
282
283
					<?php do_action( 'sensei_course_image', $post_id ); ?>
284
285
					<a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>"
286
					   title="<?php echo esc_attr( $post_title ); ?>">
287
288
						<?php echo $post_title; ?>
289
290
					</a>
291
					<br />
292
293
					<?php if ( isset( Sensei()->settings->settings[ 'course_author' ] ) && ( Sensei()->settings->settings[ 'course_author' ] ) ) { ?>
294
						<span class="course-author">
295
							<?php _e( 'by ', 'woothemes-sensei' ); ?>
296
							<a href="<?php echo esc_url( $author_link ); ?>" title="<?php echo esc_attr( $author_display_name ); ?>">
297
								<?php echo esc_html( $author_display_name ); ?>
298
							</a>
299
						</span>
300
						<br />
301
					<?php } // End If Statement ?>
302
303
					<span class="course-lesson-count">
304
						<?php echo Sensei()->course->course_lesson_count( $post_id ) . '&nbsp;' . __( 'Lessons', 'woothemes-sensei' ); ?>
305
					</span>
306
307
					<br />
308
309
					<?php sensei_simple_course_price( $post_id ); ?>
310
311
				</li>
312
313
			<?php
314
			} // End For Loop
315
316
			if ( 'activecourses' == esc_attr( $this->instance['component'] ) || 'completedcourses' == esc_attr( $this->instance['component'] ) ) {
317
				$my_account_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] );
318
				echo '<li class="my-account fix"><a href="'. esc_url( get_permalink( $my_account_page_id ) ) .'">'
319
				     .__('My Courses', 'woothemes-sensei')
320
				     .'<span class="meta-nav"></span></a></li>';
321
			} // End If Statement
322
323
			?>
324
		</ul>
325
326
	<?php }
327
328
	/**
329
	 * The default course query args
330
	 *
331
	 * @return array
332
	 */
333
	public function get_default_query_args(){
334
335
		return array(
336
				'post_type' => 'course',
337
				'orderby'         	=> 'date',
338
				'order'           	=> 'DESC',
339
				'post_status'      	=> 'publish',
340
				'posts_per_page' => $this->instance['limit'],
341
				'suppress_filters' => 0,
342
		);
343
344
	}
345
346
	/**
347
	 * Get all new course IDS
348
	 * @since 1.9.2
349
	 *
350
	 * @return array $courses
351
	 */
352
	public function get_new_courses ( ) {
353
354
		return get_posts( $this->get_default_query_args( ) );
355
356
	}
357
358
	/**
359
	 * Get all active course IDS for the current user
360
	 * @since 1.9.2
361
	 *
362
	 * @return array $courses
363
	 */
364 View Code Duplication
	public function get_active_courses ( ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
365
366
		$courses = array();
367
		$activity_args = array( 'user_id' => get_current_user_id(), 'type' => 'sensei_course_status', 'status' => 'in-progress' );
368
		$user_courses_activity = (array) Sensei_Utils::sensei_check_for_activity( $activity_args, true );
369
370
		foreach( $user_courses_activity AS $activity ) {
371
			$courses[] = get_post( $activity->comment_post_ID );
372
		}
373
374
		return $courses;
375
376
	}
377
378
	/**
379
	 * Get all active course IDS for the current user
380
	 * @since 1.9.2
381
	 *
382
	 * @return array $courses
383
	 */
384 View Code Duplication
	public function get_completed_courses ( ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
385
386
		$courses = array();
387
		$activity_args = array( 'user_id' => get_current_user_id(), 'type' => 'sensei_course_status', 'status' => 'complete' );
388
		$user_courses_activity = (array) Sensei_Utils::sensei_check_for_activity( $activity_args , true );
389
390
		foreach( $user_courses_activity AS $activity ) {
391
			$courses[] = get_post( $activity->comment_post_ID );
392
		}
393
		return $courses;
394
	}
395
396
	/**
397
	 * Get all active course IDS for the current user
398
	 * @since 1.9.2
399
	 *
400
	 * @return array $courses
401
	 */
402
	public function get_featured_courses ( ) {
403
404
		$query_args = $this->get_default_query_args();
405
		$query_args[ 'meta_key' ] = '_course_featured';
406
		$query_args[ 'meta_value' ] = 'featured';
407
		$query_args[ 'meta_compare' ] = '=';
408
409
		return get_posts( $query_args );
410
411
	}
412
} // End Class