← All changes
|
inc/jwt/rest-api/version1/class-lp-rest-sections-v1-controller.php
+193
-193
4.3.8
→
4.4.9
View file →
| @@ -1,193 +1,193 @@ | ||
| 1 | -<?php | |
| 2 | -class LP_Jwt_Sections_V1_Controller extends LP_REST_Jwt_Controller { | |
| 3 | - protected $namespace = 'learnpress/v1'; | |
| 4 | - | |
| 5 | - protected $rest_base = 'sections'; | |
| 6 | - | |
| 7 | - public function register_routes() { | |
| 8 | - register_rest_route( | |
| 9 | - $this->namespace, | |
| 10 | - '/' . $this->rest_base . '/sections-by-course-id/(?P<course_id>[\d]+)', | |
| 11 | - array( | |
| 12 | - 'args' => array( | |
| 13 | - 'course_id' => array( | |
| 14 | - 'description' => esc_html__( 'Course ID.', 'learnpress' ), | |
| 15 | - 'type' => 'integer', | |
| 16 | - ), | |
| 17 | - ), | |
| 18 | - array( | |
| 19 | - 'methods' => WP_REST_Server::READABLE, | |
| 20 | - 'callback' => array( $this, 'get_sections_by_course' ), | |
| 21 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), | |
| 22 | - 'args' => $this->get_collection_params(), | |
| 23 | - ), | |
| 24 | - 'schema' => array( $this, 'get_public_item_schema' ), | |
| 25 | - ) | |
| 26 | - ); | |
| 27 | - } | |
| 28 | - | |
| 29 | - public function get_items_permissions_check( $request ) { | |
| 30 | - if ( empty( $request['course_id'] ) ) { | |
| 31 | - return new WP_Error( 'lp_section_not_course_id', __( 'Sorry, Invalid course ID param.', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) ); | |
| 32 | - } | |
| 33 | - | |
| 34 | - $course_id = absint( $request['course_id'] ); | |
| 35 | - $post = get_post( $course_id ); | |
| 36 | - | |
| 37 | - $post_status_obj = get_post_status_object( $post->post_status ); | |
| 38 | - | |
| 39 | - if ( ! $post_status_obj || ! $post_status_obj->public ) { | |
| 40 | - return new WP_Error( 'lp_section_not_public', __( 'The course is not public', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) ); | |
| 41 | - } | |
| 42 | - | |
| 43 | - return true; | |
| 44 | - } | |
| 45 | - | |
| 46 | - public function get_sections_by_course( $request ) { | |
| 47 | - $filters = new LP_Section_Filter(); | |
| 48 | - $filters->section_course_id = $request['course_id']; | |
| 49 | - $filters->limit = $request['per_page']; | |
| 50 | - $filters->page = $request['page']; | |
| 51 | - $filters->order = $request['order']; | |
| 52 | - $filters->search_section = $request['search']; | |
| 53 | - $filters->section_ids = $request['include']; | |
| 54 | - $filters->section_not_ids = $request['exclude']; | |
| 55 | - | |
| 56 | - $query_results = LP_Section_DB::getInstance()->get_sections_by_course_id( $filters ); | |
| 57 | - | |
| 58 | - if ( is_wp_error( $query_results ) ) { | |
| 59 | - return $query_results; | |
| 60 | - } | |
| 61 | - | |
| 62 | - $results = $query_results['results']; | |
| 63 | - | |
| 64 | - $return = array(); | |
| 65 | - | |
| 66 | - foreach ( $results as $result ) { | |
| 67 | - $data = $this->prepare_object_for_response( $result, $request ); | |
| 68 | - $return[] = $this->prepare_response_for_collection( $data ); | |
| 69 | - } | |
| 70 | - | |
| 71 | - $response = rest_ensure_response( $return ); | |
| 72 | - | |
| 73 | - $response->header( 'X-WP-Total', $query_results['total'] ); | |
| 74 | - $response->header( 'X-WP-TotalPages', (int) $query_results['pages'] ); | |
| 75 | - | |
| 76 | - return $response; | |
| 77 | - } | |
| 78 | - | |
| 79 | - public function prepare_object_for_response( $section_data, $request ) { | |
| 80 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | |
| 81 | - | |
| 82 | - $data = $this->get_sections_data( $section_data, $context, $request ); | |
| 83 | - | |
| 84 | - $response = rest_ensure_response( $data ); | |
| 85 | - | |
| 86 | - return apply_filters( 'lp_jwt_rest_prepare_sections_object', $response, $section_data, $request ); | |
| 87 | - } | |
| 88 | - | |
| 89 | - public function get_sections_data( $section_data, $context, $request ) { | |
| 90 | - $fields = $this->get_fields_for_response( $request ); | |
| 91 | - | |
| 92 | - foreach ( $fields as $field ) { | |
| 93 | - switch ( $field ) { | |
| 94 | - case 'id': | |
| 95 | - $data['id'] = $section_data['section_id'] ?? 0; | |
| 96 | - break; | |
| 97 | - case 'name': | |
| 98 | - $data['name'] = $section_data['section_name'] ?? ''; | |
| 99 | - break; | |
| 100 | - case 'description': | |
| 101 | - $data['description'] = $section_data['section_description'] ?? ''; | |
| 102 | - break; | |
| 103 | - } | |
| 104 | - } | |
| 105 | - | |
| 106 | - return $data; | |
| 107 | - } | |
| 108 | - | |
| 109 | - public function get_item_schema() { | |
| 110 | - $schema = array( | |
| 111 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 112 | - 'title' => 'learnpress_sections', | |
| 113 | - 'type' => 'object', | |
| 114 | - 'properties' => array( | |
| 115 | - 'id' => array( | |
| 116 | - 'description' => __( 'A unique identifier for the resource.', 'learnpress' ), | |
| 117 | - 'type' => 'integer', | |
| 118 | - 'context' => array( 'view', 'edit' ), | |
| 119 | - 'readonly' => true, | |
| 120 | - ), | |
| 121 | - 'name' => array( | |
| 122 | - 'description' => __( 'Section name.', 'learnpress' ), | |
| 123 | - 'type' => 'string', | |
| 124 | - 'context' => array( 'view', 'edit' ), | |
| 125 | - ), | |
| 126 | - 'description' => array( | |
| 127 | - 'description' => __( 'Section description.', 'learnpress' ), | |
| 128 | - 'type' => 'string', | |
| 129 | - 'context' => array( 'view', 'edit' ), | |
| 130 | - ), | |
| 131 | - ), | |
| 132 | - ); | |
| 133 | - | |
| 134 | - return $this->add_additional_fields_schema( $schema ); | |
| 135 | - } | |
| 136 | - | |
| 137 | - public function get_collection_params() { | |
| 138 | - $params = array(); | |
| 139 | - $params['context'] = $this->get_context_param(); | |
| 140 | - $params['context']['default'] = 'view'; | |
| 141 | - | |
| 142 | - $params['page'] = array( | |
| 143 | - 'description' => __( 'The current page of the collection.', 'learnpress' ), | |
| 144 | - 'type' => 'integer', | |
| 145 | - 'default' => 1, | |
| 146 | - 'sanitize_callback' => 'absint', | |
| 147 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 148 | - 'minimum' => 1, | |
| 149 | - ); | |
| 150 | - $params['per_page'] = array( | |
| 151 | - 'description' => __( 'The maximum number of items to be returned in the result set.', 'learnpress' ), | |
| 152 | - 'type' => 'integer', | |
| 153 | - 'default' => 10, | |
| 154 | - 'minimum' => 1, | |
| 155 | - 'maximum' => 100, | |
| 156 | - 'sanitize_callback' => 'absint', | |
| 157 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 158 | - ); | |
| 159 | - $params['order'] = array( | |
| 160 | - 'description' => __( 'Sorting attributes in ascending or descending order.', 'learnpress' ), | |
| 161 | - 'type' => 'string', | |
| 162 | - 'default' => 'asc', | |
| 163 | - 'enum' => array( 'asc', 'desc' ), | |
| 164 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 165 | - ); | |
| 166 | - $params['search'] = array( | |
| 167 | - 'description' => __( 'Limit results to those matching a string.', 'learnpress' ), | |
| 168 | - 'type' => 'string', | |
| 169 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 170 | - 'validate_callback' => 'rest_validate_request_arg', | |
| 171 | - ); | |
| 172 | - $params['exclude'] = array( | |
| 173 | - 'description' => __( 'Ensure the result set excludes specific IDs.', 'learnpress' ), | |
| 174 | - 'type' => 'array', | |
| 175 | - 'items' => array( | |
| 176 | - 'type' => 'integer', | |
| 177 | - ), | |
| 178 | - 'default' => array(), | |
| 179 | - 'sanitize_callback' => 'wp_parse_id_list', | |
| 180 | - ); | |
| 181 | - $params['include'] = array( | |
| 182 | - 'description' => __( 'Limit the result set to specific IDs.', 'learnpress' ), | |
| 183 | - 'type' => 'array', | |
| 184 | - 'items' => array( | |
| 185 | - 'type' => 'integer', | |
| 186 | - ), | |
| 187 | - 'default' => array(), | |
| 188 | - 'sanitize_callback' => 'wp_parse_id_list', | |
| 189 | - ); | |
| 190 | - | |
| 191 | - return apply_filters( 'lp_rest_sections_collection_params', $params ); | |
| 192 | - } | |
| 193 | -} | |
| 1 | +<?php | |
| 2 | +class LP_Jwt_Sections_V1_Controller extends LP_REST_Jwt_Controller { | |
| 3 | + protected $namespace = 'learnpress/v1'; | |
| 4 | + | |
| 5 | + protected $rest_base = 'sections'; | |
| 6 | + | |
| 7 | + public function register_routes() { | |
| 8 | + register_rest_route( | |
| 9 | + $this->namespace, | |
| 10 | + '/' . $this->rest_base . '/sections-by-course-id/(?P<course_id>[\d]+)', | |
| 11 | + array( | |
| 12 | + 'args' => array( | |
| 13 | + 'course_id' => array( | |
| 14 | + 'description' => esc_html__( 'Course ID.', 'learnpress' ), | |
| 15 | + 'type' => 'integer', | |
| 16 | + ), | |
| 17 | + ), | |
| 18 | + array( | |
| 19 | + 'methods' => WP_REST_Server::READABLE, | |
| 20 | + 'callback' => array( $this, 'get_sections_by_course' ), | |
| 21 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), | |
| 22 | + 'args' => $this->get_collection_params(), | |
| 23 | + ), | |
| 24 | + 'schema' => array( $this, 'get_public_item_schema' ), | |
| 25 | + ) | |
| 26 | + ); | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function get_items_permissions_check( $request ) { | |
| 30 | + if ( empty( $request['course_id'] ) ) { | |
| 31 | + return new WP_Error( 'lp_section_not_course_id', __( 'Sorry, Invalid course ID param.', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) ); | |
| 32 | + } | |
| 33 | + | |
| 34 | + $course_id = absint( $request['course_id'] ); | |
| 35 | + $post = get_post( $course_id ); | |
| 36 | + | |
| 37 | + $post_status_obj = get_post_status_object( $post->post_status ); | |
| 38 | + | |
| 39 | + if ( ! $post_status_obj || ! $post_status_obj->public ) { | |
| 40 | + return new WP_Error( 'lp_section_not_public', __( 'The course is not public', 'learnpress' ), array( 'status' => rest_authorization_required_code() ) ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + return true; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public function get_sections_by_course( $request ) { | |
| 47 | + $filters = new LP_Section_Filter(); | |
| 48 | + $filters->section_course_id = $request['course_id']; | |
| 49 | + $filters->limit = $request['per_page']; | |
| 50 | + $filters->page = $request['page']; | |
| 51 | + $filters->order = $request['order']; | |
| 52 | + $filters->search_section = $request['search']; | |
| 53 | + $filters->section_ids = $request['include']; | |
| 54 | + $filters->section_not_ids = $request['exclude']; | |
| 55 | + | |
| 56 | + $query_results = LP_Section_DB::getInstance()->get_sections_by_course_id( $filters ); | |
| 57 | + | |
| 58 | + if ( is_wp_error( $query_results ) ) { | |
| 59 | + return $query_results; | |
| 60 | + } | |
| 61 | + | |
| 62 | + $results = $query_results['results']; | |
| 63 | + | |
| 64 | + $return = array(); | |
| 65 | + | |
| 66 | + foreach ( $results as $result ) { | |
| 67 | + $data = $this->prepare_object_for_response( $result, $request ); | |
| 68 | + $return[] = $this->prepare_response_for_collection( $data ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + $response = rest_ensure_response( $return ); | |
| 72 | + | |
| 73 | + $response->header( 'X-WP-Total', $query_results['total'] ); | |
| 74 | + $response->header( 'X-WP-TotalPages', (int) $query_results['pages'] ); | |
| 75 | + | |
| 76 | + return $response; | |
| 77 | + } | |
| 78 | + | |
| 79 | + public function prepare_object_for_response( $section_data, $request ) { | |
| 80 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | |
| 81 | + | |
| 82 | + $data = $this->get_sections_data( $section_data, $context, $request ); | |
| 83 | + | |
| 84 | + $response = rest_ensure_response( $data ); | |
| 85 | + | |
| 86 | + return apply_filters( 'lp_jwt_rest_prepare_sections_object', $response, $section_data, $request ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function get_sections_data( $section_data, $context, $request ) { | |
| 90 | + $fields = $this->get_fields_for_response( $request ); | |
| 91 | + | |
| 92 | + foreach ( $fields as $field ) { | |
| 93 | + switch ( $field ) { | |
| 94 | + case 'id': | |
| 95 | + $data['id'] = $section_data['section_id'] ?? 0; | |
| 96 | + break; | |
| 97 | + case 'name': | |
| 98 | + $data['name'] = $section_data['section_name'] ?? ''; | |
| 99 | + break; | |
| 100 | + case 'description': | |
| 101 | + $data['description'] = $section_data['section_description'] ?? ''; | |
| 102 | + break; | |
| 103 | + } | |
| 104 | + } | |
| 105 | + | |
| 106 | + return $data; | |
| 107 | + } | |
| 108 | + | |
| 109 | + public function get_item_schema() { | |
| 110 | + $schema = array( | |
| 111 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 112 | + 'title' => 'learnpress_sections', | |
| 113 | + 'type' => 'object', | |
| 114 | + 'properties' => array( | |
| 115 | + 'id' => array( | |
| 116 | + 'description' => __( 'A unique identifier for the resource.', 'learnpress' ), | |
| 117 | + 'type' => 'integer', | |
| 118 | + 'context' => array( 'view', 'edit' ), | |
| 119 | + 'readonly' => true, | |
| 120 | + ), | |
| 121 | + 'name' => array( | |
| 122 | + 'description' => __( 'Section name.', 'learnpress' ), | |
| 123 | + 'type' => 'string', | |
| 124 | + 'context' => array( 'view', 'edit' ), | |
| 125 | + ), | |
| 126 | + 'description' => array( | |
| 127 | + 'description' => __( 'Section description.', 'learnpress' ), | |
| 128 | + 'type' => 'string', | |
| 129 | + 'context' => array( 'view', 'edit' ), | |
| 130 | + ), | |
| 131 | + ), | |
| 132 | + ); | |
| 133 | + | |
| 134 | + return $this->add_additional_fields_schema( $schema ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + public function get_collection_params() { | |
| 138 | + $params = array(); | |
| 139 | + $params['context'] = $this->get_context_param(); | |
| 140 | + $params['context']['default'] = 'view'; | |
| 141 | + | |
| 142 | + $params['page'] = array( | |
| 143 | + 'description' => __( 'The current page of the collection.', 'learnpress' ), | |
| 144 | + 'type' => 'integer', | |
| 145 | + 'default' => 1, | |
| 146 | + 'sanitize_callback' => 'absint', | |
| 147 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 148 | + 'minimum' => 1, | |
| 149 | + ); | |
| 150 | + $params['per_page'] = array( | |
| 151 | + 'description' => __( 'The maximum number of items to be returned in the result set.', 'learnpress' ), | |
| 152 | + 'type' => 'integer', | |
| 153 | + 'default' => 10, | |
| 154 | + 'minimum' => 1, | |
| 155 | + 'maximum' => 100, | |
| 156 | + 'sanitize_callback' => 'absint', | |
| 157 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 158 | + ); | |
| 159 | + $params['order'] = array( | |
| 160 | + 'description' => __( 'Sorting attributes in ascending or descending order.', 'learnpress' ), | |
| 161 | + 'type' => 'string', | |
| 162 | + 'default' => 'asc', | |
| 163 | + 'enum' => array( 'asc', 'desc' ), | |
| 164 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 165 | + ); | |
| 166 | + $params['search'] = array( | |
| 167 | + 'description' => __( 'Limit results to those matching a string.', 'learnpress' ), | |
| 168 | + 'type' => 'string', | |
| 169 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 170 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 171 | + ); | |
| 172 | + $params['exclude'] = array( | |
| 173 | + 'description' => __( 'Ensure the result set excludes specific IDs.', 'learnpress' ), | |
| 174 | + 'type' => 'array', | |
| 175 | + 'items' => array( | |
| 176 | + 'type' => 'integer', | |
| 177 | + ), | |
| 178 | + 'default' => array(), | |
| 179 | + 'sanitize_callback' => 'wp_parse_id_list', | |
| 180 | + ); | |
| 181 | + $params['include'] = array( | |
| 182 | + 'description' => __( 'Limit the result set to specific IDs.', 'learnpress' ), | |
| 183 | + 'type' => 'array', | |
| 184 | + 'items' => array( | |
| 185 | + 'type' => 'integer', | |
| 186 | + ), | |
| 187 | + 'default' => array(), | |
| 188 | + 'sanitize_callback' => 'wp_parse_id_list', | |
| 189 | + ); | |
| 190 | + | |
| 191 | + return apply_filters( 'lp_rest_sections_collection_params', $params ); | |
| 192 | + } | |
| 193 | +} | |