| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_Attachment_Cache. |
| 5 |
*/ |
| 6 |
class Optml_Attachment_Cache { |
| 7 |
const CACHE_GROUP = 'optml_attachment_ids'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Get the cached attachment ID. |
| 11 |
* |
| 12 |
* @param string $url the URL of the attachment. |
| 13 |
* |
| 14 |
* @return bool|mixed |
| 15 |
*/ |
| 16 |
public static function get_cached_attachment_id( $url ) { |
| 17 |
$cache_key = self::get_cache_key( $url ); |
| 18 |
|
| 19 |
return wp_cache_get( $cache_key, self::CACHE_GROUP ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Set the cached attachment ID. |
| 24 |
* |
| 25 |
* @param string $url the URL of the attachment. |
| 26 |
* @param int $id the attachment ID. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public static function set_cached_attachment_id( $url, $id ) { |
| 31 |
$cache_key = self::get_cache_key( $url ); |
| 32 |
|
| 33 |
wp_cache_set( $cache_key, $id, self::CACHE_GROUP, DAY_IN_SECONDS ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Generate cache key for URL. |
| 38 |
* |
| 39 |
* @param string $url the URL to generate the cache key for. |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
private static function get_cache_key( $url ) { |
| 44 |
$url = strtok( $url, '?' ); |
| 45 |
|
| 46 |
return 'attachment_id_' . crc32( $url ); |
| 47 |
} |
| 48 |
} |
| 49 |
|