Passed
Push — master ( 1cfb85...cddbb8 )
by Warwick
46s queued 11s
created

LSX_Sensei_Lesson::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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