Completed
Push — master ( 2e6b5e...0255b8 )
by Warwick
07:43
created

LSX_Sensei_Course::display_course_amount()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 0
dl 0
loc 15
rs 8.8333
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit; // Exit if accessed directly
4
}
5
6
/**
7
 * LSX Sensei Course Class
8
 */
9
class LSX_Sensei_Course {
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
	} // End __construct()
24
25
	/**
26
	 * Fetches an instance of the class.
27
	 *
28
	 * @return self
29
	 */
30
	public static function instance() {
31
		if ( ! self::$instance ) {
32
			self::$instance = new self();
33
		}
34
		return self::$instance;
35
	}
36
37
	/**
38
	 * Run our changes.
39
	 */
40
	public function init() {
41
42
		global $woothemes_sensei;
43
44
		//Switching the course filters and the headers around
45
		remove_action( 'sensei_archive_before_course_loop', array( 'Sensei_Course', 'archive_header' ), 10, 0 );
46
		remove_action( 'sensei_archive_before_course_loop', array( 'Sensei_Course', 'course_archive_sorting' ) );
47
		remove_action( 'sensei_archive_before_course_loop', array( 'Sensei_Course', 'course_archive_filters' ) );
48
		add_action( 'sensei_archive_before_course_loop', array( 'Sensei_Course', 'archive_header' ), 11, 0 );
49
		add_action( 'sensei_archive_before_course_loop', array( 'Sensei_Course', 'course_archive_sorting' ), 12 );
50
		add_action( 'sensei_archive_before_course_loop', array( 'Sensei_Course', 'course_archive_filters' ), 12 );
51
52
		// First add the thumbnail.
53
		add_action( 'sensei_course_content_inside_before', array( $this, 'get_course_thumbnail' ), 1 );
54
55
		// This is for our wrapper, we run it on 2, after the thumbnail we added.
56
		add_action( 'sensei_course_content_inside_before', array( $this, 'course_body_div_open' ), 1 );
57
		add_action( 'sensei_course_content_inside_after', array( $this, 'course_body_div_close' ), 50 );
58
59
		// This is for our wrapper, we run it on 2, after the thumbnail we added.
60
		add_action( 'sensei_course_content_inside_before', array( $this, 'course_body_div_results_open' ), 20 );
61
		add_action( 'sensei_course_content_inside_after', array( $this, 'course_body_div_results_close' ), 49 );
62
63
		add_action( 'sensei_single_course_content_inside_before', array( $this, 'display_course_amount' ), 20 );
64
65
		// removes the course image above the content
66
		remove_action( 'sensei_course_content_inside_before', array( $woothemes_sensei->course, 'course_image' ), 30, 1 );
67
		// add the course image to the left of the content
68
		add_action( 'lsx_sensei_course_content_inside_before', array( $woothemes_sensei->course, 'course_image' ), 30, 1 );
69
70
		add_filter( 'attach_shortcode_hooks', 'lsx_attach_shortcode_hooks', 10, 1 );
71
72
	}
73
74
	/**
75
	 * Gets the current courses thumbnail for content-course.php
76
	 *
77
	 * @return void
78
	 */
79
	public function get_course_thumbnail() {
80
		?>
81
		<div class="course-thumbnail">
82
			<?php do_action( 'lsx_sensei_course_content_inside_before', get_the_ID() ); ?>
83
		</div>
84
		<?php
85
	}
86
87
	/**
88
	 * <div class="course-body"> for content-course.php
89
	 *
90
	 * @return void
91
	 */
92
	public function course_body_div_open() {
93
		global $post, $current_user;
94
		$is_user_taking_course = Sensei_Utils::user_started_course( $post->ID, $current_user->ID );
95
		$user_taking_course_class = '';
96
		if ( ! empty( $is_user_taking_course ) ) {
97
			$user_taking_course_class = 'currently-in-course';
98
		}
99
		?>
100
		<div class="course-body <?php echo esc_html( $user_taking_course_class ); ?>">
101
		<?php
102
	}
103
104
	/**
105
	 * The closing </div> for <div class="course-body"> content-course.php
106
	 *
107
	 * @return void
108
	 */
109
	public function course_body_div_close() {
110
		?>
111
		</div>
112
		<?php
113
	}
114
115
	/**
116
	 * <div class="course-details-info"> for content-course.php, just for the info after the meta
117
	 *
118
	 * @return void
119
	 */
120
	public function course_body_div_results_open() {
121
		?>
122
		<div class="course-details-info">
123
		<?php
124
	}
125
126
	/**
127
	 * The closing </div> for <div class="course-details-info"> content-course.php, just for the info after the meta
128
	 *
129
	 * @return void
130
	 */
131
	public function course_body_div_results_close() {
132
		?>
133
		</div>
134
		<?php
135
	}
136
137
	/**
138
	 * Display the course price on a single course.
139
	 *
140
	 * @return void
141
	 */
142
	public function display_course_amount() {
143
		global $post, $current_user;
144
		$is_user_taking_course = Sensei_Utils::user_started_course( $post->ID, $current_user->ID );
145
		$wc_post_id            = absint( get_post_meta( $post->ID, '_course_woocommerce_product', true ) );
146
		if ( class_exists( 'Sensei_WC' ) ) {
147
			$course_purchasable    = Sensei_WC::is_course_purchasable( $post->ID );
148
			$currency              = get_woocommerce_currency_symbol();
149
			$product               = new WC_Product( $wc_post_id );
150
			if ( ( ! empty( $product->price ) ) && ( ! $is_user_taking_course ) ) {
151
				echo '<span class="course-product-price price"><span>' . esc_html( $currency ) . ' </span>' . sprintf( '%0.2f', esc_html( $product->price ) ) . '</span>';
152
			} elseif ( ( '' === $product->get_price() || 0 == $product->get_price() ) && $course_purchasable ) {
153
				echo '<span class="course-product-price price">' . wp_kses_post( 'Free!', 'lsx' ) . '</span>';
154
			}
155
		}
156
	}
157
158
} // End Class
159
new LSX_Sensei_Course();
160