File manager - Edit - /home/theblueo/tv/fb4e3b/v1.tar
Back
class-ld-rest-users-groups-controller.php 0000666 00000027303 15214246251 0014602 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Users_Groups_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Users_Groups_Controller_V1 extends LD_REST_Posts_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'post__not_in', 'include' => 'post__in', 'offset' => 'offset', 'order' => 'order', 'orderby' => 'orderby', 'per_page' => 'posts_per_page', 'page' => 'paged', 'search' => 's', 'fields' => 'fields' ); public function __construct( ) { $this->post_type = 'groups'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'users' ); } /** * Registers the routes for the objects of the controller. * * @since 4.7.0 * * @see register_rest_route() */ public function register_routes() { $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/groups', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'User ID', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_user_groups' ), 'permission_callback' => array( $this, 'get_user_groups_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_user_groups' ), 'permission_callback' => array( $this, 'update_user_groups_permissions_check' ), 'args' => array( 'group_ids' => array( 'description' => esc_html__( 'Group IDs to add to User.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_user_groups' ), 'permission_callback' => array( $this, 'delete_user_groups_permissions_check' ), 'args' => array( 'group_ids' => array( 'description' => esc_html__( 'Group IDs to remove from User.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function get_user_groups_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } else if ( get_current_user_id() == $request['id'] ) { return true; } } function update_user_groups_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } else if ( get_current_user_id() == $request['id'] ) { return true; } } function delete_user_groups_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } else if ( get_current_user_id() == $request['id'] ) { return true; } } function update_user_groups( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid User ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $group_ids = $request['group_ids']; if ( ( !is_array( $group_ids ) ) || ( empty( $group_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Group IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $group_ids = array_map( 'intval', $group_ids ); } foreach( $group_ids as $group_id ) { ld_update_group_access( $user_id, $group_id, false ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function delete_user_groups( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid User ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $group_ids = $request['group_ids']; if ( ( !is_array( $group_ids ) ) || ( empty( $group_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Group IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $group_ids = array_map( 'intval', $group_ids ); } foreach( $group_ids as $group_id ) { ld_update_group_access( $user_id, $group_id, true ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function get_user_groups( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_user_invalid_id', esc_html__( 'Invalid user ID. #1', 'learndash' ), array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Ensure a search string is set in case the orderby is set to 'relevance'. if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.', 'learndash' ), array( 'status' => 400 ) ); } // Ensure an include parameter is set in case the orderby is set to 'include'. if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.', 'learndash' ), array( 'status' => 400 ) ); } // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $args[ $wp_param ] = $request[ $api_param ]; } } // Check for & assign any parameters which require special handling or setting. $args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $registered['before'], $request['before'] ) ) { $args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $registered['after'], $request['after'] ) ) { $args['date_query'][0]['after'] = $request['after']; } // Ensure our per_page parameter overrides any provided posts_per_page filter. if ( isset( $registered['per_page'] ) ) { $args['posts_per_page'] = $request['per_page']; } // Force the post_type argument, since it's not a user input variable. $args['post_type'] = $this->post_type; $args['post__in'] = array(0); $group_ids = learndash_get_users_group_ids( $user_id ); if ( !empty( $group_ids ) ) { $args['post__in'] = $group_ids; } if ( !isset( $args['fields'] ) ) $args['fields'] = 'ids'; else if ( $args['fields'] != 'ids' ) unset( $args['fields'] ); /** * Filters the query arguments for a request. * * Enables adding extra arguments or setting defaults for a post collection request. * * @since 4.7.0 * * @link https://developer.wordpress.org/reference/classes/wp_query/ * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request used. */ $args = apply_filters( "learndash_rest_user_groups_query", $args, $request ); $query_args = $this->prepare_items_query( $args, $request ); //error_log('query_args<pre>'. print_r($query_args, true) .'</pre>'); $posts_query = new WP_Query(); $query_result = $posts_query->query( $query_args ); //error_log('query_result<pre>'. print_r($query_result, true) .'</pre>'); // Allow access to all password protected posts if the context is edit. if ( 'edit' === $request['context'] ) { add_filter( 'post_password_required', '__return_false' ); } if ( ( !isset( $args['fields'] ) ) || ( $args['fields'] == 'post' ) ) { $posts = array(); foreach ( $query_result as $post ) { if ( ! $this->check_read_permission( $post ) ) { continue; } $data = $this->prepare_item_for_response( $post, $request ); $posts[] = $this->prepare_response_for_collection( $data ); } $response = rest_ensure_response( $posts ); } else { //$data = $query_result; $response = rest_ensure_response( $query_result ); } // Reset filter. if ( 'edit' === $request['context'] ) { remove_filter( 'post_password_required', '__return_false' ); } $page = (int) $query_args['paged']; $total_posts = $posts_query->found_posts; if ( $total_posts < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; } $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); if ( $page > $max_pages && $total_posts > 0 ) { return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'learndash' ), array( 'status' => 400 ) ); } $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $request_params = $request->get_query_params(); $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); //error_log('query_params_default<pre>'. print_r($query_params_default, true) .'</pre>'); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __('Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[$external_key] ) ) { $query_params[$external_key] = $query_params_default[$external_key]; } } return $query_params; } } } class-ld-rest-sections-controller.php 0000666 00000004231 15214246251 0013746 0 ustar 00 <?php /** * Sections REST API Endpoint. * * Register interface to handle sections with the REST API. * * @package LearnDash */ if ( ! class_exists( 'LD_REST_Sections_Controller_V1' ) ) { /** * Sections REST Controller. */ class LD_REST_Sections_Controller_V1 extends WP_REST_Controller { /** * Register the routes for the objects of the controller. */ public function register_routes() { $version = '1'; $namespace = LEARNDASH_REST_API_NAMESPACE . '/v' . $version; $base = 'sections'; register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'permissions_check' ), 'args' => array( 'id' => array( 'required' => true, 'validate_callback' => function( $param, $request, $key ) { return is_numeric( $param ); }, 'sanitize_callback' => 'absint', ), ), ), ) ); } /** * Check if a given request has access manage the item. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|bool */ public function permissions_check( $request ) { $params = $request->get_params(); $course_id = $params['id']; return current_user_can( 'edit_post', $course_id ); } /** * Update sections data. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Request */ public function update_item( $request ) { $params = $request->get_params(); $course_id = $params['id']; $sections = isset( $params['sections'] ) ? wp_slash( $params['sections'] ) : ''; update_post_meta( $course_id, 'course_sections', $sections ); return new WP_REST_Response( $this->get_sections_data( $course_id ), 200 ); } /** * Get sections data. * * @param int $course_id The course ID. * @return object */ public function get_sections_data( $course_id ) { $sections = get_post_meta( $course_id, 'course_sections', true ); return $sections; } } } class-ld-rest-courses-steps-controller.php 0000666 00000016254 15214246251 0014746 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Courses_Steps_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Courses_Steps_Controller_V1 extends LD_REST_Posts_Controller_V1 { private $supported_collection_params = array( 'filter' => 'filter' ); public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'sfwd-courses' ); } public function register_routes() { $this->register_fields(); //parent::register_routes_wpv2( ); //$collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/steps', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Course ID to enroll user into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_course_steps' ), 'permission_callback' => array( $this, 'get_course_steps_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_course_steps' ), 'permission_callback' => array( $this, 'update_course_steps_permissions_check' ), 'args' => $this->get_collection_params(), ), ) ); } public function get_collection_params() { $query_params_default = parent::get_collection_params(); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['type'] = array( 'description' => __('Filter returned results by step type.', 'learndash' ), 'type' => 'string', 'default' => 'all', 'enum' => array( 'all', 'h', 'l', 't', 'r', ), ); return $query_params; } function get_course_steps_permissions_check( $request ) { if ( is_user_logged_in() ) { if ( learndash_is_admin_user( ) ) { if ( LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_Admin_User', 'courses_autoenroll_admin_users' ) === 'yes' ) { return true; } } $this->enrolled_courses = learndash_user_get_enrolled_courses( get_current_user_id() ); // Ensure the user has some courses. if ( !empty( $this->enrolled_courses ) ) { /// Secondary check if they are wanting steps for a specific course ID. $course_id = $request['id']; if ( !empty( $course_id ) ) { // And if that course ID is in their enrolled courses. if ( in_array( $course_id, $this->enrolled_courses ) ) { $this->enrolled_courses = array( $course_id ); return true; } } else { // If user has enrolled courses but not requesting a specific course then good to go. return true; } } } } function get_course_steps( $request ) { $current_user_id = get_current_user_id(); if ( empty( $current_user_id ) ) { return new WP_Error( 'rest_not_logged_in', esc_html__( 'You are not currently logged in.', 'learndash' ), array( 'status' => 401 ) ); } $current_user = wp_get_current_user(); $course = $this->get_post( $request['id'] ); if ( is_wp_error( $course ) ) { return $course; } $ld_course_steps_object = LDLMS_Factory_Post::course_steps( intval( $course->ID ) ); $course_steps = $ld_course_steps_object->get_steps( $request['type'] ); $data = $course_steps; // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function update_course_steps_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_course_steps( $request ) { $current_user_id = get_current_user_id(); if ( empty( $current_user_id ) ) { return new WP_Error( 'rest_not_logged_in', esc_html__( 'You are not currently logged in.', 'learndash' ), array( 'status' => 401 ) ); } $current_user = wp_get_current_user(); $course = $this->get_post( $request['id'] ); if ( is_wp_error( $course ) ) { return $course; } $ld_course_steps_object = LDLMS_Factory_Post::course_steps( intval( $course->ID ) ); $body = $request->get_body(); if ( !empty( $body ) ) { $body = json_decode( $body, true ); if ( ( $body ) && ( json_last_error() == JSON_ERROR_NONE ) ) { $steps = array(); $steps['sfwd-lessons'] = array(); $steps['sfwd-quiz'] = array(); if ( ( isset( $body['sfwd-lessons'] ) ) && ( !empty( $body['sfwd-lessons'] ) ) ) { foreach( $body['sfwd-lessons'] as $lesson_id => $lesson_set ) { $steps['sfwd-lessons'][$lesson_id] = array(); $steps['sfwd-lessons'][$lesson_id]['sfwd-topic'] = array(); $steps['sfwd-lessons'][$lesson_id]['sfwd-quiz'] = array(); if ( ( isset( $lesson_set['sfwd-topic'] ) ) && ( !empty( $lesson_set['sfwd-topic'] ) ) ) { foreach( $lesson_set['sfwd-topic'] as $topic_id => $topic_set ) { $steps['sfwd-lessons'][$lesson_id]['sfwd-topic'][$topic_id] = array(); $steps['sfwd-lessons'][$lesson_id]['sfwd-topic'][$topic_id]['sfwd-quiz'] = array(); if ( ( isset( $topic_set['sfwd-quiz'] ) ) && ( !empty( $topic_set['sfwd-quiz'] ) ) ) { foreach( $topic_set['sfwd-quiz'] as $quiz_id => $quiz_set ) { $steps['sfwd-lessons'][$lesson_id]['sfwd-topic'][$topic_id]['sfwd-quiz'][$quiz_id] = array(); } } } } if ( ( isset( $lesson_set['sfwd-quiz'] ) ) && ( !empty( $lesson_set['sfwd-quiz'] ) ) ) { foreach( $lesson_set['sfwd-quiz'] as $quiz_id => $quiz_set ) { $steps['sfwd-lessons'][$lesson_id]['sfwd-quiz'][$quiz_id] = array(); } } } } if ( ( isset( $body['sfwd-quiz'] ) ) && ( !empty( $body['sfwd-quiz'] ) ) ) { $steps['sfwd-quiz'] = $body['sfwd-quiz']; } $ld_course_steps_object->set_steps( $steps ); } } $ld_course_steps_object->load_steps(); $course_steps = $ld_course_steps_object->get_steps( 'h' ); $data = $course_steps; // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } // End of functions } } class-ld-rest-questions-controller.php 0000666 00000015316 15214246251 0014157 0 ustar 00 <?php /** * Questions REST API Endpoint. * * Register interface to handle questions with the REST API. * * @package LearnDash */ if ( ! class_exists( 'LD_REST_Questions_Controller_V1' ) ) { /** * Questions REST Controller. */ class LD_REST_Questions_Controller_V1 extends WP_REST_Controller { /** * Register the routes for the objects of the controller. */ public function register_routes() { $version = '1'; $namespace = LEARNDASH_REST_API_NAMESPACE . '/v' . $version; $base = 'sfwd-questions'; register_rest_route( $namespace, '/' . $base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'permissions_check' ), 'args' => array(), ), ) ); register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'permissions_check' ), 'args' => array( 'id' => array( 'required' => true, 'validate_callback' => function( $param, $request, $key ) { return is_numeric( $param ); }, 'sanitize_callback' => 'absint', ), ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'permissions_check' ), 'args' => array( 'id' => array( 'required' => true, 'validate_callback' => function( $param, $request, $key ) { return is_numeric( $param ); }, 'sanitize_callback' => 'absint', ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'permissions_check' ), 'args' => array( 'id' => array( 'required' => true, 'validate_callback' => function( $param, $request, $key ) { return is_numeric( $param ); }, 'sanitize_callback' => 'absint', ), ), ), ) ); } /** * Check if a given request has access manage the item. * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|bool */ public function permissions_check( $request ) { $params = $request->get_params(); $question_id = $params['id']; return current_user_can( 'edit_post', $question_id ); } /** * Get a question items * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { $data = []; return new WP_REST_Response( $data, 200 ); } /** * Get a question item * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { $params = $request->get_params(); $question_id = $params['id']; $data = $this->get_question_data( $question_id ); return new WP_REST_Response( $data, 200 ); } /** * Delete one item from the collection * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Request */ public function delete_item( $request ) { $params = $request->get_params(); $question_id = $params['id']; $question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true ); $question_mapper = new \WpProQuiz_Model_QuestionMapper(); if ( false !== $question_mapper->delete( $question_pro_id ) && false !== wp_delete_post( $params['id'], false ) ) { return new WP_REST_Response( true, 200 ); } return new WP_Error( 'cant-delete', sprintf( // translators: placeholder: Question label. esc_html_x( 'Could not delete the %s.', 'placeholder: Question label', 'learndash' ), \LearnDash_Custom_Label::get_label( 'question' ) ), array( 'status' => 500 ) ); } /** * Update one item from the collection * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Request */ public function update_item( $request ) { $params = $request->get_params(); $question_id = $params['id']; $question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true ); $question_mapper = new \WpProQuiz_Model_QuestionMapper(); $question_model = $question_mapper->fetch( $question_pro_id ); // Update answer data if available. if ( isset( $params['_answerData'] ) && is_string( $params['_answerData'] ) ) { $params['_answerData'] = json_decode( $params['_answerData'], true ); } // Also save points at question's post meta data. if ( isset( $params['_points'] ) ) { update_post_meta( $question_id, 'question_points', $params['_points'] ); } // Update question's post content. if ( isset( $params['_question'] ) ) { wp_update_post( [ 'ID' => $question_id, 'post_content' => wp_slash( $params['_question'] ), ] ); } // Update the question object with new data. $question_model->set_array_to_object( $params ); // Save the new data to database. $question_mapper->save( $question_model ); if ( true ) { return new WP_REST_Response( $this->get_question_data( $question_id ), 200 ); } return new WP_Error( 'cant-delete', sprintf( esc_html__( 'Could not update the %s.', 'learndash' ), \LearnDash_Custom_Label::get_label( 'question' ) ), array( 'status' => 500 ) ); } /** * Get question data. * * @param int $question_id The question ID. * @return object */ public function get_question_data( $question_id ) { // Get Answers from Question. $question_pro_id = (int) get_post_meta( $question_id, 'question_pro_id', true ); $question_mapper = new \WpProQuiz_Model_QuestionMapper(); if ( ! empty( $question_pro_id ) ) { $question_model = $question_mapper->fetch( $question_pro_id ); } else { $question_model = $question_mapper->fetch( null ); } // Get data as array. $question_data = $question_model->get_object_as_array(); $answer_data = []; // Get answer data. foreach ( $question_data['_answerData'] as $answer ) { $answer_data[] = $answer->get_object_as_array(); } unset( $question_data['_answerData'] ); $question_data['_answerData'] = $answer_data; // Generate output object. $data = array_merge( $question_data, [ 'question_id' => $question_id, 'question_post_title' => get_the_title( $question_id ), ] ); return $data; } } } class-ld-rest-groups-leaders-controller.php 0000666 00000024372 15214246251 0015063 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Groups_Leaders_Controller_V1' ) ) && ( class_exists( 'LD_REST_Users_Controller_V1' ) ) ) { class LD_REST_Groups_Leaders_Controller_V1 extends LD_REST_Users_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'exclude', 'include' => 'include', 'offset' => 'offset', 'order' => 'order', 'page' => 'paged', 'per_page' => 'number', 'search' => 'search', 'roles' => 'role__in', 'slug' => 'nicename__in', ); public function __construct( ) { parent::__construct( ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'groups' ); } public function register_routes() { $this->meta = new WP_REST_User_Meta_Fields(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/leaders', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Group ID to enroll group leader into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_groups_leaders' ), 'permission_callback' => array( $this, 'get_groups_leaders_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_groups_leaders' ), 'permission_callback' => array( $this, 'update_groups_leaders_permissions_check' ), 'args' => array( 'user_ids' => array( 'description' => esc_html__( 'Group Leader User IDs to enroll into Group.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_groups_leaders' ), 'permission_callback' => array( $this, 'delete_groups_leaders_permissions_check' ), 'args' => array( 'user_ids' => array( 'description' => esc_html__( 'Group Leader User IDs to remove from Group.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function get_groups_leaders_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_groups_leaders_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function delete_groups_leaders_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_groups_leaders( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $user_ids = $request['user_ids']; if ( ( !is_array( $user_ids ) ) || ( empty( $user_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Group Leader User IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $user_ids = array_map( 'intval', $user_ids ); } foreach( $user_ids as $user_id ) { ld_update_leader_group_access( $user_id, $group_id, false ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function delete_groups_leaders( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $user_ids = $request['user_ids']; if ( ( !is_array( $user_ids ) ) || ( empty( $user_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Group Leader User IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $user_ids = array_map( 'intval', $user_ids ); } foreach( $user_ids as $user_id ) { ld_update_leader_group_access( $user_id, $group_id, true ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } public function get_groups_leaders( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $prepared_args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $prepared_args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $prepared_args[ $wp_param ] = $request[ $api_param ]; } } if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) { $prepared_args['offset'] = $request['offset']; } else { $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; } if ( isset( $registered['orderby'] ) ) { $orderby_possibles = array( 'id' => 'ID', 'include' => 'include', 'name' => 'display_name', 'registered_date' => 'registered', 'slug' => 'user_nicename', 'include_slugs' => 'nicename__in', 'email' => 'user_email', 'url' => 'user_url', ); $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; } if ( LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_Admin_User', 'courses_autoenroll_admin_users' ) === 'yes' ) { $exclude_admin = true; } else { $exclude_admin = false; } $group_users = learndash_get_groups_administrator_ids( $group_id ); if ( !empty( $group_users ) ) $prepared_args['include'] = $group_users; else $prepared_args['include'] = array(0); if ( ! empty( $prepared_args['search'] ) ) { $prepared_args['search'] = '*' . $prepared_args['search'] . '*'; } if ( !isset( $prepared_args['fields'] ) ) { $prepared_args['fields'] = array('ID'); } /** * Filters WP_User_Query arguments when querying users via the REST API. * * @link https://developer.wordpress.org/reference/classes/wp_user_query/ * * @since 4.7.0 * * @param array $prepared_args Array of arguments for WP_User_Query. * @param WP_REST_Request $request The current request. */ $prepared_args = apply_filters( 'learndash_rest_groups_leaders_query', $prepared_args, $request ); $query = new WP_User_Query( $prepared_args ); $users = array(); foreach ( $query->results as $user ) { if ( is_a( $user, 'WP_User' ) ) { $data = $this->prepare_item_for_response( $user, $request ); $users[] = $this->prepare_response_for_collection( $data ); } else { $users[] = $user->ID; } } $response = rest_ensure_response( $users ); // Store pagination values for headers then unset for count query. $per_page = (int) $prepared_args['number']; $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); $prepared_args['fields'] = 'ID'; $total_users = $query->get_total(); if ( $total_users < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $prepared_args['number'], $prepared_args['offset'] ); $count_query = new WP_User_Query( $prepared_args ); $total_users = $count_query->get_total(); } $response->header( 'X-WP-Total', (int) $total_users ); $max_pages = ceil( $total_users / $per_page ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __('Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[$external_key] ) ) { $query_params[$external_key] = $query_params_default[$external_key]; } } return $query_params; } // End of functions } } class-ld-rest-courses-groups-controller.php 0000666 00000027541 15214246251 0015130 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Courses_Groups_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Courses_Groups_Controller_V1 extends LD_REST_Posts_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'post__not_in', 'include' => 'post__in', 'offset' => 'offset', 'order' => 'order', 'orderby' => 'orderby', 'per_page' => 'posts_per_page', 'page' => 'paged', 'search' => 's', 'fields' => 'fields' ); public function __construct( $post_type = '' ) { $this->post_type = 'groups'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'sfwd-courses' ); } public function register_routes() { $this->register_fields(); parent::register_routes_wpv2( ); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/groups', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Course ID to enroll into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_courses_groups' ), 'permission_callback' => array( $this, 'get_courses_groups_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_courses_groups' ), 'permission_callback' => array( $this, 'update_courses_groups_permissions_check' ), 'args' => array( 'group_ids' => array( 'description' => esc_html__( 'Group IDs to enroll into Course.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_courses_groups' ), 'permission_callback' => array( $this, 'delete_courses_groups_permissions_check' ), 'args' => array( 'group_ids' => array( 'description' => esc_html__( 'Group IDs to remove from Course.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function get_courses_groups_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_courses_groups_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function delete_courses_groups_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_courses_groups( $request ) { $course_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $group_ids = $request['group_ids']; if ( ( !is_array( $group_ids ) ) || ( empty( $group_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Group IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $group_ids = array_map( 'intval', $group_ids ); } foreach( $group_ids as $group_id ) { ld_update_course_group_access( $course_id, $group_id, false ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function delete_courses_groups( $request ) { $course_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $group_ids = $request['group_ids']; if ( ( !is_array( $group_ids ) ) || ( empty( $group_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Group IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $group_ids = array_map( 'intval', $group_ids ); } foreach( $group_ids as $group_id ) { ld_update_course_group_access( $course_id, $group_id, true ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function get_courses_groups( $request ) { $course_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Ensure a search string is set in case the orderby is set to 'relevance'. if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.', 'learndash' ), array( 'status' => 400 ) ); } // Ensure an include parameter is set in case the orderby is set to 'include'. if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.', 'learndash' ), array( 'status' => 400 ) ); } // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $args[ $wp_param ] = $request[ $api_param ]; } } // Check for & assign any parameters which require special handling or setting. $args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $registered['before'], $request['before'] ) ) { $args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $registered['after'], $request['after'] ) ) { $args['date_query'][0]['after'] = $request['after']; } // Ensure our per_page parameter overrides any provided posts_per_page filter. if ( isset( $registered['per_page'] ) ) { $args['posts_per_page'] = $request['per_page']; } // Force the post_type argument, since it's not a user input variable. $args['post_type'] = $this->post_type; $args['post__in'] = array(0); $group_ids = learndash_get_course_groups( $course_id, true ); if ( !empty( $group_ids ) ) { $args['post__in'] = $group_ids; } if ( !isset( $args['fields'] ) ) $args['fields'] = 'ids'; else if ( $args['fields'] != 'ids' ) unset( $args['fields'] ); /** * Filters the query arguments for a request. * * Enables adding extra arguments or setting defaults for a post collection request. * * @since 4.7.0 * * @link https://developer.wordpress.org/reference/classes/wp_query/ * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request used. */ $args = apply_filters( 'learndash_rest_courses_groups_query', $args, $request ); $query_args = $this->prepare_items_query( $args, $request ); //error_log('query_args<pre>'. print_r($query_args, true) .'</pre>'); $posts_query = new WP_Query(); $query_result = $posts_query->query( $query_args ); //error_log('query_result<pre>'. print_r($query_result, true) .'</pre>'); // Allow access to all password protected posts if the context is edit. if ( 'edit' === $request['context'] ) { add_filter( 'post_password_required', '__return_false' ); } if ( ( !isset( $args['fields'] ) ) || ( $args['fields'] == 'post' ) ) { $posts = array(); foreach ( $query_result as $post ) { if ( ! $this->check_read_permission( $post ) ) { continue; } $data = $this->prepare_item_for_response( $post, $request ); $posts[] = $this->prepare_response_for_collection( $data ); } $response = rest_ensure_response( $posts ); } else { //$data = $query_result; $response = rest_ensure_response( $query_result ); } // Reset filter. if ( 'edit' === $request['context'] ) { remove_filter( 'post_password_required', '__return_false' ); } $page = (int) $query_args['paged']; $total_posts = $posts_query->found_posts; if ( $total_posts < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; } $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); if ( $page > $max_pages && $total_posts > 0 ) { return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'learndash' ), array( 'status' => 400 ) ); } $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $request_params = $request->get_query_params(); $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); //error_log('query_params_default<pre>'. print_r($query_params_default, true) .'</pre>'); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __('Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[$external_key] ) ) { $query_params[$external_key] = $query_params_default[$external_key]; } } return $query_params; } // End of functions } } class-ld-rest-courses-controller.php 0000666 00000012076 15214246251 0013610 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Courses_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Courses_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'sfwd-courses' ); } public function register_routes() { $this->register_fields(); parent::register_routes_wpv2(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Unique identifier for the object.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => $get_item_args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'args' => array( 'force' => array( 'type' => 'boolean', 'default' => false, 'description' => esc_html__( 'Whether to bypass trash and force deletion.', 'learndash' ), ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); include( LEARNDASH_REST_API_DIR . '/'. $this->version.'/class-ld-rest-courses-steps-controller.php' ); $this->sub_controllers['class-ld-rest-courses-steps-controller'] = new LD_REST_Courses_Steps_Controller_V1(); $this->sub_controllers['class-ld-rest-courses-steps-controller']->register_routes(); include( LEARNDASH_REST_API_DIR . '/'. $this->version.'/class-ld-rest-courses-users-controller.php' ); $this->sub_controllers['class-ld-rest-courses-users-controller'] = new LD_REST_Courses_Users_Controller_V1(); $this->sub_controllers['class-ld-rest-courses-users-controller']->register_routes(); include( LEARNDASH_REST_API_DIR . '/'. $this->version.'/class-ld-rest-courses-groups-controller.php' ); $this->sub_controllers['class-ld-rest-courses-groups-controller'] = new LD_REST_Courses_Groups_Controller_V1(); $this->sub_controllers['class-ld-rest-courses-groups-controller']->register_routes(); } function rest_prepare_response_filter( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) { $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); $links = array(); if ( ! isset( $response->links['steps'] ) ) { $links['steps'] = array( 'href' => rest_url( trailingslashit( $base ) . $post->ID ) .'/steps', 'embeddable' => true, ); } if ( ! isset( $response->links['users'] ) ) { $links['users'] = array( 'href' => rest_url( trailingslashit( $base ) . $post->ID ) .'/users', 'embeddable' => true, ); } if ( ! isset( $response->links['groups'] ) ) { $links['groups'] = array( 'href' => rest_url( trailingslashit( $base ) . $post->ID ) .'/groups', 'embeddable' => true, ); } if ( !empty( $links ) ) { $response->add_links( $links ); } return $response; } // End of functions. } } class-ld-rest-quizzes-controller.php 0000666 00000031017 15214246251 0013633 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Quizzes_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Quizzes_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-quiz'; parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', $this->post_type ); } public function register_routes() { $this->register_fields(); parent::register_routes_wpv2(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } // Quiz Default. register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Unique identifier for the Quiz object.', 'learndash' ), 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => $get_item_args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'args' => array( 'force' => array( 'type' => 'boolean', 'default' => false, 'description' => esc_html__( 'Whether to bypass trash and force deletion.', 'learndash' ), ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); } function rest_collection_params_filter( $query_params, $post_type ) { $query_params = parent::rest_collection_params_filter( $query_params, $post_type ); if ( ! isset( $query_params['course'] ) ) { $query_params['course'] = array( 'description' => sprintf( // translators: placeholder: course. esc_html_x( 'Limit results to be within a specific %s. Required for non-admin users.', 'placeholder: course', 'learndash' ), LearnDash_Custom_Label::get_label( 'course' ) ), 'type' => 'integer', ); } if ( ! isset( $query_params['lesson'] ) ) { $query_params['lesson'] = array( 'description' => sprintf( // translators: placeholder: lesson, course, quizzes. esc_html_x( 'Limit results to be within a specific %1$s. Pass zero to show global %2$s %3$s. Must be used with course parameter.', 'placeholder: lesson, course, quizzes', 'learndash' ), LearnDash_Custom_Label::get_label( 'lesson' ), LearnDash_Custom_Label::get_label( 'course' ), LearnDash_Custom_Label::get_label( 'quizzes' ) ), 'type' => 'integer', ); } if ( ! isset( $query_params['topic'] ) ) { $query_params['topic'] = array( 'description' => sprintf( // translators: placeholder: topic. esc_html_x( 'Limit results to be within a specific %s. Must be used with course parameter.', 'placeholder: topic', 'learndash' ), LearnDash_Custom_Label::get_label( 'topic' ) ), 'type' => 'integer', ); } return $query_params; } function get_item_permissions_check( $request ) { $return = parent::get_item_permissions_check( $request ); if ( ( true === $return ) && ( ! learndash_is_admin_user() ) ) { $course_id = (int) $request['course']; // If we don't have a course parameter we need to get all the courses the user has access to and all // the courses the lesson is avaiable in and compare. if ( empty( $course_id ) ) { $user_enrolled_courses = learndash_user_get_enrolled_courses( get_current_user_id() ); if ( empty( $user_enrolled_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $step_courses = learndash_get_courses_for_step( $request['id'], true ); if ( empty( $step_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $user_enrolled_courses = array_intersect( $user_enrolled_courses, array_keys( $step_courses ) ); if ( empty( $user_enrolled_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } else { // But if the course parameter is provided we need to check the user has access and also // check the step is part of that course. $this->course_post = get_post( $course_id ); if ( ( ! $this->course_post ) || ( ! is_a( $this->course_post, 'WP_Post' ) ) || ( 'sfwd-courses' !== $this->course_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( ! sfwd_lms_has_access( $this->course_post->ID ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $this->ld_course_steps_object = LDLMS_Factory_Post::course_steps( $this->course_post->ID ); $this->ld_course_steps_object->load_steps(); $lesson_ids = $this->ld_course_steps_object->get_children_steps( $this->course_post->ID, $this->post_type ); if ( empty( $lesson_ids ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } if ( ! in_array( $request['id'], $lesson_ids ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } } return $return; } function get_item( $request ) { return parent::get_item( $request ); } function get_items_permissions_check( $request ) { $return = parent::get_items_permissions_check( $request ); if ( ( true === $return ) && ( 'view' === $request['context'] ) ) { $course_id = (int) $request['course']; if ( ! empty( $course_id ) ) { $this->course_post = get_post( $course_id ); if ( ( ! $this->course_post ) || ( ! is_a( $this->course_post, 'WP_Post' ) ) || ( 'sfwd-courses' !== $this->course_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } } $lesson_id = (int) $request['lesson']; if ( ! empty( $lesson_id ) ) { $this->lesson_post = get_post( $lesson_id ); if ( ( ! $this->lesson_post ) || ( ! is_a( $this->lesson_post, 'WP_Post' ) ) || ( 'sfwd-lessons' !== $this->lesson_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Lesson ID.', 'learndash' ), array( 'status' => 404 ) ); } } $topic_id = (int) $request['topic']; if ( ! empty( $topic_id ) ) { $this->topic_post = get_post( $topic_id ); if ( ( ! $this->topic_post ) || ( ! is_a( $this->topic_post, 'WP_Post' ) ) || ( 'sfwd-topic' !== $this->topic_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Topic ID.', 'learndash' ), array( 'status' => 404 ) ); } } if ( ! learndash_is_admin_user() ) { if ( $this->topic_post ) { if ( ! $this->course_post ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( ! $this->lesson_post ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } } if ( $this->lesson_post ) { if ( ! $this->course_post ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } } if ( ( $this->course_post ) && ( ! sfwd_lms_has_access( $this->course_post->ID ) ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } } return $return; } function get_items( $request ) { return parent::get_items( $request ); } function rest_query_filter( $args, $request ) { $step_ids = array(); // The course_post should be set in the local method get_items_permissions_check(). if ( ( $this->course_post ) && ( is_a( $this->course_post, 'WP_Post' ) ) && ( 'sfwd-courses' === $this->course_post->post_type ) ) { if ( $this->topic_post ) { $step_ids = learndash_course_get_children_of_step( $this->course_post->ID, $this->topic_post->ID, $this->post_type ); } else if ( $this->lesson_post ) { $step_ids = learndash_course_get_children_of_step( $this->course_post->ID, $this->lesson_post->ID, $this->post_type ); } else if ( $this->course_post ) { //$global_quizzes = $request['global']; //if ( $global_quizzes == "true" ) { $lesson_id = $request['lesson']; //error_log('lesson_id=['. $lesson_id .']'); if ( 0 === $lesson_id ) { $step_ids = learndash_course_get_children_of_step( $this->course_post->ID, $this->course_post->ID, $this->post_type ); } else { $step_ids = learndash_course_get_steps_by_type( $this->course_post->ID, $this->post_type ); } } if ( ! empty( $step_ids ) ) { $args['post__in'] = $args['post__in'] ? array_intersect( $step_ids, $args['post__in'] ) : $step_ids; $course_lessons_args = learndash_get_course_lessons_order( $this->course_post->ID ); if ( !isset( $_GET['orderby'] ) ) { if ( isset( $course_lessons_args['orderby'] ) ) $args['orderby'] = $course_lessons_args['orderby']; else $args['orderby'] = 'title'; } if ( !isset( $_GET['order'] ) ) { if ( isset( $course_lessons_args['order'] ) ) $args['order'] = $course_lessons_args['order']; else $args['order'] = 'ASC'; } } else { $args['post__in'] = array(0); } } else { if ( get_current_user_id() ) { /** * If the user is logged in they can see all GLOBAL quizzes or those not * associated with a course. */ $step_ids = learndash_get_non_course_qizzes(); } else { /** * If the user is NOT logged in they can see all OPEN quizzes or those not * associated with a course AND allowed to be viewed by non-logged in users. */ $step_ids = learndash_get_open_quizzes(true); } if ( ! empty( $step_ids ) ) { $args['post__in'] = $args['post__in'] ? array_intersect( $step_ids, $args['post__in'] ) : $step_ids; } } return $args; } // End of functions } } class-ld-rest-users-course-progress-controller.php 0000666 00000035273 15214246251 0016432 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Users_Course_Progress_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Users_Course_Progress_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'users' ); } /** * Registers the routes for the objects of the controller. * * @since 4.7.0 * * @see register_rest_route() */ public function register_routes() { $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/course-progress/', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'User ID to show course progress', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => 'GET', 'callback' => array( $this, 'get_users_progress' ), 'permission_callback' => array( $this, 'get_users_progress_permissions_check' ), 'args' => $this->get_collection_params(), ), ) ); /* register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/course-progress/(?P<course_id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'User ID to enroll user into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), 'course_id' => array( 'description' => esc_html__( 'Course ID to enroll.', 'learndash' ), 'required' => false, 'items' => array( 'type' => 'integer', ), ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'set_items' ), ), ) ); */ } function get_users_progress_permissions_check( $request ) { $user_id = $request['id']; if ( learndash_is_admin_user() ) { return true; } else if ( get_current_user_id() === $user_id ) { return true; } else if ( learndash_is_group_leader_user() ) { if ( learndash_is_group_leader_of_user( get_current_user_id(), $user_id ) ) { return true; } } } function get_users_progress( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_user_invalid_id', esc_html__( 'Invalid user ID. #1', 'learndash' ), array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; $data = array(); $user_course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true ); $user_course_progress = !empty( $user_course_progress ) ? $user_course_progress : array(); $courses_registered = ld_get_mycourses( $user_id ); $courses_registered = !empty( $courses_registered ) ? $courses_registered : array(); $user_course_ids = array_keys( $user_course_progress ); $user_course_ids = array_merge( $user_course_ids, $courses_registered ); $user_course_ids = array_unique( $user_course_ids ); if ( ( !empty( $user_course_ids ) ) && ( learndash_is_group_leader_user() ) ) { $gl_groups_corses = learndash_get_group_leader_groups_courses( get_current_user_id() ); error_log('gl_groups_corses<pre>'. print_r($gl_groups_corses, true) .'</pre>'); if ( !empty( $gl_groups_corses ) ) { $user_course_ids = array_intersect( $gl_groups_corses, $user_course_ids ); } } if ( !empty( $user_course_ids ) ) { // Ensure a search string is set in case the orderby is set to 'relevance'. if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.', 'learndash' ), array( 'status' => 400 ) ); } // Ensure an include parameter is set in case the orderby is set to 'include'. if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.', 'learndash' ), array( 'status' => 400 ) ); } // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $args = array(); /* * This array defines mappings between public API query parameters whose * values are accepted as-passed, and their internal WP_Query parameter * name equivalents (some are the same). Only values which are also * present in $registered will be set. */ $parameter_mappings = array( 'author' => 'author__in', 'author_exclude' => 'author__not_in', 'exclude' => 'post__not_in', 'include' => 'post__in', 'menu_order' => 'menu_order', 'offset' => 'offset', 'order' => 'order', 'orderby' => 'orderby', 'page' => 'paged', 'parent' => 'post_parent__in', 'parent_exclude' => 'post_parent__not_in', 'search' => 's', 'slug' => 'post_name__in', 'status' => 'post_status', ); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $args. */ foreach ( $parameter_mappings as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $args[ $wp_param ] = $request[ $api_param ]; } } // Check for & assign any parameters which require special handling or setting. $args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $registered['before'], $request['before'] ) ) { $args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $registered['after'], $request['after'] ) ) { $args['date_query'][0]['after'] = $request['after']; } // Ensure our per_page parameter overrides any provided posts_per_page filter. if ( isset( $registered['per_page'] ) ) { $args['posts_per_page'] = $request['per_page']; } if ( isset( $registered['sticky'], $request['sticky'] ) ) { $sticky_posts = get_option( 'sticky_posts', array() ); if ( ! is_array( $sticky_posts ) ) { $sticky_posts = array(); } if ( $request['sticky'] ) { /* * As post__in will be used to only get sticky posts, * we have to support the case where post__in was already * specified. */ $args['post__in'] = $args['post__in'] ? array_intersect( $sticky_posts, $args['post__in'] ) : $sticky_posts; /* * If we intersected, but there are no post ids in common, * WP_Query won't return "no posts" for post__in = array() * so we have to fake it a bit. */ if ( ! $args['post__in'] ) { $args['post__in'] = array( 0 ); } } elseif ( $sticky_posts ) { /* * As post___not_in will be used to only get posts that * are not sticky, we have to support the case where post__not_in * was already specified. */ $args['post__not_in'] = array_merge( $args['post__not_in'], $sticky_posts ); } } // Force the post_type argument, since it's not a user input variable. $args['post_type'] = $this->post_type; $args['post__in'] = $user_course_ids; $args['fields'] = 'ids'; /** * Filters the query arguments for a request. * * Enables adding extra arguments or setting defaults for a post collection request. * * @since 4.7.0 * * @link https://developer.wordpress.org/reference/classes/wp_query/ * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request used. */ $args = apply_filters( "learndash_rest_users_course_progress_query", $args, $request ); $query_args = $this->prepare_items_query( $args, $request ); $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); foreach ( $taxonomies as $taxonomy ) { $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; $tax_exclude = $base . '_exclude'; if ( ! empty( $request[ $base ] ) ) { $query_args['tax_query'][] = array( 'taxonomy' => $taxonomy->name, 'field' => 'term_id', 'terms' => $request[ $base ], 'include_children' => false, ); } if ( ! empty( $request[ $tax_exclude ] ) ) { $query_args['tax_query'][] = array( 'taxonomy' => $taxonomy->name, 'field' => 'term_id', 'terms' => $request[ $tax_exclude ], 'include_children' => false, 'operator' => 'NOT IN', ); } } error_log('query_args<pre>'. print_r($query_args, true) .'</pre>'); $posts_query = new WP_Query(); $query_result = $posts_query->query( $query_args ); error_log('query_result<pre>'. print_r($query_result, true) .'</pre>'); // Allow access to all password protected posts if the context is edit. if ( 'edit' === $request['context'] ) { add_filter( 'post_password_required', '__return_false' ); } foreach ( $query_result as $course_id ) { $data[$course_id] = array(); if ( isset( $user_course_progress[$course_id] ) ) { $converted = $this->user_meta_progress_normalized( $user_course_progress[$course_id] ); } else { $converted = array(); } $ld_course_steps_object = LDLMS_Factory_Post::course_steps( intval( $course_id ) ); $ld_course_steps_object->load_steps(); $course_steps_l = $ld_course_steps_object->get_steps( 'l' ); if ( !empty( $course_steps_l ) ) { foreach( $course_steps_l as $step_key ) { list( $step_type, $step_id ) = explode( ':', $step_key ); if ( isset( $converted[$step_key] ) ) { $completed = $converted[$step_key]; } else { $completed = 0; } $data[$course_id][$step_id] = $completed; } } } // Reset filter. if ( 'edit' === $request['context'] ) { remove_filter( 'post_password_required', '__return_false' ); } $page = (int) $query_args['paged']; $total_posts = $posts_query->found_posts; if ( $total_posts < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; } $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); if ( $page > $max_pages && $total_posts > 0 ) { return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'learndash' ), array( 'status' => 400 ) ); } $response = rest_ensure_response( $data ); $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $request_params = $request->get_query_params(); $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } } return $response; } function user_meta_progress_normalized( $progress = array() ) { $converted = array(); if ( ( isset( $progress['lessons'] ) ) && ( !empty( $progress['lessons'] ) ) ) { foreach( $progress['lessons'] as $lesson_id => $lesson_complete ) { $converted['sfwd-lessons:' . $lesson_id] = $lesson_complete; if ( ( isset( $progress['topics'][$lesson_id] ) ) && ( !empty( $progress['topics'][$lesson_id] ) ) ) { foreach( $progress['topics'][$lesson_id] as $topic_id => $topic_complete ) { $converted['sfwd-topic:' . $topic_id] = $topic_complete; } } } } //error_log('converted<pre>'. print_r($converted, true) .'</pre>'); return $converted; } /* function set_items( $request ) { $data = array(); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } */ /* function lesson_mark_complete( $request ) { $course_id = $request['course']; $lesson_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id_X', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( empty( $lesson_id ) ) { return new WP_Error( 'rest_post_invalid_id_Y', esc_html__( 'Invalid Lesson ID.', 'learndash' ), array( 'status' => 404 ) ); } $current_user_id = get_current_user_id(); if ( empty( $current_user_id ) ) { return new WP_Error( 'rest_not_logged_in', esc_html__( 'You are not currently logged in.', 'learndash' ), array( 'status' => 401 ) ); } //$current_user = wp_get_current_user(); $has_access = sfwd_lms_has_access( $course->ID, $current_user->ID ); if ( ( ! $has_access ) && ( $course_price_type != 'open' ) ) { return new WP_Error( 'rest_cannot_view', esc_html__( 'Sorry, you are not allowed view items.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $return = learndash_process_mark_complete( $current_user_id, $lesson_id ); if ( $return === true ) { $data = array( 'completed_status' => true, 'completed_date_gmt' => $this->prepare_date_response( current_time( 'mysql' ) ) ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } } */ } } class-ld-rest-groups-courses-controller.php 0000666 00000026643 15214246251 0015132 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Groups_Courses_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Groups_Courses_Controller_V1 extends LD_REST_Posts_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'post__not_in', 'include' => 'post__in', 'offset' => 'offset', 'order' => 'order', 'orderby' => 'orderby', 'per_page' => 'posts_per_page', 'page' => 'paged', 'search' => 's', 'fields' => 'fields' ); public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'groups' ); } public function register_routes() { $this->register_fields(); parent::register_routes_wpv2(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/courses', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Group ID to enroll into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_groups_courses' ), 'permission_callback' => array( $this, 'get_groups_courses_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_groups_courses' ), 'permission_callback' => array( $this, 'update_groups_courses_permissions_check' ), 'args' => array( 'course_ids' => array( 'description' => esc_html__( 'Course IDs to enroll into Group.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_groups_courses' ), 'permission_callback' => array( $this, 'delete_groups_courses_permissions_check' ), 'args' => array( 'course_ids' => array( 'description' => esc_html__( 'Course IDs to remove from Group.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function get_groups_courses_permissions_check( $request ) { if ( learndash_is_admin_user() ) { return true; } } function update_groups_courses_permissions_check( $request ) { if ( learndash_is_admin_user() ) { return true; } } function update_groups_courses( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $course_ids = $request['course_ids']; if ( ( ! is_array( $course_ids ) ) || ( empty( $course_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Course IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $course_ids = array_map( 'intval', $course_ids ); } foreach ( $course_ids as $course_id ) { ld_update_course_group_access( $course_id, $group_id, false ); } $data = array(); // Create the response object. $response = rest_ensure_response( $data ); // Add a custom status code. $response->set_status( 200 ); return $response; } function delete_groups_courses_permissions_check( $request ) { if ( learndash_is_admin_user() ) { return true; } } function delete_groups_courses( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ), array( 'status' => 404 ) ); } $course_ids = $request['course_ids']; if ( ( !is_array( $course_ids ) ) || ( empty( $course_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Course ID.', 'learndash' ), array( 'status' => 404 ) ); } else { $course_ids = array_map( 'intval', $course_ids ); } foreach ( $course_ids as $course_id ) { ld_update_course_group_access( $course_id, $group_id, true ); } $data = array(); // Create the response object. $response = rest_ensure_response( $data ); // Add a custom status code. $response->set_status( 200 ); return $response; } function get_groups_courses( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Ensure a search string is set in case the orderby is set to 'relevance'. if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.', 'learndash' ), array( 'status' => 400 ) ); } // Ensure an include parameter is set in case the orderby is set to 'include'. if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.', 'learndash' ), array( 'status' => 400 ) ); } // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $args[ $wp_param ] = $request[ $api_param ]; } } // Check for & assign any parameters which require special handling or setting. $args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $registered['before'], $request['before'] ) ) { $args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $registered['after'], $request['after'] ) ) { $args['date_query'][0]['after'] = $request['after']; } // Ensure our per_page parameter overrides any provided posts_per_page filter. if ( isset( $registered['per_page'] ) ) { $args['posts_per_page'] = $request['per_page']; } // Force the post_type argument, since it's not a user input variable. $args['post_type'] = $this->post_type; $group_course_ids = learndash_group_enrolled_courses( $group_id ); if ( ! empty( $group_course_ids ) ) { $args['post__in'] = $group_course_ids; } else { $args['post__in'] = array( 0 ); } if ( ! isset( $args['fields'] ) ) { $args['fields'] = 'ids'; } else if ( $args['fields'] != 'ids' ) { unset( $args['fields'] ); } /** * Filters the query arguments for a request. * * Enables adding extra arguments or setting defaults for a post collection request. * * @since 4.7.0 * * @link https://developer.wordpress.org/reference/classes/wp_query/ * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request used. */ $args = apply_filters( 'learndash_rest_groups_courses_query', $args, $request ); $query_args = $this->prepare_items_query( $args, $request ); $posts_query = new WP_Query(); $query_result = $posts_query->query( $query_args ); // Allow access to all password protected posts if the context is edit. if ( 'edit' === $request['context'] ) { add_filter( 'post_password_required', '__return_false' ); } if ( ( ! isset( $args['fields'] ) ) || ( $args['fields'] == 'post' ) ) { $posts = array(); foreach ( $query_result as $post ) { if ( ! $this->check_read_permission( $post ) ) { continue; } $data = $this->prepare_item_for_response( $post, $request ); $posts[] = $this->prepare_response_for_collection( $data ); } $response = rest_ensure_response( $posts ); } else { $response = rest_ensure_response( $query_result ); } // Reset filter. if ( 'edit' === $request['context'] ) { remove_filter( 'post_password_required', '__return_false' ); } $page = (int) $query_args['paged']; $total_posts = $posts_query->found_posts; if ( $total_posts < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; } $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); if ( $page > $max_pages && $total_posts > 0 ) { return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'learndash' ), array( 'status' => 400 ) ); } $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $request_params = $request->get_query_params(); $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __('Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[ $external_key ] ) ) { $query_params[$external_key] = $query_params_default[ $external_key ]; } } return $query_params; } // End of functions. } } class-ld-rest-users-courses-controller.php 0000666 00000027350 15214246251 0014750 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Users_Courses_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Users_Courses_Controller_V1 extends LD_REST_Posts_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'post__not_in', 'include' => 'post__in', 'offset' => 'offset', 'order' => 'order', 'orderby' => 'orderby', 'per_page' => 'posts_per_page', 'page' => 'paged', 'search' => 's', 'fields' => 'fields' ); public function __construct( ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'users' ); } /** * Registers the routes for the objects of the controller. * * @since 4.7.0 * * @see register_rest_route() */ public function register_routes() { $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/courses/', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'User ID to enroll user into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_user_courses' ), 'permission_callback' => array( $this, 'get_user_courses_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_user_courses' ), 'permission_callback' => array( $this, 'update_user_courses_permissions_check' ), 'args' => array( 'course_ids' => array( 'description' => esc_html__( 'Courses IDs to add to User.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_user_courses' ), 'permission_callback' => array( $this, 'delete_user_courses_permissions_check' ), 'args' => array( 'course_ids' => array( 'description' => esc_html__( 'Course IDs to remove from User.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function get_user_courses_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } else if ( get_current_user_id() == $request['id'] ) { return true; } } function update_user_courses_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } else if ( get_current_user_id() == $request['id'] ) { return true; } } function delete_user_courses_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } else if ( get_current_user_id() == $request['id'] ) { return true; } } function update_user_courses( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid User ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $course_ids = $request['course_ids']; if ( ( !is_array( $course_ids ) ) || ( empty( $course_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Course IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $course_ids = array_map( 'intval', $course_ids ); } foreach( $course_ids as $course_id ) { ld_update_course_access( $user_id, $course_id, false ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function delete_user_courses( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid User ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $course_ids = $request['course_ids']; if ( ( !is_array( $course_ids ) ) || ( empty( $course_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing Course IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $course_ids = array_map( 'intval', $course_ids ); } foreach( $course_ids as $course_id ) { ld_update_course_access( $user_id, $course_id, true ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function get_user_courses( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_user_invalid_id', esc_html__( 'Invalid user ID. #1', 'learndash' ), array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Ensure a search string is set in case the orderby is set to 'relevance'. if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.', 'learndash' ), array( 'status' => 400 ) ); } // Ensure an include parameter is set in case the orderby is set to 'include'. if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.', 'learndash' ), array( 'status' => 400 ) ); } // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $args[ $wp_param ] = $request[ $api_param ]; } } // Check for & assign any parameters which require special handling or setting. $args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $registered['before'], $request['before'] ) ) { $args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $registered['after'], $request['after'] ) ) { $args['date_query'][0]['after'] = $request['after']; } // Ensure our per_page parameter overrides any provided posts_per_page filter. if ( isset( $registered['per_page'] ) ) { $args['posts_per_page'] = $request['per_page']; } // Force the post_type argument, since it's not a user input variable. $args['post_type'] = $this->post_type; $args['post__in'] = array(0); $course_ids = learndash_user_get_enrolled_courses( $user_id, array(), true ); if ( !empty( $course_ids ) ) { $args['post__in'] = $course_ids; } if ( !isset( $args['fields'] ) ) $args['fields'] = 'ids'; else if ( $args['fields'] != 'ids' ) unset( $args['fields'] ); /** * Filters the query arguments for a request. * * Enables adding extra arguments or setting defaults for a post collection request. * * @since 4.7.0 * * @link https://developer.wordpress.org/reference/classes/wp_query/ * * @param array $args Key value array of query var to query value. * @param WP_REST_Request $request The request used. */ $args = apply_filters( "learndash_rest_user_courses_query", $args, $request ); $query_args = $this->prepare_items_query( $args, $request ); $posts_query = new WP_Query(); $query_result = $posts_query->query( $query_args ); //error_log('query_result<pre>'. print_r($query_result, true) .'</pre>'); // Allow access to all password protected posts if the context is edit. if ( 'edit' === $request['context'] ) { add_filter( 'post_password_required', '__return_false' ); } if ( ( !isset( $args['fields'] ) ) || ( $args['fields'] == 'post' ) ) { $posts = array(); foreach ( $query_result as $post ) { if ( ! $this->check_read_permission( $post ) ) { continue; } $data = $this->prepare_item_for_response( $post, $request ); $posts[] = $this->prepare_response_for_collection( $data ); } $response = rest_ensure_response( $posts ); } else { //$data = $query_result; $response = rest_ensure_response( $query_result ); } // Reset filter. if ( 'edit' === $request['context'] ) { remove_filter( 'post_password_required', '__return_false' ); } $page = (int) $query_args['paged']; $total_posts = $posts_query->found_posts; if ( $total_posts < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $query_args['paged'] ); $count_query = new WP_Query(); $count_query->query( $query_args ); $total_posts = $count_query->found_posts; } $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); if ( $page > $max_pages && $total_posts > 0 ) { return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.', 'learndash' ), array( 'status' => 400 ) ); } $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $request_params = $request->get_query_params(); $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); //error_log('query_params_default<pre>'. print_r($query_params_default, true) .'</pre>'); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __('Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[$external_key] ) ) { $query_params[$external_key] = $query_params_default[$external_key]; } } return $query_params; } } } class-ld-rest-courses-users-controller.php 0000666 00000024306 15214246251 0014746 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Courses_Users_Controller_V1' ) ) && ( class_exists( 'LD_REST_Users_Controller_V1' ) ) ) { class LD_REST_Courses_Users_Controller_V1 extends LD_REST_Users_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'exclude', 'include' => 'include', 'offset' => 'offset', 'order' => 'order', 'page' => 'paged', 'per_page' => 'number', 'search' => 'search', 'roles' => 'role__in', 'slug' => 'nicename__in', ); public function __construct( ) { parent::__construct( ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'sfwd-courses' ); } public function register_routes() { $this->meta = new WP_REST_User_Meta_Fields(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/users', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Course ID.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_courses_users' ), 'permission_callback' => array( $this, 'get_courses_users_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_courses_users' ), 'permission_callback' => array( $this, 'update_courses_users_permissions_check' ), 'args' => array( 'user_ids' => array( 'description' => esc_html__( 'User IDs to enroll into Course. Limit 50 per request.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_courses_users' ), 'permission_callback' => array( $this, 'delete_courses_users_permissions_check' ), 'args' => array( 'user_ids' => array( 'description' => esc_html__( 'User IDs to remove from Group. Limit 50 per request.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function update_courses_users_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function delete_courses_users_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function get_courses_users_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_courses_users( $request ) { $course_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } $user_ids = $request['user_ids']; if ( ( !is_array( $user_ids ) ) || ( empty( $user_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing User IDs.', 'learndash' ), array( 'status' => 404 ) ); } else { $user_ids = array_map( 'intval', $user_ids ); } foreach( $user_ids as $user_id ) { ld_update_course_access( $user_id, $course_id ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function delete_courses_users( $request ) { $course_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } $user_ids = $request['user_ids']; if ( ( !is_array( $user_ids ) ) || ( empty( $user_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing User IDs.', 'learndash' ), array( 'status' => 404 ) ); } else { $user_ids = array_map( 'intval', $user_ids ); } foreach( $user_ids as $user_id ) { ld_update_course_access( $user_id, $course_id, true ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } public function get_courses_users( $request ) { $course_id = $request['id']; if ( empty( $course_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $prepared_args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $prepared_args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $prepared_args[ $wp_param ] = $request[ $api_param ]; } } if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) { $prepared_args['offset'] = $request['offset']; } else { $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; } if ( isset( $registered['orderby'] ) ) { $orderby_possibles = array( 'id' => 'ID', 'include' => 'include', 'name' => 'display_name', 'registered_date' => 'registered', 'slug' => 'user_nicename', 'include_slugs' => 'nicename__in', 'email' => 'user_email', 'url' => 'user_url', ); $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; } if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_Admin_User', 'courses_autoenroll_admin_users' ) === 'yes' ) { $exclude_admin = true; } else { $exclude_admin = false; } $course_users_query = learndash_get_users_for_course( $course_id, array(), $exclude_admin ); if ( $course_users_query instanceof WP_User_Query ) { $course_user_ids = $course_users_query->get_results(); $prepared_args['include'] = $course_user_ids; } else { $prepared_args['include'] = array( 0 ); } if ( ! empty( $prepared_args['search'] ) ) { $prepared_args['search'] = '*' . $prepared_args['search'] . '*'; } if ( ! isset( $prepared_args['fields'] ) ) { $prepared_args['fields'] = array('ID'); } /** * Filters WP_User_Query arguments when querying users via the REST API. * * @link https://developer.wordpress.org/reference/classes/wp_user_query/ * * @since 4.7.0 * * @param array $prepared_args Array of arguments for WP_User_Query. * @param WP_REST_Request $request The current request. */ $prepared_args = apply_filters( 'learndash_rest_courses_users_query', $prepared_args, $request ); $query = new WP_User_Query( $prepared_args ); $users = array(); foreach ( $query->results as $user ) { if ( is_a( $user, 'WP_User' ) ) { $data = $this->prepare_item_for_response( $user, $request ); $users[] = $this->prepare_response_for_collection( $data ); } else { $users[] = $user->ID; } } $response = rest_ensure_response( $users ); // Store pagination values for headers then unset for count query. $per_page = (int) $prepared_args['number']; $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); $prepared_args['fields'] = 'ID'; $total_users = $query->get_total(); if ( $total_users < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $prepared_args['number'], $prepared_args['offset'] ); $count_query = new WP_User_Query( $prepared_args ); $total_users = $count_query->get_total(); } $response->header( 'X-WP-Total', (int) $total_users ); $max_pages = ceil( $total_users / $per_page ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __( 'Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach ( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[ $external_key ] ) ) { $query_params[ $external_key ] = $query_params_default[ $external_key ]; } } return $query_params; } // End of functions } } class-ld-rest-courses-enroll-controller.php 0000666 00000013147 15214246251 0015101 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Courses_Steps_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Courses_Steps_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Permalinks', 'courses' ); } public function register_routes() { $this->register_fields(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/steps', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Course ID to enroll user into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_course_enrollment' ), 'permission_callback' => array( $this, 'get_course_enrollment_permissions_check' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_course_enrollment' ), 'permission_callback' => array( $this, 'update_course_enrollment_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), ) ); } function get_course_enrollment_permissions_check( $request ) { if ( learndash_is_admin_user() ) { return true; } } function get_course_enrollment( $request ) { $current_user_id = get_current_user_id(); if ( empty( $current_user_id ) ) { return new WP_Error( 'rest_not_logged_in', esc_html__( 'You are not currently logged in.', 'learndash' ), array( 'status' => 401 ) ); } $current_user = wp_get_current_user(); $course = $this->get_post( $request['id'] ); if ( is_wp_error( $course ) ) { return $course; } $ld_course_steps_object = LDLMS_Factory_Post::course_steps( intval( $course->ID ) ); $ld_course_steps_object->load_steps(); $course_steps = $ld_course_steps_object->get_steps( 'h' ); $data = $course_steps; // Create the response object. $response = rest_ensure_response( $data ); // Add a custom status code. $response->set_status( 200 ); return $response; } function update_course_enrollment_permissions_check( $request ) { if ( learndash_is_admin_user() ) { return true; } } function update_course_enrollment( $request ) { $current_user_id = get_current_user_id(); if ( empty( $current_user_id ) ) { return new WP_Error( 'rest_not_logged_in', esc_html__( 'You are not currently logged in.', 'learndash' ), array( 'status' => 401 ) ); } $current_user = wp_get_current_user(); $course = $this->get_post( $request['id'] ); if ( is_wp_error( $course ) ) { return $course; } $ld_course_steps_object = LDLMS_Factory_Post::course_steps( intval( $course->ID ) ); $body = $request->get_body(); if ( !empty( $body ) ) { $body = json_decode( $body, true ); if ( ( $body ) && ( json_last_error() == JSON_ERROR_NONE ) ) { $steps = array(); $steps['sfwd-lessons'] = array(); $steps['sfwd-quiz'] = array(); if ( ( isset( $body['sfwd-lessons'] ) ) && ( ! empty( $body['sfwd-lessons'] ) ) ) { foreach ( $body['sfwd-lessons'] as $lesson_id => $lesson_set ) { $steps['sfwd-lessons'][ $lesson_id ] = array(); $steps['sfwd-lessons'][ $lesson_id ]['sfwd-topic'] = array(); $steps['sfwd-lessons'][ $lesson_id ]['sfwd-quiz'] = array(); if ( ( isset( $lesson_set['sfwd-topic'] ) ) && ( ! empty( $lesson_set['sfwd-topic'] ) ) ) { foreach( $lesson_set['sfwd-topic'] as $topic_id => $topic_set ) { $steps['sfwd-lessons'][ $lesson_id ]['sfwd-topic'][ $topic_id ] = array(); $steps['sfwd-lessons'][ $lesson_id ]['sfwd-topic'][ $topic_id ]['sfwd-quiz'] = array(); if ( ( isset( $topic_set['sfwd-quiz'] ) ) && ( ! empty( $topic_set['sfwd-quiz'] ) ) ) { foreach( $topic_set['sfwd-quiz'] as $quiz_id => $quiz_set ) { $steps['sfwd-lessons'][ $lesson_id ]['sfwd-topic'][ $topic_id ]['sfwd-quiz'][ $quiz_id ] = array(); } } } } if ( ( isset( $lesson_set['sfwd-quiz'] ) ) && ( ! empty( $lesson_set['sfwd-quiz'] ) ) ) { foreach ( $lesson_set['sfwd-quiz'] as $quiz_id => $quiz_set ) { $steps['sfwd-lessons'][ $lesson_id ]['sfwd-quiz'][ $quiz_id ] = array(); } } } } if ( ( isset( $body['sfwd-quiz'] ) ) && ( ! empty( $body['sfwd-quiz'] ) ) ) { $steps['sfwd-quiz'] = $body['sfwd-quiz']; } $ld_course_steps_object->set_steps( $steps ); } } $ld_course_steps_object->load_steps(); $course_steps = $ld_course_steps_object->get_steps( 'h' ); $data = $course_steps; // Create the response object. $response = rest_ensure_response( $data ); // Add a custom status code. $response->set_status( 200 ); return $response; } // End of functions. } } class-ld-rest-lessons-controller.php 0000666 00000021660 15214246251 0013612 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Lessons_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Lessons_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-lessons'; parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', $this->post_type ); } public function register_routes() { parent::register_routes_wpv2(); $this->register_fields(); $collection_params = $this->get_collection_params(); register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Unique identifier for the object.', 'learndash' ), 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => $get_item_args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'args' => array( 'force' => array( 'type' => 'boolean', 'default' => false, 'description' => esc_html__( 'Whether to bypass trash and force deletion.', 'learndash' ), ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); } function rest_collection_params_filter( $query_params, $post_type ) { $query_params = parent::rest_collection_params_filter( $query_params, $post_type ); if ( ! isset( $query_params['course'] ) ) { $query_params['course'] = array( 'description' => sprintf( // translators: placeholder: course. esc_html_x( 'Limit results to be within a specific %s. Required for non-admin users.', 'placeholder: course', 'learndash' ), LearnDash_Custom_Label::get_label( 'course' ) ), 'type' => 'integer', ); } return $query_params; } function get_item_permissions_check( $request ) { $return = parent::get_item_permissions_check( $request ); if ( ( true === $return ) && ( ! learndash_is_admin_user() ) ) { $course_id = (int) $request['course']; // If we don't have a course parameter we need to get all the courses the user has access to and all // the courses the lesson is avaiable in and compare. if ( empty( $course_id ) ) { $user_enrolled_courses = learndash_user_get_enrolled_courses( get_current_user_id() ); if ( empty( $user_enrolled_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $step_courses = learndash_get_courses_for_step( $request['id'], true ); if ( empty( $step_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $user_enrolled_courses = array_intersect( $user_enrolled_courses, array_keys( $step_courses ) ); if ( empty( $user_enrolled_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } else { /** * But if the course parameter is provided we need to check the user has access and * also check the step is part of that course. */ $this->course_post = get_post( $course_id ); if ( ( ! $this->course_post ) || ( ! is_a( $this->course_post, 'WP_Post' ) ) || ( 'sfwd-courses' !== $this->course_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( ! sfwd_lms_has_access( $this->course_post->ID ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $this->ld_course_steps_object = LDLMS_Factory_Post::course_steps( $this->course_post->ID ); $this->ld_course_steps_object->load_steps(); $lesson_ids = $this->ld_course_steps_object->get_children_steps( $this->course_post->ID, $this->post_type ); if ( empty( $lesson_ids ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } if ( ! in_array( $request['id'], $lesson_ids ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } } return $return; } function get_item( $request ) { return parent::get_item( $request ); } function get_items_permissions_check( $request ) { $return = parent::get_items_permissions_check( $request ); if ( ( true === $return ) && ( 'view' === $request['context'] ) ) { $course_id = (int) $request['course']; if ( ! empty( $course_id ) ) { $this->course_post = get_post( $course_id ); if ( ( ! $this->course_post ) || ( ! is_a( $this->course_post, 'WP_Post' ) ) || ( 'sfwd-courses' !== $this->course_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } } if ( ! learndash_is_admin_user() ) { if ( ! $this->course_post ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } else if ( ! sfwd_lms_has_access( $this->course_post->ID ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } } return $return; } function get_items( $request ) { return parent::get_items( $request ); } function rest_query_filter( $args, $request ) { // The course_post should be set in the local method get_items_permissions_check() if ( ( $this->course_post ) && ( is_a( $this->course_post, 'WP_Post' ) ) && ( 'sfwd-courses' === $this->course_post->post_type ) ) { $step_ids = learndash_course_get_steps_by_type( $this->course_post->ID, $this->post_type ); if ( ! empty( $step_ids ) ) { $args['post__in'] = $args[ 'post__in' ] ? array_intersect( $step_ids, $args[ 'post__in' ] ) : $step_ids; $course_lessons_args = learndash_get_course_lessons_order( $this->course_post->ID ); if ( !isset( $_GET[ 'orderby' ] ) ) { if ( isset( $course_lessons_args[ 'orderby' ] ) ) $args['orderby'] = $course_lessons_args['orderby']; else $args['orderby'] = 'title'; } if ( !isset( $_GET['order'] ) ) { if ( isset( $course_lessons_args[ 'order' ] ) ) $args['order'] = $course_lessons_args['order']; else $args['order'] = 'ASC'; } } else { $args['post__in'] = array(0 ); } } return $args; } // End of functions } } class-ld-rest-posts-controller.php 0000666 00000017246 15214246251 0013301 0 ustar 00 <?php if ( !class_exists('LD_REST_Posts_Controller_V1' ) ) { abstract class LD_REST_Posts_Controller_V1 extends WP_REST_Posts_Controller { protected $version = 'v1'; protected $sub_controllers = array(); protected $course_post = null; protected $lesson_post = null; protected $topic_post = null; public function __construct( $post_type = '' ) { parent::__construct( $post_type ); add_filter( "rest_{$this->post_type}_collection_params", array( $this, 'rest_collection_params_filter' ), 20, 2 ); add_filter( "rest_{$this->post_type}_query", array( $this, 'rest_query_filter' ), 20, 2 ); add_filter( "rest_prepare_{$this->post_type}", array( $this, 'rest_prepare_response_filter'), 20, 3 ); } public function register_routes_wpv2() { //if ( ( class_exists( 'LD_REST_Posts_Gutenberg_Controller' ) ) && ( LearnDash_REST_API::gutenberg_enabled( $this->post_type ) ) ) { if ( class_exists( 'LD_REST_Posts_Gutenberg_Controller' ) ) { $g = new LD_REST_Posts_Gutenberg_Controller( $this->post_type ); $g->register_routes(); } } function register_fields() { global $sfwd_lms; $post_args_fields = $sfwd_lms->get_post_args_section( $this->post_type, 'fields' ); if ( !empty( $post_args_fields ) ) { foreach( $post_args_fields as $field_key => $field_set ) { if ( ( isset( $field_set['show_in_rest'] ) ) && ( $field_set['show_in_rest'] === true ) ) { if ( ( isset( $field_set['rest_args'] ) ) && ( is_array( $field_set['rest_args'] ) ) ) { $field_args = $field_set['rest_args']; } else { $field_args = array(); } if ( ! isset( $field_args['get_callback'] ) ) { $field_args['get_callback'] = array( $this, 'ld_get_field_value' ); } if ( ! isset( $rest_field_args['update_callback'] ) ) { $field_args['update_callback'] = array( $this, 'ld_update_field_value' ); } //if ( ! isset( $field_args['schema']['sanitize_callback'] ) ) { // $field_args['schema']['sanitize_callback'] = 'sanitize_key'; //} if ( ! isset( $field_args['sanitize_callback'] ) ) { $field_args['sanitize_callback'] = 'sanitize_key'; } //if ( ! isset( $field_args['schema']['validate_callback'] ) ) { $field_args['schema']['validate_callback'] = array( $this, 'ld_rest_validate_request_arg' ); //} //if ( ! isset( $field_args['validate_callback'] ) ) { // $field_args['validate_callback'] = array( $this, 'ld_rest_validate_request_arg' ); //} if ( ( !isset( $field_args['schema'] ) ) || ( empty( $field_args['schema'] ) ) ) { $field_args['schema'] = array(); } if ( ( !isset( $field_args['schema']['description'] ) ) && ( isset( $field_set['name'] ) ) ) { $field_args['schema']['description'] = $field_set['name']; } if ( ( !isset( $field_args['schema']['type'] ) ) && ( isset( $field_set['type'] ) ) ) { switch( $field_set['type'] ) { case 'select': case 'multiselect': $field_args['schema']['type'] = 'string'; break; case 'checkbox': $field_args['schema']['type'] = 'boolean'; break; default: $field_args['schema']['type'] = $field_set['type']; break; } } if ( ( !isset( $field_args['schema']['required'] ) ) || ( empty( $field_args['schema']['required'] ) ) ) { $field_args['schema']['required'] = false; } if ( ( !isset( $field_args['schema']['default'] ) ) && ( isset( $field_set['default'] ) ) ) { $field_args['schema']['default'] = $field_set['default']; } if ( ( !isset( $field_args['schema']['enum'] ) ) && ( ( isset( $field_set['initial_options'] ) ) && ( !empty( $field_set['initial_options'] ) ) ) ) { $field_args['schema']['enum'] = array_keys( $field_set['initial_options'] ); } if ( !isset( $field_args['schema']['context'] ) ) { $field_args['schema']['context'] = array( 'view', 'edit' ); } register_rest_field( $this->post_type, $field_key, $field_args ); } } } } function ld_rest_validate_request_arg( $value, $args, $param = '' ) { error_log('in '. __FUNCTION__ ); error_log('value<pre>'. print_r($value, true) .'</pre>'); error_log('args<pre>'. print_r($args, true) .'</pre>'); error_log('param<pre>'. print_r($param, true) .'</pre>'); return true; } function ld_get_field_value( array $postdata, $field_name, WP_REST_Request $request, $post_type ) { if ( ( isset( $postdata['id'] ) ) && ( !empty( $postdata['id'] ) ) ) { $ld_post = get_post( $postdata['id'] ); if ( ( is_a( $ld_post, 'WP_Post' ) ) && ( $ld_post->post_type == $this->post_type ) ) { $field_value = learndash_get_setting( $ld_post, $field_name ); switch ( $field_name ) { case 'course_materials': $field_value = wp_specialchars_decode( $field_value, ENT_QUOTES ); if ( ! empty( $field_value ) ) { $field_value = do_shortcode( $field_value ); } break; case 'course_price_type': if ( $field_value === 'paynow' ) $field_value = 'buynow'; break; default: break; } return $field_value; } } } function ld_update_field_value( $value, WP_Post $post, $field_name, WP_REST_Request $request, $post_type ) { switch ( $field_name ) { case 'course_prerequisite_enabled': if ( true === $value ) { $value = 'on'; } break; case 'course_price_type': if ( 'buynow' === $value ) { $value = 'paynow'; } break; default: break; } learndash_update_setting( $post->ID, $field_name, $value ); return true; } /** * For LearnDash post type we override the default order/orderby * to ASC/title instead of the WP default DESC/date. */ function rest_collection_params_filter( $query_params, $post_type ) { global $learndash_post_types; if ( in_array( $this->post_type, $learndash_post_types ) ) { if ( ( isset( $query_params['orderby']['default'] ) ) && ( $query_params['orderby']['default'] != 'title' ) ) $query_params['orderby']['default'] = 'title'; if ( ( isset( $query_params['order']['default'] ) ) && ( $query_params['order']['default'] != 'asc' ) ) $query_params['order']['default'] = 'asc'; } return $query_params; } function rest_query_filter( $args, $request ) { return $args; } /** * Override the REST response links. This is needed when Course Shared Steps is enabled. * * @since 3.0 * @param object $response WP_REST_Response instance. * @param object $post WP_Post instance. * @param object $request WP_REST_Request instance. */ function rest_prepare_response_filter( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) { if ( ( LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_Permalinks', 'nested_urls' ) == 'yes' ) && ( in_array( $post->post_type, learndash_get_post_types( 'course_steps' ) ) ) ) { $request_params_json = $request->get_json_params(); if ( ( isset( $request_params_json['course_id'] ) ) && ( ! empty( $request_params_json['course_id'] ) ) ) { $course_id = absint( $request_params_json['course_id'] ); if ( ! empty( $course_id ) ) { $link = learndash_get_step_permalink( $post->ID, $course_id ); $response->data['link'] = $link; $response->data['permalink_template'] = str_replace( $post->post_name, '%pagename%', $link ); // These are not needed or used on the Gutenberg UI but change anyway. $response->data['guid']['rendered'] = $link; $response->data['guid']['raw'] = $link; } } } return $response; } // End of functions } } class-ld-rest-topics-controller.php 0000666 00000023722 15214246251 0013426 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Topics_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Topics_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( $post_type = '' ) { $this->post_type = 'sfwd-topic'; parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', $this->post_type ); } public function register_routes() { parent::register_routes_wpv2(); $this->register_fields(); $collection_params = $this->get_collection_params(); register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Unique identifier for the object.', 'learndash' ), 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => $get_item_args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'args' => array( 'force' => array( 'type' => 'boolean', 'default' => false, 'description' => esc_html__( 'Whether to bypass trash and force deletion.', 'learndash' ), ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); } function rest_collection_params_filter( $query_params, $post_type ) { $query_params = parent::rest_collection_params_filter( $query_params, $post_type ); if ( ! isset( $query_params['course'] ) ) { $query_params['course'] = array( 'description' => sprintf( // translators: placeholder: course. esc_html_x( 'Limit results to be within a specific %s. Required for non-admin users.', 'placeholder: course', 'learndash' ), LearnDash_Custom_Label::get_label( 'course' ) ), 'type' => 'integer', ); } if ( ! isset( $query_params['lesson'] ) ) { $query_params['lesson'] = array( 'description' => sprintf( // translators: placeholder: lesson. esc_html_x ( 'Limit results to be within a specific %s. Must be used with course parameter.', 'placeholder: lesson', 'learndash' ), LearnDash_Custom_Label::get_label( 'lesson' ) ), 'type' => 'integer', ); } return $query_params; } function get_item_permissions_check( $request ) { $return = parent::get_item_permissions_check( $request ); if ( ( true === $return ) && ( ! learndash_is_admin_user() ) ) { $course_id = (int) $request['course']; // If we don't have a course parameter we need to get all the courses the user has access to and all // the courses the lesson is avaiable in and compare. if ( empty( $course_id ) ) { $user_enrolled_courses = learndash_user_get_enrolled_courses( get_current_user_id() ); if ( empty( $user_enrolled_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $step_courses = learndash_get_courses_for_step( $request['id'], true ); if ( empty( $step_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $user_enrolled_courses = array_intersect( $user_enrolled_courses, array_keys( $step_courses ) ); if ( empty( $user_enrolled_courses ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } else { // But if the course parameter is provided we need to check the user has access and also // check the step is part of that course. $this->course_post = get_post( $course_id ); if ( ( ! $this->course_post ) || ( ! is_a( $this->course_post, 'WP_Post' ) ) || ( 'sfwd-courses' !== $this->course_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } if ( ! sfwd_lms_has_access( $this->course_post->ID ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } $this->ld_course_steps_object = LDLMS_Factory_Post::course_steps( $this->course_post->ID ); $this->ld_course_steps_object->load_steps(); $lesson_ids = $this->ld_course_steps_object->get_children_steps( $this->course_post->ID, $this->post_type ); if ( empty( $lesson_ids ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } if ( ! in_array( $request['id'], $lesson_ids ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } } return $return; } function get_item( $request ) { return parent::get_item( $request ); } function get_items_permissions_check( $request ) { $return = parent::get_items_permissions_check( $request ); if ( ( true === $return ) && ( 'view' === $request['context'] ) ) { $course_id = (int) $request['course']; if ( ! empty( $course_id ) ) { $this->course_post = get_post( $course_id ); if ( ( ! $this->course_post ) || ( ! is_a( $this->course_post, 'WP_Post' ) ) || ( 'sfwd-courses' !== $this->course_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } } $lesson_id = (int) $request['lesson']; if ( !empty( $lesson_id ) ) { $this->lesson_post = get_post( $lesson_id ); if ( ( ! $this->lesson_post ) || ( ! is_a( $this->lesson_post, 'WP_Post' ) ) || ( 'sfwd-lessons' !== $this->lesson_post->post_type ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Lesson ID.', 'learndash' ), array( 'status' => 404 ) ); } } if ( ! learndash_is_admin_user() ) { if ( ! $this->course_post ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid Course ID.', 'learndash' ), array( 'status' => 404 ) ); } else if ( ! sfwd_lms_has_access( $this->course_post->ID ) ) { return new WP_Error( 'ld_rest_cannot_view', __( 'Sorry, you are not allowed to view this item.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } } return $return; } function get_items( $request ) { return parent::get_items( $request ); } function rest_query_filter( $args, $request ) { // The course_post should be set in the local method get_items_permissions_check() if ( ( $this->course_post ) && ( is_a( $this->course_post, 'WP_Post' ) ) && ( 'sfwd-courses' === $this->course_post->post_type ) ) { $step_ids = array(); $step_ids = array(); if ( $this->lesson_post ) { $step_ids = learndash_course_get_children_of_step( $this->course_post->ID, $this->lesson_post->ID, $this->post_type ); } else if ( $this->course_post ) { $step_ids = learndash_course_get_steps_by_type( $this->course_post->ID, $this->post_type ); } if ( ! empty( $step_ids ) ) { $args['post__in'] = $args['post__in'] ? array_intersect( $step_ids, $args['post__in'] ) : $step_ids; $course_lessons_args = learndash_get_course_lessons_order( $this->course_post->ID ); if ( !isset( $_GET['orderby'] ) ) { if ( isset( $course_lessons_args['orderby'] ) ) $args['orderby'] = $course_lessons_args['orderby']; else $args['orderby'] = 'title'; } if ( !isset( $_GET['order'] ) ) { if ( isset( $course_lessons_args['order'] ) ) $args['order'] = $course_lessons_args['order']; else $args['order'] = 'ASC'; } } else { $args['post__in'] = array(0); } } return $args; } // End of functions } } class-ld-rest-groups-controller.php 0000666 00000013047 15214246251 0013443 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Groups_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Groups_Controller_V1 extends LD_REST_Posts_Controller_V1 { public function __construct( $post_type = '' ) { $this->post_type = 'groups'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', $this->post_type ); } public function register_routes() { $this->register_fields(); parent::register_routes_wpv2(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'get_items_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_item' ), 'permission_callback' => array( $this, 'create_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Unique identifier for the object.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => $get_item_args, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'update_item_permissions_check' ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 'args' => array( 'force' => array( 'type' => 'boolean', 'default' => false, 'description' => esc_html__( 'Whether to bypass trash and force deletion.', 'learndash' ), ), ), ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); include( LEARNDASH_REST_API_DIR . '/'. $this->version.'/class-ld-rest-groups-courses-controller.php' ); $this->sub_controllers['class-ld-rest-groups-courses-controller'] = new LD_REST_Groups_Courses_Controller_V1(); $this->sub_controllers['class-ld-rest-groups-courses-controller']->register_routes(); include( LEARNDASH_REST_API_DIR . '/'. $this->version.'/class-ld-rest-groups-leaders-controller.php' ); $this->sub_controllers['class-ld-rest-groups-leaders-controller'] = new LD_REST_Groups_Leaders_Controller_V1(); $this->sub_controllers['class-ld-rest-groups-leaders-controller']->register_routes(); include( LEARNDASH_REST_API_DIR . '/'. $this->version.'/class-ld-rest-groups-users-controller.php' ); $this->sub_controllers['class-ld-rest-groups-users-controller'] = new LD_REST_Groups_Users_Controller_V1(); $this->sub_controllers['class-ld-rest-groups-users-controller']->register_routes(); } function get_items_permissions_check( $request ) { if ( ( learndash_is_admin_user( ) ) || ( learndash_is_group_leader_user() ) ) { return true; } } function get_items( $request ) { return parent::get_items( $request ); } function get_item_permissions_check( $request ) { if ( ( learndash_is_admin_user( ) ) || ( learndash_is_group_leader_user() ) ) { return true; } } function get_item( $request ) { return parent::get_item( $request ); } function rest_query_filter( $args, $request ) { if ( learndash_is_group_leader_user() ) { $group_ids = learndash_get_administrators_group_ids( get_current_user_id() ); if ( ! empty( $group_ids ) ) $args['post__in'] = $group_ids; else $args['post__in'] = array(0); } return $args; } function rest_prepare_response( $response, $post, $request ) { $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); // Entity meta. $links = array( 'users' => array( 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/users' ), ), 'leaders' => array( 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/leaders' ), ), 'courses' => array( 'href' => rest_url( trailingslashit( $base ) . $post->ID . '/courses' ), ), ); $response->add_links( $links ); return $response; } // End of functions } } class-ld-rest-users-controller.php 0000666 00000001622 15214246251 0013261 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Users_Controller_V1' ) ) && ( class_exists( 'WP_REST_Users_Controller' ) ) ) { class LD_REST_Users_Controller_V1 extends WP_REST_Users_Controller { protected $version = 'v1'; protected $sub_controllers = array(); public function __construct() { parent::__construct(); $this->namespace = LEARNDASH_REST_API_NAMESPACE . '/' . $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'users' ); } /** * Registers the routes for the objects of the controller. * * @since 4.7.0 * * @see register_rest_route() */ public function register_routes() { $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); } } } class-ld-rest-echo-controller.php 0000666 00000006040 15214246251 0013035 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Echo_Controller_V1' ) ) && ( class_exists( 'WP_REST_Controller' ) ) ) { class LD_REST_Echo_Controller_V1 extends WP_REST_Controller { protected $version = 'v1'; /** * Constructor. * * @since 5.0.0 */ public function __construct() { $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = 'echo'; } /** * Registers the routes for the objects of the controller. * * @since 5.0.0 * * @see register_rest_route() */ public function register_routes() { register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_repsonse' ), 'permission_callback' => array( $this, 'get_repsonse_permissions_check' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'get_repsonse' ), 'permission_callback' => array( $this, 'get_repsonse_permissions_check' ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'get_repsonse' ), 'permission_callback' => array( $this, 'get_repsonse_permissions_check' ), ), 'schema' => array( $this, 'get_item_schema' ), ) ); } /** * Checks if a given request has access to read the theme. * * @since 5.0.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object. */ public function get_repsonse_permissions_check( $request ) { return true; } /** * Retrieves a collection of themes. * * @since 5.0.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_repsonse( $request ) { $response_array = array(); $response_array['method'] = $request->get_method(); $response_array['route'] = $request->get_route(); $response_array['authenticated'] = is_user_logged_in() ? 1 : 0; $response_array['query_params'] = $request->get_query_params(); $request_body = $request->get_body(); if ( ! empty( $request_body ) ) { $request_body = json_decode( $request_body, true ); $response_array['content-type'] = $request->get_header( 'content-type' ); $response_array['body'] = $request_body; } else { $response_array['body'] = ''; } $response = rest_ensure_response( $response_array ); $response->header( 'X-WP-Total', count( $response_array ) ); $response->header( 'X-WP-TotalPages', count( $response_array ) ); return $response; } /** * Retrieves the theme's schema, conforming to JSON Schema. * * @since 5.0.0 * * @return array Item schema data. */ public function get_item_schema() { $schema = array(); return $this->add_additional_fields_schema( $schema ); } } } class-ld-rest-groups-users-controller.php 0000666 00000024653 15214246251 0014607 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Groups_Users_Controller_V1' ) ) && ( class_exists( 'LD_REST_Users_Controller_V1' ) ) ) { class LD_REST_Groups_Users_Controller_V1 extends LD_REST_Users_Controller_V1 { private $supported_collection_params = array( 'exclude' => 'exclude', 'include' => 'include', 'offset' => 'offset', 'order' => 'order', 'page' => 'paged', 'per_page' => 'number', 'search' => 'search', 'roles' => 'role__in', 'slug' => 'nicename__in', ); public function __construct( ) { parent::__construct( ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'groups' ); } public function register_routes() { $this->meta = new WP_REST_User_Meta_Fields(); $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); if ( isset( $schema['properties']['password'] ) ) { $get_item_args['password'] = array( 'description' => esc_html__( 'The password for the post if it is password protected.', 'learndash' ), 'type' => 'string', ); } register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/users', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'Group ID to enroll group leader into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_groups_users' ), 'permission_callback' => array( $this, 'get_groups_users_permissions_check' ), 'args' => $this->get_collection_params(), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_groups_users' ), 'permission_callback' => array( $this, 'update_groups_users_permissions_check' ), 'args' => array( 'user_ids' => array( 'description' => esc_html__( 'User IDs to enroll into Group.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_groups_users' ), 'permission_callback' => array( $this, 'delete_groups_users_permissions_check' ), 'args' => array( 'user_ids' => array( 'description' => esc_html__( 'User IDs to remove from Group.', 'learndash' ), 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } function get_groups_users_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } if ( learndash_is_group_leader_user() ) { $group_id = $request['id']; $leader_groups = learndash_get_administrators_group_ids( get_current_user_id() ); if ( ( ! empty( $leader_groups ) ) && ( in_array( $group_id, $leader_groups ) ) ) { return true; } } } public function get_groups_users( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $prepared_args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $prepared_args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $prepared_args[ $wp_param ] = $request[ $api_param ]; } } if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) { $prepared_args['offset'] = $request['offset']; } else { $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; } if ( isset( $registered['orderby'] ) ) { $orderby_possibles = array( 'id' => 'ID', 'include' => 'include', 'name' => 'display_name', 'registered_date' => 'registered', 'slug' => 'user_nicename', 'include_slugs' => 'nicename__in', 'email' => 'user_email', 'url' => 'user_url', ); $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; } if ( LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_Admin_User', 'courses_autoenroll_admin_users' ) === 'yes' ) { $exclude_admin = true; } else { $exclude_admin = false; } $group_users = learndash_get_groups_user_ids( $group_id ); if ( !empty( $group_users ) ) $prepared_args['include'] = $group_users; else $prepared_args['include'] = array(0); if ( ! empty( $prepared_args['search'] ) ) { $prepared_args['search'] = '*' . $prepared_args['search'] . '*'; } if ( !isset( $prepared_args['fields'] ) ) { $prepared_args['fields'] = array('ID'); } /** * Filters WP_User_Query arguments when querying users via the REST API. * * @link https://developer.wordpress.org/reference/classes/wp_user_query/ * * @since 4.7.0 * * @param array $prepared_args Array of arguments for WP_User_Query. * @param WP_REST_Request $request The current request. */ $prepared_args = apply_filters( 'learndash_rest_groups_users_query', $prepared_args, $request ); $query = new WP_User_Query( $prepared_args ); $users = array(); foreach ( $query->results as $user ) { if ( is_a( $user, 'WP_User' ) ) { $data = $this->prepare_item_for_response( $user, $request ); $users[] = $this->prepare_response_for_collection( $data ); } else { $users[] = $user->ID; } } $response = rest_ensure_response( $users ); // Store pagination values for headers then unset for count query. $per_page = (int) $prepared_args['number']; $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); $prepared_args['fields'] = 'ID'; $total_users = $query->get_total(); if ( $total_users < 1 ) { // Out-of-bounds, run the query again without LIMIT for total count. unset( $prepared_args['number'], $prepared_args['offset'] ); $count_query = new WP_User_Query( $prepared_args ); $total_users = $count_query->get_total(); } $response->header( 'X-WP-Total', (int) $total_users ); $max_pages = ceil( $total_users / $per_page ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } return $response; } function delete_groups_users_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_groups_users_permissions_check( $request ) { if ( learndash_is_admin_user( ) ) { return true; } } function update_groups_users( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $user_ids = $request['user_ids']; if ( ( !is_array( $user_ids ) ) || ( empty( $user_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing User IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $user_ids = array_map( 'intval', $user_ids ); } foreach( $user_ids as $user_id ) { ld_update_group_access( $user_id, $group_id, false ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } function delete_groups_users( $request ) { $group_id = $request['id']; if ( empty( $group_id ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Invalid group ID.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } $user_ids = $request['user_ids']; if ( ( !is_array( $user_ids ) ) || ( empty( $user_ids ) ) ) { return new WP_Error( 'rest_post_invalid_id', esc_html__( 'Missing User IDs.', 'learndash' ) .' '. __CLASS__, array( 'status' => 404 ) ); } else { $user_ids = array_map( 'intval', $user_ids ); } foreach( $user_ids as $user_id ) { ld_update_group_access( $user_id, $group_id, true ); } $data = array( ); // Create the response object $response = rest_ensure_response( $data ); // Add a custom status code $response->set_status( 200 ); return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; $query_params['fields'] = array( 'description' => __('Returned values.', 'learndash' ), 'type' => 'string', 'type' => 'string', 'default' => 'ids', 'enum' => array( 'ids', 'objects', ), ); foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[$external_key] ) ) { $query_params[$external_key] = $query_params_default[$external_key]; } } return $query_params; } // End of functions } } class-ld-rest-users-quiz-attempts-controller.php 0000666 00000017177 15214246251 0016122 0 ustar 00 <?php if ( ( !class_exists( 'LD_REST_Users_Quiz_Attempts_Controller_V1' ) ) && ( class_exists( 'LD_REST_Posts_Controller_V1' ) ) ) { class LD_REST_Users_Quiz_Attempts_Controller_V1 extends LD_REST_Posts_Controller_V1 { private $supported_collection_params = array( 'offset' => 'offset', 'order' => 'order', //'orderby' => 'orderby', 'per_page' => 'posts_per_page', 'page' => 'paged', 'search' => 's', ); public function __construct( ) { $this->post_type = 'sfwd-courses'; $this->taxonomies = array(); parent::__construct( $this->post_type ); $this->namespace = LEARNDASH_REST_API_NAMESPACE .'/'. $this->version; $this->rest_base = LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Section_General_REST_API', 'users' ); } /** * Registers the routes for the objects of the controller. * * @since 4.7.0 * * @see register_rest_route() */ public function register_routes() { $collection_params = $this->get_collection_params(); $schema = $this->get_item_schema(); $get_item_args = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/quiz-attempts/', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'User ID to show course progress', 'learndash' ), 'required' => true, 'type' => 'integer', ), ), array( 'methods' => 'GET', 'callback' => array( $this, 'get_quiz_attempts' ), 'permission_callback' => array( $this, 'get_quiz_attempts_permissions_check' ), 'args' => $this->get_collection_params(), ), ) ); /* register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/course-progress/(?P<course_id>[\d]+)', array( 'args' => array( 'id' => array( 'description' => esc_html__( 'User ID to enroll user into.', 'learndash' ), 'required' => true, 'type' => 'integer', ), 'course_id' => array( 'description' => esc_html__( 'Course ID to enroll.', 'learndash' ), 'required' => false, 'items' => array( 'type' => 'integer', ), ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'set_items' ), ), ) ); */ } function get_quiz_attempts_permissions_check( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_user_invalid_id', esc_html__( 'Invalid user ID. #1', 'learndash' ), array( 'status' => 404 ) ); } if ( is_user_logged_in() ) $current_user_id = get_current_user_id(); else $current_user_id = 0; if ( empty( $current_user_id ) ) { if ( ! current_user_can( 'edit_user', $user_id ) ) { return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.', 'learndash' ), array( 'status' => 404 ) ); } } if ( ( $user_id != $current_user_id ) && ( ! learndash_is_admin_user( $current_user_id ) ) ) { if ( ! current_user_can( 'edit_user', $user_id ) ) { return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this user.', 'learndash' ), array( 'status' => rest_authorization_required_code() ) ); } } return true; } function get_quiz_attempts( $request ) { $user_id = $request['id']; if ( empty( $user_id ) ) { return new WP_Error( 'rest_user_invalid_id', esc_html__( 'Invalid user ID. #1', 'learndash' ), array( 'status' => 404 ) ); } // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); $args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $args. */ foreach ( $this->supported_collection_params as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $args[ $wp_param ] = $request[ $api_param ]; } } //error_log( 'args<pre>'. print_r( $args, true ) .'</pre>' ); $atts = array( 'return' => true, 'type' => array( 'quiz' ), 'quiz_num' => $args['posts_per_page'], 'quiz_orderby' => 'taken', 'quiz_order' => 'DESC' ); //$atts = apply_filters('learndash_profile_course_info_atts', $atts, $user ); $course_info = SFWD_LMS::get_course_info( $user_id, $atts ); //error_log('course_info<pre>'. print_r($course_info, true) .'</pre>'); if ( ( isset( $course_info['quizzes'] ) ) && ( !empty( $course_info['quizzes'] ) ) ) { $course_info['quizzes'] = array_values( $course_info['quizzes'] ); // Need to convert the timestamp integer value to proper YYYY-MM-DD HH:MM:SS values for response. foreach( $course_info['quizzes'] as &$quiz ) { if ( ( isset( $quiz['time'] ) ) && ( !empty( $quiz['time'] ) ) ) { $quiz['time'] = $this->prepare_date_response( date('Y-m-d h:i:s', $quiz['time'] ) ); } if ( ( isset( $quiz['m_edit_time'] ) ) && ( !empty( $quiz['m_edit_time'] ) ) ) { $quiz['m_edit_time'] = $this->prepare_date_response( date('Y-m-d h:i:s', $quiz['m_edit_time'] ) ); } } $response = rest_ensure_response( $course_info['quizzes'] ); if ( isset( $course_info['quizzes_pager'] ) ) { $response->header( 'X-WP-Total', (int) $course_info['quizzes_pager']['total_items'] ); $response->header( 'X-WP-TotalPages', (int) $course_info['quizzes_pager']['total_pages'] ); $request_params = $request->get_query_params(); $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); $max_pages = (int) $course_info['quizzes_pager']['total_pages']; $page = (int) $course_info['quizzes_pager']['paged']; if ( $page > 1 ) { $prev_page = $page - 1; if ( $prev_page > $max_pages ) { $prev_page = $max_pages; } $prev_link = add_query_arg( 'page', $prev_page, $base ); $response->link_header( 'prev', $prev_link ); } if ( $max_pages > $page ) { $next_page = $page + 1; $next_link = add_query_arg( 'page', $next_page, $base ); $response->link_header( 'next', $next_link ); } } } else { $response = rest_ensure_response( array() ); } return $response; } public function get_collection_params() { $query_params_default = parent::get_collection_params(); //error_log('query_params_default<pre>'. print_r($query_params_default, true) .'</pre>'); $query_params_default['context']['default'] = 'view'; $query_params = array(); $query_params['context'] = $query_params_default['context']; /* $query_params['include'] = array( 'description' => __('Fitler results by quiz IDs', 'learndash' ), 'required' => false, 'type' => 'array', 'default' => [], 'items' => array( 'type' => 'integer' ) ); */ $query_params['orderby']['default'] = 'taken'; $query_params['orderby']['enum'] = array( 'taken', 'title', 'id', 'date', 'menu_order' ); /* $query_params['course'] = array( 'description' => __('Fitler results by course ID', 'learndash' ), 'required' => false, 'type' => 'integer', ); */ foreach( $this->supported_collection_params as $external_key => $internal_key ) { if ( isset( $query_params_default[$external_key] ) ) { $query_params[$external_key] = $query_params_default[$external_key]; } } return $query_params; } // End of functions } }
| ver. 1.4 |
Github
|
.
| PHP 7.0.33 | Generation time: 0 |
proxy
|
phpinfo
|
Settings