Inventory_Presser_Photo_Numberer::save_meta_photo_number( int $post_id, int $parent_post_id, int $sequence_number = null )

Saves a sequence number like 1, 2, or 99 in attachment post meta. This number dictates the order in which the photos will be disabled in sliders and galleries.


Parameters Parameters

$post_id

(int) (Required) The post ID of the attachment.

$parent_post_id

(int) (Required) The post ID of the vehicle to which $post_id is a child.

$sequence_number

(int) (Optional) The sequence number to save. Do not provide to append.

Default value: null


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-photo-numberer.php

	 * number dictates the order in which the photos will be disabled in sliders
	 * and galleries.
	 *
	 * @param  int $post_id         The post ID of the attachment.
	 * @param  int $parent_post_id  The post ID of the vehicle to which $post_id is a child.
	 * @param  int $sequence_number The sequence number to save. Do not provide to append.
	 * @return void
	 */
	public static function save_meta_photo_number( $post_id, $parent_post_id, $sequence_number = null ) {
		if ( null === $sequence_number ) {
			// Does this photo already have a sequence number?
			if ( ! empty( INVP::get_meta( 'photo_number', $post_id ) ) ) {
				// Yes.
				return;
			}

			// 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\-.*/', sanitize_text_field( wp_unslash( $_POST['slug'] ) ), $matches ) ) {
				$number = intval( $matches[1] );
			} else {
				/**
				 * This hook fires after the attachment(s) are added, so the
				 * vehicle may have 1 photo that is numbered, and the 3 that
				 * were just added have the vehicle as a parent but do not have
				 * number meta values. This request is for one of the 3, they
				 * are all happening simultaneously.
				 */
				global $wpdb;
				$numbered_count = (int) $wpdb->get_var(
					$wpdb->prepare(
						"SELECT		COUNT( `{$wpdb->prefix}posts`.`ID` )

FROM		`{$wpdb->prefix}postmeta`
			LEFT JOIN `{$wpdb->prefix}posts` ON `{$wpdb->prefix}posts`.`ID` = `{$wpdb->prefix}postmeta`.`post_id`

WHERE		`{$wpdb->prefix}posts`.`post_parent` = %d
			AND `post_type` = 'attachment'
			AND `meta_key` = %s",
						$parent_post_id,
						apply_filters( 'invp_prefix_meta_key', 'photo_number' )
					)
				);

				// How many unnumbered photos does this vehicle have?
				$unnumbered_count = invp_get_the_photo_count( $parent_post_id ) - $numbered_count;
				if ( 1 < $unnumbered_count ) {
					/**
					 * Abort. There are a couple unnumbered photos running this
					 * method simultaneously. Let the photos remain unnumbered
					 * until the user presses the Save Draft or Update button in
					 * the editor.
					 */
					return;
				}

				$number = 1 + $numbered_count;

				if ( 1 === $number ) {
					// This is photo number 1, it should be the featured image.
					set_post_thumbnail( $parent_post_id, $post_id );
				}
			}
		} else {