/**
  * Hook into admin AJAX to delete a link
  *
  * @access public
  * @return void
  */
 public function run()
 {
     // id,
     if (!isset($_POST['id'])) {
         exit;
     }
     // Post id into $post_id
     $post_id = $_POST['id'];
     // Check nonce
     check_ajax_referer('rp4wp-ajax-nonce-omgrandomword', 'nonce');
     // Check if user is allowed to do this
     if (!current_user_can('edit_posts')) {
         return;
     }
     //  Load post
     $target_post = get_post($post_id);
     // Only delete post type we control
     if ($target_post->post_type != RP4WP_Constants::LINK_PT) {
         return;
     }
     // Delete link
     $post_link_manager = new RP4WP_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;
 }
 /**
  * Handle the bulk creation of links
  */
 private function handle_bulk_link()
 {
     if (isset($_POST['rp4wp_bulk']) && isset($_GET['rp4wp_parent'])) {
         // Get parent
         $parent = $_GET['rp4wp_parent'];
         // Check if user is allowed to do this
         $this->check_if_allowed();
         // Post Link Manager
         $post_link_manager = new RP4WP_Post_Link_Manager();
         if (count($_POST['rp4wp_bulk']) > 0) {
             foreach ($_POST['rp4wp_bulk'] as $bulk_post) {
                 // Create link
                 $post_link_manager->add($parent, $bulk_post);
             }
         }
         // Send back
         $redirect_url = get_admin_url() . "post.php?post={$parent}&action=edit";
         // WPML check
         if (isset($_GET['lang'])) {
             $redirect_url .= "&lang=" . $_GET['lang'];
         }
         wp_redirect($redirect_url);
         exit;
     }
 }
 /**
  * Output the shortcode
  *
  * @since  1.3.0
  * @access public
  *
  */
 public function output($atts)
 {
     $atts = shortcode_atts(array('id' => get_the_ID(), 'limit' => -1), $atts);
     // Post Link Manager
     $pl_manager = new RP4WP_Post_Link_Manager();
     // Generate the children list
     return $pl_manager->generate_children_list($atts['id'], $atts['limit']);
 }
 /**
  * Output the shortcode
  *
  * @since  1.3.0
  * @access public
  *
  */
 public function output()
 {
     // Get the current ID
     $id = get_the_ID();
     // Post Link Manager
     $pl_manager = new RP4WP_Post_Link_Manager();
     // Generate the children list
     echo $pl_manager->generate_children_list($id);
 }
 /**
  * Metabox content
  *
  * @access public
  * @return void
  */
 public function callback($post)
 {
     echo "<div class='rp4wp_mb_manage'>\n";
     // Add nonce
     echo "<input type='hidden' name='rp4wp-ajax-nonce' id='rp4wp-ajax-nonce' value='" . wp_create_nonce('rp4wp-ajax-nonce-omgrandomword') . "' />\n";
     // Output plugin URL in hidden val
     echo "<input type='hidden' name='rp4wp-dir-img' id='rp4wp-dir-img' value='" . plugins_url('/assets/images/', RP4WP::get_plugin_file()) . "' />\n";
     // Create a Post Link Manager object
     $post_link_manager = new RP4WP_Post_Link_Manager();
     // Get the children
     $children = $post_link_manager->get_children($post->ID);
     echo "<div class='rp4wp_button_holder'>\n";
     // Build the related post link
     $url = get_admin_url() . "admin.php?page=rp4wp_link_related&amp;rp4wp_parent=" . $post->ID;
     // WPML check
     if (isset($_GET['lang'])) {
         $url .= "&amp;lang=" . $_GET['lang'];
     }
     echo "<span id='view-post-btn'>";
     echo "<a href='" . $url . "' class='button button-primary'>";
     _e('Add Related Posts', 'related-posts-for-wp');
     echo "</a>";
     echo "</span>\n";
     echo "</div>\n";
     if (count($children) > 0) {
         // Managet table
         echo "<table class='wp-list-table widefat fixed pages rp4wp_table_manage sortable'>\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;rp4wp_parent={$post->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 Post', 'related-posts-for-wp') . "'>";
             _e('Edit Post', 'related-posts-for-wp');
             echo "</a> | </span>";
             echo "<span class='trash'><a class='submitdelete' title='" . __('Unlink Related Post', 'related-posts-for-wp') . "' href='javascript:;'>";
             _e('Unlink Related Post', 'related-posts-for-wp');
             echo "</a></span>";
             echo "</div>\n";
             echo "</td>\n";
             echo "</tr>\n";
             $i++;
         }
         echo "</tbody>\n";
         echo "</table>\n";
     } else {
         echo '<br/>';
         _e('No related posts found.', 'related-posts-for-wp');
     }
     // Reset Post Data
     wp_reset_postdata();
     echo "</div>\n";
 }
 /**
  * Link x related posts to post
  *
  * @param $post_id
  * @param $amount
  *
  * @return boolean
  */
 public function link_related_post($post_id, $amount)
 {
     $related_posts = $this->get_related_posts($post_id, $amount);
     if (count($related_posts) > 0) {
         $post_link_manager = new RP4WP_Post_Link_Manager();
         foreach ($related_posts as $related_post) {
             $post_link_manager->add($post_id, $related_post->ID);
         }
     }
     update_post_meta($post_id, RP4WP_Constants::PM_POST_AUTO_LINKED, 1);
     return true;
 }
Example #7
0
function get_related_posts($id = NULL)
{
    // Get the current ID if ID not set
    if (!$id) {
        $id = get_the_ID();
    }
    // Post Link Manager
    $pl_manager = new RP4WP_Post_Link_Manager();
    // Get list of related posts
    $related_posts = $pl_manager->get_children($id);
    ob_start();
    include 'templates/related-posts.php';
    return ob_get_clean();
}
 /**
  * Generate the Related Posts for WordPress children list
  *
  * @param bool $id
  * @param bool $output
  *
  * @since  1.0.0
  * @access public
  *
  * @return string
  */
 function rp4wp_children($id = false, $output = true)
 {
     // Get the current ID if ID not set
     if (false === $id) {
         $id = get_the_ID();
     }
     // Post Link Manager
     $pl_manager = new RP4WP_Post_Link_Manager();
     // Generate the children list
     $content = $pl_manager->generate_children_list($id);
     // Output or return the content
     if ($output) {
         echo $content;
     } else {
         return $content;
     }
 }
 public function widget($args, $instance)
 {
     // Get the current ID
     $id = get_the_ID();
     // Not on frontpage please
     if (is_front_page() && false === is_page()) {
         return;
     }
     // Post Link Manager
     $pl_manager = new RP4WP_Post_Link_Manager();
     // Get content
     $widget_content = $pl_manager->generate_children_list($id);
     // Only display if there's content
     if ('' != $widget_content) {
         // Output the widget
         echo $args['before_widget'];
         echo $widget_content;
         echo $args['after_widget'];
     }
 }
 public function run($new_status, $old_status, $post)
 {
     // verify this is not an auto save routine.
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return;
     }
     // Post status must be publish
     if ('publish' != $new_status) {
         return;
     }
     // Check if this post type is installed
     $pt_manager = new RP4WP_Post_Type_Manager();
     if (!$pt_manager->is_post_type_installed($post->post_type)) {
         return;
     }
     // Is automatic linking enabled?
     if (1 != RP4WP::get()->settings['general_' . $post->post_type]->get_option('automatic_linking')) {
         return;
     }
     // Check if the current post is already auto linked
     if (1 != get_post_meta($post->ID, RP4WP_Constants::PM_POST_AUTO_LINKED, true)) {
         // Post Link Manager
         $pl_manager = new RP4WP_Post_Link_Manager();
         // Get automatic linking post amount
         $automatic_linking_post_amount = RP4WP::get()->settings['general_' . $post->post_type]->get_option('automatic_linking_post_amount');
         // Count already linked posts
         $already_linked_posts = $pl_manager->get_children_count($post->ID);
         // Subtract already linked post count from posts to link amount
         if (count($already_linked_posts) > 0) {
             $automatic_linking_post_amount = $automatic_linking_post_amount - $already_linked_posts;
         }
         // Related Posts Manager
         $related_post_manager = new RP4WP_Related_Post_Manager();
         // Link related posts
         $related_post_manager->link_related_post($post->ID, $post->post_type, $automatic_linking_post_amount);
         // Set the auto linked meta
         update_post_meta($post->ID, RP4WP_Constants::PM_POST_AUTO_LINKED, 1);
     }
 }
 /**
  * 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;
     }
     // Allow disabling content filter
     if (false === apply_filters('rp4wp_append_content', true)) {
         return $content;
     }
     // Post Link Manager
     $pl_manager = new RP4WP_Post_Link_Manager();
     // Generate the content
     $content .= $pl_manager->generate_children_list($id);
     // Return content
     return $content;
 }
 /**
  * Link x related posts to post
  *
  * @param $post_id
  * @param $amount
  *
  * @return boolean
  */
 public function link_related_post($post_id, $amount)
 {
     $related_posts = $this->get_related_posts($post_id, $amount);
     if (count($related_posts) > 0) {
         global $wpdb;
         $post_link_manager = new RP4WP_Post_Link_Manager();
         $batch_data = array();
         foreach ($related_posts as $related_post) {
             $batch_data[] = $post_link_manager->add($post_id, $related_post->ID, true);
         }
         // Do batch insert
         $wpdb->query("INSERT INTO `{$wpdb->posts}`\n\t\t\t\t\t\t(`post_date`,`post_date_gmt`,`post_content`,`post_title`,`post_type`,`post_status`)\n\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t" . implode(',', array_map(array($this, 'batch_data_get_post'), $batch_data)) . "\n\t\t\t\t\t\t");
         // Get the first post link insert ID
         $pid = $wpdb->insert_id;
         // Set the correct ID's for batch meta insert
         foreach ($batch_data as $bk => $bd) {
             $batch_data[$bk]['meta'] = array_map(array($this, 'batch_data_set_pid'), $bd['meta'], array_fill(0, count($bd['meta']), $pid));
             $pid++;
         }
         // Insert all the meta
         $wpdb->query("INSERT INTO `{$wpdb->postmeta}`\n\t\t\t\t(`post_id`,`meta_key`,`meta_value`)\n\t\t\t\tVALUES\n\t\t\t\t" . implode(',', array_map(array($this, 'batch_data_get_meta'), $batch_data)) . "\n\t\t\t\t");
     }
     update_post_meta($post_id, RP4WP_Constants::PM_POST_AUTO_LINKED, 1);
     return true;
 }
 /**
  * Generate the related posts list
  *
  * @param int $post_id
  * @param string $template
  * @param int $limit
  *
  * @return string
  */
 public function generate_related_posts_list($post_id, $template = 'related-posts-default.php', $limit = -1)
 {
     // the output
     $output = '';
     // get the post type
     $post_type = get_post_type($post_id);
     // The settings object
     $pt_settings = RP4WP()->settings['general_' . $post_type];
     // Post Link Manager
     $pl_manager = new RP4WP_Post_Link_Manager();
     // Get the children
     $related_posts = $pl_manager->get_children($post_id, array('posts_per_page' => $limit));
     // Count
     if (count($related_posts) > 0) {
         // Manager Template
         $manager_template = new RP4WP_Manager_Template();
         // Load the template output
         ob_start();
         $manager_template->get_template($template, array('related_posts' => $related_posts, 'heading_text' => $pt_settings->get_option('heading_text'), 'excerpt_length' => $pt_settings->get_option('excerpt_length'), 'post_type' => $post_type));
         $output = trim(ob_get_clean());
     }
     return $output;
 }