| 1 |
<?php |
| 2 |
class LP_Jwt_Questions_V1_Controller extends LP_REST_Jwt_Posts_Controller { |
| 3 |
protected $namespace = 'learnpress/v1'; |
| 4 |
|
| 5 |
protected $rest_base = 'questions'; |
| 6 |
|
| 7 |
protected $post_type = LP_QUESTION_CPT; |
| 8 |
|
| 9 |
protected $hierarchical = true; |
| 10 |
|
| 11 |
public function register_routes() { |
| 12 |
register_rest_route( |
| 13 |
$this->namespace, |
| 14 |
'/' . $this->rest_base, |
| 15 |
array( |
| 16 |
array( |
| 17 |
'methods' => WP_REST_Server::READABLE, |
| 18 |
'callback' => array( $this, 'get_items' ), |
| 19 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 20 |
'args' => $this->get_collection_params(), |
| 21 |
), |
| 22 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 23 |
) |
| 24 |
); |
| 25 |
|
| 26 |
register_rest_route( |
| 27 |
$this->namespace, |
| 28 |
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
| 29 |
array( |
| 30 |
'args' => array( |
| 31 |
'id' => array( |
| 32 |
'description' => esc_html__( 'A unique identifier for the resource.', 'learnpress' ), |
| 33 |
'type' => 'integer', |
| 34 |
), |
| 35 |
), |
| 36 |
array( |
| 37 |
'methods' => WP_REST_Server::READABLE, |
| 38 |
'callback' => array( $this, 'get_item' ), |
| 39 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 40 |
'args' => array( |
| 41 |
'context' => $this->get_context_param( |
| 42 |
array( |
| 43 |
'default' => 'view', |
| 44 |
) |
| 45 |
), |
| 46 |
), |
| 47 |
), |
| 48 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 49 |
) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
protected function get_object( $question = 0 ) { |
| 54 |
global $post; |
| 55 |
|
| 56 |
if ( false === $question && isset( $post, $post->ID ) && LP_QUESTION_CPT === get_post_type( $post->ID ) ) { |
| 57 |
$id = absint( $post->ID ); |
| 58 |
} elseif ( is_numeric( $question ) ) { |
| 59 |
$id = $question; |
| 60 |
} elseif ( $question instanceof LP_Question ) { |
| 61 |
$id = $question->get_id(); |
| 62 |
} elseif ( ! empty( $question->ID ) ) { |
| 63 |
$id = $question->ID; |
| 64 |
} |
| 65 |
|
| 66 |
return learn_press_get_question( $id ); |
| 67 |
} |
| 68 |
|
| 69 |
public function prepare_object_for_response( $object, $request ) { |
| 70 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 71 |
$data = $this->get_lesson_data( $object, $context, $request ); |
| 72 |
|
| 73 |
$response = rest_ensure_response( $data ); |
| 74 |
|
| 75 |
return apply_filters( "lp_jwt_rest_prepare_{$this->post_type}_object", $response, $object, $request ); |
| 76 |
} |
| 77 |
|
| 78 |
protected function get_lesson_data( $object, $context = 'view' ) { |
| 79 |
$request = func_num_args() >= 2 ? func_get_arg( 2 ) : new WP_REST_Request( '', '', array( 'context' => $context ) ); |
| 80 |
$fields = $this->get_fields_for_response( $request ); |
| 81 |
|
| 82 |
$id = ! empty( $object->ID ) ? $object->ID : $object->get_id(); |
| 83 |
$post = get_post( $id ); |
| 84 |
$data = array(); |
| 85 |
|
| 86 |
$assigned = $this->get_assigned( $id ); |
| 87 |
|
| 88 |
if ( ! empty( $assigned ) && method_exists( $object, 'set_course' ) ) { |
| 89 |
$course_id = $assigned['course']['id']; |
| 90 |
$object->set_course( $course_id ); |
| 91 |
} |
| 92 |
|
| 93 |
foreach ( $fields as $field ) { |
| 94 |
switch ( $field ) { |
| 95 |
case 'id': |
| 96 |
$data['id'] = $id; |
| 97 |
break; |
| 98 |
case 'name': |
| 99 |
$data['name'] = $post->post_title; |
| 100 |
break; |
| 101 |
case 'slug': |
| 102 |
$data['slug'] = $post->post_name; |
| 103 |
break; |
| 104 |
case 'permalink': |
| 105 |
$data['permalink'] = $object->get_permalink(); |
| 106 |
break; |
| 107 |
case 'date_created': |
| 108 |
$data['date_created'] = lp_jwt_prepare_date_response( $post->post_date_gmt, $post->post_date ); |
| 109 |
break; |
| 110 |
case 'date_created_gmt': |
| 111 |
$data['date_created_gmt'] = lp_jwt_prepare_date_response( $post->post_date_gmt ); |
| 112 |
break; |
| 113 |
case 'date_modified': |
| 114 |
$data['date_modified'] = lp_jwt_prepare_date_response( $post->post_modified_gmt, $post->post_modified ); |
| 115 |
break; |
| 116 |
case 'date_modified_gmt': |
| 117 |
$data['date_modified_gmt'] = lp_jwt_prepare_date_response( $post->post_modified_gmt ); |
| 118 |
break; |
| 119 |
case 'status': |
| 120 |
$data['status'] = $post->post_status; |
| 121 |
break; |
| 122 |
case 'content': |
| 123 |
$data['content'] = 'view' === $context ? apply_filters( 'the_content', $post->post_content ) : $post->post_content; |
| 124 |
break; |
| 125 |
case 'excerpt': |
| 126 |
$data['excerpt'] = $post->post_excerpt; |
| 127 |
break; |
| 128 |
case 'assigned': |
| 129 |
$data['assigned'] = $assigned; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
$data['meta_data'] = $this->get_question_meta( $id ); |
| 135 |
|
| 136 |
return $data; |
| 137 |
} |
| 138 |
|
| 139 |
public function get_question_meta( $id ) { |
| 140 |
if ( ! class_exists( 'LP_Meta_Box_Question' ) ) { |
| 141 |
include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/question/settings.php'; |
| 142 |
} |
| 143 |
|
| 144 |
$metabox = new LP_Meta_Box_Question(); |
| 145 |
|
| 146 |
$output = array(); |
| 147 |
foreach ( $metabox->metabox( $id ) as $meta_key => $object ) { |
| 148 |
if ( is_a( $object, 'LP_Meta_Box_Field' ) ) { |
| 149 |
$object->id = $meta_key; |
| 150 |
$output[ $meta_key ] = $object->meta_value( $id ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return $output; |
| 155 |
} |
| 156 |
|
| 157 |
public function get_assigned( $id ) { |
| 158 |
$curd = new LP_Question_CURD(); |
| 159 |
$quiz = $curd->get_quiz( $id ); |
| 160 |
|
| 161 |
$output = array(); |
| 162 |
|
| 163 |
if ( $quiz ) { |
| 164 |
$courses = learn_press_get_item_courses( $quiz->ID ); |
| 165 |
|
| 166 |
$output['quiz'] = array( |
| 167 |
'id' => $quiz->ID, |
| 168 |
'title' => $quiz->post_title, |
| 169 |
'slug' => $quiz->post_name, |
| 170 |
'content' => $quiz->post_content, |
| 171 |
'author' => $quiz->post_author, |
| 172 |
); |
| 173 |
|
| 174 |
if ( $courses ) { |
| 175 |
foreach ( $courses as $course ) { |
| 176 |
$output['course'] = array( |
| 177 |
'id' => $course->ID, |
| 178 |
'title' => $course->post_title, |
| 179 |
'slug' => $course->post_name, |
| 180 |
'content' => $course->post_content, |
| 181 |
'author' => $course->post_author, |
| 182 |
); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $output; |
| 188 |
} |
| 189 |
|
| 190 |
public function get_item_schema() { |
| 191 |
$schema = array( |
| 192 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 193 |
'title' => $this->post_type, |
| 194 |
'type' => 'object', |
| 195 |
'properties' => array( |
| 196 |
'id' => array( |
| 197 |
'description' => __( 'A unique identifier for the resource.', 'learnpress' ), |
| 198 |
'type' => 'integer', |
| 199 |
'context' => array( 'view', 'edit' ), |
| 200 |
'readonly' => true, |
| 201 |
), |
| 202 |
'name' => array( |
| 203 |
'description' => __( 'Course name.', 'learnpress' ), |
| 204 |
'type' => 'string', |
| 205 |
'context' => array( 'view', 'edit' ), |
| 206 |
), |
| 207 |
'slug' => array( |
| 208 |
'description' => __( 'Course slug.', 'learnpress' ), |
| 209 |
'type' => 'string', |
| 210 |
'context' => array( 'view', 'edit' ), |
| 211 |
), |
| 212 |
'permalink' => array( |
| 213 |
'description' => __( 'Course URL.', 'learnpress' ), |
| 214 |
'type' => 'string', |
| 215 |
'format' => 'uri', |
| 216 |
'context' => array( 'view', 'edit' ), |
| 217 |
'readonly' => true, |
| 218 |
), |
| 219 |
'date_created' => array( |
| 220 |
'description' => __( "The date the Course was created, in the site's timezone.", 'learnpress' ), |
| 221 |
'type' => 'date-time', |
| 222 |
'context' => array( 'view', 'edit' ), |
| 223 |
'readonly' => true, |
| 224 |
), |
| 225 |
'date_created_gmt' => array( |
| 226 |
'description' => __( 'The date the Course was created, as GMT.', 'learnpress' ), |
| 227 |
'type' => 'date-time', |
| 228 |
'context' => array( 'view', 'edit' ), |
| 229 |
'readonly' => true, |
| 230 |
), |
| 231 |
'date_modified' => array( |
| 232 |
'description' => __( "The date the Course was last modified, in the site's timezone.", 'learnpress' ), |
| 233 |
'type' => 'date-time', |
| 234 |
'context' => array( 'view', 'edit' ), |
| 235 |
'readonly' => true, |
| 236 |
), |
| 237 |
'date_modified_gmt' => array( |
| 238 |
'description' => __( 'The date the Course was last modified, as GMT.', 'learnpress' ), |
| 239 |
'type' => 'date-time', |
| 240 |
'context' => array( 'view', 'edit' ), |
| 241 |
'readonly' => true, |
| 242 |
), |
| 243 |
'status' => array( |
| 244 |
'description' => __( 'Course status (post status).', 'learnpress' ), |
| 245 |
'type' => 'string', |
| 246 |
'default' => 'publish', |
| 247 |
'enum' => array_merge( array_keys( get_post_statuses() ), array( 'future' ) ), |
| 248 |
'context' => array( 'view', 'edit' ), |
| 249 |
), |
| 250 |
'content' => array( |
| 251 |
'description' => __( 'Course content.', 'learnpress' ), |
| 252 |
'type' => 'string', |
| 253 |
'context' => array( 'view', 'edit' ), |
| 254 |
), |
| 255 |
'excerpt' => array( |
| 256 |
'description' => __( 'Retrieves the course excerpt..', 'learnpress' ), |
| 257 |
'type' => 'string', |
| 258 |
'context' => array( 'view', 'edit' ), |
| 259 |
), |
| 260 |
'assigned' => array( |
| 261 |
'description' => __( 'Assigned.', 'learnpress' ), |
| 262 |
'type' => 'array', |
| 263 |
'context' => array( 'view', 'edit' ), |
| 264 |
'items' => array( |
| 265 |
'id' => array( |
| 266 |
'description' => __( 'Item ID.', 'learnpress' ), |
| 267 |
'type' => 'integer', |
| 268 |
'context' => array( 'view', 'edit' ), |
| 269 |
), |
| 270 |
'title' => array( |
| 271 |
'description' => __( 'Title.', 'learnpress' ), |
| 272 |
'type' => 'string', |
| 273 |
'context' => array( 'view', 'edit' ), |
| 274 |
), |
| 275 |
'slug' => array( |
| 276 |
'description' => __( 'Item slug.', 'learnpress' ), |
| 277 |
'type' => 'string', |
| 278 |
'context' => array( 'view', 'edit' ), |
| 279 |
), |
| 280 |
'content' => array( |
| 281 |
'description' => __( 'Item Content.', 'learnpress' ), |
| 282 |
'type' => 'string', |
| 283 |
'context' => array( 'view', 'edit' ), |
| 284 |
), |
| 285 |
'author' => array( |
| 286 |
'description' => __( 'Item Author.', 'learnpress' ), |
| 287 |
'type' => 'integer', |
| 288 |
'context' => array( 'view', 'edit' ), |
| 289 |
), |
| 290 |
), |
| 291 |
), |
| 292 |
), |
| 293 |
); |
| 294 |
|
| 295 |
return $this->add_additional_fields_schema( $schema ); |
| 296 |
} |
| 297 |
} |
| 298 |
|