/** * 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('relation' => 'OR', array('key' => SP_Constants::PM_PTL_APDC, 'value' => '1'), array('key' => SP_Constants::PM_PTL_APDP, 'value' => '1'))); // Get the connections $relations = $ptl_manager->get_connections($args); // Check relations if (count($relations) > 0) { // Store children and parents $children_relations = array(); $parent_relations = array(); // 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; } // Check if this relation allows parents links to show if ('1' === $relation->get_after_post_display_parents()) { $parent_relations[] = $relation; } } // Are the relations that want to show linked child posts if (count($children_relations) > 0) { // Opening the wrapper div $content .= "<div class='pc-post-children'>\n"; foreach ($children_relations as $children_relation) { // Generate list and append is there's content $children_list = $pl_manager->generate_children_list($children_relation->get_slug(), $post_id, true, $children_relation->get_after_post_display_children_excerpt(), 'b', $children_relation->get_after_post_display_children_image()); if ('' != $children_list) { $content .= "<h3>" . $children_relation->get_title() . "</h3> \n"; $content .= $children_list; } } // Close the wrapper div $content .= "</div>\n"; } // Are the relations that want to show linked parent posts if (count($parent_relations) > 0) { // Opening the wrapper div $content .= "<div class='pc-post-parents'>\n"; foreach ($parent_relations as $parent_relation) { // Get the linked posts $pc_posts = $pl_manager->get_parents($parent_relation->get_slug(), $post_id); if (count($pc_posts) > 0) { $content .= $this->create_post_list($parent_relation->get_slug(), $parent_relation->get_title(), $pc_posts, false, false); } } // Close the wrapper div $content .= "</div>\n"; } } return $content; }
/** * 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'] : ''; // $parent_posts = Post_Connector::API()->get_parents( $this->ptl->get_slug(), $post->ID ); // Create a Post Link Manager object $post_link_manager = new SP_Post_Link_Manager(); // Get the parents $parent_posts = $post_link_manager->get_parents($this->ptl->get_slug(), $post->ID); $parent_post_type = get_post_type_object($this->ptl->get_parent()); echo "<div class='pt_button_holder'>\n"; // Check if user is allowed to add new children if ($this->ptl->get_add_new() == '1') { // Build the Post Connector link existing post URL $url = get_admin_url() . "post-new.php?post_type=" . $this->ptl->get_parent() . "&sp_parent=" . SP_Parent_Param::generate_sp_parent_param($post->ID, $sp_pt_link, $sp_parent, 1) . "&sp_pt_link=" . $this->ptl->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'), $parent_post_type->labels->singular_name); echo "</a>"; echo "</span>\n"; } // Check if user is allowed to add existing children if ($this->ptl->get_add_existing() == '1') { // Build the Post Connector link existing post URL $url = get_admin_url() . "admin.php?page=link_post_screen&sp_parent=" . SP_Parent_Param::generate_sp_parent_param($post->ID, $sp_pt_link, $sp_parent, 1) . "&sp_pt_link=" . $this->ptl->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 existing %s', 'post-connector'), $parent_post_type->labels->singular_name); echo "</a>"; echo "</span>\n"; } echo "</div>\n"; if (count($parent_posts) > 0) { echo "<table class='wp-list-table widefat fixed pages pt_table_manage'>\n"; echo "<tbody>\n"; $i = 0; foreach ($parent_posts as $link_id => $parent) { $child_id = $parent->ID; $edit_url = get_admin_url() . "post.php?post={$child_id}&action=edit&sp_parent=" . SP_Parent_Param::generate_sp_parent_param($post->ID, $sp_pt_link, $sp_parent, 1) . "&sp_pt_link=" . $this->ptl->get_id(); echo "<tr id='{$link_id}'>\n"; echo "<td>"; echo "<strong><a href='{$edit_url}' class='row-title' title='{$parent->post_title}'>{$parent->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'), $parent_post_type->labels->name); } // Reset Post Data wp_reset_postdata(); echo "</div>\n"; }