Advertisement
Guest User

Wordpress JSON API delete_post controller • by Freshcode.nl

a guest
Feb 17th, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Controller name: Posts Extra
  5. Controller description: delete_post method by Dave van Hoorn • www.freshcode.nl
  6. */
  7.  
  8. class JSON_API_Postsextra_Controller {
  9.    
  10.     public function delete_post() {
  11.        
  12.         // Global json_api needs to be initialized
  13.         global $json_api;
  14.        
  15.         // Check if the current user can edit_posts, if not, error
  16.         if (!current_user_can('edit_posts')) {
  17.             $json_api->error("You need to login with a user capable of creating posts to delete a post.");
  18.         }
  19.        
  20.         // Only allow the removal of posts if a nonce (number used once) is included, for safety
  21.         if (!$json_api->query->nonce) {
  22.             $json_api->error("You must include a 'nonce' value to delete posts. Use the `get_nonce` Core API method.");
  23.         }
  24.        
  25.         // Get a nonce for the deletion of a post
  26.         $nonce_id = $json_api->get_nonce_id('postsextra', 'delete_post');
  27.        
  28.         // If the nonce isn't correct, say someone has to get one
  29.         if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
  30.             $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
  31.         }
  32.        
  33.         // Prevent caching?
  34.         nocache_headers();
  35.        
  36.         // Get the ID from the URL Query
  37.         $id = $json_api->query->id;
  38.        
  39.         // Get the post with the given ID to check if it still exists
  40.         $postexists = get_post($id);
  41.        
  42.         // If the post exists, delete it
  43.         if ($postexists){
  44.            
  45.             // Delete the post
  46.             wp_delete_post( $id, true );
  47.            
  48.             // Let the user know the post has been deleted
  49.             return array(
  50.                 "Message" => "Post with ID: $id has been deleted"
  51.             );
  52.        
  53.         // Post with the Id was never found, did it actually exist?
  54.         } else {
  55.            
  56.             // Post wasn't found, so return a message
  57.             return array(
  58.                 "Message" => "Post with ID: $id wasn't found, does it actually exist?"
  59.             );
  60.         }  
  61.     }
  62.    
  63. }
  64.  
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement