/** * Checks the given subset of the post hierarchy for hierarchy loops. * Prevents loops from forming and breaks those that it finds. * * Attached to the nxt_insert_post_parent filter. * * @since 3.1.0 * @uses nxt_find_hierarchy_loop() * * @param int $post_parent ID of the parent for the post we're checking. * @parem int $post_ID ID of the post we're checking. * * @return int The new post_parent for the post. */ function nxt_check_post_hierarchy_for_loops($post_parent, $post_ID) { // Nothing fancy here - bail if (!$post_parent) { return 0; } // New post can't cause a loop if (empty($post_ID)) { return $post_parent; } // Can't be its own parent if ($post_parent == $post_ID) { return 0; } // Now look for larger loops if (!($loop = nxt_find_hierarchy_loop('nxt_get_post_parent_id', $post_ID, $post_parent))) { return $post_parent; } // No loop // Setting $post_parent to the given value causes a loop if (isset($loop[$post_ID])) { return 0; } // There's a loop, but it doesn't contain $post_ID. Break the loop. foreach (array_keys($loop) as $loop_member) { nxt_update_post(array('ID' => $loop_member, 'post_parent' => 0)); } return $post_parent; }
/** * Checks the given subset of the term hierarchy for hierarchy loops. * Prevents loops from forming and breaks those that it finds. * * Attached to the nxt_update_term_parent filter. * * @since 3.1.0 * @uses nxt_find_hierarchy_loop() * * @param int $parent term_id of the parent for the term we're checking. * @param int $term_id The term we're checking. * @param string $taxonomy The taxonomy of the term we're checking. * * @return int The new parent for the term. */ function nxt_check_term_hierarchy_for_loops($parent, $term_id, $taxonomy) { // Nothing fancy here - bail if (!$parent) { return 0; } // Can't be its own parent if ($parent == $term_id) { return 0; } // Now look for larger loops if (!($loop = nxt_find_hierarchy_loop('nxt_get_term_taxonomy_parent_id', $term_id, $parent, array($taxonomy)))) { return $parent; } // No loop // Setting $parent to the given value causes a loop if (isset($loop[$term_id])) { return 0; } // There's a loop, but it doesn't contain $term_id. Break the loop. foreach (array_keys($loop) as $loop_member) { nxt_update_term($loop_member, $taxonomy, array('parent' => 0)); } return $parent; }