/**
  * Hook into admin AJAX to delete a link
  *
  * @access public
  * @return void
  */
 public function run()
 {
     $post_id = $_POST['id'];
     // Check nonce
     check_ajax_referer('post-connector-ajax-nonce-omgrandomword', 'nonce');
     // Check if user is allowed to do this
     if (!current_user_can(SP_Cap_Manager::get_capability($post_id))) {
         return;
     }
     //  Load post
     $target_post = get_post($post_id);
     // Only delete post type we control
     if ($target_post->post_type != SP_Constants::CPT_LINK) {
         return;
     }
     // Delete link
     $post_link_manager = new SP_Post_Link_Manager();
     $post_link_manager->delete($target_post->ID);
     // Generate JSON response
     $response = json_encode(array('success' => true));
     header('Content-Type: application/json');
     echo $response;
     // Bye
     exit;
 }
 /**
  * Hook into admin AJAX to save our custum menu order
  *
  * @access public
  * @return void
  */
 public function metabox_manage_save_order()
 {
     global $wpdb;
     $items = explode(',', $_POST['sp_items']);
     // Check if there are items posted
     if (count($items) == 0) {
         return;
     }
     // Check nonce
     check_ajax_referer('post-connector-ajax-nonce-omgrandomword', 'nonce');
     // Check if user is allowed to do this
     if (!current_user_can(SP_Cap_Manager::get_capability($items[0]))) {
         return;
     }
     // Check if the items are set
     if (!isset($_POST['sp_items'])) {
         return;
     }
     // Change order
     $counter = 0;
     foreach ($items as $item_id) {
         $wpdb->update($wpdb->posts, array('menu_order' => $counter), array('ID' => $item_id));
         $counter++;
     }
     // Generate JSON response
     $response = json_encode(array('success' => true));
     header('Content-Type: application/json');
     echo $response;
     // Bye
     exit;
 }
 /**
  * Handle the bulk creation of links
  */
 private function handle_bulk_link()
 {
     if (isset($_POST['sp_bulk'])) {
         // Get parent
         $parent = SP_Parent_Param::get_current_parent($_GET['sp_parent']);
         // Check if user is allowed to do this
         if (!current_user_can(SP_Cap_Manager::get_capability($parent))) {
             return;
         }
         // Post Link Manager
         $post_link_manager = new SP_Post_Link_Manager();
         if (count($_POST['sp_bulk']) > 0) {
             foreach ($_POST['sp_bulk'] as $bulk_post) {
                 // Check what way we're linking
                 if (1 == $parent[2]) {
                     // Create a 'backwards' child < parent link
                     $post_link_manager->add($_GET['sp_pt_link'], $bulk_post, $parent[0]);
                 } else {
                     // Create a 'normal' parent > child link
                     $post_link_manager->add($_GET['sp_pt_link'], $parent[0], $bulk_post);
                 }
             }
         }
         // Send back
         $redirect_url = get_admin_url() . "post.php?post={$parent[0]}&action=edit";
         // Check if parent as a ptl
         if (isset($parent[1]) && $parent[1] != '') {
             $redirect_url .= '&sp_pt_link=' . $parent[1];
         }
         // Check if there are any parents left
         $sp_parent_rest = SP_Parent_Param::strip_sp_parent_parent($_GET['sp_parent']);
         if ($sp_parent_rest != '') {
             $redirect_url .= '&sp_parent=' . $sp_parent_rest;
         }
         wp_redirect($redirect_url);
         exit;
     }
 }
 /**
  * Save hook, create the link
  *
  * @param int    $post_id
  * @param object $post
  *
  * @access public
  * @return void
  */
 public function save($post_id, $post)
 {
     // Check nonce
     if (!isset($_POST['sp_meta_nonce']) || !wp_verify_nonce($_POST['sp_meta_nonce'], plugin_basename(__FILE__))) {
         return;
     }
     // Check if user is allowed to do this
     if (!current_user_can(SP_Cap_Manager::get_capability($post_id))) {
         return;
     }
     // Verify post is not a revision
     if (wp_is_post_revision($post_id)) {
         return $post_id;
     }
     // Check autosave
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return $post_id;
     }
     // Check meta settings
     if (!isset($_POST['sp_meta'])) {
         return;
     }
     // Check post exists
     if ($post == null) {
         return;
     }
     // Check if it's a publish
     if ($post->post_status != 'publish') {
         return;
     }
     // Check if allowed
     $this->check_if_allowed($_POST['sp_pt_link']);
     // Get parent id
     $parent = SP_Parent_Param::get_current_parent($_POST['sp_parent']);
     // Check if post type equals the child or parent post type
     if ('1' == $parent[2]) {
         if ($post->post_type != $this->ptl->get_parent()) {
             return;
         }
     } else {
         if ($post->post_type != $this->ptl->get_child()) {
             return;
         }
     }
     // Create link if it's a new post
     if (isset($_POST['sp_new'])) {
         // Unhook the save hook to avoid an infinite loop
         remove_action('save_post', array($this, 'save'), 99);
         // Create link
         $post_link_manager = new SP_Post_Link_Manager();
         // Check which way to link
         if ('1' == $parent[2]) {
             // Create a backwards link
             $post_link_manager->add($_POST['sp_pt_link'], $post_id, $parent[0]);
         } else {
             // Create a 'normal' link
             $post_link_manager->add($_POST['sp_pt_link'], $parent[0], $post_id);
         }
         // Re-hook hook
         add_action('save_post', array($this, 'save'), 99);
     }
     // Send back
     $redirect_url = get_admin_url() . "post.php?post={$parent[0]}&action=edit";
     // Check if parent as a ptl
     if (isset($parent[1]) && $parent[1] != '') {
         $redirect_url .= '&sp_pt_link=' . $parent[1];
     }
     // Check if there are any parents left
     $sp_parent_rest = SP_Parent_Param::strip_sp_parent_parent($_POST['sp_parent']);
     if ($sp_parent_rest != '') {
         $redirect_url .= '&sp_parent=' . $sp_parent_rest;
     }
     // Redirecting user
     wp_redirect($redirect_url);
     exit;
 }