Check for circular post parent dependencies. Exclude the current post and its children from the parent dropdown.

git-svn-id: http://svn.automattic.com/wordpress/trunk@10129 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-12-08 19:29:42 +00:00
parent fd64845396
commit 504bc044d8
2 changed files with 30 additions and 2 deletions

View File

@ -270,7 +270,7 @@ function page_attributes_meta_box($post){
?> ?>
<h5><?php _e('Parent') ?></h5> <h5><?php _e('Parent') ?></h5>
<label class="hidden" for="parent_id"><?php _e('Page Parent') ?></label> <label class="hidden" for="parent_id"><?php _e('Page Parent') ?></label>
<?php wp_dropdown_pages(array('selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('Main Page (no parent)'))); ?> <?php wp_dropdown_pages(array('exclude_tree' => $post->ID, 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('Main Page (no parent)'))); ?>
<p><?php _e('You can arrange your pages in hierarchies, for example you could have an &#8220;About&#8221; page that has &#8220;Life Story&#8221; and &#8220;My Dog&#8221; pages under it. There are no limits to how deeply nested you can make pages.'); ?></p> <p><?php _e('You can arrange your pages in hierarchies, for example you could have an &#8220;About&#8221; page that has &#8220;Life Story&#8221; and &#8220;My Dog&#8221; pages under it. There are no limits to how deeply nested you can make pages.'); ?></p>
<?php <?php
if ( 0 != count( get_page_templates() ) ) { if ( 0 != count( get_page_templates() ) ) {

View File

@ -1417,6 +1417,18 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
else else
$post_parent = 0; $post_parent = 0;
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 ( $parent_post->post_parent == $post_ID )
$post_parent = 0;
}
}
if ( isset($menu_order) ) if ( isset($menu_order) )
$menu_order = (int) $menu_order; $menu_order = (int) $menu_order;
else else
@ -2059,7 +2071,7 @@ function &get_pages($args = '') {
'sort_column' => 'post_title', 'hierarchical' => 1, 'sort_column' => 'post_title', 'hierarchical' => 1,
'exclude' => '', 'include' => '', 'exclude' => '', 'include' => '',
'meta_key' => '', 'meta_value' => '', 'meta_key' => '', 'meta_value' => '',
'authors' => '', 'parent' => -1 'authors' => '', 'parent' => -1, 'exclude_tree' => ''
); );
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
@ -2170,6 +2182,22 @@ function &get_pages($args = '') {
if ( $child_of || $hierarchical ) if ( $child_of || $hierarchical )
$pages = & get_page_children($child_of, $pages); $pages = & get_page_children($child_of, $pages);
if ( !empty($exclude_tree) ) {
$exclude = array();
$exclude = (int) $exclude_tree;
$children = get_page_children($exclude, $pages);
$excludes = array();
foreach ( $children as $child )
$excludes[] = $child->ID;
$excludes[] = $exclude;
$total = count($pages);
for ( $i = 0; $i < $total; $i++ ) {
if ( in_array($pages[$i]->ID, $excludes) )
unset($pages[$i]);
}
}
$cache[ $key ] = $pages; $cache[ $key ] = $pages;
wp_cache_set( 'get_pages', $cache, 'posts' ); wp_cache_set( 'get_pages', $cache, 'posts' );