class-wpml-st-file-hashing.php
1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class WPML_ST_File_Hashing {
const OPTION_NAME = 'wpml-scanning-files-hashing';
/** @var array */
private $hashes;
public function __construct() {
$this->hashes = $this->get_hashes();
}
/**
* @param string $file
*/
private function set_hash( $file ) {
$this->hashes[ $file ] = md5_file( $file );
}
/**
* @param string $file
*
* @return bool
*/
public function hash_changed( $file ) {
return ! array_key_exists( $file, $this->hashes ) || md5_file( $file ) !== $this->hashes[ $file ];
}
public function save_hash() {
$needs_to_save = false;
if ( array_key_exists( 'files', $_POST ) ) {
foreach ( $_POST['files'] as $file_path ) {
if ( realpath( $file_path ) ) {
$this->set_hash( $file_path );
$needs_to_save = true;
}
}
}
if ( $needs_to_save ) {
update_option( self::OPTION_NAME, $this->hashes );
}
wp_send_json_success();
}
/**
* @return array
*/
private function get_hashes() {
return get_option( self::OPTION_NAME ) ? get_option( self::OPTION_NAME ) : array();
}
public function clean_hashes() {
delete_option( self::OPTION_NAME );
}
}