Guest User

Untitled

a guest
Oct 25th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. /* -----------------------------------------------------------------------------
  2. * REST API SETUP
  3. * -------------------------------------------------------------------------- */
  4. add_action( 'rest_api_init', function () {
  5. register_rest_route( 'users', '/auth/', array(
  6. 'methods' => 'POST',
  7. 'callback' => 'meditation_user_auth'
  8. ) );
  9. } );
  10.  
  11. function meditation_user_auth(WP_REST_Request $request) {
  12. $email = isset($request['email']) ? $request['email'] : '';
  13. $password = isset($request['password']) ? $request['password'] : '';
  14.  
  15. if(empty($email)) {
  16. return new WP_Error( 'invalid_email', 'Invalid email', array( 'status' => 404 ) );
  17. }
  18. if(empty($password)) {
  19. return new WP_Error( 'invalid_password', 'Invalid password', array( 'status' => 404 ) );
  20. }
  21. $response = array();
  22. $user = get_user_by( 'email', $email );
  23. $authentication = wp_authenticate($user->user_login,$password);
  24. if (!is_wp_error( $authentication )){
  25.  
  26. $response = array(
  27. 'success' => true,
  28. 'message' => 'Login Successful',
  29. 'userid' =>$user->ID,
  30. 'token' => 'meditationpeggyapp',
  31. 'user_name'=>$user->display_name,
  32. 'user_email'=>$email,
  33. );
  34. }
  35. else{
  36. $response = array(
  37. 'success' => false,
  38. 'message' => 'Invalid email or password',
  39. 'token' => '',
  40. 'user_id'=>'',
  41. 'user_name'=>'',
  42. 'user_email'=>'',
  43. );
  44. }
  45. $message = new WP_REST_Response($response, 200); // data => array of returned data
  46. return $message;
  47.  
  48. }
  49. add_action('rest_api_init',function(){
  50. register_rest_route('uploads','/audios/',array(
  51. 'methods'=>'GET',
  52. 'callback'=>'meditation_upload_file_auth',
  53. ));
  54. });
  55. function meditation_upload_file_auth(WP_REST_Request $request) {
  56. $user_id = isset($_GET['userid']) ? (int)$_GET['userid'] : '';
  57. $user_data = get_user_by('ID', $user_id);
  58. $token = $request->get_header('token');
  59. // $token = $attribues['args']['token'][0];
  60. $subtitles = ['Day 1-7 Audios','Day 8-14 Audios','Day 15-21 Audios','Day 22-28 Audios','Day 29-30 Audios'];
  61.  
  62. if(empty($token)) {
  63. return new WP_Error( 'empty_token', 'Empty Token', array( 'status' => 404 ) );
  64. }
  65. if($token !='meditationpeggyapp'){
  66. return new WP_Error( 'invalid_token', 'Invalid Token', array( 'status' => 404 ) );
  67. }
  68. if(empty($user_id)) {
  69. return new WP_Error( 'empty_userID', 'Empty Userid', array( 'status' => 404 ) );
  70. }
  71. if($user_data == false) {
  72. return new WP_Error( 'invalid_userID', 'Invalid Userid', array( 'status' => 404 ) );
  73. } else {
  74. $upload_dir = wp_upload_dir();
  75.  
  76. if( $dir = opendir($upload_dir['basedir'].'/audios')) {
  77.  
  78. $modules = array();
  79. while(false !== ($file=readdir($dir))) {
  80. if( $file != "." && $file != '..') {
  81. $modules[] = $file;
  82. }
  83. }
  84. closedir($dir);
  85. sort($modules);
  86. $response = array();
  87. $count = 1;
  88. for( $i=0,$j=0; $i<count($modules),$j<count($subtitles); $i++,$j++) {
  89.  
  90. $purchased_products = array();
  91. $purchased_products['module_title'] = $modules[$i];
  92. $purchased_products['module_subtitle'] = $subtitles[$j];
  93. $listed_audios = glob($upload_dir['basedir'].'/audios/'.$modules[$i].'/*');
  94.  
  95. foreach($listed_audios as $single_audio){
  96. $purchased_products['products'][] = array(
  97. 'title' => 'Day '.$count,
  98. 'url' => str_replace("/home/admin/web/meditationwithpeggygaines.com/public_html/wp-content/uploads",$upload_dir['baseurl'],$single_audio),
  99. 'id' => (int)sprintf("%03d", $count),
  100. );
  101. $count++;
  102. }
  103. $response[] = $purchased_products;
  104. }
  105. // foreach($modules as $module) {
  106. // $count = 0;
  107. // $purchased_products = array();
  108. // $purchased_products['module_title'] = $module;
  109. // $purchased_products['module_subtitle'] = $subtitles[$subtitle_count];
  110. // $listed_audios = glob($upload_dir['basedir'].'/audios/'.$module.'/*');
  111. // $count = 1;
  112. // foreach($listed_audios as $single_audio){
  113.  
  114. // $purchased_products['products'][] = array(
  115. // 'title'=>'audio '.$count,
  116. // 'url'=> str_replace("/home/admin/web/meditationwithpeggygaines.com/public_html/wp-content/",$upload_dir['baseurl'].'/',$single_audio),
  117. // );
  118. // $count++;
  119. // }
  120. // $response[] = $purchased_products;
  121. // $subtitle_count++;
  122. // }
  123.  
  124. $json = array();
  125. $json['success'] = true;
  126. $json['message'] = 'Product fetch successful';
  127. $json['purchased_products'] = $response;
  128. $message = new WP_REST_Response($json, 200); // data => array of returned data
  129. return $message;
  130.  
  131. }
  132. }
  133.  
  134.  
  135. }
Add Comment
Please, Sign In to add comment