/**
  * 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;
 }
 public function run($post_id)
 {
     // Remove Hook
     remove_action($this->tag, array($this, 'run'), $this->priority, $this->args);
     // Delete all links related to post_id
     $post_link_manager = new SP_Post_Link_Manager();
     $post_link_manager->delete_links_related_to($post_id);
     // Re-add Hook
     add_action($this->tag, array($this, 'run'), $this->priority, $this->args);
 }
 public function run($atts)
 {
     extract(shortcode_atts($this->arguments, $atts));
     if ($slug == "") {
         return;
     }
     if (null === $parent) {
         $parent = get_the_ID();
     }
     $post_link_manager = new SP_Post_Link_Manager();
     return $post_link_manager->generate_children_list($slug, $parent, $link, $excerpt, 'b', $image);
 }
 /**
  * the_content filter that will add linked posts to the bottom of the main post content
  *
  * @param $content
  *
  * @return string
  */
 public function run($content)
 {
     /**
      * Wow, what's going on here?! Well, setup_postdata() sets a lot of variables but does not change the $post variable.
      * All checks return the main queried ID but we want to check if this specific filter call is the for the 'main' content.
      * The method setup_postdata() does global and set the $id variable, so we're checking that.
      */
     global $id;
     // Only run on single
     if (!is_singular() || !is_main_query() || $id != get_queried_object_id()) {
         return $content;
     }
     $ptl_manager = new SP_Connection_Manager();
     // Add a meta query so we only get relations that have the PM_PTL_APDC or PM_PTL_APDP set to true (1)
     $args = array('meta_query' => array(array('key' => SP_Constants::PM_PTL_APDC, 'value' => '1')));
     // Get the connections
     $relations = $ptl_manager->get_connections($args);
     // Check relations
     if (count($relations) > 0) {
         // Post Link Manager
         $pl_manager = new SP_Post_Link_Manager();
         // Current post ID
         $post_id = get_the_ID();
         // Loop relations
         foreach ($relations as $relation) {
             // Check if this relation allows children links to show
             if ('1' === $relation->get_after_post_display_children()) {
                 $children_relations[] = $relation;
             }
         }
         // Opening the wrapper div
         $content .= "<div class='pc-post-children'>\n";
         foreach ($relations as $relation) {
             // Get the linked posts
             $pc_posts = $pl_manager->get_children($relation->get_slug(), $post_id);
             if (count($pc_posts) > 0) {
                 $content .= $this->create_post_list($relation->get_slug(), $relation->get_title(), $pc_posts);
             }
         }
         // Close the wrapper div
         $content .= "</div>\n";
     }
     return $content;
 }
 public function widget($args, $instance)
 {
     // Setup widget vars
     $instance = array_merge($this->default_vars, $instance);
     // Don't output anything if there's no postlink
     if ($instance['postlink'] == '') {
         return;
     }
     // Don't output the widget on the frontpage if the frontpage show_on_front is posts
     if (is_front_page() && false === is_page()) {
         return;
     }
     // Use custom post of parent is null
     if (null === $instance['parent']) {
         $instance['parent'] = get_the_ID();
     }
     // Load the PTL
     $post_type_link_manager = new SP_Connection_Manager();
     $connection = $post_type_link_manager->get_connection($instance['postlink']);
     // Return if there is no connection
     if (null == $connection) {
         return;
     }
     // Only display widget on pages where the post type is the $instance['parent'] equals the Connection post type
     if ($connection->get_parent() != get_post_type($instance['parent'])) {
         return;
     }
     // Setup the post link manager
     $post_link_manager = new SP_Post_Link_Manager();
     // Generate the widget content
     $widget_content = $post_link_manager->generate_children_list($connection->get_slug(), $instance['parent'], $instance['link'], $instance['excerpt'], 'b', $instance['thumbnail']);
     // Don't ouput the widget if there is no widget content
     if ('' == $widget_content) {
         return;
     }
     // Output the widget
     echo $args['before_widget'];
     echo $args['before_title'] . $instance['title'] . $args['after_title'] . "\n";
     echo $widget_content;
     echo $args['after_widget'];
 }
 /**
  * Method to delete a PostTypeLink
  *
  * @access public
  *
  * @param int $id
  *
  * @return boolean
  */
 public function delete($id)
 {
     /**
      * Action: 'pc_before_connection_delete' - Action that is ran before the connection is deleted
      *
      * @api int $id The Connection id
      */
     do_action('pc_before_connection_delete', $id);
     // Backwards compatibility
     do_action('sp_before_post_type_link_delete', $id);
     // Can't remove this because of BC
     // Delete all links
     $post_link_manager = new SP_Post_Link_Manager();
     $links = $post_link_manager->get_all_links($id);
     if (count($links) > 0) {
         foreach ($links as $link) {
             wp_delete_post($link->ID, true);
         }
     }
     // Delete the connection
     wp_delete_post($id, true);
     /**
      * Action: 'pc_after_connection_delete' - Action that is ran after the connection is deleted
      *
      * @api int $id The Connection id
      */
     do_action('pc_after_connection_delete', $id);
     // Backwards compatibility
     do_action('sp_after_post_type_link_delete', $id);
     // Can't remove this because of BC
     return true;
 }
 /**
  * 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;
 }
 /**
  * Metabox content
  *
  * @access public
  * @return void
  */
 public function callback($post)
 {
     echo "<div class='sp_mb_manage'>\n";
     // Add nonce
     echo "<input type='hidden' name='sp-ajax-nonce' id='sp-ajax-nonce' value='" . wp_create_nonce('post-connector-ajax-nonce-omgrandomword') . "' />\n";
     // Output plugin URL in hidden val
     echo "<input type='hidden' name='sp-dir-img' id='sp-dir-img' value='" . plugins_url('/core/assets/images/', Post_Connector::get_plugin_file()) . "' />\n";
     // Setup vars
     $sp_parent = isset($_GET['sp_parent']) ? $_GET['sp_parent'] : '';
     $sp_pt_link = isset($_GET['sp_pt_link']) ? $_GET['sp_pt_link'] : '';
     // Create a Post Link Manager object
     $post_link_manager = new SP_Post_Link_Manager();
     // Get the children
     $children = $post_link_manager->get_children($this->connection->get_slug(), $post->ID);
     $child_post_type = get_post_type_object($this->connection->get_child());
     echo "<div class='pt_button_holder'>\n";
     // Check if user is allowed to add new children
     if ($this->connection->get_add_new() == '1') {
         // Build the Post Connector link existing post URL
         $url = get_admin_url() . "post-new.php?post_type=" . $this->connection->get_child() . "&amp;sp_parent=" . SP_Parent_Param::generate_sp_parent_param($post->ID, $sp_pt_link, $sp_parent, 0) . "&amp;sp_pt_link=" . $this->connection->get_id();
         // WPML check
         if (isset($_GET['lang'])) {
             $url .= "&lang=" . $_GET['lang'];
         }
         echo "<span id='view-post-btn'>";
         echo "<a href='" . $url . "' class='button'>";
         printf(__('Add new %s', 'post-connector'), $child_post_type->labels->singular_name);
         echo "</a>";
         echo "</span>\n";
     }
     // Check if user is allowed to add existing children
     if ($this->connection->get_add_existing() == '1') {
         // Build the Post Connector link existing post URL
         $url = get_admin_url() . "admin.php?page=link_post_screen&amp;sp_parent=" . SP_Parent_Param::generate_sp_parent_param($post->ID, $sp_pt_link, $sp_parent, 0) . "&amp;sp_pt_link=" . $this->connection->get_id();
         // WPML check
         if (isset($_GET['lang'])) {
             $url .= "&amp;lang=" . $_GET['lang'];
         }
         /**
          * Action: 'pc_meta_box_manage_add_existing_url' - Allow adjusting of 'add existing' URL
          *
          * @api string $url The URL
          * @param SP_Connection $connection The connection
          */
         $url = apply_filters('pc_meta_box_manage_add_existing_url', $url, $this->connection);
         echo "<span id='view-post-btn'>";
         echo "<a href='" . $url . "' class='button'>";
         printf(__('Add existing %s', 'post-connector'), $child_post_type->labels->singular_name);
         echo "</a>";
         echo "</span>\n";
     }
     echo "</div>\n";
     if (count($children) > 0) {
         $table_classes = 'wp-list-table widefat fixed pages pt_table_manage';
         /**
          * Action: 'pc_meta_box_manage_table_classes' - Allow adjusting meta box manage table classes
          *
          * @api string $table_classes The table classes
          * @param SP_Connection $connection The connection
          */
         $table_classes = apply_filters('pc_meta_box_manage_table_classes', $table_classes, $this->connection);
         // Managet table
         echo "<table class='" . $table_classes . "'>\n";
         echo "<tbody>\n";
         $i = 0;
         foreach ($children as $link_id => $child) {
             $child_id = $child->ID;
             $edit_url = get_admin_url() . "post.php?post={$child_id}&amp;action=edit&amp;sp_parent=" . SP_Parent_Param::generate_sp_parent_param($post->ID, $sp_pt_link, $sp_parent, 0) . "&sp_pt_link=" . $this->connection->get_id();
             echo "<tr id='{$link_id}'>\n";
             echo "<td>";
             echo "<strong><a href='{$edit_url}' class='row-title' title='{$child->post_title}'>{$child->post_title}</a></strong>\n";
             echo "<div class='row-actions'>\n";
             echo "<span class='edit'><a href='{$edit_url}' title='" . __('Edit this item', 'post-connector') . "'>";
             _e('Edit', 'post-connector');
             echo "</a> | </span>";
             echo "<span class='trash'><a class='submitdelete' title='" . __('Delete this item', 'post-connector') . "' href='javascript:;'>";
             _e('Delete', 'post-connector');
             echo "</a></span>";
             echo "</div>\n";
             echo "</td>\n";
             echo "</tr>\n";
             $i++;
         }
         echo "</tbody>\n";
         echo "</table>\n";
     } else {
         echo '<br/>';
         printf(__('No %s found.', 'post-connector'), $child_post_type->labels->name);
     }
     // Reset Post Data
     wp_reset_postdata();
     echo "</div>\n";
 }