Passed
Push — master ( cdd39e...ebde67 )
by Warwick
02:47 queued 12s
created

LSX_Sensei   F

Complexity

Total Complexity 77

Size/Duplication

Total Lines 638
Duplicated Lines 25.55 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 163
loc 638
rs 2.202
c 0
b 0
f 0
wmc 77
lcom 0
cbo 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 59 1
A lsx_sensei_scripts_add_styles() 0 4 1
A lsx_sensei_theme_wrapper_start() 7 7 1
A lsx_sensei_theme_wrapper_end() 0 7 1
B lsx_sensei_modify_archive_title() 0 15 9
A lsx_sensei_disable_lsx_banner() 0 7 2
A lsx_sensei_archive_title() 0 4 1
A lsx_sensei_category_title() 0 4 1
A lsx_sensei_add_buttons() 0 17 3
A lsx_sensei_add_to_cart_text() 0 4 1
B lsx_sensei_results_header() 5 46 7
A lsx_sensei_course_breadcrumb_filter() 17 34 5
B lsx_sensei_lesson_breadcrumb_filter() 55 55 8
B lsx_sensei_module_breadcrumb_filter() 17 37 6
B lsx_sensei_learner_breadcrumb_filter() 5 41 6
B lsx_sensei_messages_breadcrumb_filter() 0 37 6
B lsx_sensei_quiz_breadcrumb_filter() 57 57 8
B lsx_sensei_results_breadcrumb_filter() 0 54 8
A lsx_sensei_view_message_button() 0 4 1
A lsx_sensei_back_message_button() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like LSX_Sensei often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LSX_Sensei, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * LSX Sensei Class
4
 *
5
 * @package    lsx
6
 * @subpackage sensei
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
if ( ! class_exists( 'LSX_Sensei' ) ) :
14
15
	/**
16
	 * The LSX Sensei integration class
17
	 */
18
	class LSX_Sensei {
19
20
		/**
21
		 * Holds the LSX_Sensei_Course() variable.
22
		 *
23
		 * @var LSX_Sensei_Course()
24
		 */
25
		public $lsx_sensei_course = false;
26
27
		/**
28
		 * Setup class.
29
		 *
30
		 * @since 1.0
31
		 */
32
		public function __construct() {
33
34
			$this->lsx_sensei_course = require_once get_stylesheet_directory() . '/includes/sensei/class-lsx-sensei-course.php';
35
36
			global $woothemes_sensei;
37
38
			add_action( 'wp_enqueue_scripts', array( $this, 'lsx_sensei_scripts_add_styles' ) );
39
40
			remove_action( 'sensei_before_main_content', array( $woothemes_sensei->frontend, 'sensei_output_content_wrapper' ), 10 );
41
			add_action( 'sensei_before_main_content', array( $this, 'lsx_sensei_theme_wrapper_start' ) );
42
43
			remove_action( 'sensei_after_main_content', array( $woothemes_sensei->frontend, 'sensei_output_content_wrapper_end' ), 10 );
44
			add_action( 'sensei_after_main_content', array( $this, 'lsx_sensei_theme_wrapper_end' ) );
45
46
			add_filter( 'get_the_archive_title', array( $this, 'lsx_sensei_modify_archive_title' ), 99, 1 );
47
48
			// LSX
49
			add_filter( 'lsx_global_header_disable', array( $this, 'lsx_sensei_disable_lsx_banner' ) );
50
			// LSX Banners - Plugin, Placeholders
51
			add_filter( 'lsx_banner_plugin_disable', array( $this, 'lsx_sensei_disable_lsx_banner' ) );
52
			// LSX Banners - Banner
53
			add_filter( 'lsx_banner_disable', array( $this, 'lsx_sensei_disable_lsx_banner' ) );
54
55
			add_filter( 'course_archive_title', array( $this, 'lsx_sensei_archive_title' ), 10, 1 );
56
			add_filter( 'sensei_lesson_archive_title', array( $this, 'lsx_sensei_archive_title' ), 10, 1 );
57
58
			add_filter( 'course_category_title', array( $this, 'lsx_sensei_category_title' ), 10, 1 );
59
60
			add_action( 'sensei_course_content_inside_after', array( $this, 'lsx_sensei_add_buttons' ), 9 );
61
62
			add_filter( 'sensei_wc_single_add_to_cart_button_text', array( $this, 'lsx_sensei_add_to_cart_text' ) );
63
64
			add_action( 'lsx_content_wrap_before', array( $this, 'lsx_sensei_results_header' ) );
65
66
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_course_breadcrumb_filter' ), 40, 1 );
67
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_course_breadcrumb_filter' ), 40, 1 );
68
69
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_lesson_breadcrumb_filter' ), 40, 1 );
70
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_lesson_breadcrumb_filter' ), 40, 1 );
71
72
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_module_breadcrumb_filter' ), 40, 1 );
73
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_module_breadcrumb_filter' ), 40, 1 );
74
75
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_learner_breadcrumb_filter' ), 40, 1 );
76
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_learner_breadcrumb_filter' ), 40, 1 );
77
78
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_quiz_breadcrumb_filter' ), 40, 1 );
79
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_quiz_breadcrumb_filter' ), 40, 1 );
80
81
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_messages_breadcrumb_filter' ), 40, 1 );
82
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_messages_breadcrumb_filter' ), 40, 1 );
83
84
			add_filter( 'wpseo_breadcrumb_links', array( $this, 'lsx_sensei_results_breadcrumb_filter' ), 40, 1 );
85
			add_filter( 'woocommerce_get_breadcrumb', array( $this, 'lsx_sensei_results_breadcrumb_filter' ), 40, 1 );
86
87
			add_action( 'sensei_archive_before_message_loop', array( $this, 'lsx_sensei_back_message_button' ) );
88
			add_action( 'sensei_content_message_after', array( $this, 'lsx_sensei_view_message_button' ) );
89
90
		}
91
92
		/**
93
		 * Sensei enqueue styles.
94
		 *
95
		 * @package    lsx
96
		 * @subpackage sensei
97
		 */
98
		public function lsx_sensei_scripts_add_styles() {
99
			wp_enqueue_style( 'sensei-lsx', get_template_directory_uri() . '/assets/css/sensei/sensei.css', array( 'lsx_main' ), LSX_VERSION );
100
			wp_style_add_data( 'sensei-lsx', 'rtl', 'replace' );
101
		}
102
103
		/**
104
		 * Sensei wrapper start.
105
		 *
106
		 * @package    lsx
107
		 * @subpackage sensei
108
		 */
109 View Code Duplication
		public function lsx_sensei_theme_wrapper_start() {
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...
110
			lsx_content_wrap_before();
111
			echo '<div id="primary" class="content-area ' . esc_attr( lsx_main_class() ) . '">';
112
			lsx_content_before();
113
			echo '<main id="main" class="site-main" role="main">';
114
			lsx_content_top();
115
		}
116
117
		/**
118
		 * Sensei wrapper end.
119
		 *
120
		 * @package    lsx
121
		 * @subpackage sensei
122
		 */
123
		public function lsx_sensei_theme_wrapper_end() {
124
			lsx_content_bottom();
125
			echo '</main>';
126
			lsx_content_after();
127
			echo '</div>';
128
			lsx_content_wrap_after();
129
		}
130
131
		/**
132
		 * Remove "Archives:"  from the courses archive title.
133
		 *
134
		 * @param [type] $title
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
		 * @return void
136
		 */
137
		public function lsx_sensei_modify_archive_title( $title ) {
138
			if ( is_archive() && is_post_type_archive( 'course' ) ) {
139
				$title = __( 'Courses', 'lsx' );
140
			}
141
			if ( is_archive() && is_post_type_archive( 'sensei_message' ) ) {
142
				$title = __( 'Messages', 'lsx' );
143
			}
144
			if ( is_archive() && is_post_type_archive( 'lesson' ) ) {
145
				$title = __( 'Lessons', 'lsx' );
146
			}
147
			if ( is_archive() && is_tax() ) {
148
				$title = single_term_title( '', false );
149
			}
150
			return $title;
151
		}
152
153
		/**
154
		 * Disable LSX Banners in some Sensei pages.
155
		 *
156
		 * @package    lsx
157
		 * @subpackage sensei
158
		 */
159
		public function lsx_sensei_disable_lsx_banner( $disabled ) {
160
			if ( is_sensei() ) {
161
				$disabled = true;
162
			}
163
164
			return $disabled;
165
		}
166
167
		/**
168
		 * Filters the archive title.
169
		 *
170
		 * @package    lsx
171
		 * @subpackage sensei
172
		 */
173
		public function lsx_sensei_archive_title( $html ) {
174
			$html = preg_replace( '/<header class="archive-header"><h1>([^<]+)<\/h1><\/header>/i', '<h1>$1</h1>', $html );
175
			return $html;
176
		}
177
178
		/**
179
		 * Filters the archive title.
180
		 *
181
		 * @package    lsx
182
		 * @subpackage sensei
183
		 */
184
		public function lsx_sensei_category_title( $html ) {
185
			$html = str_replace( 'h2', 'h1', $html );
186
			return $html;
187
		}
188
189
		/**
190
		 * Add extra buttons to the single view on lists.
191
		 *
192
		 * @package    lsx
193
		 * @subpackage sensei
194
		 */
195
		public function lsx_sensei_add_buttons( $course_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $course_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
196
			global $post, $current_user;
197
			$is_user_taking_course = Sensei_Utils::user_started_course( $post->ID, $current_user->ID );
198
			$course_purchasable = Sensei_WC::is_course_purchasable( $post->ID );
199
200
			?>
201
				<section class="entry-actions">
202
					<a class="button" href="<?php echo esc_url( tribe_get_event_link() ); ?>"><?php esc_html_e( 'View course', 'lsx' ); ?></a>
203
204
					<?php
205
					if ( ( ! $is_user_taking_course ) && $course_purchasable ) {
206
						Sensei_WC::the_add_to_cart_button_html( $post->ID );
207
					}
208
					?>
209
				</section>
210
			<?php
211
		}
212
213
		/**
214
		 * Change add to cart button text.
215
		 *
216
		 * @package    lsx
217
		 * @subpackage sensei
218
		 */
219
		public function lsx_sensei_add_to_cart_text( $text ) {
0 ignored issues
show
Unused Code introduced by
The parameter $text is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
220
			$text = esc_html__( 'Add to cart', 'lsx' );
221
			return $text;
222
		}
223
224
		/**
225
		 * Displays the Results header.
226
		 *
227
		 * @package    lsx
228
		 * @subpackage layout
229
		 */
230
		public function lsx_sensei_results_header( $user ) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
231
232
			$default_size = 'sm';
233
			$size         = apply_filters( 'lsx_bootstrap_column_size', $default_size );
234
			global $wp_query;
235
			if ( isset( $wp_query->query_vars['course_results'] ) ) {
236
				$is_results = $wp_query->query_vars['course_results'];
237
			} else {
238
				$is_results = false;
239
			}
240 View Code Duplication
			if ( isset( $wp_query->query_vars['learner_profile'] ) ) {
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...
241
				$is_profile = $wp_query->query_vars['learner_profile'];
242
			} else {
243
				$is_profile = false;
244
			}
245
246
			if ( is_sticky() && $is_results ) :
247
				$course_for_results = get_page_by_path( $is_results, OBJECT, 'course' );
248
249
					$course_title = esc_html( $course_for_results->post_title );
250
				?>
251
				<div class="archive-header-wrapper banner-single col-<?php echo esc_attr( $size ); ?>-12">
252
					<?php lsx_global_header_inner_bottom(); ?>
253
					<header class="archive-header">
254
						<h1 class="archive-title"><?php echo wp_kses_post( $course_title ); ?></h1>
255
					</header>
256
257
				</div>
258
				<?php
259
			endif;
260
261
			if ( is_sticky() && $is_profile ) :
262
				$query_var    = $wp_query->query_vars['learner_profile'];
263
				$learner_user = Sensei_Learner::find_by_query_var( $query_var );
264
				$learner_name = $learner_user->display_name;
265
				?>
266
				<div class="archive-header-wrapper banner-single col-<?php echo esc_attr( $size ); ?>-12">
267
					<?php lsx_global_header_inner_bottom(); ?>
268
					<header class="archive-header">
269
						<h1 class="archive-title"><?php echo esc_html( $learner_name ); ?></h1>
270
					</header>
271
272
				</div>
273
				<?php
274
			endif;
275
		}
276
277
		/**
278
		 * Add the Parent Course link to the course breadcrumbs
279
		 * @param $crumbs
280
		 * @return array
281
		 */
282
		public function lsx_sensei_course_breadcrumb_filter( $crumbs, $id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
283
			if ( is_single() && ( is_singular( 'course' ) ) ) {
284
				global $course;
285
				$lesson          = get_the_title();
286
				$course_page_url = intval( Sensei()->settings->settings['course_page'] );
287
				$course_page_url = get_permalink( $course_page_url );
288
289
				if ( $lesson ) {
290
291
					$new_crumbs    = array();
292
					$new_crumbs[0] = $crumbs[0];
293
294 View Code Duplication
					if ( function_exists( 'woocommerce_breadcrumb' ) ) {
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...
295
						$new_crumbs[1] = array(
296
							0 => __( 'All Courses', 'lsx' ),
297
							1 => $course_page_url,
298
						);
299
						$new_crumbs[2] = array(
300
							0 => $lesson,
301
						);
302
					} else {
303
						$new_crumbs[1] = array(
304
							'text' => __( 'All Courses', 'lsx' ),
305
							'url'  => $course_page_url,
306
						);
307
						$new_crumbs[2] = array(
308
							'text' => $lesson,
309
						);
310
					}
311
					$crumbs = $new_crumbs;
312
				}
313
			}
314
			return $crumbs;
315
		}
316
317
		/**
318
		 * Add the Parent Course link to the lessons breadcrumbs
319
		 * @param $crumbs
320
		 * @return array
321
		 */
322 View Code Duplication
		public function lsx_sensei_lesson_breadcrumb_filter( $crumbs, $id = 0 ) {
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...
323
			if ( is_single() && ( is_singular( 'lesson' ) ) ) {
324
				global $course;
325
				$lesson          = get_the_title();
326
				$course_page_url = intval( Sensei()->settings->settings['course_page'] );
327
				$course_page_url = get_permalink( $course_page_url );
328
329
				if ( empty( $id ) ) {
330
					$id = get_the_ID();
331
				}
332
333
				if ( 0 < intval( $id ) ) {
334
					$course       = intval( get_post_meta( $id, '_lesson_course', true ) );
335
					$course_id    = esc_url( get_permalink( $course ) );
336
					$course_title = esc_html( get_the_title( $course ) );
337
					if ( ! $course ) {
338
						return;
339
					}
340
				}
341
342
				if ( $course_id ) {
343
344
					$new_crumbs    = array();
345
					$new_crumbs[0] = $crumbs[0];
346
347
					if ( function_exists( 'woocommerce_breadcrumb' ) ) {
348
						$new_crumbs[1] = array(
349
							0 => __( 'Courses', 'lsx' ),
350
							1 => $course_page_url,
351
						);
352
						$new_crumbs[2] = array(
353
							0 => $course_title,
0 ignored issues
show
Bug introduced by
The variable $course_title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
354
							1 => $course_id,
0 ignored issues
show
Bug introduced by
The variable $course_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
355
						);
356
						$new_crumbs[3] = array(
357
							0 => $lesson,
358
						);
359
					} else {
360
						$new_crumbs[1] = array(
361
							'text' => __( 'Courses', 'lsx' ),
362
							'url'  => $course_page_url,
363
						);
364
						$new_crumbs[2] = array(
365
							'text' => $course_title,
366
							'url'  => $course_id,
367
						);
368
						$new_crumbs[3] = array(
369
							'text' => $lesson,
370
						);
371
					}
372
					$crumbs = $new_crumbs;
373
				}
374
			}
375
			return $crumbs;
376
		}
377
378
		/**
379
		 * Add the Parent Course link to the module breadcrumbs
380
		 * @param $crumbs
381
		 * @return array
382
		 */
383
		public function lsx_sensei_module_breadcrumb_filter( $crumbs, $id = 0 ) {
384
			$title = apply_filters( 'sensei_module_archive_title', get_queried_object()->name );
385
386
			if ( is_tax() && is_archive() && ( ! empty( $title ) ) ) {
387
388
				$lesson          = get_the_archive_title();
389
				$course_page_url = intval( Sensei()->settings->settings['course_page'] );
390
				$course_page_url = get_permalink( $course_page_url );
391
392
				if ( empty( $id ) ) {
393
					$id = get_the_ID();
0 ignored issues
show
Unused Code introduced by
$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...
394
				}
395
396
				$new_crumbs    = array();
397
				$new_crumbs[0] = $crumbs[0];
398
399 View Code Duplication
				if ( function_exists( 'woocommerce_breadcrumb' ) ) {
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...
400
					$new_crumbs[1] = array(
401
						0 => __( 'Courses', 'lsx' ),
402
						1 => $course_page_url,
403
					);
404
					$new_crumbs[2] = array(
405
						0 => $lesson,
406
					);
407
				} else {
408
					$new_crumbs[1] = array(
409
						'text' => __( 'Courses', 'lsx' ),
410
						'url'  => $course_page_url,
411
					);
412
					$new_crumbs[2] = array(
413
						'text' => $lesson,
414
					);
415
				}
416
				$crumbs = $new_crumbs;
417
			}
418
			return $crumbs;
419
		}
420
421
		/**
422
		 * Add the Parent Course link to the Learner breadcrumbs
423
		 * @param $crumbs
424
		 * @return array
425
		 */
426
		public function lsx_sensei_learner_breadcrumb_filter( $crumbs, $id = 0 ) {
427
			global $wp_query;
428
429 View Code Duplication
			if ( isset( $wp_query->query_vars['learner_profile'] ) ) {
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...
430
				$is_profile = $wp_query->query_vars['learner_profile'];
431
			} else {
432
				$is_profile = false;
433
			}
434
435
			if ( is_sticky() && $is_profile ) {
436
437
				if ( empty( $id ) ) {
438
					$id = get_the_ID();
0 ignored issues
show
Unused Code introduced by
$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...
439
				}
440
441
				$query_var    = $wp_query->query_vars['learner_profile'];
442
				$learner_user = Sensei_Learner::find_by_query_var( $query_var );
443
				$learner_name = $learner_user->display_name;
444
445
				$new_crumbs    = array();
446
				$new_crumbs[0] = $crumbs[0];
447
448
				if ( function_exists( 'woocommerce_breadcrumb' ) ) {
449
					$new_crumbs[1] = array(
450
						0 => __( 'Learners', 'lsx' ),
451
					);
452
					$new_crumbs[2] = array(
453
						0 => $learner_name,
454
					);
455
				} else {
456
					$new_crumbs[1] = array(
457
						'text' => __( 'Learners', 'lsx' ),
458
					);
459
					$new_crumbs[2] = array(
460
						'text' => $learner_name,
461
					);
462
				}
463
				$crumbs = $new_crumbs;
464
			}
465
			return $crumbs;
466
		}
467
468
		/**
469
		 * Add the Parent Course link to the messages breadcrumbs
470
		 * @param $crumbs
471
		 * @return array
472
		 */
473
		public function lsx_sensei_messages_breadcrumb_filter( $crumbs, $id = 0 ) {
474
			if ( is_archive() && ( is_post_type_archive( 'sensei_message' ) ) ) {
475
476
				$course_page_url = intval( Sensei()->settings->settings['course_page'] );
477
				$course_page_url = get_permalink( $course_page_url );
478
479
				if ( empty( $id ) ) {
480
					$id = get_the_ID();
481
				}
482
483
				if ( $id ) {
484
485
					$new_crumbs    = array();
486
					$new_crumbs[0] = $crumbs[0];
487
488
					if ( function_exists( 'woocommerce_breadcrumb' ) ) {
489
						$new_crumbs[1] = array(
490
							0 => __( 'Courses', 'lsx' ),
491
							1 => $course_page_url,
492
						);
493
						$new_crumbs[2] = array(
494
							0 => __( 'Messages', 'lsx' ),
495
						);
496
					} else {
497
						$new_crumbs[1] = array(
498
							'text' => __( 'Courses', 'lsx' ),
499
							'url'  => $course_page_url,
500
						);
501
						$new_crumbs[2] = array(
502
							'text' => __( 'Messages', 'lsx' ),
503
						);
504
					}
505
					$crumbs = $new_crumbs;
506
				}
507
			}
508
			return $crumbs;
509
		}
510
511
		/**
512
		 * Add the Parent Course link to the quiz breadcrumbs
513
		 * @param $crumbs
514
		 * @return array
515
		 */
516 View Code Duplication
		public function lsx_sensei_quiz_breadcrumb_filter( $crumbs, $id = 0 ) {
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...
517
			if ( ( is_single() && ( is_singular( 'quiz' ) ) ) ) {
518
				global $course;
519
				$course_page_url = intval( Sensei()->settings->settings['course_page'] );
520
				$course_page_url = get_permalink( $course_page_url );
521
				$lesson          = get_the_title();
522
523
				if ( empty( $id ) ) {
524
					$id = get_the_ID();
525
				}
526
527
				if ( 0 < intval( $id ) ) {
528
529
					$course       = intval( get_post_meta( $id, '_quiz_lesson', true ) );
530
					$course_id    = esc_url( get_permalink( $course ) );
531
					$course_title = esc_html( get_the_title( $course ) );
532
					if ( ! $course ) {
533
						return;
534
					}
535
				}
536
537
				if ( $course_id ) {
538
539
					$new_crumbs    = array();
540
					$new_crumbs[0] = $crumbs[0];
541
542
					if ( function_exists( 'woocommerce_breadcrumb' ) ) {
543
						$new_crumbs[1] = array(
544
							0 => __( 'Courses', 'lsx' ),
545
							1 => $course_page_url,
546
						);
547
						$new_crumbs[2] = array(
548
							0 => $course_title,
0 ignored issues
show
Bug introduced by
The variable $course_title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
549
							1 => $course_id,
0 ignored issues
show
Bug introduced by
The variable $course_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
550
						);
551
						$new_crumbs[3] = array(
552
							0 => $lesson,
553
						);
554
					} else {
555
						$new_crumbs[1] = array(
556
							'text' => __( 'Courses', 'lsx' ),
557
							'url'  => $course_page_url,
558
						);
559
						$new_crumbs[2] = array(
560
							'text' => $course_title,
561
							'url'  => $course_id,
562
						);
563
						$new_crumbs[3] = array(
564
							'text' => $lesson,
565
						);
566
					}
567
568
					$crumbs = $new_crumbs;
569
				}
570
			}
571
			return $crumbs;
572
		}
573
574
		/**
575
		 * Add the Parent Course link to the results breadcrumbs
576
		 * @param $crumbs
577
		 * @return array
578
		 */
579
		public function lsx_sensei_results_breadcrumb_filter( $crumbs, $id = 0 ) {
580
			if ( is_sticky() ) {
581
				global $wp_query;
582
				$course_id = '';
583
				if ( isset( $wp_query->query_vars['course_results'] ) ) {
584
					$is_results = $wp_query->query_vars['course_results'];
585
				}
586
				$course_page_url = intval( Sensei()->settings->settings['course_page'] );
587
				$course_page_url = get_permalink( $course_page_url );
588
589
				if ( empty( $id ) ) {
590
					$id = get_the_ID();
0 ignored issues
show
Unused Code introduced by
$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...
591
				}
592
593
				if ( isset( $is_results ) ) {
594
					$course_for_results = get_page_by_path( $is_results, OBJECT, 'course' );
595
596
					$course_id    = esc_url( get_permalink( $course_for_results ) );
597
					$course_title = esc_html( $course_for_results->post_title );
598
599
				}
600
601
				if ( $course_id ) {
602
					$new_crumbs    = array();
603
					$new_crumbs[0] = $crumbs[0];
604
605
					if ( $is_results ) {
0 ignored issues
show
Bug introduced by
The variable $is_results does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
606
						if ( function_exists( 'woocommerce_breadcrumb' ) ) {
607
							$new_crumbs[1] = array(
608
								0 => __( 'Courses', 'lsx' ),
609
								1 => $course_page_url,
610
							);
611
							$new_crumbs[2] = array(
612
								0 => $course_title,
0 ignored issues
show
Bug introduced by
The variable $course_title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
613
								1 => $course_id,
614
							);
615
							$new_crumbs[3] = array(
616
								0 => __( 'Results', 'lsx' ),
617
							);
618
						} else {
619
							$new_crumbs[1] = array(
620
								'text' => __( 'Courses', 'lsx' ),
621
								'url'  => $course_page_url,
622
							);
623
							$new_crumbs[2] = array(
624
								'text' => __( 'Results', 'lsx' ),
625
							);
626
						}
627
					}
628
					$crumbs = $new_crumbs;
629
				}
630
			}
631
			return $crumbs;
632
		}
633
634
		/**
635
		 * Show the 'View Message' button on messages.
636
		 *
637
		 * @param [type] $message_post_id
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
638
		 * @return void
639
		 */
640
		public function lsx_sensei_view_message_button( $message_post_id ) {
641
			$message_link = get_the_permalink( $message_post_id );
642
			echo '<a href="' . esc_url_raw( $message_link ) . '" class="btn view-msg-btn">' . wp_kses_post( 'View Message', 'lsx' ) . '</a>';
643
		}
644
645
		/**
646
		 * Show the 'Back to My Courses' button on messages.
647
		 *
648
		 * @param [type] $message_post_id
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $message_post_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
649
		 * @return void
650
		 */
651
		public function lsx_sensei_back_message_button( $courses_link ) {
0 ignored issues
show
Unused Code introduced by
The parameter $courses_link is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
652
			$courses_link = '/my-courses/';
653
			echo '<a href="' . esc_url_raw( $courses_link ) . '" class="btn border-btn my-courses-btn">' . wp_kses_post( 'My Courses', 'lsx' ) . '</a>';
654
		}
655
	}
656
657
endif;
658
659
return new LSX_Sensei();
660