PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.0
Import WP – CSV & XML Import Export for WordPress v2.7.0
2.15.2 2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 All 145 releases
jc-importer / class / Common / Importer / ImporterStatusManager.php

ImporterStatusManager.php in Import WP – CSV & XML Import Export for WordPress 2.7.0, at class/Common/Importer/ImporterStatusManager.php

192 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer;
4
5 use ImportWP\Common\Model\ImporterModel;
6 use ImportWP\Container;
7
8 class ImporterStatusManager
9 {
10 public function get_importer_status(ImporterModel $importer)
11 {
12 $status_data = $importer->getStatus();
13 if (!$status_data) {
14 return false;
15 }
16
17 return new ImporterStatus($importer->getId(), $status_data);
18 }
19
20 public function get_importer_status_report(ImporterModel $immpoter_data, $session)
21 {
22 $logs = $this->get_importer_logs($immpoter_data);
23 foreach ($logs as $log) {
24 if ($log && $log['session'] === $session) {
25 return $log;
26 }
27 }
28 return false;
29 }
30
31 public function get_importer_logs(ImporterModel $importer_data, $page = 0, $per_page = -1)
32 {
33 $file_path = $this->get_status_file($importer_data->getId());
34
35 $line_counter = 0;
36 $lines = [];
37
38 $start = $end = -1;
39
40 if ($per_page > 0) {
41 $start = ($page - 1) * $per_page;
42 $end = $start + $per_page;
43 }
44
45 if (file_exists($file_path)) {
46 $fh = fopen($file_path, 'r');
47 if ($fh !== false) {
48 while (($data = fgets($fh)) !== false) {
49 if ($data[strlen($data) - 1] === "\n") {
50 $data = substr($data, 0, strlen($data) - 1);
51 }
52
53 if ($line_counter === $end) {
54 return $lines;
55 } elseif ($per_page === -1 || $line_counter >= $start) {
56 $lines[] = json_decode($data, true);
57 }
58
59 $line_counter++;
60 }
61 fclose($fh);
62 }
63 }
64 return $lines;
65 }
66
67 public function prune_importer_logs(ImporterModel $importer_data, $limit)
68 {
69 $limit = intval($limit);
70 if ($limit <= -1) {
71 return;
72 }
73
74 $file_path = $this->get_status_file($importer_data->getId());
75 $tmp_file_path = $this->get_status_file($importer_data->getId()) . '.tmp';
76 $lines = $this->get_importer_logs($importer_data);
77
78 if (count($lines) > $limit) {
79
80 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
81 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
82 $fileSystemDirect = new \WP_Filesystem_Direct(false);
83
84 $fh = fopen($tmp_file_path, 'w');
85 for ($i = 0; $i < count($lines); $i++) {
86
87 if ($i < count($lines) - $limit) {
88 $log_file_path = $this->get_log_file($importer_data->getId(), $lines[$i]['session']);
89 if ($fileSystemDirect->exists($log_file_path)) {
90
91 $fileSystemDirect->delete($log_file_path);
92
93 $tmp = $log_file_path;
94 for ($j = 0; $j < 3; $j++) {
95 $tmp = dirname($tmp);
96 $sub_files = $fileSystemDirect->dirlist($log_file_path);
97 if (empty($sub_files)) {
98 $fileSystemDirect->rmdir($tmp);
99 }
100 }
101 }
102 } else {
103 fputs($fh, json_encode($lines[$i]) . "\n");
104 }
105 }
106 fclose($fh);
107
108 if ($fileSystemDirect->move($tmp_file_path, $file_path, true)) {
109 $fileSystemDirect->delete($tmp_file_path);
110 }
111 }
112 }
113
114 public function get_importer_log(ImporterModel $importer_data, $session_id, $page = 0, $per_page = -1)
115 {
116 $file_path = $this->get_log_file($importer_data->getId(), $session_id);
117
118 $line_counter = 0;
119 $lines = [];
120 $start = $end = -1;
121
122 if ($per_page > 0) {
123 $start = ($page - 1) * $per_page;
124 $end = $start + $per_page;
125 }
126
127 if (file_exists($file_path)) {
128 $fh = fopen($file_path, 'r');
129 if ($fh !== false) {
130 while (($data = fgetcsv($fh)) !== false) {
131 if ($line_counter === $end) {
132 return $lines;
133 } elseif ($per_page === -1 || $line_counter >= $start) {
134 $lines[] = $data;
135 }
136
137 $line_counter++;
138 }
139 fclose($fh);
140 }
141 }
142 return $lines;
143 }
144
145 public function get_status_file($id)
146 {
147 /**
148 * @var ImporterManager $importer_manager
149 */
150 $importer_manager = Container::getInstance()->get('importer_manager');
151 return $importer_manager->get_config_path($id) . '.status';
152 }
153
154 public function get_log_file($importer_id, $session_id)
155 {
156 /**
157 * @var ImporterManager $importer_manager
158 */
159 $importer_manager = Container::getInstance()->get('importer_manager');
160 return $importer_manager->get_config_path($importer_id, false, $session_id) . '.logs-' . $session_id;
161 }
162
163 public function create(ImporterModel $importer)
164 {
165 $version = get_post_meta($importer->getId(), '_iwp_version', true);
166 if ($version !== false) {
167 $version++;
168 } else {
169 $version = 0;
170 }
171 update_post_meta($importer->getId(), '_iwp_version', $version);
172
173 $status = new ImporterStatus($importer->getId(), [
174 'session' => md5($importer->getId() . time()),
175 'status' => 'init',
176 'version' => $version
177 ]);
178
179 // keep track of current session
180 update_post_meta($importer->getId(), '_iwp_session', $status->get_session_id());
181
182 $importer->setStatus($status);
183 return $status;
184 }
185
186 public function clear(ImporterModel $importer_model)
187 {
188 delete_post_meta($importer_model->getId(), '_iwp_session');
189 wp_update_post(['ID' => $importer_model->getId(), 'post_excerpt' => '']);
190 }
191 }
192