Inventory_Presser_Admin_Photo_Arranger::unattach_when_removed( mixed $post_id, mixed $post )

unattach_when_removed


Parameters Parameters

$post_id

(mixed) (Required)

$post

(mixed) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/admin/class-admin-photo-arranger.php

	public function unattach_when_removed( $post_id, $post ) {

		// If this post was just trashed, who cares.
		if ( ! empty( $post->post_status ) && 'trash' === $post->post_status ) {
			return;
		}

		// Does the post have our gallery block?
		$block = $this->find_gallery_block( $post );
		if ( false === $block ) {
			return;
		}

		/**
		 * Are there attachments to this post that are no longer in the
		 * gallery block?
		 */
		$attachment_ids = get_children(
			array(
				'fields'         => 'ids',
				'post_parent'    => $post_id,
				'post_type'      => 'attachment',
				'posts_per_page' => 500,
			)
		);

		foreach ( $attachment_ids as $attachment_id ) {
			if ( $this->inner_blocks_contains_id( $block, $attachment_id ) ) {
				continue;
			}
			// Detach those from this vehicle.
			$attachment              = get_post( $attachment_id );
			$attachment->post_parent = 0;
			$this->safe_update_post( $attachment );
		}
	}