| 6076 | |
| 6077 | /** |
| 6078 | * Removes the post's parent post in case the parent gets trashed. |
| 6079 | * |
| 6080 | * Stores the old value in post meta so that it can be re-added if the |
| 6081 | * parent post is untrashed. |
| 6082 | * |
| 6083 | * @since 4.7.0 |
| 6084 | * @access private |
| 6085 | * |
| 6086 | * @global wpdb $wpdb WordPress database abstraction object. |
| 6087 | * |
| 6088 | * @param WP_Post $post Post object. |
| 6089 | */ |
| 6090 | function wp_remove_post_parent( $post ) { |
| 6091 | global $wpdb; |
| 6092 | |
| 6093 | $post = get_post( $post ); |
| 6094 | |
| 6095 | $previous_parent = $post->post_parent; |
| 6096 | |
| 6097 | update_post_meta( $post->ID, '_wp_old_post_parent', $previous_parent ); |
| 6098 | $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => $post->ID ) ); |
| 6099 | clean_post_cache( $post->ID ); |
| 6100 | |
| 6101 | do_action( 'wp_remove_post_parent', $post, $previous_parent ); |
| 6102 | } |
| 6103 | |
| 6104 | /** |
| 6105 | * Restores a posts' parent post in case the parent is untrashed. |
| 6106 | * |
| 6107 | * @since 4.7.0 |
| 6108 | * @access private |
| 6109 | * |
| 6110 | * @global wpdb $wpdb WordPress database abstraction object. |
| 6111 | * |
| 6112 | * @param WP_Post $post Post object. |
| 6113 | */ |
| 6114 | function wp_restore_post_parent( $post ) { |
| 6115 | global $wpdb; |
| 6116 | |
| 6117 | $post = get_post( $post ); |
| 6118 | |
| 6119 | $previous_parent = $post->post_parent; |
| 6120 | $old_post_parent = (int) get_post_meta( $post->ID, '_wp_old_post_parent', true ); |
| 6121 | |
| 6122 | delete_post_meta( $post->ID, '_wp_old_post_parent' ); |
| 6123 | |
| 6124 | if ( 0 === $old_post_parent || 0 !== $previous_parent || ! get_post( $old_post_parent ) ) { |
| 6125 | return; |
| 6126 | } |
| 6127 | |
| 6128 | $wpdb->update( $wpdb->posts, array( 'post_parent' => $post->post_parent ), array( 'ID' => $post->ID ) ); |
| 6129 | clean_post_cache( $post->ID ); |
| 6130 | |
| 6131 | do_action( 'wp_restore_post_parent', $post, $previous_parent ); |
| 6132 | } |
| 6133 | |
| 6134 | /** |
| 6135 | * |
| 6136 | * @param string $new_status New post status. |
| 6137 | * @param string $old_status Old post status. |
| 6138 | * @param WP_Post $post Post object. |
| 6139 | */ |
| 6140 | function _update_post_parents( $new_status, $old_status, $post ) { |
| 6141 | if ( ! is_post_type_hierarchical( $post->post_type ) ) { |
| 6142 | return; |
| 6143 | } |
| 6144 | |
| 6145 | if ( 'publish' !== $old_status && 'publish' === $new_status ) { |
| 6146 | $children = get_posts( array( |
| 6147 | 'fields' => 'ids', |
| 6148 | 'meta_key' => '_wp_old_post_parent', |
| 6149 | 'meta_value_num' => $post->ID, |
| 6150 | 'post_type' => $post->post_type, |
| 6151 | ) ); |
| 6152 | |
| 6153 | array_map( 'wp_restore_post_parent', $children ); |
| 6154 | } else if ( 'publish' === $old_status && 'publish' !== $new_status ) { |
| 6155 | $children = get_posts( array( |
| 6156 | 'fields' => 'ids', |
| 6157 | 'post_parent' => $post->ID, |
| 6158 | 'post_status' => 'any', |
| 6159 | 'post_type' => $post->post_type, |
| 6160 | ) ); |
| 6161 | |
| 6162 | array_map( 'wp_remove_post_parent', $children ); |
| 6163 | } |
| 6164 | } |