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; }
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 Post Type $post_type = isset($_POST['pt']) ? $_POST['pt'] : null; // Check if Post Type is set if (null == $post_type) { echo 'No Post Type set!'; } // the rel amount $rel_amount = 3; if (isset(RP4WP::get()->settings['general_' . $post_type])) { // set the correct options from step 3 $rel_amount = RP4WP::get()->settings['general_' . $post_type]->get_option('automatic_linking_post_amount'); } // Related Post Manager object $related_post_manager = new RP4WP_Related_Post_Manager(); // Link posts $related_post_manager->link_related_posts($rel_amount, $post_type, $ppr); // Get unlinked post count $unlinked_post_count = $related_post_manager->get_unlinked_post_count($post_type); // Echo the uncached posts echo $unlinked_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)); }