| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\MCP\Concerns; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* JSON schema builders for LearnPress abilities. |
| 9 |
*/ |
| 10 |
trait AbilitySchemas { |
| 11 |
/** |
| 12 |
* Build schema for required integer identifier. |
| 13 |
* |
| 14 |
* @param string $id Field name. |
| 15 |
* |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
protected static function schema_required_id( string $id ): array { |
| 19 |
return array( |
| 20 |
'type' => 'object', |
| 21 |
'additionalProperties' => false, |
| 22 |
'required' => array( $id ), |
| 23 |
'properties' => array( |
| 24 |
$id => array( |
| 25 |
'type' => 'integer', |
| 26 |
'minimum' => 1, |
| 27 |
), |
| 28 |
), |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Build list response schema with pagination. |
| 34 |
* |
| 35 |
* @param array $item_schema Schema for each list item. |
| 36 |
* |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
protected static function schema_list_output( array $item_schema ): array { |
| 40 |
return array( |
| 41 |
'type' => 'object', |
| 42 |
'additionalProperties' => false, |
| 43 |
'required' => array( 'items', 'pagination' ), |
| 44 |
'properties' => array( |
| 45 |
'items' => array( |
| 46 |
'type' => 'array', |
| 47 |
'items' => $item_schema, |
| 48 |
), |
| 49 |
'pagination' => self::schema_pagination(), |
| 50 |
), |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Build wrapper schema for a named object. |
| 56 |
* |
| 57 |
* @param string $key Object key. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
protected static function schema_object_output( string $key ): array { |
| 62 |
return array( |
| 63 |
'type' => 'object', |
| 64 |
'additionalProperties' => false, |
| 65 |
'required' => array( $key ), |
| 66 |
'properties' => array( $key => array( 'type' => 'object' ) ), |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Pagination schema used by list abilities. |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
protected static function schema_pagination(): array { |
| 76 |
return array( |
| 77 |
'type' => 'object', |
| 78 |
'additionalProperties' => false, |
| 79 |
'required' => array( 'page', 'per_page', 'total_items', 'total_pages' ), |
| 80 |
'properties' => array( |
| 81 |
'page' => array( 'type' => 'integer' ), |
| 82 |
'per_page' => array( 'type' => 'integer' ), |
| 83 |
'total_items' => array( 'type' => 'integer' ), |
| 84 |
'total_pages' => array( 'type' => 'integer' ), |
| 85 |
), |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Course summary schema. |
| 91 |
* |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
protected static function schema_course_summary(): array { |
| 95 |
return array( |
| 96 |
'type' => 'object', |
| 97 |
'properties' => array( |
| 98 |
'course_id' => array( 'type' => 'integer' ), |
| 99 |
'title' => array( 'type' => 'string' ), |
| 100 |
'status' => array( 'type' => 'string' ), |
| 101 |
'price' => array( 'type' => 'number' ), |
| 102 |
'duration' => array( 'type' => 'string' ), |
| 103 |
'permalink' => array( 'type' => 'string' ), |
| 104 |
'instructor' => array( 'type' => 'object' ), |
| 105 |
'categories' => array( 'type' => 'array' ), |
| 106 |
), |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Lesson summary schema. |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
protected static function schema_lesson_summary(): array { |
| 116 |
return array( |
| 117 |
'type' => 'object', |
| 118 |
'properties' => array( |
| 119 |
'lesson_id' => array( 'type' => 'integer' ), |
| 120 |
'course_id' => array( 'type' => 'integer' ), |
| 121 |
'section_id' => array( 'type' => 'integer' ), |
| 122 |
'section_name' => array( 'type' => 'string' ), |
| 123 |
'title' => array( 'type' => 'string' ), |
| 124 |
'excerpt' => array( 'type' => 'string' ), |
| 125 |
'duration' => array( 'type' => 'string' ), |
| 126 |
'preview' => array( 'type' => 'boolean' ), |
| 127 |
'status' => array( 'type' => 'string' ), |
| 128 |
'permalink' => array( 'type' => 'string' ), |
| 129 |
), |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Quiz summary schema. |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
protected static function schema_quiz_summary(): array { |
| 139 |
return array( |
| 140 |
'type' => 'object', |
| 141 |
'properties' => array( |
| 142 |
'quiz_id' => array( 'type' => 'integer' ), |
| 143 |
'course_id' => array( 'type' => 'integer' ), |
| 144 |
'section_id' => array( 'type' => 'integer' ), |
| 145 |
'section_name' => array( 'type' => 'string' ), |
| 146 |
'title' => array( 'type' => 'string' ), |
| 147 |
'duration' => array( 'type' => 'string' ), |
| 148 |
'passing_grade' => array( 'type' => 'number' ), |
| 149 |
'questions_count' => array( 'type' => 'integer' ), |
| 150 |
'status' => array( 'type' => 'string' ), |
| 151 |
'permalink' => array( 'type' => 'string' ), |
| 152 |
), |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Input schema for course listing. |
| 158 |
* |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
protected static function schema_get_courses_input(): array { |
| 162 |
return array( |
| 163 |
'type' => 'object', |
| 164 |
'additionalProperties' => false, |
| 165 |
'properties' => array( |
| 166 |
'status' => array( 'type' => array( 'string', 'array', 'null' ) ), |
| 167 |
'category' => array( |
| 168 |
'type' => 'integer', |
| 169 |
'minimum' => 1, |
| 170 |
), |
| 171 |
'instructor' => array( |
| 172 |
'type' => 'integer', |
| 173 |
'minimum' => 1, |
| 174 |
), |
| 175 |
'price_min' => array( |
| 176 |
'type' => 'number', |
| 177 |
'minimum' => 0, |
| 178 |
), |
| 179 |
'price_max' => array( |
| 180 |
'type' => 'number', |
| 181 |
'minimum' => 0, |
| 182 |
), |
| 183 |
'search' => array( 'type' => 'string' ), |
| 184 |
'page' => array( |
| 185 |
'type' => 'integer', |
| 186 |
'minimum' => 1, |
| 187 |
'default' => 1, |
| 188 |
), |
| 189 |
'per_page' => array( |
| 190 |
'type' => 'integer', |
| 191 |
'minimum' => 1, |
| 192 |
'maximum' => 100, |
| 193 |
'default' => 10, |
| 194 |
), |
| 195 |
), |
| 196 |
'default' => array(), |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Output schema for course detail. |
| 202 |
* |
| 203 |
* @return array |
| 204 |
*/ |
| 205 |
protected static function schema_course_detail_output(): array { |
| 206 |
return array( |
| 207 |
'type' => 'object', |
| 208 |
'additionalProperties' => false, |
| 209 |
'required' => array( 'course' ), |
| 210 |
'properties' => array( 'course' => array( 'type' => 'object' ) ), |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Input schema for lesson listing. |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
protected static function schema_list_lessons_input(): array { |
| 220 |
return array( |
| 221 |
'type' => 'object', |
| 222 |
'additionalProperties' => false, |
| 223 |
'required' => array( 'course_id' ), |
| 224 |
'properties' => array( |
| 225 |
'course_id' => array( |
| 226 |
'type' => 'integer', |
| 227 |
'minimum' => 1, |
| 228 |
), |
| 229 |
'section_id' => array( |
| 230 |
'type' => 'integer', |
| 231 |
'minimum' => 1, |
| 232 |
), |
| 233 |
'status' => array( 'type' => array( 'string', 'array', 'null' ) ), |
| 234 |
'page' => array( |
| 235 |
'type' => 'integer', |
| 236 |
'minimum' => 1, |
| 237 |
'default' => 1, |
| 238 |
), |
| 239 |
'per_page' => array( |
| 240 |
'type' => 'integer', |
| 241 |
'minimum' => 1, |
| 242 |
'maximum' => 100, |
| 243 |
'default' => 10, |
| 244 |
), |
| 245 |
), |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Output schema for lesson detail. |
| 251 |
* |
| 252 |
* @return array |
| 253 |
*/ |
| 254 |
protected static function schema_lesson_detail_output(): array { |
| 255 |
return array( |
| 256 |
'type' => 'object', |
| 257 |
'additionalProperties' => false, |
| 258 |
'required' => array( 'lesson' ), |
| 259 |
'properties' => array( 'lesson' => array( 'type' => 'object' ) ), |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Input schema for quiz listing. |
| 265 |
* |
| 266 |
* @return array |
| 267 |
*/ |
| 268 |
protected static function schema_list_quizzes_input(): array { |
| 269 |
return array( |
| 270 |
'type' => 'object', |
| 271 |
'additionalProperties' => false, |
| 272 |
'required' => array( 'course_id' ), |
| 273 |
'properties' => array( |
| 274 |
'course_id' => array( |
| 275 |
'type' => 'integer', |
| 276 |
'minimum' => 1, |
| 277 |
), |
| 278 |
'page' => array( |
| 279 |
'type' => 'integer', |
| 280 |
'minimum' => 1, |
| 281 |
'default' => 1, |
| 282 |
), |
| 283 |
'per_page' => array( |
| 284 |
'type' => 'integer', |
| 285 |
'minimum' => 1, |
| 286 |
'maximum' => 100, |
| 287 |
'default' => 10, |
| 288 |
), |
| 289 |
), |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Output schema for quiz detail. |
| 295 |
* |
| 296 |
* @return array |
| 297 |
*/ |
| 298 |
protected static function schema_quiz_detail_output(): array { |
| 299 |
return array( |
| 300 |
'type' => 'object', |
| 301 |
'additionalProperties' => false, |
| 302 |
'required' => array( 'quiz' ), |
| 303 |
'properties' => array( 'quiz' => array( 'type' => 'object' ) ), |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Input schema for student progress. |
| 309 |
* |
| 310 |
* @return array |
| 311 |
*/ |
| 312 |
protected static function schema_progress_input(): array { |
| 313 |
return array( |
| 314 |
'type' => 'object', |
| 315 |
'additionalProperties' => false, |
| 316 |
'required' => array( 'user_id', 'course_id' ), |
| 317 |
'properties' => array( |
| 318 |
'user_id' => array( |
| 319 |
'type' => 'integer', |
| 320 |
'minimum' => 1, |
| 321 |
), |
| 322 |
'course_id' => array( |
| 323 |
'type' => 'integer', |
| 324 |
'minimum' => 1, |
| 325 |
), |
| 326 |
), |
| 327 |
); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Input schema for enrollment listing. |
| 332 |
* |
| 333 |
* @return array |
| 334 |
*/ |
| 335 |
protected static function schema_get_enrollments_input(): array { |
| 336 |
return array( |
| 337 |
'type' => 'object', |
| 338 |
'additionalProperties' => false, |
| 339 |
'properties' => array( |
| 340 |
'course_id' => array( |
| 341 |
'type' => 'integer', |
| 342 |
'minimum' => 1, |
| 343 |
), |
| 344 |
'user_id' => array( |
| 345 |
'type' => 'integer', |
| 346 |
'minimum' => 1, |
| 347 |
), |
| 348 |
'status' => array( 'type' => array( 'string', 'array', 'null' ) ), |
| 349 |
'page' => array( |
| 350 |
'type' => 'integer', |
| 351 |
'minimum' => 1, |
| 352 |
'default' => 1, |
| 353 |
), |
| 354 |
'per_page' => array( |
| 355 |
'type' => 'integer', |
| 356 |
'minimum' => 1, |
| 357 |
'maximum' => 100, |
| 358 |
'default' => 10, |
| 359 |
), |
| 360 |
), |
| 361 |
'default' => array(), |
| 362 |
); |
| 363 |
} |
| 364 |
} |
| 365 |
|