ID, self::META, array( 'parent' => (int) $post->post_parent, 'category' => $category ? (int) $category->term_id : 0, 'kb' => $kb ? (int) $kb->term_id : 0, 'order' => (int) $post->menu_order, 'children' => $children, ) ); } /** * Trashed from anywhere (the WordPress editor included). * * @param int $post_id Post. */ public function on_trash( $post_id ) { $post = get_post( $post_id ); // The builder already saved the position together with the nested articles. if ( $post && PostTypes::ARTICLE === $post->post_type && ! metadata_exists( 'post', $post_id, self::META ) ) { self::remember( $post ); } } /** * WordPress restores trashed posts as drafts; docs return to their previous status. * * @param string $status Status WordPress will use. * @param int $post_id Post. * @param string $previous_status Status before trashing. * @return string */ public function untrash_status( $status, $post_id, $previous_status ) { if ( PostTypes::ARTICLE === get_post_type( $post_id ) && $previous_status ) { return $previous_status; } return $status; } /** * Puts a restored article back in its old position. * * @param int $post_id Post. */ public function on_untrash( $post_id ) { $position = get_post_meta( $post_id, self::META, true ); delete_post_meta( $post_id, self::META ); if ( ! is_array( $position ) || PostTypes::ARTICLE !== get_post_type( $post_id ) ) { return; } $kb = $position['kb'] && term_exists( (int) $position['kb'], Taxonomies::KB ) ? (int) $position['kb'] : 0; if ( $kb ) { wp_set_object_terms( $post_id, array( $kb ), Taxonomies::KB ); } $category = (int) $position['category']; if ( $category && term_exists( $category, Taxonomies::CATEGORY ) && ( ! $kb || Taxonomies::category_kb( $category ) === $kb ) ) { wp_set_object_terms( $post_id, array( $category ), Taxonomies::CATEGORY ); } // The old parent article must still be live and in the same knowledge base. $parent = (int) $position['parent']; $parent_kb = $parent ? Taxonomies::article_kb( $parent ) : null; if ( ! $parent || ! in_array( get_post_status( $parent ), array( 'publish', 'draft', 'pending', 'private', 'future' ), true ) || ! $parent_kb || (int) $parent_kb->term_id !== $kb ) { $parent = 0; } wp_update_post( array( 'ID' => $post_id, 'post_parent' => $parent, 'menu_order' => (int) $position['order'], ) ); // Re-nest the articles that moved up a level, unless they were moved since. foreach ( (array) $position['children'] as $child ) { $child = get_post( (int) $child ); if ( $child && PostTypes::ARTICLE === $child->post_type && 'trash' !== $child->post_status && (int) $child->post_parent === (int) $position['parent'] ) { wp_update_post( array( 'ID' => $child->ID, 'post_parent' => $post_id, ) ); } } } }