Skip to content

Instantly share code, notes, and snippets.

@chaps
Created March 14, 2016 05:22
Show Gist options
  • Save chaps/eec3769560c7d8debe59 to your computer and use it in GitHub Desktop.
Save chaps/eec3769560c7d8debe59 to your computer and use it in GitHub Desktop.
"Web Service" for toggling the status of a favorite POST from wordpress and wordpress single favorite plugin
<?php
$post_id = null;
$site_id = null;
$host = "http://localhost/wordpress";
$url = "/wp-admin/admin-ajax.php";
echo $_SERVER['REQUEST_METHOD'];
var_dump($_GET);
function parse_post($siteid, $postid){
if($_SERVER['REQUEST_METHOD'] != "POST"){
return false;
}
if( $_POST['siteid'] == null){
return false;
}
if( $_POST['postid'] == null){
return false;
}
return true;
}
function post($host, $url , $url_params, $data=null){
$headers = "Content-type: application/x-www-form-urlencoded\r\n";
foreach ($_COOKIE as $name => $value){
$cookies .= "$name=$value;";
}
$headers .= "Cookie: $cookies\r\n";
$sUrl = $host.$url.$action;
$params = array('http' => array(
'method' => 'POST',
'header' => $headers
));
if( $data != null){
$postdata = http_build_query($data);
$params["http"]["content"] = $postdata;
}
$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if (!$fp)
{
throw new Exception("Problem with $sUrl, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
return $response;
}
function is_favorite($favorites, $siteid, $postid){
foreach ($favorites as $site_favorite) {
if( $site_favorite->{'site_id'} == $siteid){
foreach ($site_favorite->{'posts'} as $id => $value) {
if($postid == $id){
return true;
}
}
}
}
return false;
}
function toggle_favorite($host, $url, $site_id, $post_id){
/*GET NONCE*/
$action = "?action=simplefavorites_nonce";
$data = array('action' => "simplefavorites_nonce");
$tjson = json_decode(post($host, $url, $action, $data));
$nonce = $tjson -> {"nonce"};
/*GET FAVORITES*/
$data = array('action' => "simplefavorites_array");
$action = "?action=simplefavorites_array";
$response = post($host, $url, $action, $data);
$tjson = json_decode($response);
$favorites = $tjson -> {"favorites"};
/*CHECK POST STATUS*/
$post_is_favorite = is_favorite($favorites, $site_id, $post_id);
if($post_is_favorite){
$status = "inactive";
}else{
$status = "active";
}
/*SET POST STATUS*/
$data = array(
'action' => "simplefavorites_favorite",
'postid' => $post_id,
'siteid' => $site_id,
'nonce' => $nonce,
'status' => $status
);
$action = "?action=simplefavorites_favorite";
$response = post($host, $url, $action, $data);
$tjson = json_decode($response);
return $tjson;
}
function test($host, $url){
$site_id = 1;
$post_id = 1;
return toggle_favorite($host, $url, $site_id, $post_id);
}
if(parse_post()){
$site_id = $_POST['siteid'];
$post_id = $_POST['postid'];
toggle_favorite($host, $url, $site_id, $post_id);
}else{
//Return {success:false, error_msg:""}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment