PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.8
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / course / class-lp-course-section.php

class-lp-course-section.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/course/class-lp-course-section.php

327 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Course_Section
5 *
6 * @since 3.0.0
7 */
8 class LP_Course_Section extends LP_Abstract_Object_Data {
9 /**
10 * Store section data
11 *
12 * @var null
13 */
14 protected $data = null;
15 /**
16 * @var int
17 */
18 protected $course_id = 0;
19 /**
20 * @var int
21 */
22 protected $course = 0;
23
24 /**
25 * @var LP_Section_CURD
26 */
27 protected $_curd = null;
28
29 /**
30 * @var array
31 */
32 protected $items = array();
33
34 /**
35 * LP_Course_Section constructor.
36 *
37 * @param $data
38 */
39 public function __construct( $data ) {
40 $data = wp_parse_args(
41 $data,
42 array(
43 'section_id' => 0,
44 'section_name' => '0',
45 'section_course_id' => 0,
46 'section_order' => 1,
47 'section_description' => '',
48 'position' => 0,
49 'items' => array(),
50 )
51 );
52
53 // Set data
54 foreach ( $data as $k => $v ) {
55 $k = str_replace( 'section_', '', $k );
56
57 if ( $k === 'course_id' ) {
58 $this->course_id = absint( $v );
59 continue;
60 }
61 $this->_data[ $k ] = $v;
62 }
63
64 $this->course = learn_press_get_course( $this->course_id );
65
66 if ( ! $this->course ) {
67 return;
68 }
69
70 $this->_curd = new LP_Section_CURD( 0 );
71 $this->set_id( $this->_data['id'] );
72 // Load section items
73 $this->load_items();
74 }
75
76 /**
77 * Load items from course curriculum to it section
78 */
79 protected function load_items() {
80 if ( ! $this->get_id() ) {
81 return false;
82 }
83
84 $course = $this->course;
85 $section_id = $this->get_id();
86
87 // All items
88 $items = $course->get_item_ids( $section_id );
89
90 foreach ( $items as $item_id ) {
91 $item_class = $course->get_item( $item_id );
92
93 if ( $item_class instanceof LP_Course_Item ) {
94 $item_class->set_section( $this );
95 $this->items[ $item_id ] = $item_class;
96 }
97 }
98 }
99
100 /**
101 * Get item class from item data
102 *
103 * @param array $item
104 *
105 * @return bool|LP_Course_Item
106 * @deprecated 4.1.6.9
107 */
108 /*protected function get_item( $item ) {
109 if ( ! is_numeric( $item ) ) {
110 $item_id = $item->item_id;
111 } else {
112 $item_id = absint( $item );
113 }
114
115 return LP_Course_Item::get_item( $item_id );
116 }*/
117
118 /**
119 * Get data to array.
120 *
121 * @return array
122 * @since 3.0.0
123 */
124 public function to_array() {
125 $data = array(
126 'id' => $this->get_id(),
127 'title' => $this->get_title(),
128 'course_id' => $this->get_course_id(),
129 'description' => $this->get_description(),
130 'items' => $this->get_items_array(),
131 'order' => $this->get_order(),
132 );
133
134 return $data;
135 }
136
137 /**
138 * Return section id
139 *
140 * @return int
141 */
142 public function get_id() {
143 return (int) $this->_data['id'];
144 }
145
146 /**
147 * Return section title
148 *
149 * @return mixed
150 */
151 public function get_title() {
152 return apply_filters( 'learn-press/section-title', $this->_data['name'], $this );
153 }
154
155 /**
156 * Return section course id
157 *
158 * @return mixed
159 */
160 public function get_course_id() {
161 return $this->course_id;
162 }
163
164 /**
165 * Return section order
166 *
167 * @return mixed
168 */
169 public function get_order() {
170 return $this->_data['order'];
171 }
172
173 /**
174 * Return section description
175 *
176 * @return mixed
177 */
178 public function get_description() {
179 return apply_filters( 'learn-press/section-description', $this->_data['description'], $this );
180 }
181
182 /**
183 * Get items in this section.
184 *
185 * @param string|array $type
186 * @param bool $preview
187 *
188 * @return array
189 */
190 public function get_items( $type = '', $preview = true ) {
191 /**
192 * @var LP_Course_Item[] $items
193 */
194 $items = apply_filters( 'learn-press/section-items', $this->items, $this );
195
196 if ( ! $items ) {
197 return $items;
198 }
199
200 if ( $type || ! $preview ) {
201 $filtered_items = array();
202
203 if ( $type ) {
204 settype( $type, 'array' );
205 }
206
207 foreach ( $items as $item ) {
208 if ( ! $preview ) {
209 if ( $item->is_preview() ) {
210
211 continue;
212 }
213 }
214 if ( ! $type || $type && in_array( learn_press_get_post_type( $item->get_id() ), $type ) ) {
215 $filtered_items[] = $item;
216 }
217 }
218
219 $items = $filtered_items;
220 }
221
222 return $items;
223 }
224
225 /**
226 * Get items in this section to array.
227 *
228 * @return array
229 * @since 3.0.0
230 */
231 public function get_items_array() {
232 $items = $this->get_items();
233
234 $data = array();
235 foreach ( $items as $item ) {
236 $data[] = $item->to_array();
237 }
238
239 return $data;
240 }
241
242 /**
243 * Get completed items of a specific user.
244 * If user does not pass into then get from current user.
245 *
246 * @param int $user_id
247 *
248 * @return mixed
249 * @deprecated 4.2.7.1
250 */
251 /*public function get_completed_items( $user_id = 0 ) {
252 $items = array();
253
254 return $items;
255 }*/
256
257 /**
258 * Count number of items in section.
259 *
260 * @param string $type
261 * @param bool $preview
262 *
263 * @return int
264 */
265 public function count_items( $type = '', $preview = true ) {
266 $items = $this->get_items( $type, $preview );
267
268 return is_array( $items ) ? sizeof( $items ) : 0;
269 }
270
271 /**
272 * Check if a section contains an item.
273 *
274 * @param $item_id
275 *
276 * @return bool
277 */
278 public function has_item( $item_id ) {
279 $found = false;
280 $items = $this->get_items();
281
282 if ( $items ) {
283 $found = ! empty( $items[ $item_id ] );
284 }
285
286 return apply_filters(
287 'learn-press/section-has-item',
288 $found,
289 $item_id,
290 $this->get_id(),
291 $this->get_course_id()
292 );
293 }
294
295 public function get_slug() {
296 return $this->get_title() ? sanitize_title( $this->get_title() ) . '-' . $this->get_id() : $this->get_id();
297 }
298
299 /**
300 * Set class section
301 */
302 public function main_class() {
303 $class = array( 'section' );
304
305 if ( ! $this->count_items() ) {
306 $class[] = 'section-empty';
307 }
308
309 $closed = learn_press_cookie_get( 'closed-section-' . $this->get_course_id() );
310
311 if ( $closed && in_array( $this->get_id(), $closed ) ) {
312 $class[] = 'closed';
313 }
314
315 $output = 'class="' . esc_attr( implode( ' ', $class ) ) . '"';
316 echo ' ' . $output;
317 }
318
319 public function set_position( $position ) {
320 $this->_data['position'] = $position;
321 }
322
323 public function get_position() {
324 return ! empty( $this->_data['position'] ) ? absint( $this->_data['position'] ) : 0;
325 }
326 }
327