/**
  * 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;
     }
     // The Post Type
     $post_type = get_post_type($id);
     // The Post Type Manager
     $pt_manager = new RP4WP_Post_Type_Manager();
     // Check if this Post Type is installed
     if ($pt_manager->is_post_type_installed($post_type) && isset(RP4WP()->settings['general_' . $post_type])) {
         // Related Post Manager
         $related_post_manager = new RP4WP_Related_Post_Manager();
         // The Output
         $output = $related_post_manager->generate_related_posts_list($id);
         // Add output if there is any
         if ('' != $output) {
             $content .= $output;
         }
     }
     // Return the content
     return $content;
 }
 public function run()
 {
     // Check nonce
     check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce');
     // Get the PPR
     $ppr = isset($_POST['ppr']) ? $_POST['ppr'] : 5;
     // Get the rel amount
     $rel_amount = isset($_POST['rel_amount']) ? $_POST['rel_amount'] : 3;
     // Related Post Manager object
     $related_post_manager = new RP4WP_Related_Post_Manager();
     // Link posts
     $related_post_manager->link_related_posts($rel_amount, $ppr);
     // Get uncached post count
     $unlinked_post_count = $related_post_manager->get_unlinked_post_count();
     // Check if we're done
     if (0 == $unlinked_post_count) {
         // Save the wizard setting as the option
         $options = RP4WP()->settings->get_options();
         $options['automatic_linking_post_amount'] = $rel_amount;
         update_option('rp4wp', $options);
     }
     // Echo the uncached posts
     echo $unlinked_post_count;
     exit;
 }
 /**
  * Get the weight of a type
  *
  * @param $type
  *
  * @return int
  */
 public function get_weight($type)
 {
     $weight = 1;
     if (in_array($type, $this->weight_types)) {
         $weight = apply_filters('rp4wp_weight_' . $type, RP4WP()->settings['weights']->get_option('weight_' . $type));
     }
     return $weight;
 }
 /**
  * Add custom plugin links
  *
  * @param array $links
  *
  * @since  1.4.0
  * @access public
  *
  * @return array
  */
 public function run($links)
 {
     // Get the first setting page
     $local_settings = (array) RP4WP()->settings;
     $first_setting = array_shift($local_settings);
     // Add link
     array_unshift($links, '<a href="' . admin_url(sprintf('options-general.php?page=%s', $first_setting->get_page())) . '">' . __('Settings', 'related-posts-for-wp') . '</a>');
     return $links;
 }
function rp4wp_pro_load_plugin()
{
    // Define
    define('RP4WP_PLUGIN_FILE', __FILE__);
    // include files
    require_once dirname(__FILE__) . '/vendor/autoload_52.php';
    require_once dirname(__FILE__) . '/includes/functions.php';
    // Instantiate main plugin object
    RP4WP();
}
function rp4wp_load_plugin()
{
    if (defined('RP4WP_PLUGIN_FILE')) {
        return false;
    }
    // Define
    define('RP4WP_PLUGIN_FILE', __FILE__);
    // Load main plugin file
    require_once plugin_dir_path(RP4WP_PLUGIN_FILE) . 'classes/class-rp4wp.php';
    RP4WP();
}
function rp4wp_load_plugin()
{
    if (defined('RP4WP_PLUGIN_FILE')) {
        return false;
    }
    // Define
    define('RP4WP_PLUGIN_FILE', __FILE__);
    require dirname(__FILE__) . '/vendor/autoload_52.php';
    require dirname(__FILE__) . '/includes/functions.php';
    // Instantiate main plugin object
    RP4WP();
}
 /**
  * Attempt t deactivate a license
  */
 public static function deactivate($args)
 {
     $defaults = array('request' => 'deactivate', 'instance' => site_url());
     $args = wp_parse_args($defaults, $args);
     // SSL Verify Check
     $request_args = array();
     if (1 == RP4WP()->settings['misc']->get_option('disable_sslverify')) {
         $request_args['sslverify'] = false;
     }
     $request = wp_remote_get(self::$endpoint . '&' . http_build_query($args, '', '&'), $request_args);
     if (is_wp_error($request) || wp_remote_retrieve_response_code($request) != 200) {
         return false;
     } else {
         return wp_remote_retrieve_body($request);
     }
 }
 /**
  * Locate a template and return the path for inclusion.
  *
  * This is the load order:
  *
  *        yourtheme        /    $template_path    /    $template_name
  *        yourtheme        /    $template_name
  *        $default_path    /    $template_name
  *
  * @access public
  *
  * @param string $template_name
  * @param string $template_path (default: '')
  * @param string $default_path (default: '')
  *
  * @return string
  */
 public function locate_template($template_name, $template_path = '', $default_path = '')
 {
     if (!$template_path) {
         $template_path = RP4WP()->template_path();
     }
     if (!$default_path) {
         $default_path = RP4WP()->plugin_path() . '/templates/';
     }
     // Look within passed path within the theme - this is priority
     $template = locate_template(array(trailingslashit($template_path) . $template_name, $template_name));
     // Get default template
     if (!$template || defined('RP4WP_TEMPLATE_DEBUG_MODE') && true === RP4WP_TEMPLATE_DEBUG_MODE) {
         $template = $default_path . $template_name;
     }
     // Return what we found
     return apply_filters('rp4wp_locate_template', $template, $template_name, $template_path);
 }
 /**
  * Generate the Related Posts for WordPress children list
  *
  * @param bool $id
  * @param bool $output
  * @param string $template
  * @param int $limit
  *
  * @since  1.0.0
  * @access public
  *
  * @return string
  */
 function rp4wp_children($id = false, $output = true, $template = 'related-posts-default.php', $limit = -1)
 {
     // Get the current ID if ID not set
     if (false === $id) {
         $id = get_the_ID();
     }
     // Get the post type
     $post_type = get_post_type($id);
     // Check if this Post Type is installed
     $pt_manager = new RP4WP_Post_Type_Manager();
     if ($pt_manager->is_post_type_installed($post_type) && isset(RP4WP()->settings['general_' . $post_type])) {
         // related Post Manager
         $related_post_manager = new RP4WP_Related_Post_Manager();
         // the Output
         $content = $related_post_manager->generate_related_posts_list($id, $template, $limit);
         // Output or return the content
         if ($output) {
             echo $content;
         } else {
             return $content;
         }
     }
 }
 /**
  * Get joined words array that includes replacements
  *
  * @return array
  */
 public function get_joined_words()
 {
     // load transient
     $joined_words = get_transient(RP4WP_Constants::TRANSIENT_JOINED_WORDS);
     // check transient
     if (false === $joined_words) {
         $joined_words = array();
         // get joined words and explode them on new line
         $joined_words_raw = explode(PHP_EOL, RP4WP()->settings['words']->get_option('joined_words'));
         // check
         if (count($joined_words_raw) > 0) {
             // loop
             foreach ($joined_words_raw as $joined_word) {
                 $joined_words[sanitize_title($joined_word)] = trim($joined_word);
             }
         }
         // set transient
         set_transient(RP4WP_Constants::TRANSIENT_JOINED_WORDS, $joined_words, 7 * DAY_IN_SECONDS);
     }
     // return joined words
     return $joined_words;
 }
    /**
     * Settings screen output
     *
     * @since  1.1.0
     * @access public
     */
    public function screen()
    {
        // The current page
        $cur_page = sanitize_text_field($_GET['page']);
        $page_title = '';
        $settings_key = substr($cur_page, 6);
        if (isset(RP4WP()->settings[$settings_key])) {
            $page_title = RP4WP()->settings[$settings_key]->get_title();
        }
        ?>
		<div class="wrap">
			<?php 
        $this->tabs($cur_page);
        ?>



			<div class="rp4wp-content">
				<?php 
        if (false !== strstr($settings_key, 'general_')) {
            $this->post_type_sections($cur_page);
        }
        ?>
				<h2><?php 
        echo $page_title;
        ?>
</h2>

				<form method="post" action="options.php" id="rp4wp-settings-form">
					<?php 
        settings_fields($cur_page);
        // pass slug name of page, also referred to in Settings API as option group name
        do_settings_sections($cur_page);
        // pass slug name of page
        submit_button(RP4WP()->settings[$settings_key]->get_button_title());
        ?>
				</form>
			</div>
			<?php 
        $this->sidebar();
        ?>
		</div>
	<?php 
    }
$o_post = $post;
$row_counter = 0;
foreach ($related_posts as $post) {
    // Setup the postdata
    setup_postdata($post);
    // Load the content template
    $manager_template = new RP4WP_Manager_Template();
    $manager_template->get_template('related-post-default.php', array('related_post' => $post, 'excerpt_length' => $excerpt_length, 'row_counter' => $row_counter, 'parent' => $o_post));
    $row_counter++;
}
$post = $o_post;
wp_reset_postdata();
?>
	</ul>
	<?php 
if ('1' == RP4WP()->settings['misc']->get_option('show_love')) {
    // Base
    $base_url = "https://www.relatedpostsforwp.com";
    $query_string = "?";
    // Allow affiliates to add affiliate ID to Power By link
    $ref = apply_filters('rp4wp_poweredby_affiliate_id', '');
    if ('' !== $ref) {
        $ref = intval($ref);
        $query_string .= "ref=" . $ref . '&';
    }
    // The UTM campaign stuff
    $query_string .= sprintf("utm_source=%s&utm_medium=link&utm_campaign=poweredby", strtolower(preg_replace("`[^A-z0-9\\-.]+`i", '', str_ireplace(' ', '-', html_entity_decode(get_bloginfo('name'))))));
    // The URL
    $url = esc_url($base_url . htmlentities($query_string));
    $manager_template->get_template('show-love.php', array('link' => $url));
}
    /**
     * The screen content
     *
     * @since  1.0.0
     * @access public
     */
    public function content()
    {
        // Check nonce
        $installer_nonce = isset($_GET['rp4wp_nonce']) ? $_GET['rp4wp_nonce'] : '';
        if (!wp_verify_nonce($installer_nonce, RP4WP_Constants::NONCE_INSTALL)) {
            wp_die('Woah! It looks like something else tried to run the Related Posts for WordPress installation wizard! We were able to stop them, nothing was lost. Please report this incident at <a href="http://wordpress.org/support/plugin/related-posts-for-wp" target="_blank">our forums.</a>');
        }
        // The steps
        $steps = array(1 => __('Welcome', 'related-posts-for-wp'), 2 => __('Caching Posts', 'related-posts-for-wp'), 3 => __('Linking Posts', 'related-posts-for-wp'), 4 => __('Finished', 'related-posts-for-wp'));
        // What's the current step?
        $cur_step = intval(isset($_GET['step']) ? $_GET['step'] : 1);
        // Set the post type in var
        $post_type = esc_html(isset($_GET['pt']) ? $_GET['pt'] : '');
        // Check if the post type is set
        if ($cur_step > 1 && empty($post_type)) {
            wp_die(sprintf(__('Missing post type parameter, please report this incident via %sour website%s.', 'related-posts-for-wp'), '<a href="https://www.relatedpostsforwp.com/support/" target="_blank">', '</a>'));
        }
        // Try to create the cache table if we're in step 1 and this is a multisite
        if (1 == $cur_step && is_multisite()) {
            require_once plugin_dir_path(RP4WP_PLUGIN_FILE) . 'includes/installer-functions.php';
            rp4wp_premium_create_db_table();
        }
        // Check installer resume options
        if (2 == $cur_step) {
            // Add is installing site option
            add_option(RP4WP_Constants::OPTION_IS_INSTALLING, $post_type);
            // Get current linked post type
            $linked_pt_cur = absint(isset($_GET['cur']) ? $_GET['cur'] : 0);
            // Get the available post types
            $ptm = new RP4WP_Post_Type_Manager();
            $linked_pt_count = count($ptm->get_installed_post_type($post_type));
        } elseif (4 == $cur_step) {
            // Installer is done, remove the option
            delete_option(RP4WP_Constants::OPTION_IS_INSTALLING);
        }
        ?>
		<div class="wrap">
			<h2>Related Posts for WordPress <?php 
        _e('Installation', 'related-posts-for-wp');
        ?>
</h2>

			<ul class="install-steps">
				<?php 
        foreach ($steps as $step => $label) {
            echo "<li id='step-bar-" . $step . "'" . ($cur_step == $step ? " class='step-bar-active'" : "") . "><span>" . $step . '. ' . $label . "</span></li>" . PHP_EOL;
        }
        ?>
			</ul>
			<br class="clear" />

			<h3><?php 
        echo $steps[$cur_step];
        ?>
</h3>

			<?php 
        echo "<div class='rp4wp-step rp4wp-step-" . $cur_step . "' rel='" . $cur_step . "'>";
        // Hidden fields
        echo "<input type='hidden' id='rp4wp_admin_url' value='" . admin_url() . "' />" . PHP_EOL;
        // Echo the post type & total posts when post type is set
        if (!empty($post_type)) {
            echo "<input type='hidden' id='rp4wp_post_type' value='" . $post_type . "' />" . PHP_EOL;
            echo "<input type='hidden' id='rp4wp_total_posts' value='" . wp_count_posts($post_type)->publish . "' />" . PHP_EOL;
        }
        // Echo the amount of linked post types
        if (isset($linked_pt_count)) {
            echo "<input type='hidden' id='linked_pt_count' value='" . $linked_pt_count . "' />" . PHP_EOL;
        }
        // Echo the current linked post type
        if (isset($linked_pt_cur)) {
            echo "<input type='hidden' id='linked_pt_cur' value='" . $linked_pt_cur . "' />" . PHP_EOL;
        }
        // Echo the nonce
        if (!empty($installer_nonce)) {
            echo "<input type='hidden' id='rp4wp_nonce' value='" . $installer_nonce . "' />" . PHP_EOL;
        }
        // AJAX nonce
        echo '<input type="hidden" name="rp4wp-ajax-nonce" id="rp4wp-ajax-nonce" value="' . wp_create_nonce(RP4WP_Constants::NONCE_AJAX) . '" />';
        switch ($cur_step) {
            case 1:
                ?>
				<p><?php 
                _e('Thank you for choosing Related Posts for WordPress!', 'related-posts-for-wp');
                ?>
</p>
				<p><?php 
                _e("Below you'll find your post types available for installation, by installing a post type we'll set up a cache and offer you the ability to automatic link (custom) posts. To install a post type, simply click the 'Install Post Type' button.", 'related-posts-for-wp');
                ?>
</p>
				<h3>Post Types</h3>
				<?php 
                // Get the available post types
                $ptm = new RP4WP_Post_Type_Manager();
                // Get installed post types
                $installed_post_types = $ptm->get_installed_post_types();
                // Available post types
                $available_post_types = $ptm->get_available_post_types();
                echo '<input type="hidden" id="rp4wp-availabe_post_types" value="' . implode(',', array_keys($available_post_types)) . '" />';
                echo '<table cellpadding="0" cellspacing="0" border="0" class="rp4wp-table-pt-overview">' . PHP_EOL;
                echo "<tr>" . PHP_EOL;
                echo "<th>" . __('Post Type', 'related-posts-for-wp') . "</th>" . PHP_EOL;
                echo "<th>" . __('Related to Post Types', 'related-posts-for-wp') . "</th>" . PHP_EOL;
                echo "<th>&nbsp;</th>" . PHP_EOL;
                echo "</tr>" . PHP_EOL;
                // Da loop
                if (count($available_post_types) > 0) {
                    foreach ($available_post_types as $pt_key => $pt_label) {
                        $is_active = false;
                        if (isset($installed_post_types[$pt_key]) && count($installed_post_types[$pt_key]) > 0) {
                            $is_active = true;
                        }
                        echo '<tr rel="' . $pt_key . '" ' . (!$is_active ? 'class="inactive"' : '') . '>';
                        echo '<td class="rp4wp-parent">' . $pt_label . '</td>' . PHP_EOL;
                        echo '<td class="rp4wp-children">';
                        echo '<ul>' . PHP_EOL;
                        if ($is_active) {
                            foreach ($installed_post_types[$pt_key] as $linked_pt) {
                                echo '<li id="' . $linked_pt . '"><span>' . $available_post_types[$linked_pt] . '</span></li>' . PHP_EOL;
                            }
                        }
                        echo '</ul>' . PHP_EOL;
                        echo '</td>' . PHP_EOL;
                        echo '<td class="rp4wp-button">';
                        echo '<div class="rp4wp-buttons-wrap">' . PHP_EOL;
                        echo '<a href="javascript:;" class="button button-primary rp4wp-btn-edit rp4wp-has-tip" title="' . __('Edit this post type', 'related-posts-for-wp') . '" rel="edit"></a>';
                        echo '</div>' . PHP_EOL;
                        echo '</td>' . PHP_EOL;
                        echo '</tr>';
                    }
                }
                echo '</table>' . PHP_EOL;
                break;
            case 2:
                // Echo current uncached posts
                $related_word_manager = new RP4WP_Related_Word_Manager();
                // amount of posts needs caching
                $posts_todo = $related_word_manager->get_uncached_post_count($post_type);
                echo "<input type='hidden' id='rp4wp_posts_todo' value='" . $posts_todo . "' />" . PHP_EOL;
                ?>
				<p><?php 
                _e('Thank you for choosing Related Posts for WordPress!', 'related-posts-for-wp');
                ?>
</p>
				<p><?php 
                _e('Before you can start using Related Posts for WordPress we need to cache your current posts.', 'related-posts-for-wp');
                ?>
</p>
				<p><?php 
                _e("This is a one time process which might take some time now, depending on the amount of posts you have, but will ensure your website's performance when using the plugin.", 'related-posts-for-wp');
                ?>
</p>

				<p style="font-weight: bold;"><?php 
                _e('Do NOT close this window, wait for this process to finish and this wizard to take you to the next step.', 'related-posts-for-wp');
                ?>
</p>

				<div id="progress-container">
					<div id="progressbar"></div>
					<p>Todo: <span id="progress-todo"><?php 
                echo $posts_todo;
                ?>
</span></p>
					<p>Done: <span id="progress-done">0</span></p>
				</div>

				<?php 
                break;
            case 3:
                // Echo current unlinked posts
                $related_post_manager = new RP4WP_Related_Post_Manager();
                // amount of posts need linking
                $posts_todo = $related_post_manager->get_unlinked_post_count($post_type);
                echo "<input type='hidden' id='rp4wp_posts_todo' value='" . $posts_todo . "' />" . PHP_EOL;
                // Get the automatic linking post amount
                $alpa = 3;
                if (isset(RP4WP()->settings['general_' . $post_type])) {
                    $alpa = RP4WP()->settings['general_' . $post_type]->get_option('automatic_linking_post_amount');
                }
                // get the post age
                // Get the automatic linking post amount
                $max_post_age = 0;
                if (isset(RP4WP()->settings['general_' . $post_type])) {
                    $max_post_age = RP4WP()->settings['general_' . $post_type]->get_option('max_post_age');
                }
                ?>
				<p style="font-weight: bold;"><?php 
                _e('Great! All your posts were successfully cached!', 'related-posts-for-wp');
                ?>
</p>
				<p><?php 
                _e("You can let me link your posts, based on what I think is related, to each other. And don't worry, if I made a mistake at one of your posts you can easily correct this by editing it manually!", 'related-posts-for-wp');
                ?>
</p>
				<p><?php 
                _e('Want me to start linking posts to each other? Fill in the amount of related posts each post should have and click on the "Link now" button. Rather link your posts manually? Click "Skip linking".', 'related-posts-for-wp');
                ?>
</p>
				<p style="font-weight: bold;"><?php 
                _e('Do NOT close this window if you click the "Link now" button, wait for this process to finish and this wizard to take you to the next step.', 'related-posts-for-wp');
                ?>
</p>
				<br class="clear"/>

				<div class="rp4wp-install-link-box">
					<table cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td><label for="rp4wp_related_posts_amount"><?php 
                _e('Amount of related posts:', 'related-posts-for-wp');
                ?>
</label></td>
							<td><input class="form-input-tip" type="text" id="rp4wp_related_posts_amount" value="<?php 
                echo $alpa;
                ?>
" /></td>
							<td class="rp4pw-install-step3-table-desc"><?php 
                printf(__('The amount of related items per %s', 'related-posts-for-wp'), $post_type);
                ?>
</td>
						</tr>


						<tr>
							<td><label for="rp4wp_related_posts_age"><?php 
                _e('Maximum Post Age:', 'related-posts-for-wp');
                ?>
</label></td>
							<td><input class="form-input-tip" type="text" id="rp4wp_related_posts_age" value="<?php 
                echo $max_post_age;
                ?>
"/></td>
							<td class="rp4pw-install-step3-table-desc"><?php 
                printf(__('The maximum age in days of %s that will be linked. (0 = unlimited)', 'related-posts-for-wp'), $post_type);
                ?>
</td>
						</tr>

						<tr>
							<td>&nbsp;</td>
							<td colspan="2" class="rp4pw-install-step3-table-buttons">
								<a href="javascript:;" class="button button-primary button-large rp4wp-link-now-btn" id="rp4wp-link-now"><?php 
                _e('Link now', 'related-posts-for-wp');
                ?>
</a>
								<a href="<?php 
                echo admin_url(sprintf('?page=rp4wp_install&step=4&pt=%s&&rp4wp_nonce=%s', $post_type, wp_create_nonce(RP4WP_Constants::NONCE_INSTALL)));
                ?>
" class="button"><?php 
                _e('Skip linking', 'related-posts-for-wp');
                ?>
</a>
							</td>
						</tr>

					</table>
				</div>

				<br class="clear"/>

				<div id="progress-container">
					<div id="progressbar"></div>
					<p>Todo: <span id="progress-todo"><?php 
                echo $posts_todo;
                ?>
</span></p>
					<p>Done: <span id="progress-done">0</span></p>
				</div>
				<?php 
                break;
            case 4:
                ?>
				<p><?php 
                _e("That's it, you're good to go!", 'related-posts-for-wp');
                ?>
</p>
				<p><?php 
                printf(__('Thanks again for using Related Posts for WordPress and if you have any questions be sure to ask them at the %sWordPress.org forums.%s', 'related-posts-for-wp'), '<a href="http://wordpress.org/support/plugin/related-posts-for-wp" target="_blank">', '</a>');
                ?>
</p>
				<p>
					<a href="<?php 
                echo admin_url(sprintf('?page=rp4wp_install&rp4wp_nonce=%s', wp_create_nonce(RP4WP_Constants::NONCE_INSTALL)));
                ?>
"
					   class="button button-primary"><?php 
                _e('Click here to return to step 1', 'related-posts-for-wp');
                ?>
</a>
				</p>
				<p>
					<?php 
                $local_settings = (array) RP4WP()->settings;
                $first_setting = array_shift($local_settings);
                echo '<a href="' . admin_url('options-general.php?page=' . $first_setting->get_page()) . '" class="button button-primary">' . __('Click here to go to the settings page', 'related-posts-for-wp') . '</a>';
                ?>
				</p>
			<?php 
        }
        ?>
		</div>

		</div>

	<?php 
    }
 /**
  * Sends and receives data to and from the server API
  * @return object $response
  */
 public function do_license_request($args)
 {
     // SSL Verify Check
     $request_args = array();
     if (1 == RP4WP()->settings['misc']->get_option('disable_sslverify')) {
         $request_args['sslverify'] = false;
     }
     $request = wp_remote_get($this->api_url . '&' . http_build_query($args, '', '&'), $request_args);
     if (is_wp_error($request) || wp_remote_retrieve_response_code($request) != 200) {
         return false;
     }
     $response = maybe_unserialize(wp_remote_retrieve_body($request));
     if (is_object($response)) {
         return $response;
     } else {
         return false;
     }
 }
    /**
     * The screen content
     *
     * @since  1.0.0
     * @access public
     */
    public function content()
    {
        // Do we have a reinstall?
        if (isset($_GET['reinstall'])) {
            // Check nonce
            if (!wp_verify_nonce(isset($_GET['rp4wp_nonce']) ? $_GET['rp4wp_nonce'] : '', RP4WP_Constants::NONCE_REINSTALL)) {
                wp_die('Woah! It looks like something else tried to run the Related Posts for WordPress installation wizard! We were able to stop them, nothing was lost. Please report this incident at <a href="http://wordpress.org/support/plugin/related-posts-for-wp" target="_blank">our forums.</a>');
            }
            global $wpdb;
            // Get ID's of related post link posts
            $link_ids = get_posts(array('post_type' => RP4WP_Constants::LINK_PT, 'fields' => 'ids', 'posts_per_page' => -1));
            // Only run queries if we have ID's
            if (count($link_ids) > 0) {
                // Delete all link posts
                $wpdb->query("DELETE FROM {$wpdb->posts} WHERE `ID` IN (" . implode(",", $link_ids) . ");");
                // Delete all link post meta
                $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE `post_id` IN (" . implode(",", $link_ids) . ");");
            }
            // Remove the post meta we attached to posts
            $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE `meta_key` = 'rp4wp_auto_linked' OR `meta_key` = 'rp4wp_cached' ");
            // Empty word cache
            $wpdb->query("DELETE FROM " . RP4WP_Related_Word_Manager::get_database_table() . " WHERE 1=1");
        }
        // The steps
        $steps = array(1 => __('Caching Posts', 'related-posts-for-wp'), 2 => __('Linking Posts', 'related-posts-for-wp'), 3 => __('Finished', 'related-posts-for-wp'));
        // What's the current step?
        $cur_step = isset($_GET['step']) ? $_GET['step'] : 1;
        // Check installer resume options
        if (1 == $cur_step) {
            // Add is installing site option
            add_site_option(RP4WP_Constants::OPTION_IS_INSTALLING, true);
        } elseif (3 == $cur_step) {
            // Installer is done, remove the option
            delete_site_option(RP4WP_Constants::OPTION_IS_INSTALLING);
        }
        ?>
		<div class="wrap">
			<h2>Related Posts for WordPress <?php 
        _e('Installation', 'related-posts-for-wp');
        ?>
</h2>

			<ul class="install-steps">
				<?php 
        foreach ($steps as $step => $label) {
            echo "<li id='step-bar-" . $step . "'" . ($cur_step == $step ? " class='step-bar-active'" : "") . "><span>" . $step . '. ' . $label . "</span></li>" . PHP_EOL;
        }
        ?>
			</ul>
			<br class="clear" />

			<h3><?php 
        echo $steps[$cur_step];
        ?>
</h3>

			<?php 
        $cur_step = isset($_GET['step']) ? $_GET['step'] : 1;
        echo "<div class='rp4wp-step rp4wp-step-" . $cur_step . "' rel='" . $cur_step . "'>";
        echo "<input type='hidden' id='rp4wp_total_posts' value='" . wp_count_posts('post')->publish . "' />" . PHP_EOL;
        echo "<input type='hidden' id='rp4wp_admin_url' value='" . admin_url() . "' />" . PHP_EOL;
        if (1 == $cur_step) {
            // Echo current uncached posts
            $related_word_manager = new RP4WP_Related_Word_Manager();
            echo "<input type='hidden' id='rp4wp_uncached_posts' value='" . $related_word_manager->get_uncached_post_count() . "' />" . PHP_EOL;
            ?>
				<p><?php 
            _e('Thank you for choosing Related Posts for WordPress!', 'related-posts-for-wp');
            ?>
</p>
				<p><?php 
            _e('Before you can start using Related Posts for WordPress we need to cache your current posts.', 'related-posts-for-wp');
            ?>
</p>
				<p><?php 
            _e("This is a one time process which might take some time now, depending on the amount of posts you have, but will ensure your website's performance when using the plugin.", 'related-posts-for-wp');
            ?>
</p>

				<p style="font-weight: bold;"><?php 
            _e('Do NOT close this window, wait for this process to finish and this wizard to take you to the next step.', 'related-posts-for-wp');
            ?>
</p>

				<div id="progressbar"></div>
			<?php 
        } elseif (2 == $cur_step) {
            // Echo current uncached posts
            $related_post_manager = new RP4WP_Related_Post_Manager();
            echo "<input type='hidden' id='rp4wp_uncached_posts' value='" . $related_post_manager->get_uncached_post_count() . "' />" . PHP_EOL;
            ?>
				<p style="font-weight: bold;"><?php 
            _e('Great! All your posts were successfully cached!', 'related-posts-for-wp');
            ?>
</p>
				<p><?php 
            _e("You can let me link your posts, based on what I think is related, to each other. And don't worry, if I made a mistake at one of your posts you can easily correct this by editing it manually!", 'related-posts-for-wp');
            ?>
</p>
				<p><?php 
            _e('Want me to start linking posts to each other? Fill in the amount of related posts each post should have and click on the "Link now" button. Rather link your posts manually? Click "Skip linking".', 'related-posts-for-wp');
            ?>
</p>
				<p style="font-weight: bold;"><?php 
            _e('Do NOT close this window if you click the "Link now" button, wait for this process to finish and this wizard to take you to the next step.', 'related-posts-for-wp');
            ?>
</p>
				<br class="clear" />
				<p class="rp4wp-install-link-box">
					<label for="rp4wp_related_posts_amount"><?php 
            _e('Amount of related posts per post:', 'related-posts-for-wp');
            ?>
</label><input class="form-input-tip" type="text" id="rp4wp_related_posts_amount" value="<?php 
            echo RP4WP()->settings->get_option('automatic_linking_post_amount');
            ?>
" />
					<a href="javascript:;" class="button button-primary button-large rp4wp-link-now-btn" id="rp4wp-link-now"><?php 
            _e('Link now', 'related-posts-for-wp');
            ?>
</a>
					<a href="<?php 
            echo admin_url();
            ?>
?page=rp4wp_install&step=3" class="button"><?php 
            _e('Skip linking', 'related-posts-for-wp');
            ?>
</a>
				</p>
				<br class="clear" />
				<div id="progressbar"></div>
			<?php 
        } elseif (3 == $cur_step) {
            ?>
				<p><?php 
            _e("That's it, you're good to go!", 'related-posts-for-wp');
            ?>
</p>
				<p><?php 
            printf(__('Thanks again for using Related Posts for WordPress and if you have any questions be sure to ask them at the %sWordPress.org forums.%s', 'related-posts-for-wp'), '<a href="http://wordpress.org/support/plugin/related-posts-for-wp" target="_blank">', '</a>');
            ?>
</p>
			<?php 
        }
        ?>
		</div>

		</div>

	<?php 
    }
 /**
  * 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;
 }