Inventory_Presser_Admin_Photo_Arranger::add_attachment_to_gallery( mixed $post_id, mixed $parent_id = null )
add_attachment_to_gallery
Parameters Parameters
- $post_id
-
(mixed) (Required)
- $parent_id
-
(mixed) (Optional)
Default value: null
Return Return
(void)
Source Source
File: includes/admin/class-admin-photo-arranger.php
public function add_attachment_to_gallery( $post_id, $parent_id = null ) { $attachment = get_post( $post_id ); // Is this attachment an image? if ( ! wp_attachment_is_image( $attachment ) ) { // No. return; } // Is this new attachment even attached to a post? $parent; if ( ! empty( $attachment->post_parent ) ) { $parent = get_post( $attachment->post_parent ); } elseif ( ! empty( $parent_id ) ) { $parent = get_post( $parent_id ); } else { return; } // Is the new attachment attached to a vehicle? if ( empty( $parent->post_type ) || INVP::POST_TYPE !== $parent->post_type ) { // Parent post isn't a vehicle. return; } // Update the photo's post_parent. if ( empty( $attachment->post_parent ) || $attachment->post_parent !== $parent->ID ) { $attachment->post_parent = $parent->ID; $this->safe_update_post( $attachment ); } // Loop over all the post's blocks in search of our Gallery. $blocks = parse_blocks( $parent->post_content ); foreach ( $blocks as $index => &$block ) { // Is this a core gallery block? With a specific CSS class? if ( ! $this->is_gallery_block_with_specific_css_class( $block ) ) { continue; } // Does the block already have this attachment? if ( ! $this->inner_blocks_contains_id( $block, $post_id ) ) { // Add the uploaded attachment to this gallery. $block['innerBlocks'][] = array( 'blockName' => 'core/image', 'attrs' => array( 'id' => $post_id, 'sizeSlug' => 'large', 'linkDestination' => 'none', ), 'innerBlocks' => array(), 'innerHTML' => '', 'innerContent' => array( 0 => '', ), ); } $photo_count = count( $block['innerBlocks'] ); // Change a CSS class to reflect the number of photos in the Gallery // Replace all 'columns-#'. $block['innerContent'][0] = preg_replace( '/ columns-[0-9]+/', ' columns-' . $photo_count ?? 1, $block['innerContent'][0] ); // Do it again with extra parameter to replace just the first with a max of 'columns-3'. $block['innerContent'][0] = preg_replace( '/ columns-[0-9]+/', ' columns-' . min( 3, $photo_count ?? 4 ), $block['innerContent'][0], 1 ); if ( false === strpos( $block['attrs']['className'] ?? '', 'columns-' ) ) { $block['attrs']['className'] = sprintf( 'columns-%s %s', $photo_count ?? 1, $block['attrs']['className'] ); } else { $block['attrs']['className'] = preg_replace( '/columns-[0-9]+/', 'columns-' . $photo_count ?? 1, $block['attrs']['className'], 1 ); } // Add HTML that renders the image in the gallery // Is this image already in the Gallery HTML though? if ( false === mb_strpos( $block['innerContent'][0], "class=\"wp-image-$post_id\"" ) ) { // No. $position_list_end = mb_strpos( $block['innerContent'][0], "</figure>\r\n<!-- /wp:gallery -->" ); $new_html = sprintf( '<!-- wp:image {"id":%1$d,"sizeSlug":"large","linkDestination":"none"} --><figure class="wp-block-image size-large"><img src="%2$s" alt="" class="wp-image-%1$d"/></figure><!-- /wp:image -->', $post_id, $attachment->guid ); $block['innerContent'][0] = substr( $block['innerContent'][0], 0, $position_list_end ) . $new_html . substr( $block['innerContent'][0], ( $position_list_end ) ); } // Update the block in the $blocks array. $blocks[ $index ] = $block; // and then update the post. $parent->post_content = serialize_blocks( $blocks ); $this->safe_update_post( $parent ); break; } }
Expand full source codeCollapse full source codeView on Github