-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.php
220 lines (152 loc) · 6.2 KB
/
import.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/*
Template Name: Import
*/
include( ABSPATH . 'wp-admin/includes/image.php' );
echo '<!DOCTYPE html>';
$conn = new mysqli('localhost','emergentartspace','4BUcJ42csYKdy3Uz','emergentartspace');
if (!isset($_POST['ID'])) {
$artists = $conn->query("select * from wp_posts where post_type = 'art' and post_status = 'publish' order by ID asc");
?><form action="" method="post"><select name="ID"><?php
while ($artist = $artists->fetch_assoc()) {
?>
<option value="<?php echo $artist['ID']; ?>"><?php echo $artist['post_title']; ?></option>
<?php
}
?></select>
<input type="submit" value="submit">
</form>
<?php
} else {
$result = $conn->query('select * from wp_posts where ID = '.$_POST['ID']);
while ($row = $result->fetch_assoc()) {
$artist['display_name'] = utf8_encode($row['post_title']);
$artist['user_registered'] = $row['post_date'];
$submission = $conn->query("select s2.field_value as email from wp_cf7dbplugin_submits s1 join wp_cf7dbplugin_submits s2 on s1.submit_time = s2.submit_time where s1.field_name like 'name-333' and lower(s1.field_value) like '%".strtolower($artist['display_name'])."%' and s2.field_name like 'email-333'");
while ($sub = $submission->fetch_assoc()) {
$artist['email'] = $sub['email'];
$olduserquery = $conn->query("select * from wp_users where lower(user_email) like '%".strtolower($artist['email'])."%'");
while ($olduser = $olduserquery->fetch_assoc()) {
$artist['user_login'] = $olduser['user_login'];
$artist['user_url'] = $olduser['user_url'];
$artist['user_registered'] = $olduser['user_registered'];
}
}
$images = array();
$post_content = $row['post_content'];
$matches = array();
$loc = 0;
$pattern = '/\[artistdata](.+?)\[\/artistdata]/s';
preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE);
$artist['data'] = preg_replace('/\[\/artistdataitem]\[artistdataitem]/', ';', $matches[1][0]);
$artist['data'] = preg_replace('/\[\/?artistdataitem]/', '', $artist['data']);
$artist['data'] = preg_replace('/\[\/?artistdatalabel]/', ' ', $artist['data']);
$pattern = '/\/artistdata](.+?)\[/s';
preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE);
$artist['description'] = utf8_encode($matches[1][0]);
$pattern = '/alignleft.+src="(.+?)"/';
if (preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE) > 0) {
$artist['biophoto'] = str_replace('-150x150', '', $matches[1][0]);
}
$orig_pattern = '/wp-image-([0-9]+?)[^0-9]/';
$pattern = $orig_pattern;
while (preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE) > 0) {
$img = array();
$i = 0;
do {
if ($i) preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE);
$img['old_ID'] = $matches[1][0];
$loc += $matches[1][1] + strlen($matches[1][0]);
$imgquery = $conn->query('select guid from wp_posts where ID = '.$img['old_ID']);
while ($this_img = $imgquery->fetch_assoc()) {
$img['src'] = $this_img['guid'];
}
$i++;
} while ($img['src'] == $artist['biophoto']);
$pattern = '/h3.+>(.+?)[^\[]\[/';
preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE);
$img['title'] = utf8_encode(trim($matches[1][0]));
$loc += $matches[1][1] + strlen($matches[1][0]);
$pattern = '/medium\](.+)?\[/';
preg_match($pattern, substr($post_content, $loc), $matches, PREG_OFFSET_CAPTURE);
$img['medium'] = utf8_encode(trim($matches[1][0]));
$loc += $matches[1][1] + strlen($matches[1][0]);
array_push($images, $img);
$pattern = $orig_pattern;
}
$artist['images'] = $images;
$importantfields = array('email', 'user_login');
if (!isset($_POST['adduser'])) {
_d($artist);
?>
<form action="" method="post">
<input type="hidden" name="ID" value="<?php echo $_POST['ID']; ?>">
<?php
foreach ($importantfields as $field) {
if (!isset($artist[$field])) {
?>
<input type="text" name="<?php echo $field; ?>" placeholder="<?php echo $field; ?>">
<?php
}
}
?>
<input type="hidden" name="adduser" value="true">
<input type="submit" value="Import User">
</form>
<?php
}
}
}
if (isset($_POST['adduser'])) {
if (!isset($artist['user_login'])) {
$email_splode = explode('@', $artist['email']);
$artist['user_login'] = $email_splode[0];
}
foreach ($importantfields as $field) {
if (isset($_POST[$field])) {
$artist[$field] = $_POST[$field];
}
}
$row = $wpdb->insert('wp_users', array(
'user_login' => $artist['user_login'],
'user_nicename' => $artist['display_name'],
'user_email' => $artist['email'],
'user_registered' => $artist['user_registered'],
'display_name' => $artist['display_name']
));
$userid = $wpdb->insert_id;
$wp_user_object = new WP_User($userid);
$wp_user_object->set_role('artist');
if (isset($artist['biophoto'])) {
_d(str_replace('emergentartspace.org','staging.emergentartspace.org',$artist['biophoto']));
$avatarid = upload_file_from_url();
update_user_meta($userid, 'avatar', $avatarid);
}
update_user_meta($userid, 'description', $artist['description']);
update_user_meta($userid, 'nickname', $artist['display_name']);
foreach ($artist['images'] as $this_img) {
$new_post = array(
'post_title' => $this_img['title'],
'post_date' => $artist['user_registered'],
'post_status' => 'publish',
'post_author' => $userid,
'post_type' => 'artwork',
'post_category' => array(0)
);
$artwork_id = wp_insert_post($new_post, false);
update_post_meta($artwork_id, 'medium', $this_img['medium']);
$img_id = upload_file_from_url(str_replace('emergentartspace.org','staging.emergentartspace.org',$this_img['src']), $artwork_id);
}
echo '<a href="'.eas_artist_page_url($userid).'" target="_blank">Clicky</a><p>';
$artists = $conn->query("select * from wp_posts where post_type = 'art' and post_status = 'publish' order by ID asc");
?><form action="" method="post"><select name="ID"><?php
while ($artist = $artists->fetch_assoc()) {
?>
<option value="<?php echo $artist['ID']; ?>"><?php echo $artist['post_title']; ?></option>
<?php
}
?></select>
<input type="submit" value="submit">
</form>
<?php
}