|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\League\Csv\Reader; |
|
6
|
|
|
use GeminiLabs\League\Csv\Statement; |
|
7
|
|
|
use GeminiLabs\League\Csv\TabularDataReader; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Database\ReviewManager; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Database\Tables\TableTmp; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Defaults\ImportResultDefaults; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Request; |
|
15
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
16
|
|
|
|
|
17
|
|
|
class ImportManager |
|
18
|
|
|
{ |
|
19
|
|
|
public function flush(): void |
|
20
|
|
|
{ |
|
21
|
|
|
glsr(TableTmp::class)->drop(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function import(int $limit = 1, int $offset = 0): array |
|
25
|
|
|
{ |
|
26
|
|
|
if (!defined('WP_IMPORTING')) { |
|
27
|
|
|
define('WP_IMPORTING', true); |
|
28
|
|
|
} |
|
29
|
|
|
set_time_limit(0); |
|
30
|
|
|
wp_raise_memory_limit('admin'); |
|
31
|
|
|
wp_defer_term_counting(true); |
|
32
|
|
|
wp_suspend_cache_invalidation(true); |
|
33
|
|
|
$records = $this->records($limit, $offset); |
|
34
|
|
|
$result = glsr(ImportResultDefaults::class)->defaults(); |
|
35
|
|
|
foreach ($records as $values) { |
|
36
|
|
|
$request = new Request($values); |
|
37
|
|
|
$command = new CreateReview($request); |
|
38
|
|
|
if ($review = $this->importedReview($command->request)) { |
|
39
|
|
|
$result['attachments'] += glsr()->filterInt('import/review/attachments', 0, $request, $review, false); |
|
40
|
|
|
$result['skipped']++; |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
if ($review = glsr(ReviewManager::class)->create($command)) { |
|
44
|
|
|
$result['attachments'] += glsr()->filterInt('import/review/attachments', 0, $request, $review, true); |
|
45
|
|
|
$result['imported']++; |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
$result['skipped']++; |
|
49
|
|
|
} |
|
50
|
|
|
wp_defer_term_counting(false); |
|
51
|
|
|
wp_suspend_cache_invalidation(false); |
|
52
|
|
|
unset($records); |
|
53
|
|
|
return glsr(ImportResultDefaults::class)->restrict($result); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function importAttachments(int $limit = 1, int $offset = 0): array |
|
57
|
|
|
{ |
|
58
|
|
|
if (!defined('WP_IMPORTING')) { |
|
59
|
|
|
define('WP_IMPORTING', true); |
|
60
|
|
|
} |
|
61
|
|
|
set_time_limit(0); |
|
62
|
|
|
wp_raise_memory_limit('image'); |
|
63
|
|
|
wp_suspend_cache_invalidation(true); |
|
64
|
|
|
$limit = max(1, $limit); |
|
65
|
|
|
$offset = max(0, $offset); |
|
66
|
|
|
$result = glsr()->filterArray('import/reviews/attachments', [], $limit, $offset); |
|
67
|
|
|
wp_suspend_cache_invalidation(false); |
|
68
|
|
|
return glsr(ImportResultDefaults::class)->restrict($result); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function importedReview(Request $request): ?Review |
|
72
|
|
|
{ |
|
73
|
|
|
$submitted = glsr(ReviewManager::class)->submittedMeta($request); |
|
74
|
|
|
$submittedHash = md5(maybe_serialize($submitted)); |
|
75
|
|
|
$sql = " |
|
76
|
|
|
SELECT p.ID |
|
77
|
|
|
FROM table|posts AS p |
|
78
|
|
|
INNER JOIN table|postmeta AS pm ON (pm.post_id = p.ID) |
|
79
|
|
|
WHERE 1=1 |
|
80
|
|
|
AND p.post_type = %s |
|
81
|
|
|
AND pm.meta_key = '_submitted_hash' |
|
82
|
|
|
AND pm.meta_value = %s |
|
83
|
|
|
"; |
|
84
|
|
|
$sql = glsr(Query::class)->sql($sql, glsr()->post_type, $submittedHash); |
|
85
|
|
|
$reviewId = glsr(Database::class)->dbGetVar($sql); |
|
86
|
|
|
$reviewId = Cast::toInt($reviewId); |
|
87
|
|
|
$review = glsr_get_review($reviewId); |
|
88
|
|
|
if (!$review->isValid()) { |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
return $review; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function prepare(): void |
|
95
|
|
|
{ |
|
96
|
|
|
glsr(TableTmp::class)->create(); // create a temporary table for importing |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function records(int $limit = 1, int $offset = 0): TabularDataReader |
|
100
|
|
|
{ |
|
101
|
|
|
$this->prepare(); |
|
102
|
|
|
$reader = Reader::createFromPath($this->tempFilePath()); |
|
|
|
|
|
|
103
|
|
|
$reader->setHeaderOffset(0); |
|
104
|
|
|
return Statement::create() |
|
|
|
|
|
|
105
|
|
|
->offset(max(0, $offset)) |
|
106
|
|
|
->limit(max(1, $limit)) |
|
107
|
|
|
->process($reader); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function tempFilePath(): string |
|
111
|
|
|
{ |
|
112
|
|
|
$uploads = wp_upload_dir(); |
|
113
|
|
|
if (!file_exists($uploads['basedir'])) { |
|
114
|
|
|
$uploads = wp_upload_dir(null, true, true); // maybe the site has been moved, so refresh the cached uploads path |
|
115
|
|
|
} |
|
116
|
|
|
$path = trailingslashit($uploads['basedir']); |
|
117
|
|
|
$path = trailingslashit($path.glsr()->id); |
|
118
|
|
|
$path = trailingslashit($path.'temp'); |
|
119
|
|
|
wp_mkdir_p($path); |
|
120
|
|
|
return $path.'import.csv'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function unlinkTempFile(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
$path = $this->tempFilePath(); |
|
126
|
|
|
if (!is_file($path)) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
unlink($path); // delete the temporary import file |
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.