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.
On This Page
Parameters Parameters
- $post_id
-
(mixed) (Required)
- $post
-
(mixed) (Required)
Return Return
(void)
Source 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;
}
// Don't renumber right after we renumber.
remove_action( 'save_post_' . INVP::POST_TYPE, array( 'Inventory_Presser_Photo_Numberer', 'renumber_photos' ), 10, 1 );
// 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 the VIN in the photo meta.
Inventory_Presser_Photo_Numberer::save_meta_vin( $attachment->ID, $attachment->post_parent );
// Save a md5 hash checksum of the attachment in meta.
Inventory_Presser_Photo_Numberer::save_meta_hash( $attachment->ID );
// Force save the sequence number.
Inventory_Presser_Photo_Numberer::save_meta_photo_number( $attachment->ID, $post_id, strval( $index + 1 ) );
}
}
}
Inventory_Presser_Photo_Numberer::delete_photo_transients( $post_id );
}
Expand full source codeCollapse full source codeView on Github