Last active
October 30, 2018 07:45
-
-
Save sudipto-me/e7093892f4e035835ebd9b4fa31bbfc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ----------------------------------------------------------------------------- | |
* REST API SETUP | |
* -------------------------------------------------------------------------- */ | |
add_action( 'rest_api_init', function () { | |
register_rest_route( 'users', '/auth/', array( | |
'methods' => 'POST', | |
'callback' => 'meditation_user_auth' | |
) ); | |
} ); | |
function meditation_user_auth(WP_REST_Request $request) { | |
$email = isset($request['email']) ? $request['email'] : ''; | |
$password = isset($request['password']) ? $request['password'] : ''; | |
if(empty($email)) { | |
return new WP_Error( 'invalid_email', 'Invalid email', array( 'status' => 404 ) ); | |
} | |
if(empty($password)) { | |
return new WP_Error( 'invalid_password', 'Invalid password', array( 'status' => 404 ) ); | |
} | |
$response = array(); | |
$user = get_user_by( 'email', $email ); | |
$authentication = wp_authenticate($user->user_login,$password); | |
if (!is_wp_error( $authentication )){ | |
$response = array( | |
'success' => true, | |
'message' => 'Login Successful', | |
'userid' =>$user->ID, | |
'token' => 'meditationpeggyapp', | |
'user_name'=>$user->display_name, | |
'user_email'=>$email, | |
); | |
} | |
else{ | |
$response = array( | |
'success' => false, | |
'message' => 'Invalid email or password', | |
'token' => '', | |
'user_id'=>'', | |
'user_name'=>'', | |
'user_email'=>'', | |
); | |
} | |
$message = new WP_REST_Response($response, 200); // data => array of returned data | |
return $message; | |
} | |
add_action('rest_api_init',function(){ | |
register_rest_route('uploads','/audios/',array( | |
'methods'=>'GET', | |
'callback'=>'meditation_upload_file_auth', | |
)); | |
}); | |
function meditation_upload_file_auth(WP_REST_Request $request) { | |
$user_id = isset($_GET['userid']) ? (int)$_GET['userid'] : ''; | |
$user_data = get_user_by('ID', $user_id); | |
$token = $request->get_header('token'); | |
// $token = $attribues['args']['token'][0]; | |
$subtitles = ['Day 1-7 Audios','Day 8-14 Audios','Day 15-21 Audios','Day 22-28 Audios','Day 29-30 Audios']; | |
if(empty($token)) { | |
return new WP_Error( 'empty_token', 'Empty Token', array( 'status' => 404 ) ); | |
} | |
if($token !='meditationpeggyapp'){ | |
return new WP_Error( 'invalid_token', 'Invalid Token', array( 'status' => 404 ) ); | |
} | |
if(empty($user_id)) { | |
return new WP_Error( 'empty_userID', 'Empty Userid', array( 'status' => 404 ) ); | |
} | |
if($user_data == false) { | |
return new WP_Error( 'invalid_userID', 'Invalid Userid', array( 'status' => 404 ) ); | |
} else { | |
$upload_dir = wp_upload_dir(); | |
if( $dir = opendir($upload_dir['basedir'].'/audios')) { | |
$modules = array(); | |
while(false !== ($file=readdir($dir))) { | |
if( $file != "." && $file != '..') { | |
$modules[] = $file; | |
} | |
} | |
closedir($dir); | |
sort($modules); | |
$response = array(); | |
$count = 1; | |
for( $i=0,$j=0; $i<count($modules),$j<count($subtitles); $i++,$j++) { | |
$purchased_products = array(); | |
$purchased_products['module_title'] = $modules[$i]; | |
$purchased_products['module_subtitle'] = $subtitles[$j]; | |
$listed_audios = glob($upload_dir['basedir'].'/audios/'.$modules[$i].'/*'); | |
foreach($listed_audios as $single_audio){ | |
$purchased_products['products'][] = array( | |
'title' => 'Day '.$count, | |
'url' => str_replace("/home/admin/web/meditationwithpeggygaines.com/public_html/wp-content/uploads",$upload_dir['baseurl'],$single_audio), | |
'id' => (int)sprintf("%03d", $count), | |
); | |
$count++; | |
} | |
$response[] = $purchased_products; | |
} | |
// foreach($modules as $module) { | |
// $count = 0; | |
// $purchased_products = array(); | |
// $purchased_products['module_title'] = $module; | |
// $purchased_products['module_subtitle'] = $subtitles[$subtitle_count]; | |
// $listed_audios = glob($upload_dir['basedir'].'/audios/'.$module.'/*'); | |
// $count = 1; | |
// foreach($listed_audios as $single_audio){ | |
// $purchased_products['products'][] = array( | |
// 'title'=>'audio '.$count, | |
// 'url'=> str_replace("/home/admin/web/meditationwithpeggygaines.com/public_html/wp-content/",$upload_dir['baseurl'].'/',$single_audio), | |
// ); | |
// $count++; | |
// } | |
// $response[] = $purchased_products; | |
// $subtitle_count++; | |
// } | |
$json = array(); | |
$json['success'] = true; | |
$json['message'] = 'Product fetch successful'; | |
$json['purchased_products'] = $response; | |
$message = new WP_REST_Response($json, 200); // data => array of returned data | |
return $message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment