PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9.1
4.4.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 All 139 releases
learnpress / inc / MCP / Concerns / AbilityHelpers.php

AbilityHelpers.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9.1, at inc/MCP/Concerns/AbilityHelpers.php

261 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\MCP\Concerns;
4
5 use LearnPress\Models\CourseModel;
6 use LearnPress\Models\LessonPostModel;
7 use LearnPress\Models\QuizPostModel;
8 use LearnPress\Models\UserModel;
9 use LP_Helper;
10 use WP_Error;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Shared helper methods used across ability executors.
16 */
17 trait AbilityHelpers {
18 /**
19 * Normalize ability input to array.
20 *
21 * @param mixed $input Raw ability input.
22 * @param string $ability Ability name for error context.
23 *
24 * @return array|WP_Error
25 */
26 protected static function input_arr( $input, string $ability ) {
27 if ( null === $input ) {
28 return array();
29 }
30 if ( is_array( $input ) ) {
31 return $input;
32 }
33
34 return self::invalid(
35 sprintf(
36 __( 'Invalid input for ability "%s". Input must be an object.', 'learnpress' ),
37 $ability
38 )
39 );
40 }
41
42 /**
43 * Sanitize page number.
44 *
45 * @param mixed $value Raw page value.
46 *
47 * @return int
48 */
49 protected static function page( $value ): int {
50 $page = absint( $value );
51 return $page > 0 ? $page : 1;
52 }
53
54 /**
55 * Sanitize per-page number and clamp to safe range.
56 *
57 * @param mixed $value Raw per-page value.
58 *
59 * @return int
60 */
61 protected static function per_page( $value ): int {
62 $per_page = absint( $value );
63 if ( $per_page < 1 ) {
64 $per_page = 10;
65 }
66 if ( $per_page > 100 ) {
67 $per_page = 100;
68 }
69
70 return $per_page;
71 }
72
73 /**
74 * Normalize status input into unique sanitized status list.
75 *
76 * @param mixed $input String, CSV string, array, or null.
77 *
78 * @return array
79 */
80 protected static function status_list( $input ): array {
81 if ( null === $input || '' === $input ) {
82 return array();
83 }
84
85 $values = is_array( $input )
86 ? $input
87 : ( false !== strpos( (string) $input, ',' ) ? explode( ',', (string) $input ) : array( (string) $input ) );
88
89 $out = array();
90 foreach ( $values as $value ) {
91 $status = LP_Helper::sanitize_params_submitted( (string) $value, 'key' );
92 if ( '' !== $status ) {
93 $out[] = $status;
94 }
95 }
96
97 return array_values( array_unique( $out ) );
98 }
99
100 /**
101 * Calculate total pages from total item count.
102 *
103 * @param int $total_items Total items.
104 * @param int $per_page Items per page.
105 *
106 * @return int
107 */
108 protected static function total_pages( int $total_items, int $per_page ): int {
109 return $per_page > 0 ? (int) ceil( $total_items / $per_page ) : 0;
110 }
111
112 /**
113 * Build invalid input error.
114 *
115 * @param string $message Error message.
116 *
117 * @return WP_Error
118 */
119 protected static function invalid( string $message ): WP_Error {
120 return new WP_Error( 'lp_mcp_invalid_input', $message, array( 'status' => 400 ) );
121 }
122
123 /**
124 * Build not-found error.
125 *
126 * @param string $message Error message.
127 *
128 * @return WP_Error
129 */
130 protected static function not_found( string $message ): WP_Error {
131 return new WP_Error( 'lp_mcp_not_found', $message, array( 'status' => 404 ) );
132 }
133
134 /**
135 * Build generic internal error.
136 *
137 * @return WP_Error
138 */
139 protected static function internal(): WP_Error {
140 return new WP_Error(
141 'lp_mcp_internal_error',
142 __( 'An internal LearnPress MCP error occurred.', 'learnpress' ),
143 array( 'status' => 500 )
144 );
145 }
146
147 /**
148 * Map course model into ability response summary.
149 *
150 * @param CourseModel $course Course model.
151 *
152 * @return array
153 */
154 protected static function course_summary( CourseModel $course ): array {
155 $categories = array();
156 foreach ( $course->get_categories() as $term ) {
157 $categories[] = array(
158 'term_id' => (int) $term->term_id,
159 'name' => (string) $term->name,
160 'slug' => (string) $term->slug,
161 );
162 }
163 $author = $course->get_author_model();
164
165 return array(
166 'course_id' => $course->get_id(),
167 'title' => (string) $course->get_title(),
168 'status' => (string) $course->get_status(),
169 'price' => (float) $course->get_price(),
170 'duration' => (string) $course->get_duration(),
171 'permalink' => (string) $course->get_permalink(),
172 'instructor' => array(
173 'user_id' => $author instanceof UserModel ? $author->get_id() : 0,
174 'display_name' => $author instanceof UserModel ? $author->get_display_name() : '',
175 ),
176 'categories' => $categories,
177 );
178 }
179
180 /**
181 * Map lesson model + curriculum ref into summary response.
182 *
183 * @param LessonPostModel $lesson Lesson model.
184 * @param array $ref Curriculum reference.
185 *
186 * @return array
187 */
188 protected static function lesson_summary( LessonPostModel $lesson, array $ref ): array {
189 return array(
190 'lesson_id' => $lesson->get_id(),
191 'course_id' => (int) $ref['course_id'],
192 'section_id' => (int) $ref['section_id'],
193 'section_name' => (string) $ref['section_name'],
194 'title' => (string) $lesson->get_the_title(),
195 'excerpt' => (string) $lesson->get_the_excerpt(),
196 'duration' => (string) $lesson->get_duration(),
197 'preview' => (bool) $ref['preview'],
198 'status' => (string) $lesson->post_status,
199 'permalink' => (string) $lesson->get_permalink(),
200 );
201 }
202
203 /**
204 * Map quiz model + curriculum ref into summary response.
205 *
206 * @param QuizPostModel $quiz Quiz model.
207 * @param array $ref Curriculum reference.
208 *
209 * @return array
210 */
211 protected static function quiz_summary( QuizPostModel $quiz, array $ref ): array {
212 return array(
213 'quiz_id' => $quiz->get_id(),
214 'course_id' => (int) $ref['course_id'],
215 'section_id' => (int) $ref['section_id'],
216 'section_name' => (string) $ref['section_name'],
217 'title' => (string) $quiz->get_the_title(),
218 'duration' => (string) $quiz->get_duration(),
219 'passing_grade' => (float) $quiz->get_passing_grade(),
220 'questions_count' => (int) $quiz->count_questions(),
221 'status' => (string) $quiz->post_status,
222 'permalink' => (string) $quiz->get_permalink(),
223 );
224 }
225
226 /**
227 * Collect course items by type, optionally filtered by section.
228 *
229 * @param CourseModel $course Course model.
230 * @param string $item_type LearnPress item type.
231 * @param int $section_id Optional section filter.
232 *
233 * @return array
234 */
235 protected static function collect_items( CourseModel $course, string $item_type, int $section_id = 0 ): array {
236 $items = array();
237 foreach ( $course->get_section_items() as $section ) {
238 $current_section_id = absint( $section->section_id ?? $section->id ?? 0 );
239 if ( $section_id > 0 && $section_id !== $current_section_id ) {
240 continue;
241 }
242
243 foreach ( $section->items as $item ) {
244 $current_type = (string) ( $item->item_type ?? $item->type ?? '' );
245 if ( $item_type !== $current_type ) {
246 continue;
247 }
248 $items[] = array(
249 'course_id' => $course->get_id(),
250 'section_id' => $current_section_id,
251 'section_name' => (string) ( $section->section_name ?? $section->title ?? '' ),
252 'item_id' => absint( $item->item_id ?? $item->id ?? 0 ),
253 'preview' => ! empty( $item->preview ),
254 );
255 }
256 }
257
258 return $items;
259 }
260 }
261