| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our class responsible for versions storage services. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh\Services; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class for versions storage services. |
| 12 |
*/ |
| 13 |
class Versions_Storage_Service { |
| 14 |
|
| 15 |
const OPTION_SITE_VERSION = 'force_refresh_current_site_version'; |
| 16 |
|
| 17 |
const OPTION_PAGE_VERSION = 'force_refresh_current_page_version'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Method for setting a site version. |
| 21 |
* |
| 22 |
* @param string $new_version The new site version. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public static function set_site_version( string $new_version ): void { |
| 27 |
// Remove the old option. |
| 28 |
delete_option( self::OPTION_SITE_VERSION ); |
| 29 |
// Add the new option. |
| 30 |
add_option( self::OPTION_SITE_VERSION, $new_version ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Method to get the current version of the site. |
| 35 |
* |
| 36 |
* @return string The version of the site |
| 37 |
*/ |
| 38 |
public static function get_site_version(): string { |
| 39 |
$current_site_version = get_option( self::OPTION_SITE_VERSION ); |
| 40 |
return (bool) $current_site_version ? $current_site_version : '0'; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Method for setting a page version. |
| 45 |
* |
| 46 |
* @param int $page_id The post ID to update. |
| 47 |
* @param string $page_version The new page version. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public static function set_page_version( int $page_id, string $page_version ): void { |
| 52 |
// Remove the old. |
| 53 |
delete_post_meta( $page_id, self::OPTION_PAGE_VERSION ); |
| 54 |
|
| 55 |
// Add the new. |
| 56 |
update_post_meta( |
| 57 |
$page_id, |
| 58 |
self::OPTION_PAGE_VERSION, |
| 59 |
$page_version, |
| 60 |
null, |
| 61 |
'no' |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Method to retrieve a hash that can be used as the version. This function will |
| 67 |
* deliver a unique ID that can be used to identify a unique version of a site, page, |
| 68 |
* or post. |
| 69 |
* |
| 70 |
* @return string The unique version ID |
| 71 |
*/ |
| 72 |
public static function get_new_version(): string { |
| 73 |
$time = current_time( 'mysql' ); |
| 74 |
$site_version_hash = wp_hash( $time ); |
| 75 |
// Get the first eight characters (the chance of having a duplicate hash from |
| 76 |
// the first 8 characters is low). |
| 77 |
$site_version = substr( $site_version_hash, 0, 8 ); |
| 78 |
return $site_version; |
| 79 |
} |
| 80 |
} |
| 81 |
|