<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[15806] trunk/wp-includes: Prevent post and term hierarchy loops.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15806">15806</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-10-14 15:09:04 +0000 (Thu, 14 Oct 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Prevent post and term hierarchy loops. Props mdawaffe. fixes <a href="http://trac.wordpress.org/ticket/14662">#14662</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesdefaultfiltersphp">trunk/wp-includes/default-filters.php</a></li>
<li><a href="#trunkwpincludesfunctionsphp">trunk/wp-includes/functions.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
<li><a href="#trunkwpincludestaxonomyphp">trunk/wp-includes/taxonomy.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-filters.php (15805 => 15806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-filters.php        2010-10-14 14:02:06 UTC (rev 15805)
+++ trunk/wp-includes/default-filters.php        2010-10-14 15:09:04 UTC (rev 15806)
</span><span class="lines">@@ -117,6 +117,10 @@
</span><span class="cx">         add_filter( $filter, 'convert_chars' );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+// Pre save hierarchy
+add_filter( 'wp_insert_post_parent', 'wp_check_post_hierarchy_for_loops', 10, 2 );
+add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
+
</ins><span class="cx"> // Display filters
</span><span class="cx"> add_filter( 'the_title', 'wptexturize' );
</span><span class="cx"> add_filter( 'the_title', 'convert_chars' );
</span></span></pre></div>
<a id="trunkwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/functions.php (15805 => 15806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/functions.php        2010-10-14 14:02:06 UTC (rev 15805)
+++ trunk/wp-includes/functions.php        2010-10-14 15:09:04 UTC (rev 15806)
</span><span class="lines">@@ -4338,4 +4338,74 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><del>-?>
</del><ins>+/**
+ * Finds hierarchy loops using a callback function that maps objects to parents.
+ *
+ * @since 3.1
+ *
+ * @param callback $callback function that accepts ( ID, callback_arg, ... ) and outputs parent_ID
+ * @param $start The ID to start the loop check at
+ * @param $start_parent the parent_ID of $start to use instead of calling $callback( $start ). Use null to always use $callback
+ * @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
+ * @param array $callback_arg optional additional arguments to send to $callback
+ *
+ * @internal
+ *
+ * @return array IDs of all members of loop
+ */
+function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
+        $override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
+
+        echo "wp_find_hierarchy_loop: $callback, $start, $callback_args\n";
+        if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
+                return array();
+
+        return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
+}
+
+/**
+ * Uses the "The Tortoise and the Hare" algorithm to detect loops.
+ *
+ * For every step of the algorithm, the hare takes two steps and the tortoise one.
+ * If the hare ever laps the tortoise, there must be a loop.
+ *
+ * @since 3.1
+ *
+ * @param callback $callback function that accupts ( ID, callback_arg, ... ) and outputs parent_ID
+ * @param $start The ID to start the loop check at
+ * @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
+ * @param array $callback_args optional additional arguments to send to $callback
+ * @param bool $_return_loop Return loop members or just detect presence of loop?
+ * Only set to true if you already know the given $start is part of a loop
+ * (otherwise the returned array might include branches)
+ *
+ * @internal
+ *
+ * @return mixed scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop
+ */
+function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
+        $tortoise = $hare = $evanescent_hare = $start;
+        $return = array();
+
+        // Set evanescent_hare to one past hare
+        // Increment hare two steps
+        while (
+                $tortoise
+        &&
+                ( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
+        &&
+                ( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
+        ) {
+                if ( $_return_loop )
+                        $return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
+
+                // tortoise got lapped - must be a loop
+                if ( $tortoise == $evanescent_hare || $tortoise == $hare )
+                        return $_return_loop ? $return : $tortoise;
+
+                // Increment tortoise by one step
+                $tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
+        }
+
+        return false;
+}
</ins></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (15805 => 15806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-10-14 14:02:06 UTC (rev 15805)
+++ trunk/wp-includes/post.php        2010-10-14 15:09:04 UTC (rev 15806)
</span><span class="lines">@@ -2337,17 +2337,8 @@
</span><span class="cx">         else
</span><span class="cx">                 $post_parent = 0;
</span><span class="cx">
</span><del>-        if ( !empty($post_ID) ) {
-                if ( $post_parent == $post_ID ) {
-                        // Post can't be its own parent
-                        $post_parent = 0;
-                } elseif ( !empty($post_parent) ) {
-                        $parent_post = get_post($post_parent);
-                        // Check for circular dependency
-                        if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
-                                $post_parent = 0;
-                }
-        }
</del><ins>+        // Check the post_parent to see if it will cause a hierarchy loop
+        $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
</ins><span class="cx">
</span><span class="cx">         if ( isset($menu_order) )
</span><span class="cx">                 $menu_order = (int) $menu_order;
</span><span class="lines">@@ -4804,8 +4795,66 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Returns the post's parent's post_ID
+ *
+ * @since 3.1
+ *
+ * @param int $post_id
+ *
+ * @return int|bool false on error
+ */
+function wp_get_post_parent_id( $post_ID ) {
+        $post = get_post( $post_ID );
+        if ( !$post || is_wp_error( $post ) )
+                return false;
+        return (int) $post->post_parent;
+}
</ins><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Checks the given subset of the post hierarchy for hierarchy loops.
+ * Prevents loops from forming and breaks those that it finds.
+ *
+ * Attached to the wp_insert_post_parent filter.
+ *
+ * @since 3.1
+ * @uses wp_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 wp_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 = wp_find_hierarchy_loop( 'wp_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 )
+                wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
+
+        return $post_parent;
+}
+
+/**
</ins><span class="cx"> * Default post information to use when populating the "Write Post" form.
</span><span class="cx"> *
</span><span class="cx"> * @since unknown
</span></span></pre></div>
<a id="trunkwpincludestaxonomyphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/taxonomy.php (15805 => 15806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/taxonomy.php        2010-10-14 14:02:06 UTC (rev 15805)
+++ trunk/wp-includes/taxonomy.php        2010-10-14 15:09:04 UTC (rev 15806)
</span><span class="lines">@@ -2039,6 +2039,9 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        // Check $parent to see if it will cause a hierarchy loop
+        $parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args );
+
</ins><span class="cx">         // Check for duplicate slug
</span><span class="cx">         $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
</span><span class="cx">         if ( $id && ($id != $term_id) ) {
</span><span class="lines">@@ -2879,3 +2882,62 @@
</span><span class="cx">
</span><span class="cx">         return $tags_to_edit;
</span><span class="cx"> }
</span><ins>+
+/**
+ * Returns the term's parent's term_ID
+ *
+ * @since 3.1
+ *
+ * @param int $term_id
+ * @param string $taxonomy
+ *
+ * @return int|bool false on error
+ */
+function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
+        $term = get_term( $term_id, $taxonomy );
+        if ( !$term || is_wp_error( $term ) )
+                return false;
+        return (int) $term->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 wp_update_term_parent filter.
+ *
+ * @since 3.1
+ * @uses wp_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 wp_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;
+
+        echo "larger loops\n";
+
+        // Now look for larger loops
+
+        if ( !$loop = wp_find_hierarchy_loop( 'wp_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 )
+                wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
+
+        return $parent;
+}
</ins></span></pre>
</div>
</div>
</body>
</html>