Skip to content

Instantly share code, notes, and snippets.

@TylerB24890
Last active January 7, 2022 16:52
Show Gist options
  • Save TylerB24890/308c4f268fbf0f29fa0965a9d7a320ad to your computer and use it in GitHub Desktop.
Save TylerB24890/308c4f268fbf0f29fa0965a9d7a320ad to your computer and use it in GitHub Desktop.
Add timestamp to end of media assets replaced by Enable Media Replace
<?php
/**
* Automatic Media & Post Cache Purging
* when using the Enable Media Replace plugin.
*
* @package Tyme
*/
namespace Tyme\CacheManager;
/**
* Hook actions into WP
*
* @return void
*/
function setup() {
$n = function( $func ) {
return __NAMESPACE__ . "\\$func";
};
// Media Cache
if ( apply_filters( 'tyme_media_cache_manager', true ) ) {
add_action( 'enable-media-replace-upload-done', $n( 'add_replaced_timestamp' ) );
add_filter( 'wp_get_attachment_url', $n( 'add_cache_buster' ), 10, 2 );
add_filter( 'the_content', $n( 'add_cache_buster_post_content' ) );
}
}
/**
* Add cache buster query parameter to attachment URL
*
* @param string $url Attachment URL
* @param integer $attach_id Attachment ID
* @return string Modified attachment URL
*/
function add_cache_buster( $url, $attach_id ) {
$file_version = get_file_version( $attach_id );
if ( ! $file_version || '' === trim( $file_version ) ) {
return $url;
}
if ( check_file_types( $url ) ) {
return add_query_arg( 'v', rawurlencode( $file_version ), $url );
}
return $url;
}
/**
* Add Cache Buster parameter to post content links
* Extracts all links from the $content parameter and pulls its
* cache busting URL with query parameter
*
* @param string $content Post content
* @return string Modified post content
*/
function add_cache_buster_post_content( $content ) {
$links = check_file_types( wp_extract_urls( $content ) );
$replace = [];
if ( ! empty( $links ) ) {
foreach ( $links as $link ) {
$busted = add_cache_buster( $link, get_id_by_url( $link ) );
if ( $busted !== $link ) {
$replace[ $link ] = $busted;
}
}
}
if ( ! empty( $replace ) ) {
$content = str_replace( array_keys( $replace ), array_values( $replace ), $content );
}
return $content;
}
/**
* Check URLs for non-cached file types
*
* @param array|string $links Array of URLs or single URL
* @return array|string Array of URLs or single URL
*/
function check_file_types( $links ) {
$dont_cache = apply_filters( 'tyme_no_cache_filetypes', [ 'pdf' ] );
$filtered = [];
if ( is_array( $links ) ) {
$filtered = array_filter(
$links,
function( $link ) use ( $dont_cache ) {
return wp_check_filetype( basename( $link ), $dont_cache );
}
);
} else {
return wp_check_filetype( basename( $links ), $dont_cache );
}
return $filtered;
}
/**
* Only add cache buster if file is recently replaced
* Removes the query string after 5 days of being cached
*
* @param integer $attach_id WP Attachment ID
* @return boolean
*/
function get_file_version( $attach_id ) {
return get_post_meta( $attach_id, 'tyme_attachment_replaced', true );
}
/**
* Get attachment ID by URL
*
* @param string $attach_url Attachment URL
* @return integer Attachment ID
*/
function get_id_by_url( $attach_url ) {
$attach_id = false;
if ( function_exists( 'wpcom_vip_attachment_url_to_postid' ) ) {
$attach_id = wpcom_vip_attachment_url_to_postid( $attach_url );
}
return $attach_id;
}
/**
* Add Attachment Metadata on Media Replace
*
* @param string $target_url The attachment URL
* @return string The attachment URL
*/
function add_replaced_timestamp( $target_url ) {
if ( check_file_types( $target_url ) ) {
$attach_id = get_id_by_url( $target_url );
if ( $attach_id ) {
update_post_meta( $attach_id, 'tyme_attachment_replaced', strtotime( 'now' ) );
}
}
return $target_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment