Last active
October 26, 2018 12:02
-
-
Save sanjeevbishnoi/21877629f7c4b326d6791f12660950c1 to your computer and use it in GitHub Desktop.
Wordpress REST api file (without using any plugin)
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
<?php | |
//api/api.php?method=ForgotPassword | |
include('../wp-load.php'); | |
class myapiclass { | |
public function commonGetData($postPerPage=NULL,$category=NULL,$post_type=NULL, $orderBy=NULL,$order=NULL,$notRequiredData=NULL,$status='publish'){ | |
// echo $category." in common data"; | |
$args=array(); | |
$finalresult=array(); | |
if(strlen($postPerPage)>0){ | |
$args['posts_per_page']= $postPerPage; | |
} | |
if(strlen($category)>0){ | |
$args['category']= $this->getCategoryBySlug($category); | |
} | |
if(strlen($post_type)>0){ | |
$args['post_type']= $post_type; | |
} | |
if(strlen($orderBy)>0){ | |
if(($category=='places')|| ($category=='intro')){ | |
$orderBy='title'; | |
}else if(($category=='shabadvaani') || ($category=='bishnoiratan')){ | |
$orderBy='date'; | |
} | |
$args['orderby']= $orderBy; | |
} | |
if(strlen($order)>0){ | |
if(($category=='places')||($category=='intro') || ($category=='shabadvaani')){ | |
$order='ASC'; | |
}else if(($category=='bishnoiratan')){ | |
$order='ASC'; | |
} | |
$args['order']= $order; | |
} | |
/* echo "<pre>"; | |
print_r($args); | |
die;*/ | |
$myposts = get_posts($args); | |
foreach ($myposts as $post) { | |
$postdata['post_id'] = $post->ID; | |
$postdata['title'] = $post->post_title; | |
$postdata['author'] = $post->post_author; | |
$postdata['post_data'] = $post->post_content; | |
$postdata['post_content'] =get_the_date( 'd-m-Y',$post); | |
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID)); | |
if($image == ""){ | |
$postdata['image_url'] = ""; | |
} else { | |
$postdata['image_url'] = $image; | |
} | |
$meta_data = get_post_meta($postdata['post_id'], $key = '', $single = false ); | |
if(strlen($post_type)>0 && $post_type == 'hotel_list'){ | |
$postdata['post_content'] = $post->post_content; | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
} | |
if(strlen($category)>0 && $category == 'places'){ | |
$locationdata=get_field('newtitle',$postdata['post_id']); | |
$postdata['thumbnail'] = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'attractions')[0]; | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
} | |
$postType=get_field('post_extra',$postdata['post_id']); | |
if($postType==''){ | |
$postType='normal'; | |
} | |
$postdata['post_type'] = $postType; | |
$postdata['description'] = get_field('description',$postdata['post_id']); | |
$postdata['link'] = get_field('link',$postdata['post_id']); | |
$finalresult[] = $postdata; | |
} | |
return $finalresult; | |
} | |
public function RecentPosts(){ | |
$args = array( 'numberposts' => '50'); | |
$finalresult=array(); | |
$myposts = get_posts($args); | |
foreach ($myposts as $post) { | |
$postdata['post_id'] = $post->ID; | |
$postdata['title'] = $post->post_title; | |
$postdata['author'] = $post->post_author; | |
$postdata['post_data'] = $post->post_content; | |
$postdata['post_content'] =get_the_date( 'd-m-Y',$post); | |
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID)); | |
$category =get_the_category($post->ID)[0]->slug; | |
if($image == ""){ | |
$postdata['image_url'] = ""; | |
} else { | |
$postdata['image_url'] = $image; | |
} | |
$meta_data = get_post_meta($postdata['post_id'], $key = '', $single = false ); | |
if(strlen($post_type)>0 && $post_type == 'hotel_list'){ | |
$postdata['post_content'] = $post->post_content; | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
} | |
if(strlen($category)>0 && $category == 'places'){ | |
$locationdata=get_field('newtitle',$postdata['post_id']); | |
//$postdata['thumbnail'] = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'attractions')[0]; | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
} | |
$postType=get_field('post_extra',$postdata['post_id']); | |
if($postType==''){ | |
$postType='normal'; | |
} | |
$postdata['post_type'] = $postType; | |
$postdata['description'] = get_field('description',$postdata['post_id']); | |
$postdata['link'] = get_field('link',$postdata['post_id']); | |
$postdata['category']=$category; | |
$finalresult[] = $postdata; | |
} | |
// if(strlen($category)>0){ | |
// $sub_categories= $this->getSubCategory($this->getCategoryBySlug($category)); | |
// } | |
if(count($finalresult)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"Attractions", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"Attractions", "status" => 'OK', "GetData" => $finalresult); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function getSubCategories($cat){ | |
$list = get_categories( array( | |
'orderby' => 'name', | |
'child_of' => $cat | |
) ); | |
$newlist = array(); | |
foreach ($list as $key => $value) { | |
$newlist[]=$value; | |
} | |
return $newlist; | |
} | |
public function SearchPosts($query){ | |
$args = array( 's' => $query); | |
$finalresult=array(); | |
$myposts = get_posts($args); | |
foreach ($myposts as $post) { | |
$postdata['post_id'] = $post->ID; | |
$postdata['title'] = $post->post_title; | |
$postdata['author'] = $post->post_author; | |
$postdata['post_data'] = $post->post_content; | |
$postdata['post_content'] =get_the_date( 'd-m-Y',$post); | |
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID)); | |
$category =get_the_category($post->ID)[0]->slug; | |
if($image == ""){ | |
$postdata['image_url'] = ""; | |
} else { | |
$postdata['image_url'] = $image; | |
} | |
$meta_data = get_post_meta($postdata['post_id'], $key = '', $single = false ); | |
if(strlen($post_type)>0 && $post_type == 'hotel_list'){ | |
$postdata['post_content'] = $post->post_content; | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
} | |
if(strlen($category)>0 && $category == 'places'){ | |
$locationdata=get_field('newtitle',$postdata['post_id']); | |
//$postdata['thumbnail'] = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'attractions')[0]; | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
} | |
$postType=get_field('post_extra',$postdata['post_id']); | |
if($postType==''){ | |
$postType='normal'; | |
} | |
$postdata['post_type'] = $postType; | |
$postdata['description'] = get_field('description',$postdata['post_id']); | |
$postdata['link'] = get_field('link',$postdata['post_id']); | |
$postdata['category']=$category; | |
$finalresult[] = $postdata; | |
} | |
if(count($finalresult)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"Attractions", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"Attractions", "status" => 'OK', "GetData" => $finalresult); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function SinglePost($id){ | |
$mypost=get_post($id); | |
$postdata['post_title']=$mypost->post_title; | |
$postdata['post_content']=$content=apply_filters('the_content', get_post_field('post_content', $id)); | |
$postdata['post_date']=$mypost->post_date; | |
$postdata['guid']=$mypost->guid; | |
$image = wp_get_attachment_url( get_post_thumbnail_id($id)); | |
if($image == ""){ | |
$postdata['image_url'] = ""; | |
} else { | |
$postdata['image_url'] = $image; | |
} | |
$meta_data = get_post_meta($id, $key = '', $single = false ); | |
$locationdata=get_field('newtitle',$id); | |
$extra=get_field('extra',$id); | |
$postdata['latitude'] = $locationdata['lat']; | |
$postdata['longitude'] = $locationdata['lng']; | |
$postdata['address'] = $locationdata['address']; | |
$postdata['extra'] = $extra; | |
$imageone=get_field('imageone',$id)['url']; | |
$imagetwo=get_field('imagetwo',$id)['url']; | |
$imagethree=get_field('imagethree',$id)['url']; | |
$imagefour=get_field('imagefour',$id)['url']; | |
$postdata['images']=array(); | |
if(!empty($image)){ | |
array_push($postdata['images'],$image); | |
} | |
if(!empty($imageone)){ | |
array_push($postdata['images'],$imageone); | |
} if(!empty($imagetwo)){ | |
array_push($postdata['images'],$imagetwo); | |
}if(!empty($imagethree)){ | |
array_push($postdata['images'],$imagethree); | |
} if(!empty($imagefour)){ | |
array_push($postdata['images'],$imagefour); | |
} | |
$val = array("controller" => "myapiclass", "action" =>"SinglePost", "status" => 'OK', "GetData" => $postdata); | |
header('Content-type: application/json'); | |
echo json_encode($val); | |
} | |
public function Info(){ | |
$data=$this->commonGetData('-1','4'); | |
if(empty($data)){ | |
$val = array("controller" => "myapiclass", "action" =>"Info", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"Info", "status" => 'OK', "GetData" => $data); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function Attractions($slug=NULL) { | |
//echo $slug; | |
if(strlen($slug)>0){ | |
$sub_categories= $this->getSubCategories($this->getCategoryBySlug($slug)); | |
} | |
$data=$this->commonGetData('-1',$slug,' ','date','DESC'); | |
if(count($data)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"Attractions", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"Attractions", "status" => 'OK', "GetData" => $data,"SubCategories"=>$sub_categories); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function GetPostById(){ | |
$post_id = $_REQUEST['post_id'];//This is page id or post id | |
$content_post = get_post($post_id); | |
$post_meta = get_post_meta($post_id, $key='',$single =false); | |
$data['post_id'] = $content_post->ID; | |
$data['post_title'] = $content_post->post_title; | |
if(count($post_meta['subtitle'])>0){ | |
$data['subtitle'] = $post_meta['subtitle'][0]; | |
} else { | |
$data['subtitle'] = ""; | |
} | |
$data['image_url'] = wp_get_attachment_url( get_post_thumbnail_id($post_id), 'thumbnail' ); | |
$data['content'] = $content_post->post_content; | |
$val = array("controller" => "myapiclass", "action" =>"Post", "status" => 'OK', "GetData" => $data); | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function HotelListing() { | |
$data=$this->commonGetData('-1',' ','hotel_list'); | |
if(count($data)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"HotelListing", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"HotelListing", "status" => 'OK', "GetData" => $data); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function ContactInfo() { | |
$data=$this->commonGetData('-1','6'); | |
if(count($data)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"ContactInfo", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"ContactInfo", "status" => 'OK', "GetData" => $data); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function Travel() { | |
$data=$this->commonGetData('-1','7'); | |
if(count($data)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"Travel", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"Travel", "status" => 'OK', "GetData" => $data); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function Amenities() { | |
if($_REQUEST['type'] == "hospitals"){ | |
$data=$this->commonGetData('-1','12'); | |
} elseif($_REQUEST['type'] == "forex"){ | |
$data=$this->commonGetData('-1','13'); | |
} else { | |
$data=$this->commonGetData('-1','11'); | |
} | |
if(count($data)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"Amenities", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"Amenities", "status" => 'OK', "GetData" => $data); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function SignUp(){ | |
$name = $_REQUEST['name']; | |
$user_email = $_REQUEST['email']; | |
$password = $_REQUEST['password']; | |
$user_id = username_exists( $user_email ); | |
if ( email_exists($user_email) == false ) { | |
$user_id = wp_create_user( $user_email, $password, $user_email ); | |
$userdata = array( | |
'first_name' => $_REQUEST['name'], | |
'phonenumber' => $_REQUEST['phonenumber'], | |
'signup_type' => 'custom', | |
'device_type' => $_REQUEST['device_type'], | |
'device_token' => $_REQUEST['device_token'] | |
); | |
foreach($userdata as $meta_key=>$meta_value){ | |
update_user_meta( $user_id, $meta_key, $meta_value); | |
} | |
$creds = array(); | |
$creds['user_login'] = $user_email; | |
$creds['user_password'] = $password; | |
$user = wp_signon( $creds, false ); | |
$val = array("controller" => "myapiclass", "action" =>"SignUp", "status" => 'OK',"Message" => "Registration successfull.", "user_id" => $user_id); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"SignUp", "status" => 'NOK', "error" => "Email already exists"); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function SocialSignUp(){ | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'OK',"Message" => "Update app feature removed.", "user_id" => $user_id); | |
header('Content-type: application/json'); | |
echo json_encode($val); | |
return; | |
$name = $_REQUEST['name']; | |
$user_email = $_REQUEST['email']; | |
$password = "mrmoga29"; | |
if($_REQUEST['email']=="[email protected]"){ | |
$password = "admin@123"; | |
} | |
$userdata = array( | |
'first_name' => $_REQUEST['name'], | |
'signup_type' => $_REQUEST['signup_type'], | |
'device_type' => $_REQUEST['device_type'], | |
'device_token' => $_REQUEST['device_token'] | |
); | |
//$user_id = wp_insert_user($userdata) ; | |
/*echo "<pre>"; | |
print_r($user_id);*/ | |
//On success | |
/*if ( ! is_wp_error( $user_id ) ) { | |
echo "User created : ". $user_id; | |
}else{ | |
echo "User already existed : ". $user_id; | |
}*/ | |
$user_id = wp_create_user( $user_email, $password, $user_email ); | |
$creds = array(); | |
$creds['user_login'] = $user_email; | |
$creds['user_password'] = $password; | |
$user = wp_signon( $creds, false ); | |
if ( is_wp_error($user) ){ | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'NOK', "Message" => "Incorrect Email or Password"); | |
} else { | |
$user_id = $user->ID; | |
foreach($userdata as $meta_key=>$meta_value){ | |
update_user_meta($user_id, $meta_key, $meta_value); | |
} | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'OK',"Message" => "Login successful.", "user_id" => $user_id); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function login(){ | |
$creds = array(); | |
$creds['user_login'] = $_REQUEST['email']; | |
$creds['user_password'] = $_REQUEST['password']; | |
$user = wp_signon( $creds, false ); | |
if ( is_wp_error($user) ){ | |
$val = array("controller" => "myapiclass", "action" =>"Login", "status" => 'NOK', "error" => "Incorrect Email or Password"); | |
} else { | |
$user_id = $user->ID; | |
$val = array("controller" => "myapiclass", "action" =>"Login", "status" => 'OK',"Message" => "Login successful.", "user_id" => $user_id); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function Feedback(){ | |
$image=$_REQUEST["img"]; | |
$File_Name = $_FILES['img']['name']; | |
$Random_Number = rand(0, 9999999999); | |
$new_image = $Random_Number.$File_Name; //new file name | |
move_uploaded_file($_FILES["img"]["tmp_name"], "FeedbackImages/" . $newimage); | |
if($_REQUEST['city'] == "") { | |
$city = ""; | |
} else { | |
$city = $_REQUEST['city']; | |
} | |
if($_REQUEST['country'] == "") { | |
$country = ""; | |
} else { | |
$country = $_REQUEST['country']; | |
} | |
if($_REQUEST['gender'] == "") { | |
$gender = ""; | |
} else { | |
$gender = $_REQUEST['gender']; | |
} | |
if($image == ""){ | |
$newimage = ""; | |
} | |
$data = array( | |
'name' => $_REQUEST['name'], | |
'email' => $_REQUEST['email'], | |
'phonenumber' => $_REQUEST['phonenumber'], | |
'city' => $city, | |
'country' => $country, | |
'description' => $_REQUEST['description'], | |
'gender' => $gender, | |
'image' => $newimage | |
); | |
global $wpdb; | |
$insert = $wpdb->insert('tbl_feedback', $data); | |
$insert_id = $wpdb->insert_id; | |
$to = "<Enter Email ID>"; | |
if($image == ""){ | |
$body = "<!DOCTYPE html> | |
<head> | |
<meta content=text/html; charset=utf-8 http-equiv=Content-Type /> | |
<title>Recover Password</title> | |
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'> | |
</head> | |
<body> | |
<h1>You message with reset instruction</h1> | |
</body>"; | |
} | |
$headers = "MIME-Version: 1.0" . "\r\n"; | |
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; | |
// More headers | |
$headers .= 'From:'.$data['email'] . "\r\n" ; | |
$headers .= 'Reply-To:'.$data['email'] . "\r\n" ; | |
$headers .= 'Return-Path:'.$data['email'] . "\r\n" ; | |
mail($to,"Feedback ",$body,$headers); | |
if($insert_id != ""){ | |
$val = array("controller" => "Posts", "action" =>"Feedback", "status" => 'OK',"Message" => "Feedback submitted successfully."); | |
} else { | |
$val = array("controller" => "Posts", "action" =>"Feedback", "status" => 'NOK', "error" => "Try Again."); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function QuizList(){ | |
$data=$this->commonGetData('-1',' ','quiz'); | |
if(count($data)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"QuizList", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"QuizList", "status" => 'OK', "GetData" => $data); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function search_user(){ | |
$searchText = $_REQUEST['searchText']; | |
$user_id = $_REQUEST['user_id']; // this is logged in user id. | |
if (!filter_var($searchText, FILTER_VALIDATE_EMAIL) === false) { | |
$args = array('meta_key' => 'nickname','meta_value' => $searchText); | |
} elseif(is_numeric($searchText)) { | |
$args = array('meta_key' => 'phonenumber','meta_value' => $searchText); | |
} else { | |
$args = array('meta_key' => 'first_name','meta_value' => $searchText); | |
} | |
//$args = array('meta_key' => 'first_name','meta_value' => $name); | |
$usersList = get_users($args); | |
/*echo "<pre>"; print_r($usersList); | |
echo "<pre>"; print_r($usersList[0]->ID);*/ | |
foreach($usersList as $user){ | |
$meta_data = get_user_meta($user->ID, $key = '', true); | |
$data['friend_id'] = $user->ID; | |
$data['name'] = $meta_data['first_name'][0]; | |
$data['email'] = $user->user_email; | |
if($meta_data['phonenumber'][0] == ""){ | |
$data['phonenumber'] = ""; | |
} else { | |
$data['phonenumber'] = $meta_data['phonenumber'][0]; | |
} | |
if($meta_data['latitude'][0] == ""){ | |
$data['latitude'] = ""; | |
} else { | |
$data['latitude'] = $meta_data['latitude'][0]; | |
} | |
if($meta_data['longitude'][0] == ""){ | |
$data['longitude'] = ""; | |
} else { | |
$data['longitude'] = $meta_data['longitude'][0]; | |
} | |
//Check if user already exists in friend list or not | |
$meta_data = get_user_meta($user_id, $key = '', true); | |
if(in_array($data['friend_id'],$meta_data['friend_id'])){ | |
$data['is_friend'] = "1"; | |
} else { | |
$data['is_friend'] = "0"; | |
} | |
$result[] = $data; | |
} | |
if(count($result)==0){ | |
$val = array("controller" => "myapiclass", "action" =>"search_user", "status" => 'NOK', "error" => "No Data Found."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"search_user", "status" => 'OK', "GetData" => $result); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function update_user_details(){ | |
$user_id = $_REQUEST['user_id']; | |
if($_REQUEST['latitude'] != ""){ | |
$userdata['latitude'] = $_REQUEST['latitude']; | |
} | |
if($_REQUEST['longitude'] != ""){ | |
$userdata['longitude'] = $_REQUEST['longitude']; | |
} | |
if($_REQUEST['device_type'] != ""){ | |
$userdata['device_type'] = $_REQUEST['device_type']; | |
} | |
if($_REQUEST['device_token'] != ""){ | |
$userdata['device_token'] = $_REQUEST['device_token']; | |
} | |
global $wpdb; | |
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users WHERE ID = %d", $user_id)); | |
if($count == 1){ | |
foreach($userdata as $meta_key=>$meta_value){ | |
$update = update_user_meta( $user_id, $meta_key, $meta_value); | |
} | |
$val = array("controller" => "myapiclass", "action" =>"update_user_details", "status" => 'OK', "Message" => "User updated successfully."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"update_user_details", "status" => 'NOK', "error" => "User does not exists."); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function add_friend(){ | |
$user_id = $_REQUEST['user_id']; | |
$friend_id = $_REQUEST['friend_id']; | |
$meta_data = get_user_meta($user_id, $key = '', true); | |
if(in_array($friend_id,$meta_data['friend_id'])){ | |
$val = array("controller" => "myapiclass", "action" =>"add_friend", "status" => 'NOK', "Message" => "Already added in your friends list."); | |
} else { | |
$adduser = add_user_meta( $user_id, 'friend_id', $friend_id); | |
if($adduser != ""){ | |
$val = array("controller" => "myapiclass", "action" =>"add_friend", "status" => 'OK', "Message" => "User updated successfully."); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"add_friend", "status" => 'NOK', "error" => "Try Again."); | |
} | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function myfriend_list(){ | |
$user_id = $_REQUEST['user_id']; | |
$meta_data = get_user_meta($user_id, $key = '', true); | |
$friends_list = $meta_data['friend_id']; | |
//echo "<pre>";print_r($friends_list); | |
foreach($friends_list as $friend){ | |
//print_r($friend); | |
$friend_data = get_userdata($friend); | |
$friend_meta_data = get_user_meta($friend, $key = '', true); | |
$data['name'] = $friend_meta_data['first_name'][0]; | |
$data['email'] = $friend_data->user_email; | |
$data['phonenumber'] = $friend_meta_data['phonenumber'][0]; | |
if($friend_meta_data['latitude'][0] == ""){ | |
$data['latitude'] = ""; | |
} else { | |
$data['latitude'] = $friend_meta_data['latitude'][0]; | |
} | |
if($friend_meta_data['longitude'][0] == ""){ | |
$data['longitude'] = ""; | |
} else { | |
$data['longitude'] = $friend_meta_data['longitude'][0]; | |
} | |
$list[] = $data; | |
} | |
if(count($list)>0){ | |
$val = array("controller" => "myapiclass", "action" =>"myfriend_list", "status" => 'OK', "GetData" => $list); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"myfriend_list", "status" => 'NOK', "error" => "No Friends Added Yet."); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function sendHotelListGoIbibo() | |
{ | |
$ch = curl_init(); | |
$city_id = $_REQUEST["city_id"]; | |
// set URL and other appropriate options | |
$url = "http://developer.goibibo.com/api/voyager/get_hotels_by_cityid/?app_id=85f1996f&app_key=91fa890d3f06bb5521d0428a1752f641&city_id=$city_id"; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
// execute and return string (this should be an empty string '') | |
$str = curl_exec($curl); | |
curl_close($curl); | |
$result = json_decode($str, true); | |
$hotels = array(); | |
$m=0; | |
foreach($result["data"] as $key=>$val) | |
{ | |
$showName = addslashes($result["data"][$key]["hotel_geo_node"]["name"]); | |
if(($showName!="Hotel Amit") && ($showName!="Hotel Samrat International")) | |
{ | |
$hotelName[] = array("hotel"=> $showName, | |
"imagepath"=> $result["data"][$key]["hotel_data_node"]["img_selected"]["fs"]["l"], | |
"city"=> $result["data"][$key]["hotel_data_node"]["loc"]["city"], | |
"pin"=> $result["data"][$key]["hotel_data_node"]["loc"]["pin"], | |
"country"=> $result["data"][$key]["hotel_data_node"]["loc"]["country"], | |
"state"=> $result["data"][$key]["hotel_data_node"]["loc"]["state"], | |
"location"=> $result["data"][$key]["hotel_data_node"]["loc"]["location"], | |
"long"=> $result["data"][$key]["hotel_data_node"]["loc"]["long"], | |
"lat"=> $result["data"][$key]["hotel_data_node"]["loc"]["lat"], | |
"desc"=> $result["data"][$key]["hotel_data_node"]["desc"]["default"] | |
); | |
} | |
} | |
//$hotelName = json_encode($hotelName); | |
if(is_null($hotelName)) | |
{ | |
$this->sendHotelListGoIbibo(); | |
} | |
else if($hotelName=="null") | |
{ | |
$this->sendHotelListGoIbibo(); | |
} | |
else | |
{ | |
//echo $hotelName; | |
$val = array("controller" => "myapiclass", "action" =>"sendHotelListGoIbibo", "status" => 'OK', "HotelList" => $hotelName); | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
} | |
public function ForgotPassword(){ | |
$user_email = $_REQUEST['email']; | |
$user_id = username_exists( $user_email ); | |
if ( email_exists($user_email) == false ) { | |
$val = array("controller" => "myapiclass", "action" =>"ForgotPassword", "status" => 'NOK', "error" => "No User Found."); | |
} else { | |
$to = $user_email; | |
$meta_data = get_user_meta($user_id, $key = '', true); | |
$username = $meta_data['first_name'][0]; | |
$static_key = "afvsdsdjkldfoiuy4uiskahkhsajbjksasdasdgf43gdsddsf"; | |
$id = $user_id . "_" . $static_key; | |
$b_id = base64_encode($id); | |
$body = "<!DOCTYPE html> | |
<head> | |
<meta content=text/html; charset=utf-8 http-equiv=Content-Type /> | |
<title>Account Password Retrival</title> | |
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'> | |
</head> | |
<body> | |
</body>"; | |
$headers = "MIME-Version: 1.0" . "\r\n"; | |
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; | |
// More headers | |
$headers .= 'From:[email protected]'."\r\n" ; | |
mail($to," Account Password Retrival",$body,$headers); | |
$val = array("controller" => "myapiclass", "action" =>"ForgotPassword", "status" => 'OK', "Message" => "Password retrival mail has been sent to your registered email."); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
// sanjeev Bishnoi Starts here | |
public function getAllCategories(){ | |
$list = get_categories( array( | |
'orderby' => 'name', | |
'parent' => 0 | |
) ); | |
$newlist = array(); | |
foreach ($list as $key => $value) { | |
$newlist[]=$value; | |
} | |
if(count($newlist)>0){ | |
$val = array("controller" => "myapiclass", "action" =>"category_list", "status" => 'OK', "GetData" => $newlist); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"category_list", "status" => 'NOK', "error" => "No Category Added Yet."); | |
} | |
header('Content-type: application/json'); | |
echo json_encode($val); | |
} | |
public function getAllCategoriesWithPosts(){ | |
$list = get_categories( array( | |
'orderby' => 'name', | |
'parent' => 0 | |
) ); | |
$newlist = array(); | |
$foo = new StdClass(); | |
$foo->term_id = -1; | |
$foo->cat_name = "Recent"; | |
$foo->slug = "recent"; | |
$foo->postData=$this->commonGetData('10','',' ','date','DESC'); | |
$newlist[]=$foo; | |
foreach ($list as $key => $value) { | |
$value->postData=$this->commonGetData('5',$value->slug); | |
$newlist[]=$value; | |
} | |
if(count($newlist)>0){ | |
$val = array("controller" => "myapiclass", "action" =>"category_list", "status" => 'OK', "GetData" => $newlist); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"category_list", "status" => 'NOK', "error" => "No Category Added Yet."); | |
} | |
header('Content-type: application/json'); | |
echo json_encode($val); | |
} | |
public function getCategoryBySlug($slug){ | |
$idObj = get_category_by_slug($slug); | |
return $idObj->term_id; | |
} | |
public function NearMe($slug,$lat,$lng){ | |
global $wpdb; | |
$tableName = $wpdb->prefix . 'geodata'; | |
$posttable=$wpdb->prefix . 'posts'; | |
$origLat = $lat; | |
$origLon = $lng; | |
$dist = 20000; // This is the maximum distance (in miles) away from $origLat, $origLon in which to search | |
$query = "SELECT post_title,post_type,post_status,post_id,lat,lng FROM $tableName as g inner join $posttable as p on p.ID=g.post_id where post_type='post' and post_status='publish'"; | |
$results=$wpdb->get_results($query); | |
$finalresult=array(); | |
$order_my = array(); | |
foreach ( $results as $key=>$value) { | |
$gotdistance=$this->getDistanceValue($lat,$lng,$value->lat,$value->lng); | |
if(empty($gotdistance)){ | |
$order_my[$key]=500000; | |
}else{ | |
$order_my[$key]=$gotdistance; | |
} | |
} | |
array_multisort($order_my, SORT_ASC, $results); | |
foreach ( $results as $key=>$value) { | |
$post_title=$value->post_title; | |
$post_id=$value->post_id; | |
$distance=$this->getDistance($lat,$lng,$value->lat,$value->lng); | |
$finalresult[]=array("post_title"=>$post_title,"post_id"=>$post_id,"distance"=>$distance,"lat"=>$value->lat,"lng"=> $value->lng); | |
} | |
$val = array("controller" => "myapiclass", "action" =>"NearMe", "status" => 'OK', "GetData" =>$finalresult); | |
echo json_encode($val); | |
} | |
public function getDistance($mylat,$mylng,$destlat,$destlng){ | |
$api_key='<Use you own key>'; | |
$url ="https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$mylat.",".$mylng."&destinations=".$destlat.",".$destlng."&api_key=".$api_key; | |
$ch = curl_init(); | |
// Disable SSL verification | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
// Will return the response, if false it print the response | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// Set the url | |
curl_setopt($ch, CURLOPT_URL,$url); | |
// Execute | |
$result=curl_exec($ch); | |
// Closing | |
curl_close($ch); | |
$result_array=json_decode($result); | |
return "Distance ".$result_array->rows[0]->elements[0]->distance->text." Time ".$result_array->rows[0]->elements[0]->duration->text; | |
} | |
public function getDistanceValue($mylat,$mylng,$destlat,$destlng){ | |
$api_key='<USE YOUR OWN KEY>'; | |
$url ="https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$mylat.",".$mylng."&destinations=".$destlat.",".$destlng."&api_key=".$api_key; | |
$ch = curl_init(); | |
// Disable SSL verification | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
// Will return the response, if false it print the response | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// Set the url | |
curl_setopt($ch, CURLOPT_URL,$url); | |
// Execute | |
$result=curl_exec($ch); | |
// Closing | |
curl_close($ch); | |
$result_array=json_decode($result); | |
return $result_array->rows[0]->elements[0]->distance->value; | |
} | |
public function postNews() { | |
header('Content-type: application/json'); | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'NOK', "Message" => "Update the app,this feature is no more available"); | |
echo json_encode($val); | |
return; | |
// Initialize the page ID to -1. This indicates no action has been taken. | |
$post_id = -1; | |
// Setup the author, slug, and title for the post | |
$author_id = $_REQUEST['user_id']; | |
$slug = $_REQUEST['title']; | |
$title = $_REQUEST['title']; | |
$content= $_REQUEST['content']; | |
$image_url= $_REQUEST['image']; | |
// If the page doesn't already exist, then create it | |
if( null == get_page_by_title( $title ) ) { | |
// Set the post ID so that we know the post was created successfully | |
$post_id = wp_insert_post( | |
array( | |
'comment_status' => 'closed', | |
'ping_status' => 'closed', | |
'post_author' => $author_id, | |
'post_name' => $slug, | |
'post_title' => $title, | |
'post_content' => $content, | |
'post_status' => 'publish', | |
'post_type' => 'post' | |
) | |
); | |
if(empty($image_url)){ | |
}else{ | |
$this->generateThumbnail($image_url,$post_id); | |
} | |
} else { | |
$post_id = -2; | |
} | |
if ( $post_id<0 ){ | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'NOK', "Message" => "Post failed"); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'OK',"Message" => "Post successful.Check Samachar section", "post_id" => $post_id); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function deletePost(){ | |
header('Content-type: application/json'); | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'NOK', "Message" => "Update the app,this feature is no more available"); | |
echo json_encode($val); | |
return; | |
$post_id = $_REQUEST['post_id']; | |
$post_returned=wp_delete_post($post_id,false); | |
if($post_returned !=false){ | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'OK',"Message" => "Post deleted successfully", "post_id" => $post_id); | |
}else{ | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'NOK',"Message" => "Error occured", "post_id" => $post_id); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function editNews() { | |
// Initialize the page ID to -1. This indicates no action has been taken. | |
$post_id = $_REQUEST['post_id']; | |
// Setup the author, slug, and title for the post | |
$author_id = $_REQUEST['user_id']; | |
$slug = $_REQUEST['title']; | |
$title = $_REQUEST['title']; | |
$content= $_REQUEST['content']; | |
// If the page doesn't already exist, then create it | |
if( null == get_page_by_title( $title ) ) { | |
// Set the post ID so that we know the post was created successfully | |
$post_returned = wp_update_post( | |
array( | |
'ID' =>$post_id, | |
'comment_status' => 'closed', | |
'ping_status' => 'closed', | |
'post_author' => $author_id, | |
'post_name' => $slug, | |
'post_title' => $title, | |
'post_content' => $content, | |
'post_status' => 'publish', | |
'post_type' => 'post' | |
) | |
); | |
} else { | |
$post_returned=false; | |
} | |
if ($post_returned==false){ | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'NOK', "Message" => "Post failed"); | |
} else { | |
$val = array("controller" => "myapiclass", "action" =>"SocialSignUp", "status" => 'OK',"Message" => "Post updated successfully.Check Samachar section", "post_id" => $post_id); | |
} | |
header('Content-type: application/json'); | |
$getval = json_encode($val); | |
echo $getval; | |
} | |
public function getUsers(){ | |
$dt=array(); | |
$users = get_users( array( 'fields' => array( 'ID' ) ) ); | |
foreach($users as $user_id){ | |
$dt[]= get_user_meta ($user_id->ID)['device_token'][0]; | |
} | |
$this->sendMessage("Jambh Vaani","New notification",$dt); | |
} | |
public function sendMessage($title,$body,$target){ | |
// echo $title.$body.$target; | |
//FCM api URL | |
$url = 'https://fcm.googleapis.com/fcm/send'; | |
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key | |
$server_key = '<USE YOUR OWN KEY>'; | |
$fields = array(); | |
$fields['data'] = array('name'=>'sanjeev here'); | |
$fields['notification']=array ( | |
"body" => $body, | |
"title" => $title, | |
"icon" => "myicon" | |
); | |
if(is_array($target)){ | |
$fields['registration_ids'] = $target; | |
}else{ | |
$fields['to'] = $target; | |
} | |
//header with content_type api key | |
$headers = array( | |
'Content-Type:application/json', | |
'Authorization:key='.$server_key | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); | |
$result = curl_exec($ch); | |
if ($result === FALSE) { | |
/*die('FCM Send Error: ' . curl_error($ch));*/ | |
} | |
curl_close($ch); | |
echo "<pre>"; | |
print_r($result); | |
} | |
public function generateThumbnail($image_url,$post_id){ | |
$upload_dir = wp_upload_dir(); | |
$image_data = file_get_contents($image_url); | |
$filename = basename($image_url); | |
if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; | |
else $file = $upload_dir['basedir'] . '/' . $filename; | |
file_put_contents($file, $image_data); | |
$wp_filetype = wp_check_filetype($filename, null ); | |
$attachment = array( | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => sanitize_file_name($filename), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $file, $post_id ); | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $file ); | |
$res1= wp_update_attachment_metadata( $attach_id, $attach_data ); | |
$res2= set_post_thumbnail( $post_id, $attach_id ); | |
} | |
public function updateDeviceToken(){ | |
$device_token = $_REQUEST['device_token']; | |
global $wpdb; | |
$table_name = $wpdb->prefix."device_token"; | |
$my_query = $wpdb->get_results( "SELECT * FROM $table_name where device_token='$device_token'"); | |
if($wpdb->num_rows>0)return; | |
$wpdb->insert($table_name, array( | |
'device_token' => $device_token | |
),array( | |
'%s') | |
); | |
} | |
} | |
$data = new myapiclass(); | |
$method = $_REQUEST['method']; | |
$slug = $_REQUEST['slug']; | |
$lat = $_REQUEST['lat']; | |
$lng = $_REQUEST['lng']; | |
echo $data->$method($slug,$lat,$lng); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment