| 1 |
<?php |
| 2 |
/** |
| 3 |
* RestAPI class |
| 4 |
* |
| 5 |
* @author: themeum |
| 6 |
* @author_uri: https://themeum.com |
| 7 |
* @package Tutor |
| 8 |
* @since v.1.5.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace TUTOR; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) |
| 14 |
exit; |
| 15 |
|
| 16 |
class RestAPI { |
| 17 |
|
| 18 |
use Custom_Validation; |
| 19 |
|
| 20 |
private $namespace = 'tutor/v1'; |
| 21 |
|
| 22 |
protected $course_post_type; |
| 23 |
|
| 24 |
private $path; |
| 25 |
|
| 26 |
private $courseObj; |
| 27 |
|
| 28 |
private $topicObj; |
| 29 |
|
| 30 |
private $lessonObj; |
| 31 |
|
| 32 |
private $annoucementObj; |
| 33 |
|
| 34 |
private $quizObj; |
| 35 |
|
| 36 |
private $authorObj; |
| 37 |
|
| 38 |
private $ratingObj; |
| 39 |
|
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
$this->path = plugin_dir_path(TUTOR_FILE); |
| 43 |
|
| 44 |
spl_autoload_register(array($this, 'loader')); |
| 45 |
|
| 46 |
|
| 47 |
$this->courseObj = new REST_Course; |
| 48 |
$this->topicObj = new REST_Topic; |
| 49 |
$this->lessonObj = new REST_Lesson; |
| 50 |
$this->annoucementObj = new REST_Course_Announcement; |
| 51 |
$this->quizObj = new REST_Quiz; |
| 52 |
$this->authorObj = new REST_Author; |
| 53 |
$this->ratingObj = new REST_Rating; |
| 54 |
|
| 55 |
add_action('rest_api_init', array($this, 'init_routes')); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
private function loader($className) { |
| 60 |
if (!class_exists($className)) { |
| 61 |
$className = preg_replace( |
| 62 |
array('/([a-z])([A-Z])/', '/\\\/'), |
| 63 |
array('$1$2', DIRECTORY_SEPARATOR), |
| 64 |
$className |
| 65 |
); |
| 66 |
|
| 67 |
$className = str_replace('TUTOR' . DIRECTORY_SEPARATOR, 'restapi' . DIRECTORY_SEPARATOR, $className); |
| 68 |
$file_name = $this->path . $className . '.php'; |
| 69 |
|
| 70 |
if (file_exists($file_name)) { |
| 71 |
require_once $file_name; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
init all routes for api |
| 78 |
*/ |
| 79 |
public function init_routes() { |
| 80 |
//courses |
| 81 |
register_rest_route( |
| 82 |
$this->namespace, |
| 83 |
'/courses', |
| 84 |
array( |
| 85 |
'methods' => "GET", |
| 86 |
'callback' => array( |
| 87 |
$this->courseObj, 'course' |
| 88 |
), |
| 89 |
'permission_callback' => '__return_true' |
| 90 |
) |
| 91 |
); |
| 92 |
|
| 93 |
//courses by terms cat and tag |
| 94 |
register_rest_route( |
| 95 |
$this->namespace, |
| 96 |
'/course-by-terms', |
| 97 |
array( |
| 98 |
'methods' => "POST", |
| 99 |
'callback' => array( |
| 100 |
$this->courseObj, 'course_by_terms' |
| 101 |
), |
| 102 |
'permission_callback' => '__return_true' |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
//courses by terms cat and tag |
| 107 |
register_rest_route( |
| 108 |
$this->namespace, |
| 109 |
'/course-sorting-by-price', |
| 110 |
array( |
| 111 |
'methods' => "GET", |
| 112 |
'callback' => array( |
| 113 |
$this->courseObj, 'course_sort_by_price' |
| 114 |
), |
| 115 |
'args' => array( |
| 116 |
'order' => array( |
| 117 |
'required' => true, |
| 118 |
'type' => 'string', |
| 119 |
'validate_callback' => function ($order) { |
| 120 |
return $this->validate_order($order); |
| 121 |
} |
| 122 |
), |
| 123 |
'page' => array( |
| 124 |
'required' => false, |
| 125 |
'type' => 'number' |
| 126 |
) |
| 127 |
), |
| 128 |
'permission_callback' => '__return_true' |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
//course details |
| 133 |
register_rest_route( |
| 134 |
$this->namespace, |
| 135 |
'/course-detail/(?P<id>\d+)', |
| 136 |
array( |
| 137 |
'methods' => 'GET', |
| 138 |
'callback' => array( |
| 139 |
$this->courseObj, 'course_detail' |
| 140 |
), |
| 141 |
'args' => array( |
| 142 |
'id' => array( |
| 143 |
'validate_callback' => function ($param) { |
| 144 |
return is_numeric($param); |
| 145 |
} |
| 146 |
) |
| 147 |
), |
| 148 |
'permission_callback' => '__return_true' |
| 149 |
) |
| 150 |
); |
| 151 |
|
| 152 |
//course topic |
| 153 |
register_rest_route( |
| 154 |
$this->namespace, |
| 155 |
'/course-topic/(?P<id>\d+)', |
| 156 |
array( |
| 157 |
'methods' => 'GET', |
| 158 |
'callback' => array( |
| 159 |
$this->topicObj, 'course_topic' |
| 160 |
), |
| 161 |
'args' => array( |
| 162 |
'id' => array( |
| 163 |
'validate_callback' => function ($param) { |
| 164 |
return is_numeric($param); |
| 165 |
} |
| 166 |
) |
| 167 |
), |
| 168 |
'permission_callback' => '__return_true' |
| 169 |
) |
| 170 |
); |
| 171 |
|
| 172 |
//lesson by topic |
| 173 |
register_rest_route( |
| 174 |
$this->namespace, |
| 175 |
'/lesson/(?P<id>\d+)', |
| 176 |
array( |
| 177 |
'methods' => 'GET', |
| 178 |
'callback' => array( |
| 179 |
$this->lessonObj, 'topic_lesson' |
| 180 |
), |
| 181 |
'args' => array( |
| 182 |
'id' => array( |
| 183 |
'validate_callback' => function ($param) { |
| 184 |
return is_numeric($param); |
| 185 |
} |
| 186 |
) |
| 187 |
), |
| 188 |
'permission_callback' => '__return_true' |
| 189 |
) |
| 190 |
); |
| 191 |
|
| 192 |
//course annoucement by course id |
| 193 |
register_rest_route( |
| 194 |
$this->namespace, |
| 195 |
'/course-annoucement/(?P<id>\d+)', |
| 196 |
array( |
| 197 |
'methods' => 'GET', |
| 198 |
'callback' => array( |
| 199 |
$this->annoucementObj, 'course_annoucement' |
| 200 |
), |
| 201 |
'args' => array( |
| 202 |
'id' => array( |
| 203 |
'validate_callback' => function ($param) { |
| 204 |
return is_numeric($param); |
| 205 |
} |
| 206 |
) |
| 207 |
), |
| 208 |
'permission_callback' => '__return_true' |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
//quiz by topic id |
| 213 |
register_rest_route( |
| 214 |
$this->namespace, |
| 215 |
'/quiz/(?P<id>\d+)', |
| 216 |
array( |
| 217 |
'methods' => 'GET', |
| 218 |
'callback' => array( |
| 219 |
$this->quizObj, 'quiz_with_settings' |
| 220 |
), |
| 221 |
'args' => array( |
| 222 |
'id' => array( |
| 223 |
'validate_callback' => function ($param) { |
| 224 |
return is_numeric($param); |
| 225 |
} |
| 226 |
) |
| 227 |
), |
| 228 |
'permission_callback' => '__return_true' |
| 229 |
) |
| 230 |
); |
| 231 |
|
| 232 |
//quiz question answer by quiz id |
| 233 |
register_rest_route( |
| 234 |
$this->namespace, |
| 235 |
'/quiz-question-answer/(?P<id>\d+)', |
| 236 |
array( |
| 237 |
'methods' => 'GET', |
| 238 |
'callback' => array( |
| 239 |
$this->quizObj, 'quiz_question_ans' |
| 240 |
), |
| 241 |
'args' => array( |
| 242 |
'id' => array( |
| 243 |
'validate_callback' => function ($param) { |
| 244 |
return is_numeric($param); |
| 245 |
} |
| 246 |
) |
| 247 |
), |
| 248 |
'permission_callback' => '__return_true' |
| 249 |
) |
| 250 |
); |
| 251 |
|
| 252 |
//quiz attempt details by quiz id |
| 253 |
register_rest_route( |
| 254 |
$this->namespace, |
| 255 |
'/quiz-attempt-details/(?P<id>\d+)', |
| 256 |
array( |
| 257 |
'methods' => 'GET', |
| 258 |
'callback' => array( |
| 259 |
$this->quizObj, 'quiz_attempt_details' |
| 260 |
), |
| 261 |
'args' => array( |
| 262 |
'id' => array( |
| 263 |
'validate_callback' => function ($param) { |
| 264 |
return is_numeric($param); |
| 265 |
} |
| 266 |
) |
| 267 |
), |
| 268 |
'permission_callback' => '__return_true' |
| 269 |
) |
| 270 |
); |
| 271 |
|
| 272 |
//author detail by id |
| 273 |
register_rest_route( |
| 274 |
$this->namespace, |
| 275 |
'/author-information/(?P<id>\d+)', |
| 276 |
array( |
| 277 |
'methods' => 'GET', |
| 278 |
'callback' => array( |
| 279 |
$this->authorObj, 'author_detail' |
| 280 |
), |
| 281 |
'args' => array( |
| 282 |
'id' => array( |
| 283 |
'validate_callback' => function ($param) { |
| 284 |
return is_numeric($param); |
| 285 |
} |
| 286 |
) |
| 287 |
), |
| 288 |
'permission_callback' => '__return_true' |
| 289 |
) |
| 290 |
); |
| 291 |
|
| 292 |
//reviews by course id |
| 293 |
register_rest_route( |
| 294 |
$this->namespace, |
| 295 |
'/course-rating/(?P<id>\d+)', |
| 296 |
array( |
| 297 |
'methods' => 'GET', |
| 298 |
'callback' => array( |
| 299 |
$this->ratingObj, 'course_rating' |
| 300 |
), |
| 301 |
'args' => array( |
| 302 |
'id' => array( |
| 303 |
'validate_callback' => function ($param) { |
| 304 |
return is_numeric($param); |
| 305 |
} |
| 306 |
) |
| 307 |
), |
| 308 |
'permission_callback' => '__return_true' |
| 309 |
) |
| 310 |
); |
| 311 |
} |
| 312 |
} |
| 313 |
|