/** * Hook into admin AJAX to delete a link * * @access public * @return void */ public function run() { // Check nonce check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce'); try { // Parent if (!isset($_POST['parent'])) { throw new Exception('Parent not set'); } // Parent and post types $parent = sanitize_text_field($_POST['parent']); // Check if user is allowed to do this if (!current_user_can('edit_posts')) { return; } // Related post manager $related_post_manager = new RP4WP_Related_Post_Manager(); // Remove linked related posts $related_post_manager->remove_linked_posts($parent); // Let's go0oo $redirect = true; // Success response $response = array('result' => 'success', 'redirect' => $redirect); } catch (Exception $e) { // Failure response $response = array('result' => 'failure', 'redirect' => false, 'error' => $e->getMessage()); } // Send response wp_send_json($response); }
/** * Hook into admin AJAX to delete a link * * @todo write method * * @access public * @return void */ public function run() { // Check nonce check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce'); try { // Parent if (!isset($_POST['parent'])) { throw new Exception('Parent not set'); } // Parent and post types $parent = sanitize_text_field($_POST['parent']); // Check if user is allowed to do this if (!current_user_can('edit_posts')) { return; } // Related post manager $related_post_manager = new RP4WP_Related_Post_Manager(); // Remove linked related posts $related_post_manager->remove_linked_posts($parent); // Add the post types $ptm = new RP4WP_Post_Type_Manager(); // get children of parent $children = $ptm->get_installed_post_type($parent); // Related word manager $word_manager = new RP4WP_Related_Word_Manager(); // always delete parent words $word_manager->delete_words_by_post_type($parent); // check if there are children if (count($children) > 0) { // loop foreach ($children as $child) { // delete children words $word_manager->delete_words_by_post_type($child); } } // Redirect to setup reinstall $redirect = true; // Success response $response = array('result' => 'success', 'redirect' => $redirect); } catch (Exception $e) { // Failure response $response = array('result' => 'failure', 'redirect' => false, 'error' => $e->getMessage()); } // Send response wp_send_json($response); }
/** * Remove all links from post type * * ## OPTIONS * * <post_type> * : The post type where links are removed from * * ## EXAMPLE * * wp rp4wp remove_related post * * @synopsis <post_type> */ public function remove_related($args, $assoc_args) { // args $post_type = $args[0]; // Related post manager $related_post_manager = new RP4WP_Related_Post_Manager(); // Remove linked related posts $related_post_manager->remove_linked_posts($post_type); // output success WP_CLI::success(sprintf('Successfully removed %s', $post_type)); }
/** * Hook into admin AJAX to delete a link * * @access public * @return void */ public function run() { // Check nonce check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce'); try { // Parent if (!isset($_POST['parent'])) { throw new Exception('Parent not set'); } // Parent and post types $parent = sanitize_text_field($_POST['parent']); $post_types = array_map('sanitize_text_field', isset($_POST['post_types']) ? $_POST['post_types'] : array()); // Check if user is allowed to do this if (!current_user_can('edit_posts')) { return; } // Related post manager $related_post_manager = new RP4WP_Related_Post_Manager(); // Remove linked related posts $related_post_manager->remove_linked_posts($parent); // Add the post types $ptm = new RP4WP_Post_Type_Manager(); // Check if we're adding or removing if (count($post_types) > 0) { // Add the post type $ptm->add_post_type($parent, $post_types); // Let's go0oo $redirect = true; } else { // get children of parent $children = $ptm->get_installed_post_type($parent); // Remove post type $ptm->remove_post_type($parent); // Related word manager $word_manager = new RP4WP_Related_Word_Manager(); // check if parent is used as child in any other relations if (false === $ptm->is_post_type_used($parent)) { // delete word cache of parent $word_manager->delete_words_by_post_type($parent); } // check if there are children if (count($children) > 0) { // loop foreach ($children as $child) { // check if this child post type is used in any other relation if (false === $ptm->is_post_type_used($child)) { // delete words $word_manager->delete_words_by_post_type($child); } } } // No redirect needed $redirect = false; } // Success response $response = array('result' => 'success', 'redirect' => $redirect); } catch (Exception $e) { // Failure response $response = array('result' => 'failure', 'redirect' => false, 'error' => $e->getMessage()); } // Send response wp_send_json($response); }
/** * 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)); }