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);
     }
 }
 /**
  * Link x related posts to y not already linked posts
  *
  * @param int $rel_amount
  * @param string $post_type
  * @param int $post_amount
  *
  * @return boolean
  */
 public function link_related_posts($rel_amount, $post_type, $post_amount = -1)
 {
     global $wpdb;
     // Get unlinked posts
     $post_ids = $this->get_not_auto_linked_posts_ids($post_type, $post_amount);
     $total = count($post_ids);
     // Check & Loop
     if ($total > 0) {
         // total posts linked
         $done = 0;
         // Post Link Manager
         $pl_manager = new RP4WP_Post_Link_Manager();
         foreach ($post_ids as $post_id) {
             // amount of posts we want to link
             $posts_to_link_amount = $rel_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) {
                 $posts_to_link_amount = $posts_to_link_amount - $already_linked_posts;
             }
             // link if we've at least one post to link
             $this->link_related_post($post_id, $post_type, $posts_to_link_amount);
             // increment done
             $done++;
             // WP_CLI feedback
             if (defined('WP_CLI') && WP_CLI) {
                 $perc = ceil($done / $total * 100);
                 $bar = "\r[" . ($perc > 0 ? str_repeat("=", $perc - 1) : "") . ">";
                 $bar .= str_repeat(" ", 100 - $perc) . "] - {$perc}%";
                 print $bar;
             }
         }
         if (defined('WP_CLI') && WP_CLI) {
             WP_CLI::line('');
         }
     }
     // Done
     return true;
 }