class-ldlms-factory-post.php
6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
* LearnDash Factory Post Class.
* This is a factory class used to instantiate LD custom post type related data.
*
* @since 2.5.0
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LDLMS_Factory_Post' ) ) && ( class_exists( 'LDLMS_Factory' ) ) ) {
/**
* Class for LearnDash Factory Post.
*
* @since 2.5.0
* @uses LDLMS_Factory
*/
class LDLMS_Factory_Post extends LDLMS_Factory {
/**
* Get a Course.
*
* @param int|object $course Either course_id integer or WP_Post instance.
* @param bool $reload To force reload of instance.
*
* @return object|null Instance of `LDLMS_Model_Course` or null
*/
public static function course( $course = null, $reload = false ) {
if ( ! empty( $course ) ) {
$model = 'LDLMS_Model_Course';
$course_id = 0;
if ( ( is_a( $course, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'course' ) === $course->post_type ) ) {
$course_id = absint( $course->ID );
} else {
$course_id = absint( $course );
}
if ( ! empty( $course_id ) ) {
if ( true === $reload ) {
self::remove_instance( $model, $course_id );
}
return self::add_instance( $model, $course_id, $course_id );
}
}
return null;
}
/**
* Get a Lesson.
*
* @param int|object $lesson Either lesson_id integer or WP_Post instance.
* @param bool $reload To force reload of instance.
*
* @return object|null Instance of `LDLMS_Model_Lesson` or null
*/
public static function lesson( $lesson = null, $reload = false ) {
if ( ! empty( $lesson ) ) {
$model = 'LDLMS_Model_Lesson';
$lesson_id = 0;
if ( ( is_a( $lesson, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'lesson' ) === $lesson->post_type ) ) {
$lesson_id = absint( $lesson->ID );
} else {
$lesson_id = absint( $lesson );
}
if ( ! empty( $lesson_id ) ) {
if ( true === $reload ) {
self::remove_instance( $model, $lesson_id );
}
return self::add_instance( $model, $lesson_id, $lesson_id );
}
}
return null;
}
/**
* Get Course.
*
* @param int|object $course Either course_id integer or WP_Post instance.
*/
public static function get_course( $course ) {}
/**
* Get Course Lessons.
*
* @param int|object $course Either course_id integer or WP_Post instance.
* @param int|object $lesson Either lesson_id integer or WP_Post instance.
*
* @return array An array of course lessons
*/
public static function get_course_lessons( $course = null, $lesson = null ) {
if ( ! empty( $course ) ) {
$course = self::get_course( $course );
if ( $course ) {
$lesson_id = 0;
if ( ( is_a( $lesson, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'lesson' ) === $lesson->post_type ) ) {
$lesson_id = absint( $lesson->ID );
} else {
$lesson_id = absint( $lesson );
}
$course_lesson = $course->get_lesson( $lesson_id );
return $course_lesson;
}
}
return array();
}
/**
* Get a Quiz Questions.
*
* @param int|object $quiz Either quiz_id integer or WP_Post instance.
* @param bool $reload To force reload of instance.
*
* @return object|null Instance of `LDLMS_Quiz_Questions` or null
*/
public static function quiz_questions( $quiz = null, $reload = false ) {
if ( ! empty( $quiz ) ) {
$model = 'LDLMS_Quiz_Questions';
$quiz_id = 0;
if ( ( is_a( $quiz, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'quiz' ) === $quiz->post_type ) ) {
$quiz_id = absint( $quiz->ID );
} else {
$quiz_id = absint( $quiz );
}
if ( ! empty( $quiz_id ) ) {
if ( true === $reload ) {
self::remove_instance( $model, $quiz_id );
}
return self::add_instance( $model, $quiz_id, $quiz_id );
}
}
return null;
}
/**
* Get a Course Steps.
*
* @param int|object $course Either course_id integer or WP_Post instance.
* @param bool $reload To force reload of instance.
*
* @return object|null Instance of `LDLMS_Course_Steps` or null
*/
public static function course_steps( $course = null, $reload = false ) {
if ( ! empty( $course ) ) {
$model = 'LDLMS_Course_Steps';
$course_id = 0;
if ( ( is_a( $course, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'course' ) === $course->post_type ) ) {
$course_id = absint( $course->ID );
} else {
$course_id = absint( $course );
}
if ( ! empty( $course_id ) ) {
if ( true === $reload ) {
self::remove_instance( $model, $course_id );
}
return self::add_instance( $model, $course_id, $course_id );
}
}
return null;
}
/**
* Get a Exam.
*
* @since 4.0.0
*
* @param int|object $exam Either exam_id integer or WP_Post instance.
* @param array $atts Array of attributes (course_id, user_id).
* @param bool $reload To force reload of instance.
*
* @return object|null Instance of `LDLMS_Model_Exam` or null
*/
public static function exam( $exam = null, $atts = array(), $reload = false ) {
if ( ! empty( $exam ) ) {
$model = 'LDLMS_Model_Exam';
$exam_id = 0;
if ( ( is_a( $exam, 'WP_Post' ) ) && ( learndash_get_post_type_slug( 'exam' ) === $exam->post_type ) ) {
$exam_id = absint( $exam->ID );
} else {
$exam_id = absint( $exam );
}
if ( ! empty( $exam_id ) ) {
$model_key = $exam_id;
if ( ( is_array( $atts ) ) && ( ! empty( $atts ) ) ) {
ksort( $atts );
$model_key = $exam_id . '-' . md5( serialize( $atts ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
}
if ( true === $reload ) {
self::remove_instance( $model, $model_key );
}
return self::add_instance( $model, $model_key, $exam_id, $atts );
}
}
return null;
}
/**
* Get a Exam Question.
*
* @since 4.0.0
*
* @param array $exam_question_block Array of question attributes.
* @param boolean $reload To force reload of instance.
*
* @return object|null Instance of `LDLMS_Model_Exam_Question` or null
*/
public static function exam_question( $exam_question_block = array(), $reload = false ) {
if ( ! empty( $exam_question_block ) ) {
if ( isset( $exam_question_block['attrs']['question_type'] ) ) {
$exam_question_key = $exam_question_block['attrs']['question_type'] . '-' . ( isset( $exam_question_block['attrs']['exam_id'] ) ? $exam_question_block['attrs']['exam_id'] : '0' ) . '-' . ( isset( $exam_question_block['attrs']['question_idx'] ) ? $exam_question_block['attrs']['question_idx'] : '0' );
$model = LDLMS_Model_Exam_Question::get_model_by_type( $exam_question_block['attrs']['question_type'] );
if ( $model ) {
if ( true === $reload ) {
self::remove_instance( $model, $exam_question_key );
}
return self::add_instance( $model, $exam_question_key, $exam_question_block );
}
}
}
return null;
}
// End of functions.
}
}