| 2758 | if ( isset($post_parent) ) |
| 2759 | $post_parent = (int) $post_parent; |
| 2760 | else |
| 2761 | $post_parent = 0; |
| 2762 | // Check the post_parent to see if it will cause a hierarchy loop |
| 2763 | $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr ); |
| 2764 | //inherit parent post status when private |
| 2765 | if ( $post_parent ) { |
| 2766 | $parent_status = get_post( $post_parent )->post_status; |
| 2767 | if ( 'private' === $parent_status ) { |
| 2768 | $post_status = $parent_status; |
| 2769 | } elseif ( 'publish' !== $parent_status ) { |
| 2770 | if ( $wp_error ) { |
| 2771 | return new WP_Error( 'invalid_parent_status', |
| 2772 | __( 'The selected parent has an invalid status' ), |
| 2773 | $parent_status ); |
| 2774 | } else { |
| 2775 | return 0; |
| 2776 | } |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2989 | * Affects descendant pages on some post status changes. |
| 2990 | * @param array $data Array with new post fields |
| 2991 | * @param string $previous_status |
| 2992 | * @return int|WP_Error WP_Error on failure. Otherwise 0 |
| 2993 | */ |
| 2994 | function affect_descendants( $ID, $data, $previous_status ) { |
| 2995 | $getter_args = array(); |
| 2996 | $force_inheritance = false; |
| 2997 | //remove parentship for all childrens if post status does not support it |
| 2998 | if ( in_array( $previous_status , array( 'publish', 'private' ) ) && |
| 2999 | ! in_array( $data['post_status'], array( 'publish', 'private' ) ) ) { |
| 3000 | $getter_args = array( |
| 3001 | 'parent' => $ID, |
| 3002 | 'hierarchical' => 0, |
| 3003 | 'post_status' => 'publish,future,draft,pending,private', |
| 3004 | ); |
| 3005 | } |
| 3006 | //publish->private? set post status of all descendants to private |
| 3007 | if ( 'publish' === $previous_status && 'private' === $data['post_status'] ) { |
| 3008 | $getter_args = array( |
| 3009 | 'child_of' => $ID, |
| 3010 | 'post_status' => 'publish,future,draft,pending', |
| 3011 | ); |
| 3012 | $force_inheritance = true; |
| 3013 | } |
| 3014 | if ( ! count( $getter_args ) ) { //no need for changes |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
| 3018 | $descendants = get_pages( $getter_args ); |
| 3019 | if ( false === $descendants ) { |
| 3020 | return new WP_Error( 'get_descendant_fail', |
| 3021 | __( 'Could not get descendant' ), $getter_args ); |
| 3022 | } |
| 3023 | if ( ! count( $descendants ) ) { |
| 3024 | return 0; |
| 3025 | } |
| 3026 | global $wpdb; |
| 3027 | $sql = "UPDATE {$wpdb->posts} SET "; |
| 3028 | if ( $force_inheritance ) { |
| 3029 | $sql .= 'post_status="private"'; |
| 3030 | } else { |
| 3031 | $sql .= 'post_parent=0'; |
| 3032 | } |
| 3033 | $sql .= ' WHERE '; |
| 3034 | for ( $i = 0; $i != count( $descendants ); $i++ ) { |
| 3035 | $sql .= "ID={$descendants[$i]->ID} "; |
| 3036 | if ( count( $descendants ) - 1 != $i ) { |
| 3037 | $sql .= 'OR '; |
| 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | if ( FALSE === $wpdb->query( $sql ) ) { |
| 3042 | return new WP_Error( 'affect_descendant_fail', |
| 3043 | __( 'Could not affect descendants' ), $sql ); |
| 3044 | } |
| 3045 | return 0; |
| 3046 | } |
| 3047 | |
| 3048 | /** |
| 3049 | * |
| 3050 | |
| 3051 | /** |