forked from gocodebox/lifterlms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.llms.post.handler.php
246 lines (188 loc) · 5.81 KB
/
class.llms.post.handler.php
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
<?php
/**
* Post Handler Class
*
* Main Handler for post management in LifterLMS
*
* @package LifterLMS/Classes
*
* @since 1.0.0
* @version 5.7.0
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_Post_Handler
*
* @since 1.0.0
* @since 4.4.3 Deprecated the `LLMS_Post_Handler::create_lesson()` method with no replacement.
* @since 5.7.0 Deprecated the `LLMS_Post_Handler::create_section()` method with no replacement.
*/
class LLMS_Post_Handler {
/**
* Create Post
*
* @param string $type [optional: a post type]
* @param string $title [optional: a title for the post]
* @return int [id of section]
*/
public static function create( $type = 'post', $title = '', $excerpt = '' ) {
if ( empty( $title ) ) {
$title = 'Section 1';
}
// Create section post.
$post_data = apply_filters(
'lifterlms_new_post',
array(
'post_type' => $type,
'post_title' => $title,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_excerpt' => $excerpt,
)
);
$post_id = wp_insert_post( $post_data, true );
// Check for error in update.
if ( is_wp_error( $post_id ) ) {
// For now just log the error and set $post_id to 0 (false).
llms_log( $post_id->get_error_message() );
$post_id = 0;
}
return $post_id;
}
public static function update_title( $post_id, $title ) {
$post_data = array(
'ID' => $post_id,
'post_title' => $title,
);
// Update the post into the database.
$updated_post_id = wp_update_post( $post_data );
if ( $updated_post_id ) {
return array(
'id' => $updated_post_id,
'title' => $title,
);
}
}
public static function update_excerpt( $post_id, $excerpt ) {
$post_data = array(
'ID' => $post_id,
'post_excerpt' => $excerpt,
);
// Update the post into the database.
$updated_post_id = wp_update_post( $post_data );
if ( $updated_post_id ) {
return array(
'id' => $updated_post_id,
'post_excerpt' => $excerpt,
);
}
}
/**
* Creates a new Section
*
* @since Unknown Introduced.
* @deprecated 5.7.0 There is not a replacement.
*
* @param int $course_id The parent course id.
* @param string $title An optional title for the section.
* @return int Post id of the section.
*/
public static function create_section( $course_id, $title = '' ) {
llms_deprecated_function( __METHOD__, '5.7.0' );
// No course id? no new section!.
if ( ! isset( $course_id ) ) {
return;
}
// Set the section_order variable.
// Get the count of sections in the course and add 1.
$course = new LLMS_Course( $course_id );
$sections = $course->get_sections( 'posts' );
$section_order = count( $sections ) + 1;
$title = isset( $title ) ? $title : 'New Section';
$post_id = self::create( 'section', $title );
// If post created set parent course and order to order determined above.
if ( $post_id ) {
update_post_meta( $post_id, '_llms_order', $section_order );
$section = new LLMS_Section( $post_id );
$updated_parent_course = $section->set_parent_course( $course_id );
}
return $post_id;
}
/**
* Create lesson
*
* @since Unknown
* @deprecated 4.4.3
*
* @param int $course_id WP_Post ID of the course.
* @param int $section_id WP_Post ID of the lesson's parent section.
* @param string $title Optional lesson title.
* @param string $excerpt Option excerpt.
* @return int WP_Post ID of the created lesson.
*/
public static function create_lesson( $course_id, $section_id, $title = '', $excerpt = '' ) {
// No course id or section id? no new lesson!.
if ( ! isset( $course_id ) || ! isset( $course_id ) ) {
return;
}
// Set the lesson_order variable.
// Get the count of lessons in the section.
$section = new LLMS_Section( $section_id );
$lesson_order = $section->get_next_available_lesson_order();
$title = isset( $title ) ? $title : 'New Lesson';
$post_id = self::create( 'lesson', $title, $excerpt );
// If post created set parent section, parent course and order determined above.
if ( $post_id ) {
update_post_meta( $post_id, '_llms_order', $lesson_order );
$lesson = new LLMS_Lesson( $post_id );
$updated_parent_section = $lesson->set_parent_section( $section_id );
$updated_parent_course = $lesson->set_parent_course( $course_id );
}
return $post_id;
}
public static function get_posts( $type = 'post' ) {
$args = array(
'posts_per_page' => 1000,
'post_type' => $type,
'nopaging' => true,
'post_status' => 'publish',
'orderby' => 'post_title',
'order' => 'ASC',
'suppress_filters' => true,
);
$postslist = get_posts( $args );
if ( ! empty( $postslist ) ) {
foreach ( $postslist as $key => $value ) {
$value->edit_url = get_edit_post_link( $value->ID, false );
}
}
return $postslist;
}
public static function get_lesson_options_for_select_list() {
$lessons = self::get_posts( 'lesson' );
$options = array();
if ( ! empty( $lessons ) ) {
foreach ( $lessons as $key => $value ) {
// Get parent course if assigned.
$parent_course = get_post_meta( $value->ID, '_llms_parent_course', true );
if ( $parent_course ) {
$title = $value->post_title . ' ( ' . get_the_title( $parent_course ) . ' )';
} else {
$title = $value->post_title . ' ( ' . __( 'unassigned', 'lifterlms' ) . ' )';
}
$options[ $value->ID ] = $title;
}
}
return $options;
}
public static function get_prerequisite( $post_id ) {
$post_type = get_post_type( $post_id );
if ( 'course' === $post_type ) {
$course = new LLMS_Course( $post_id );
return $course->get_prerequisite();
} elseif ( 'lesson' === $post_type ) {
$lesson = new LLMS_Lesson( $post_id );
return $lesson->get_prerequisite();
}
}
}