DataManager.php
2.86 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Optimization\DynamicLists;
class DataManager {
/**
* Filesystem instance
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Instantiate the class
*
* @param WP_Filesystem_Direct $filesystem Filesystem instance.
*/
public function __construct( $filesystem = null ) {
$this->filesystem = is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem;
}
/**
* Gets the lists content
*
* @return object
*/
public function get_lists() {
$transient = get_transient( 'wpr_dynamic_lists' );
if ( false !== $transient ) {
return $transient;
}
$json = $this->get_lists_from_file();
$lists = json_decode( $json );
if ( empty( $lists ) ) {
return '';
}
$this->set_lists_cache( $lists );
return $lists;
}
/**
* Returns the hash of the current JSON
*
* @return string
*/
public function get_lists_hash() {
return md5( $this->get_lists_from_file() );
}
/**
* Save dynamic lists on file & transient
*
* @param string $content Lists content.
*
* @return boolean
*/
public function save_dynamic_lists( string $content ) {
$result = $this->put_lists_to_file( $content );
$lists = json_decode( $content );
$this->set_lists_cache( $lists );
/**
* Fires after saving the dynamic lists file
*
* @since 3.12.1
*/
do_action( 'rocket_after_save_dynamic_lists' );
return $result;
}
/**
* Gets the path to the dynamic lists JSON file
*
* @return string
*/
private function get_json_filepath(): string {
return rocket_get_constant( 'WP_ROCKET_CONFIG_PATH', '' ) . 'dynamic-lists.json';
}
/**
* Gets lists JSON content from file
*
* @return string
*/
private function get_lists_from_file(): string {
$content = '';
$lists_filepath = $this->get_json_filepath();
if ( $this->filesystem->exists( $lists_filepath ) ) {
$content = $this->filesystem->get_contents( $lists_filepath );
}
if ( ! empty( $content ) ) {
return $content;
}
$fallback_filepath = rocket_get_constant( 'WP_ROCKET_PATH', '' ) . 'dynamic-lists.json';
if ( $this->filesystem->exists( $fallback_filepath ) ) {
$content = $this->filesystem->get_contents( $fallback_filepath );
}
if ( ! empty( $content ) ) {
$this->put_lists_to_file( $content );
return $content;
}
return $content;
}
/**
* Write lists content to JSON file
*
* @param string $content JSON content.
*
* @return bool
*/
private function put_lists_to_file( string $content ): bool {
return $this->filesystem->put_contents( $this->get_json_filepath(), $content, rocket_get_filesystem_perms( 'file' ) );
}
/**
* Sets transient for lists content
*
* @param object $content Lists content.
*
* @return void
*/
private function set_lists_cache( $content ) {
set_transient( 'wpr_dynamic_lists', $content, WEEK_IN_SECONDS );
}
}