Addons.php
5 years ago
Admin.php
5 years ago
Ajax.php
5 years ago
Assets.php
5 years ago
Course.php
5 years ago
Course_Settings_Tabs.php
5 years ago
Course_Widget.php
5 years ago
Dashboard.php
5 years ago
Email.php
5 years ago
FormHandler.php
5 years ago
Frontend.php
5 years ago
Gutenberg.php
5 years ago
Instructor.php
5 years ago
Instructors_List.php
5 years ago
Lesson.php
5 years ago
Options.php
5 years ago
Post_types.php
5 years ago
Q_and_A.php
5 years ago
Question_Answers_List.php
5 years ago
Quiz.php
5 years ago
Quiz_Attempts_List.php
5 years ago
RestAPI.php
5 years ago
Rewrite_Rules.php
5 years ago
Shortcode.php
5 years ago
Student.php
5 years ago
Students_List.php
5 years ago
Taxonomies.php
5 years ago
Template.php
5 years ago
Theme_Compatibility.php
5 years ago
Tools.php
5 years ago
Tutor.php
5 years ago
TutorEDD.php
5 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
5 years ago
Tutor_Setup.php
5 years ago
Upgrader.php
5 years ago
User.php
5 years ago
Utils.php
5 years ago
Video_Stream.php
5 years ago
Withdraw.php
5 years ago
Withdraw_Requests_List.php
5 years ago
WooCommerce.php
5 years ago
RestAPI.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * RestAPI class |
| 5 | * |
| 6 | * @author: themeum |
| 7 | * @author_uri: https://themeum.com |
| 8 | * @package Tutor |
| 9 | * @since v.1.5.0 |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | namespace TUTOR; |
| 14 | |
| 15 | if (!defined('ABSPATH')) |
| 16 | exit; |
| 17 | |
| 18 | class RestAPI { |
| 19 | |
| 20 | protected $namespace = 'tutor/v1'; |
| 21 | protected $course_post_type; |
| 22 | |
| 23 | public function __construct() { |
| 24 | $this->course_post_type = tutor()->course_post_type; |
| 25 | |
| 26 | add_action('rest_api_init', array($this, 'rest_api_init')); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | public function rest_api_init() { |
| 31 | register_rest_route( |
| 32 | $this->namespace, 'courses', |
| 33 | array( |
| 34 | 'methods' => 'GET', |
| 35 | 'callback' => array($this, 'courses_api'), |
| 36 | 'permission_callback' => '__return_true' |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | public function courses_api() { |
| 42 | global $wpdb; |
| 43 | |
| 44 | $a = array_merge(array( |
| 45 | 'post_type' => $this->course_post_type, |
| 46 | 'post_status' => 'publish', |
| 47 | |
| 48 | 'id' => '', |
| 49 | 'exclude_ids' => '', |
| 50 | 'category' => '', |
| 51 | |
| 52 | 'orderby' => 'ID', |
| 53 | 'order' => 'DESC', |
| 54 | 'count' => '10', |
| 55 | ), $_GET); |
| 56 | |
| 57 | $limit = (int) $a['count']; |
| 58 | $exclude_ids_query = ''; |
| 59 | $in_ids_query = ''; |
| 60 | $tax_join = ''; |
| 61 | $tax_where = ''; |
| 62 | |
| 63 | $orderby = sanitize_text_field($a['orderby']); |
| 64 | $order = sanitize_text_field($a['order']); |
| 65 | |
| 66 | /** |
| 67 | * Exclude Course IDS |
| 68 | */ |
| 69 | if (!empty($a['exclude_ids'])) { |
| 70 | $exclude_ids = (array) explode(',', sanitize_text_field($a['exclude_ids'])); |
| 71 | if (tutils()->count($exclude_ids)) { |
| 72 | $exclude_ids_query = "AND ID NOT IN('$exclude_ids')"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (!empty($a['id'])) { |
| 77 | $ids = (array) explode(',', $a['id']); |
| 78 | if (tutils()->count($ids)) { |
| 79 | $in_ids_query = "AND ID IN('$ids')"; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (!empty($a['category'])) { |
| 84 | $category = (array) explode(',', $a['category']); |
| 85 | $tax = new \WP_Tax_Query( |
| 86 | array( |
| 87 | array( |
| 88 | 'taxonomy' => 'course-category', |
| 89 | 'field' => 'term_id', |
| 90 | 'terms' => $category, |
| 91 | 'operator' => 'IN', |
| 92 | ) |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | $tax_sql = $tax->get_sql($wpdb->posts, 'ID'); |
| 97 | $tax_join = tutils()->array_get('join', $tax_sql); |
| 98 | $tax_where = tutils()->array_get('where', $tax_sql); |
| 99 | } |
| 100 | |
| 101 | $course_post_type = tutor()->course_post_type; |
| 102 | $query = $wpdb->get_results("SELECT ID, post_author, post_title |
| 103 | from {$wpdb->posts} |
| 104 | |
| 105 | {$tax_join} |
| 106 | |
| 107 | WHERE 1=1 AND post_status = 'publish' |
| 108 | {$exclude_ids_query} |
| 109 | {$in_ids_query} |
| 110 | {$tax_where} |
| 111 | AND post_type = '{$course_post_type}' ORDER BY {$orderby} {$order} LIMIT {$limit} ", ARRAY_A); |
| 112 | |
| 113 | |
| 114 | if (tutils()->count($query)) { |
| 115 | $results = apply_filters('tutor/api/get_courses', $query); |
| 116 | wp_send_json_success($results); |
| 117 | } |
| 118 | wp_send_json_error(); |
| 119 | } |
| 120 | } |