Inventory_Presser_Photo_Numberer::maybe_number_photo( int $post_id )

maybe_number_photo


Description Description

Filter callback on add_attachment. Decides whether to write meta values on attachments if they are uploaded to vehicles.


Top ↑

Parameters Parameters

$post_id

(int) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-photo-numberer.php

	function maybe_number_photo( $post_id )
	{
		//Is this new attachment even attached to a post?
		$attachment = get_post( $post_id );
		if( empty( $attachment->post_parent ) )
		{
			//No
			return;
		}

		$parent = get_post( $attachment->post_parent );

		//Is this even attached to a vehicle?
		if( empty( $parent ) || INVP::POST_TYPE != $parent->post_type )
		{
			//No
			return;
		}

		//Save the VIN in the photo meta
		$vin = get_post_meta( $attachment->post_parent, apply_filters( 'invp_prefix_meta_key', 'vin' ), true );
		update_post_meta( $post_id, apply_filters( 'invp_prefix_meta_key', 'vin' ), $vin );

		//Save a md5 hash checksum of the attachment in meta
		$hash = hash_file( 'md5', get_attached_file( $post_id ) );
		update_post_meta( $post_id, apply_filters( 'invp_prefix_meta_key', 'hash' ), $hash );

		//Give it a number. Is the number in the slug?
		//photo-5-of-19-of-vinsgsdkdkdkgf
		$number = 0;
		if( ! empty( $_POST['slug'] ) && preg_match( '/photo\-([0-9]+)\-of\-[0-9]+\-of\-.*/', $_POST['slug'], $matches ) )
		{
			$number = $matches[1];
		}
		else
		{
			//Append the photo to the end
			//How many photos does this vehicle have already?
			$photos = get_posts( array(
				'meta_query'     => array(
					array(
						'key'     => apply_filters( 'invp_prefix_meta_key', 'photo_number' ),
						'compare' => 'EXISTS'
					)
				),
				'order'          => 'ASC',
				'orderby'        => 'meta_value_num',
				'post_parent'    => $parent->ID,
				'post_type'      => 'attachment',
				'posts_per_page' => -1,
			) );
			
			if( 0 < sizeof( $photos ) )
			{
				$last_photo = end( $photos );
				$last_number = intval( get_post_meta( $last_photo->ID,  apply_filters( 'invp_prefix_meta_key', 'photo_number' ), true ) );
				$number = $last_number + 1;
			}
		}
		
		update_post_meta( $attachment->ID, apply_filters( 'invp_prefix_meta_key', 'photo_number' ), $number );
	}