public function run()
 {
     // Get the PPR
     $ppr = isset($_POST['ppr']) ? $_POST['ppr'] : 25;
     // Related Post Manager
     $related_word_manager = new RP4WP_Related_Word_Manager();
     // Save words
     $related_word_manager->save_all_words($ppr);
     // Get uncached post count
     $uncached_post_count = $related_word_manager->get_uncached_post_count();
     // Echo the uncached posts
     echo $uncached_post_count;
     exit;
 }
 public function run()
 {
     // Check nonce
     check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce');
     // Get the PPR
     $ppr = isset($_POST['ppr']) ? $_POST['ppr'] : 25;
     // Related Post Manager
     $related_word_manager = new RP4WP_Related_Word_Manager();
     // Save words
     $related_word_manager->save_all_words($ppr);
     // Get uncached post count
     $uncached_post_count = $related_word_manager->get_uncached_post_count();
     // Echo the uncached posts
     echo $uncached_post_count;
     exit;
 }
 public function run()
 {
     // Check nonce
     check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce');
     // Get the PPR
     $ppr = isset($_POST['ppr']) ? $_POST['ppr'] : 25;
     // Check if Post Type is set
     if (!isset($_POST['pt'])) {
         echo 'No Post Type set!';
         exit;
     }
     if (!isset($_POST['linked_pt_cur'])) {
         echo 'No linked_pt_cur set!';
         exit;
     }
     // Post Type
     $post_type = $_POST['pt'];
     // Current index of linked posts
     $linked_pt_cur = $_POST['linked_pt_cur'];
     // Get the available post types
     $ptm = new RP4WP_Post_Type_Manager();
     // Get children
     $pt_children = $ptm->get_installed_post_type($post_type);
     // Adding parent post type to the 'to be linked' array
     array_unshift($pt_children, $post_type);
     // Check if the current linked post type exists
     if (!isset($pt_children[$linked_pt_cur])) {
         echo 'Linked post type not set';
         exit;
     }
     // Related Post Manager
     $related_word_manager = new RP4WP_Related_Word_Manager();
     // Save words
     $related_word_manager->save_all_words($pt_children[$linked_pt_cur], $ppr);
     // Get uncached post count
     $uncached_post_count = $related_word_manager->get_uncached_post_count($pt_children[$linked_pt_cur]);
     // Echo the uncached posts
     echo $uncached_post_count;
     exit;
 }
 /**
  * Install a post type. This includes caching and linking post types.
  *
  * ## OPTIONS
  *
  * <post_type>
  * : The post type where related posts are linked to
  *
  * <linked_post_types>
  * : The post types (comma separated) which are linked to the first argument
  *
  * <rel_amount>
  * : The amount of related posts per post
  *
  * [<max_post_age>]
  * : The maximum age of a related post in days
  *
  * ## EXAMPLE
  *
  *     wp rp4wp remove_related post
  *
  * @synopsis <post_type> <linked_post_types> <rel_amount> [<max_post_age>]
  */
 public function install($args, $assoc_args)
 {
     // args
     $post_type = trim($args[0]);
     $linked_post_types = explode(',', trim($args[1]));
     $rel_amount = intval($args[2]);
     $max_post_age = intval(isset($args[3]) ? $args[3] : 0);
     // we need at least one linked post type to continue
     if (count($linked_post_types) > 0) {
         /**
          * SETTINGS
          */
         // set the linked post types
         $ptm = new RP4WP_Post_Type_Manager();
         $ptm->add_post_type($post_type, $linked_post_types);
         // set the post type options
         if (isset(RP4WP::get()->settings['general_' . $post_type])) {
             $options = RP4WP::get()->settings['general_' . $post_type]->get_options();
             $options['automatic_linking_post_amount'] = $rel_amount;
             $options['max_post_age'] = $max_post_age;
             update_option('rp4wp_general_' . $post_type, $options);
         }
         /**
          * CACHE POSTS
          */
         WP_CLI::line('Caching Words');
         // related word manage
         $related_word_manager = new RP4WP_Related_Word_Manager();
         // cache words of parent post type
         $related_word_manager->save_all_words($post_type);
         // cache words of linked post types
         foreach ($linked_post_types as $linked_post_type) {
             $related_word_manager->save_all_words($linked_post_type);
         }
         /**
          * LINK POSTS
          */
         WP_CLI::line('Linking Posts');
         $related_post_manager = new RP4WP_Related_Post_Manager();
         $related_post_manager->link_related_posts($rel_amount, $post_type);
         // output success
         WP_CLI::success(sprintf('Successfully installed %s', $post_type));
     }
 }
 /**
  * Dev test flow
  *
  * ## OPTIONS
  *
  * ## EXAMPLES
  *
  *     wp rp4wp-dev test
  */
 public function test($args, $assoc_args)
 {
     $limit = 274;
     //		$limit = 10;
     WP_CLI::line('Start rp4wp-dev test');
     WP_CLI::line('---------------------');
     // args
     $post_type = 'post';
     $linked_post_types = array('post');
     $rel_amount = 3;
     $max_post_age = 0;
     /**
      * UNINSTALL
      */
     // Add the post types
     $ptm = new RP4WP_Post_Type_Manager();
     // Remove post type
     $ptm->remove_post_type($post_type);
     // Related post manager
     $related_post_manager = new RP4WP_Related_Post_Manager();
     // Remove linked related posts
     $related_post_manager->remove_linked_posts($post_type);
     // delete words
     $word_manager = new RP4WP_Related_Word_Manager();
     $word_manager->delete_words_by_post_type($post_type);
     /**
      * SETTINGS
      */
     // set the linked post types
     $ptm->add_post_type($post_type, $linked_post_types);
     // set the post type options
     if (isset(RP4WP::get()->settings['general_' . $post_type])) {
         $options = RP4WP::get()->settings['general_' . $post_type]->get_options();
         $options['automatic_linking_post_amount'] = $rel_amount;
         $options['max_post_age'] = $max_post_age;
         update_option('rp4wp_general_' . $post_type, $options);
     }
     /**
      * CACHE POSTS
      */
     WP_CLI::line('Start building word cache');
     // related word manage
     $related_word_manager = new RP4WP_Related_Word_Manager();
     $start = time();
     // cache words of parent post type
     $related_word_manager->save_all_words($post_type, $limit);
     // cache words of linked post types
     foreach ($linked_post_types as $linked_post_type) {
         $related_word_manager->save_all_words($linked_post_type, $limit);
     }
     $runtime = time() - $start;
     $avg = $runtime / $limit;
     WP_CLI::line(sprintf('Words cached - Runtime: %d - Average: %f', $runtime, $avg));
     $start = time();
     /**
      * LINK POSTS
      */
     WP_CLI::line('Start linking posts');
     $related_post_manager->link_related_posts($rel_amount, $post_type, $limit);
     $runtime = time() - $start;
     $avg = $runtime / $limit;
     WP_CLI::line(sprintf('Posts linked - Runtime: %d - Average: %f', $runtime, $avg));
     // output success
     WP_CLI::success(sprintf('Successfully installed %s', $post_type));
 }