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

Runs on edit_post hook. Changes the post_parent values on photos in our magic Gallery block.


Parameters

$post_id

(mixed) (Required)

$post

(mixed) (Required)


Return

(void)


Source

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

	public function change_parents_and_sequence( $post_id, $post ) {

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

		// Make sure the photos in the gallery block have the post_parent value.
		$block = $this->find_gallery_block( $post );
		if ( false === $block ) {
			return;
		}

		if ( empty( $block['innerBlocks'] ) ) {
			// There are no photos in the gallery.
			return;
		}

		// Set a post_parent value on every photo in the gallery block.
		foreach ( $block['innerBlocks'] as $index => $image_block ) {
			if ( empty( $image_block['blockName'] ) || 'core/image' !== $image_block['blockName'] ) {
				continue;
			}
			if ( empty( $image_block['attrs']['id'] ) ) {
				continue;
			}
			$attachment = get_post( $image_block['attrs']['id'] );
			if ( ! empty( $attachment ) ) {
				// Update the photo's post_parent.
				if ( empty( $attachment->post_parent ) || $attachment->post_parent !== $post_id ) {
					$attachment->post_parent = $post_id;
					$this->safe_update_post( $attachment );
				}

				// Does the number match?
				if ( strval( $index + 1 ) !== INVP::get_meta( 'photo_number', $attachment->ID ) ) {
					// Save VIN and photo hash in photo meta.
					Inventory_Presser_Photo_Numberer::maybe_number_photo( $attachment->ID );
					// Force save the sequence number.
					Inventory_Presser_Photo_Numberer::save_meta_photo_number( $attachment->ID, $post_id, strval( $index + 1 ) );
				}
			}
		}
	}