| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\File\CSVFile; |
| 6 |
use ImportWP\Common\Exporter\File\JSONFile; |
| 7 |
use ImportWP\Common\Exporter\File\XMLFile; |
| 8 |
use ImportWP\Common\Exporter\Mapper\CommentMapper; |
| 9 |
use ImportWP\Common\Exporter\Mapper\MapperData; |
| 10 |
use ImportWP\Common\Exporter\Mapper\PostMapper; |
| 11 |
use ImportWP\Common\Exporter\Mapper\TaxMapper; |
| 12 |
use ImportWP\Common\Exporter\Mapper\UserMapper; |
| 13 |
use ImportWP\Common\Model\ExporterModel; |
| 14 |
|
| 15 |
class ExporterManager |
| 16 |
{ |
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_action('admin_init', [$this, 'download_file']); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Download exporter file directly |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function download_file() |
| 28 |
{ |
| 29 |
|
| 30 |
if (!isset($_GET['page'], $_GET['exporter'], $_GET['download']) || $_GET['page'] !== 'importwp') { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$exporter_data = $this->get_exporter(intval($_GET['exporter'])); |
| 35 |
$file = sanitize_key($_GET['download']); |
| 36 |
|
| 37 |
$download_data = get_post_meta($exporter_data->getId(), '_ewp_file_' . $file, true); |
| 38 |
if ($download_data) { |
| 39 |
|
| 40 |
delete_post_meta($exporter_data->getId(), '_ewp_file_' . $file, $download_data); |
| 41 |
|
| 42 |
header('Content-disposition: attachment; filename="' . basename($download_data['path']) . '"'); |
| 43 |
header('Content-type: "text/' . $download_data['type'] . '"; charset="utf8"'); |
| 44 |
readfile($download_data['path']); |
| 45 |
die(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get Exporters |
| 51 |
* |
| 52 |
* @return ExporterModel[] |
| 53 |
*/ |
| 54 |
public function get_exporters() |
| 55 |
{ |
| 56 |
|
| 57 |
$result = array(); |
| 58 |
$query = new \WP_Query(array( |
| 59 |
'post_type' => EWP_POST_TYPE, |
| 60 |
'posts_per_page' => -1, |
| 61 |
)); |
| 62 |
|
| 63 |
foreach ($query->posts as $post) { |
| 64 |
$result[] = $this->get_exporter($post); |
| 65 |
} |
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get Exporter |
| 71 |
* |
| 72 |
* @param int $id |
| 73 |
* @return ExporterModel |
| 74 |
*/ |
| 75 |
public function get_exporter($id) |
| 76 |
{ |
| 77 |
if ($id instanceof ExporterModel) { |
| 78 |
return $id; |
| 79 |
} |
| 80 |
|
| 81 |
if (EWP_POST_TYPE !== get_post_type($id)) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
return new ExporterModel($id, $this->is_debug()); |
| 86 |
} |
| 87 |
|
| 88 |
public function is_debug() |
| 89 |
{ |
| 90 |
|
| 91 |
if (defined('IWP_DEBUG') && true === IWP_DEBUG) { |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
$uninstall_enabled = get_option('iwp_settings'); |
| 96 |
if (isset($uninstall_enabled['debug']) && true === $uninstall_enabled['debug']) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Delete Importer |
| 105 |
* |
| 106 |
* @param int $id |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function delete_exporter($id) |
| 110 |
{ |
| 111 |
$exporter = $this->get_exporter($id); |
| 112 |
$exporter->delete(); |
| 113 |
} |
| 114 |
|
| 115 |
public function export($id) |
| 116 |
{ |
| 117 |
$exporter_data = $this->get_exporter($id); |
| 118 |
$previous_time = microtime(true); |
| 119 |
|
| 120 |
if ($exporter_data->getFileType() === 'csv') { |
| 121 |
$file = new CSVFile($exporter_data); |
| 122 |
} elseif ($exporter_data->getFileType() === 'xml') { |
| 123 |
$file = new XMLFile($exporter_data); |
| 124 |
} else { |
| 125 |
$file = new JSONFile($exporter_data); |
| 126 |
} |
| 127 |
|
| 128 |
$type = $exporter_data->getType(); |
| 129 |
$matches = null; |
| 130 |
if (preg_match('/^ewp_tax_(.*?)$/', $type, $matches) == 1) { |
| 131 |
$taxonomy = $matches[1]; |
| 132 |
$mapper = new TaxMapper($taxonomy); |
| 133 |
} elseif (preg_match('/^ewp_comment_(.*?)$/', $type, $matches) == 1) { |
| 134 |
$post_type = $matches[1]; |
| 135 |
$mapper = new CommentMapper($post_type); |
| 136 |
} elseif ($type === 'user') { |
| 137 |
$mapper = new UserMapper(); |
| 138 |
} else { |
| 139 |
|
| 140 |
$mapper = apply_filters('iwp/exporter/load_mapper', false, $type); |
| 141 |
if (!$mapper) { |
| 142 |
$mapper = new PostMapper($type); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
$mapper->set_filters($exporter_data->getFilters()); |
| 147 |
|
| 148 |
// Default to showing all fields, if none selected |
| 149 |
if (empty($exporter_data->getFields(true))) { |
| 150 |
|
| 151 |
$fields = $mapper->get_fields(); |
| 152 |
|
| 153 |
switch ($exporter_data->getFileType()) { |
| 154 |
case 'csv': |
| 155 |
$fields = $this->flattenFields($fields); |
| 156 |
break; |
| 157 |
default: |
| 158 |
$fields = $this->nestedFields($fields); |
| 159 |
break; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
$exporter_data->setFields($fields); |
| 164 |
} |
| 165 |
|
| 166 |
$file->start(); |
| 167 |
|
| 168 |
$columns = $exporter_data->getFields(); |
| 169 |
|
| 170 |
$total = 0; |
| 171 |
$i = 0; |
| 172 |
|
| 173 |
if ($mapper->have_records()) { |
| 174 |
|
| 175 |
$total = $mapper->found_records(); |
| 176 |
|
| 177 |
for ($i = 0; $i < $total; $i++) { |
| 178 |
|
| 179 |
$mapper_data = new MapperData($mapper, $i); |
| 180 |
if (!$mapper_data->skip()) { |
| 181 |
$file->add($mapper_data); |
| 182 |
} |
| 183 |
|
| 184 |
$current_time = microtime(true); |
| 185 |
$delta_time = $current_time - $previous_time; |
| 186 |
|
| 187 |
if ($delta_time > 0.1) { |
| 188 |
$exporter_data->set_status('running', $i, $total); |
| 189 |
$previous_time = $current_time; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
$file->end(); |
| 195 |
|
| 196 |
$key = md5(time()); |
| 197 |
add_post_meta($exporter_data->getId(), '_ewp_file_' . $key, array( |
| 198 |
'url' => $file->get_file_url(), |
| 199 |
'path' => $file->get_file_path(), |
| 200 |
'type' => $exporter_data->getFileType() |
| 201 |
)); |
| 202 |
|
| 203 |
$status = $exporter_data->set_status('complete', $i, $total); |
| 204 |
$status['file'] = $key; |
| 205 |
return $status; |
| 206 |
// echo json_encode($status) . "\n"; |
| 207 |
|
| 208 |
// flush(); |
| 209 |
// ob_flush(); |
| 210 |
// die(); |
| 211 |
} |
| 212 |
|
| 213 |
public function flattenFields($fields, $prefix = '') |
| 214 |
{ |
| 215 |
$output = []; |
| 216 |
|
| 217 |
$output = array_merge($output, array_reduce($fields['fields'], function ($carry, $item) use ($prefix) { |
| 218 |
|
| 219 |
$carry[] = [ |
| 220 |
'parent' => 0, |
| 221 |
'id' => count($carry) + 1, |
| 222 |
'selection' => $prefix . $item, |
| 223 |
'loop' => false, |
| 224 |
'custom' => false |
| 225 |
]; |
| 226 |
|
| 227 |
return $carry; |
| 228 |
}, [])); |
| 229 |
|
| 230 |
if (!empty($fields['children'])) { |
| 231 |
foreach ($fields['children'] as $child) { |
| 232 |
$output = array_merge($output, $this->flattenFields($child, $child['key'] . '.')); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return $output; |
| 237 |
} |
| 238 |
|
| 239 |
private $nested_field_counter = 0; |
| 240 |
|
| 241 |
public function nestedFields($fields, $prefix = '', $parent = 0) |
| 242 |
{ |
| 243 |
$output = []; |
| 244 |
|
| 245 |
if ($parent == 0) { |
| 246 |
|
| 247 |
$this->nested_field_counter = 1; |
| 248 |
$output[] = [ |
| 249 |
'parent' => 0, |
| 250 |
'id' => 1, |
| 251 |
'selection' => '', |
| 252 |
'label' => 'records', |
| 253 |
'loop' => false, |
| 254 |
]; |
| 255 |
|
| 256 |
$this->nested_field_counter++; |
| 257 |
$output[] = [ |
| 258 |
'parent' => 1, |
| 259 |
'id' => 2, |
| 260 |
'selection' => 'main', |
| 261 |
'label' => 'record', |
| 262 |
'loop' => true, |
| 263 |
]; |
| 264 |
|
| 265 |
$parent = 2; |
| 266 |
} elseif ($fields['loop'] == true) { |
| 267 |
$this->nested_field_counter++; |
| 268 |
$output[] = [ |
| 269 |
'parent' => $parent, |
| 270 |
'id' => $this->nested_field_counter, |
| 271 |
'selection' => '', |
| 272 |
'label' => $fields['key'] . '_wrapper', |
| 273 |
'loop' => false, |
| 274 |
]; |
| 275 |
$parent = $this->nested_field_counter; |
| 276 |
|
| 277 |
$this->nested_field_counter++; |
| 278 |
$output[] = [ |
| 279 |
'parent' => $parent, |
| 280 |
'id' => $this->nested_field_counter, |
| 281 |
'selection' => $fields['key'], |
| 282 |
'label' => $fields['key'], |
| 283 |
'loop' => true, |
| 284 |
]; |
| 285 |
|
| 286 |
$parent = $this->nested_field_counter; |
| 287 |
} |
| 288 |
|
| 289 |
if (isset($fields['loop_fields']) && !empty($fields['loop_fields'])) { |
| 290 |
|
| 291 |
$output = array_merge($output, array_reduce($fields['loop_fields'], function ($carry, $item) use ($parent) { |
| 292 |
|
| 293 |
$this->nested_field_counter++; |
| 294 |
$carry[] = [ |
| 295 |
'parent' => $parent, |
| 296 |
'id' => $this->nested_field_counter, |
| 297 |
'selection' => $item, |
| 298 |
'loop' => false, |
| 299 |
'custom' => false |
| 300 |
]; |
| 301 |
|
| 302 |
return $carry; |
| 303 |
}, [])); |
| 304 |
} else { |
| 305 |
|
| 306 |
$output = array_merge($output, array_reduce($fields['fields'], function ($carry, $item) use ($prefix, $parent) { |
| 307 |
|
| 308 |
$this->nested_field_counter++; |
| 309 |
$carry[] = [ |
| 310 |
'parent' => $parent, |
| 311 |
'id' => $this->nested_field_counter, |
| 312 |
'selection' => $prefix . $item, |
| 313 |
'loop' => false, |
| 314 |
'custom' => false |
| 315 |
]; |
| 316 |
|
| 317 |
return $carry; |
| 318 |
}, [])); |
| 319 |
} |
| 320 |
|
| 321 |
if (!empty($fields['children'])) { |
| 322 |
foreach ($fields['children'] as $child) { |
| 323 |
$output = array_merge($output, $this->nestedFields($child, $child['key'] . '.', $parent)); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return $output; |
| 328 |
} |
| 329 |
} |
| 330 |
|