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

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


Parameters Parameters

$url

(string) (Required)

$post_id

(int) (Optional)

Default value: null


Top ↑

Return Return

(string) The changed URL


Top ↑

Source Source

File: inventory-presser.php

		public function change_attachment_urls( $url, $post_id = null ) {
			if ( empty( $post_id ) ) {
				$post_id = attachment_url_to_postid( $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 ( false === $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;
		}