| | 127 | * Sanity check the page to make sure the pending modification doesn't result in |
| | 128 | * an orphaned chain or form a loop. If so, don't try to change the post_parent |
| | 129 | */ |
| | 130 | function _sanity_check_page_chain( &$post_data ) { |
| | 131 | global $wpdb; |
| | 132 | |
| | 133 | if ( 'page' != $post_data[ 'post_type' ] ) |
| | 134 | return; |
| | 135 | |
| | 136 | if ( empty( $post_data[ 'post_parent' ] ) ) //skip '' or 0 |
| | 137 | return; |
| | 138 | |
| | 139 | $p = (int)( $post_data[ 'post_parent' ] ); |
| | 140 | |
| | 141 | if ( $p == $post_data[ 'post_ID' ] ){ //fix self loop |
| | 142 | $post_data[ 'post_parent' ] = 0; |
| | 143 | return; |
| | 144 | } |
| | 145 | |
| | 146 | $post_parent_db = $wpdb->get_var( $wpdb->prepare("SELECT post_parent FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_data[ 'post_ID' ] ) ); |
| | 147 | if ( is_null( $post_parent_db ) || $post_parent_db == $p ) //same value |
| | 148 | return; |
| | 149 | |
| | 150 | //try to set post_parent to a new value, make sure it does not form a loop, or being part of orphaned chain |
| | 151 | $track_parents[] = (int)$post_data[ 'post_ID' ]; |
| | 152 | |
| | 153 | while ( $p > 0 ){ |
| | 154 | |
| | 155 | $track_parents[] = $p; |
| | 156 | |
| | 157 | $p = $wpdb->get_var( $wpdb->prepare("SELECT post_parent FROM $wpdb->posts WHERE ID = %d LIMIT 1", $p ) ); |
| | 158 | |
| | 159 | if ( is_null( $p ) ){ //encountered an orphan page in the chain |
| | 160 | $post_data[ 'post_parent' ] = $post_parent_db; |
| | 161 | return; |
| | 162 | } |
| | 163 | |
| | 164 | //encountered a loop, restore the post_parent value from db |
| | 165 | if ( in_array( $p, $track_parents ) ) { |
| | 166 | $post_data[ 'post_parent' ] = $post_parent_db; |
| | 167 | return; |
| | 168 | } |
| | 169 | } |
| | 170 | } |
| | 171 | |
| | 172 | /** |