Inventory_Presser_Plugin::change_attachment_urls( string $url, int $post_id )

Adds a querystring to vehicle attachment photo URLs to fight caching.


Parameters

$url

(string) (Required) URL for the given attachment.

$post_id

(int) (Required) Attachment post ID.


Return

(string) The changed URL


Source

File: inventory-presser.php

		public function change_attachment_urls( $url, $post_id ) {
			if ( 'cropped-favico.png' === basename( $url ) || empty( $post_id ) ) {
				return $url;
			}

			if ( INVP::POST_TYPE !== get_post_type( wp_get_post_parent_id( $post_id ) ) ) {
				return $url;
			}
			// Is the URL pointing to a file with an image mime type?
			$file = get_attached_file( $post_id );
			if ( empty( $file ) || ! is_string( $file ) ) {
				return $url;
			}
			$mime_type = wp_get_image_mime( $file );
			if ( false === $mime_type || ( 5 <= strlen( $mime_type ) && 'image' !== substr( $mime_type, 0, 5 ) ) ) {
				return $url;
			}

			// Add a querystring that contains this photo's hash.
			$hash = INVP::get_meta( 'hash', $post_id );
			if ( empty( $hash ) ) {
				return $url;
			}

			$parsed = wp_parse_url( $url );
			return $url . ( empty( $parsed['query'] ) ? '?' : '&' ) . $hash;
		}