1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; // Exit if accessed directly |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* LSX Sensei Lesson Class |
8
|
|
|
*/ |
9
|
|
|
class LSX_Sensei_Lesson { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Instance of class. |
13
|
|
|
* |
14
|
|
|
* @var self |
15
|
|
|
*/ |
16
|
|
|
private static $instance; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor. |
20
|
|
|
*/ |
21
|
|
|
public function __construct() { |
22
|
|
|
add_action( 'init', array( $this, 'init' ) ); |
23
|
|
|
add_action( 'widgets_init', array( $this, 'lsx_widget_area_sensei_init' ), 100 ); |
24
|
|
|
add_filter( 'body_class', array( $this, 'lsx_widget_area_sensei_is_active' ) ); |
25
|
|
|
} // End __construct() |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Fetches an instance of the class. |
29
|
|
|
* |
30
|
|
|
* @return self |
31
|
|
|
*/ |
32
|
|
|
public static function instance() { |
33
|
|
|
if ( ! self::$instance ) { |
34
|
|
|
self::$instance = new self(); |
35
|
|
|
} |
36
|
|
|
return self::$instance; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Run our changes. |
41
|
|
|
*/ |
42
|
|
|
public function init() { |
43
|
|
|
add_action( 'lsx_content_top', array( $this, 'lsx_sensei_lesson_sidebar' ) ); |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Register a sidebar when Sensei Participants or Sensei Progress plugins are active. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function lsx_widget_area_sensei_init() { |
53
|
|
View Code Duplication |
if ( class_exists( 'Sensei_Course_Participants' ) || class_exists( 'Sensei_Course_Progress' ) ) { |
|
|
|
|
54
|
|
|
register_sidebar( array( |
55
|
|
|
'name' => esc_html__( 'LSX Sensei Sidebar', 'lsx' ), |
56
|
|
|
'id' => 'lsx-sensei-sidebar', |
57
|
|
|
'before_widget' => '<aside id="%1$s" class="widget %2$s">', |
58
|
|
|
'after_widget' => '</aside>', |
59
|
|
|
'before_title' => '<h3 class="widget-title">', |
60
|
|
|
'after_title' => '</h3>', |
61
|
|
|
) ); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function lsx_widget_area_sensei_is_active( $classes ) { |
66
|
|
|
|
67
|
|
|
if ( class_exists( 'Sensei_Lesson' ) && is_active_sidebar( 'lsx-sensei-sidebar' ) ) { |
68
|
|
|
$classes[] = 'lsx-sensei-sidebar-active'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $classes; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Adds the widget content to the lesson template if the lsx-sensei-sidebar is active. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function lsx_sensei_lesson_sidebar() { |
80
|
|
|
if ( class_exists( 'Sensei_Lesson' ) && ( class_exists( 'Sensei_Course_Participants' ) || class_exists( 'Sensei_Course_Progress' ) ) ) { |
81
|
|
|
if ( ( is_single() && ( is_singular( 'lesson' ) ) ) || ( is_single() && ( is_singular( 'quiz' ) ) ) ) { |
82
|
|
|
if ( is_active_sidebar( 'lsx-sensei-sidebar' ) ) { |
83
|
|
|
echo '<div id="secondary" class="widget-area lsx-sensei-sidebar">'; |
84
|
|
|
dynamic_sidebar( 'lsx-sensei-sidebar' ); |
85
|
|
|
echo '</div>'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} // End Class |
92
|
|
|
new LSX_Sensei_Lesson(); |
93
|
|
|
|
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.