-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoload.php
258 lines (208 loc) · 9.28 KB
/
autoload.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
require_once '../../../wp-load.php';
$json = file_get_contents('php://input');
//$json = file_get_contents('./log/superdesk.txt');
file_put_contents('./log/superdesk.txt', $json . "\n\n", FILE_APPEND);
$obj = json_decode($json, true);
if ($obj['type'] == 'text') {
$settings = get_option('superdesk_settings');
if ($obj['pubstatus'] == 'usable') {
if (isset($obj['evolvedfrom'])) {
$guid = wp_strip_all_tags($obj['evolvedfrom']);
} else {
$guid = wp_strip_all_tags($obj['guid']);
}
$sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'");
$append = false;
$prepend = false;
$updateText = '';
if($sync && $settings['update-log-option'] == 'on'){
$updateText = $settings['update-log-text'] . ' '. date( $settings['update-log-date-format']). '.';
if($settings['update-log-position'] == 'on'){
$prepend = true;
}else{
$append = true;
}
}
$content = $obj['description_html'];
$content .= '<p>';
if($prepend){
$content .= $updateText .'<br>';
}
$content .= mb_substr($obj['body_html'], mb_strpos($obj['body_html'], '>') + 1, mb_strlen($obj['body_html']));
if (!empty($obj['ednote'])) {
$content .= "<p>Editors Note: " . wp_strip_all_tags($obj['ednote']) . "</p>";
}
if($append){
$content .= "<p>".$updateText."</p>";
}
if ($settings['import-keywords'] && $settings['import-keywords'] == 'on') {
if (isset($obj['keywords']) && count($obj['keywords']) > 0) {
foreach ($obj['keywords'] as $keyword) {
$taxonomyTag[] = wp_strip_all_tags($keyword);
}
}
}
if ($settings['convert-slugline'] && $settings['convert-slugline'] == 'on') {
if (isset($obj['slugline']) && !empty($obj['slugline'])) {
$ignoreKeywords = explode(',', $settings['slugline-ignored']);
$tmpKeywords = explode($settings['slugline-separator'], $obj['slugline']);
foreach ($tmpKeywords as $word) {
if (!in_array($word, $ignoreKeywords)) {
$taxonomyTag[] = $word;
}
}
}
}
foreach ($obj['subject'] as $subject) {
if ($settings['subject-type'] == 'tags') {
$taxonomyTag[] = wp_strip_all_tags($subject['name']);
} elseif ($settings['subject-type'] == 'categories') {
$categoryExist = $wpdb->get_row("SELECT terms.term_id, term_taxonomy.term_taxonomy_id FROM " . $wpdb->prefix . "terms terms JOIN " . $wpdb->prefix . "term_taxonomy term_taxonomy ON term_taxonomy.term_id = terms.term_id WHERE term_taxonomy.taxonomy = 'category' AND terms.name = '" . wp_strip_all_tags($subject['name']) . "'");
if ($categoryExist) {
$taxonomyCategory[] = $categoryExist->term_taxonomy_id;
} else {
$category_id = wp_insert_term(wp_strip_all_tags($subject['name']), 'category');
$taxonomyCategory[] = $category_id['term_taxonomy_id'];
}
}
}
if ($settings['convert-services'] == 'on') {
foreach ($obj['service'] as $service) {
$categoryExist = $wpdb->get_row("SELECT terms.term_id, term_taxonomy.term_taxonomy_id FROM " . $wpdb->prefix . "terms terms JOIN " . $wpdb->prefix . "term_taxonomy term_taxonomy ON term_taxonomy.term_id = terms.term_id WHERE term_taxonomy.taxonomy = 'category' AND terms.name = '" . wp_strip_all_tags($service['name']) . "'");
if ($categoryExist) {
$taxonomyCategory[] = $categoryExist->term_taxonomy_id;
} else {
$category_id = wp_insert_term(wp_strip_all_tags($service['name']), 'category');
$taxonomyCategory[] = $category_id['term_taxonomy_id'];
}
}
}
if ($taxonomyCategory && !empty($taxonomyCategory)) {
$category = $taxonomyCategory;
} else {
$category = $settings['category'];
}
if ($settings['author-byline'] && $settings['author-byline'] == 'on') {
$author_name = $obj['byline'];
if (!empty($settings['byline-words'])) {
$replaceWords = explode(',', $settings['byline-words']);
foreach ($replaceWords as $value) {
$author_name = str_replace(trim($value) . " ", "", $author_name);
}
}
$authorExist = $wpdb->get_row("SELECT ID user_id FROM " . $wpdb->prefix . DB_TABLE_USERS . " WHERE display_name = '" . wp_strip_all_tags($author_name) . "'");
if (!$authorExist) {
$table_name = $wpdb->prefix . DB_TABLE_USERS;
$userArray = array(
'user_login' => strtolower(str_replace(" ", "-", $author_name)),
'user_pass' => generatePassword(),
'display_name' => wp_strip_all_tags($author_name)
);
$author_id = wp_insert_user($userArray);
} else {
$author_id = $authorExist->user_id;
}
} elseif ($settings['author-byline'] == 'on') {
$author_id = $settings['author'];
} else {
$author_id = 0;
}
$image = null;
if (isset($settings['download-images']) && $settings['download-images'] === 'on') {
$content = embed_images($content, $image);
}
if ($sync) {
$post_ID = $sync->post_id;
$edit_post = array(
'ID' => $sync->post_id,
'post_title' => wp_strip_all_tags($obj['headline']),
'post_name' => wp_strip_all_tags($obj['headline']),
'post_content' => $content,
'post_author' => (int) $author_id,
'post_content_filtered' => $content,
'post_category' => $category
);
if (isset($settings['post-formats'], $settings['post-formats-table']) and ! empty($obj['profile']) and $settings['post-formats'] == 'on') {
if (isset($settings['post-formats-table'][$obj['profile']])) {
set_post_format($post_ID, $settings['post-formats-table'][$obj['profile']]);
}
}
wp_update_post($edit_post);
$attachmentExist = get_post_thumbnail_id($post_ID);
if ($attachmentExist) {
wp_delete_attachment($attachmentExist);
}
if ($taxonomyTag && !empty($taxonomyTag)) {
wp_set_post_tags($post_ID, $taxonomyTag);
}
$wpdb->insert(
$wpdb->prefix . DB_TABLE_SYNC_POST, array(
'post_id' => $post_ID,
'guid' => wp_strip_all_tags($obj['guid']),
'time' => current_time('mysql')
)
);
} else {
$postarr = array(
'post_title' => wp_strip_all_tags($obj['headline']),
'post_name' => wp_strip_all_tags($obj['headline']),
'post_content' => $content,
'post_content_filtered' => $content,
'post_author' => (int) $author_id,
'post_status' => $settings['status'],
'post_category' => $category,
);
$post_ID = wp_insert_post($postarr, true);
if (isset($settings['post-formats'], $settings['post-formats-table']) and ! empty($obj['profile']) and $settings['post-formats'] == 'on') {
if (isset($settings['post-formats-table'][$obj['profile']])) {
set_post_format($post_ID, $settings['post-formats-table'][$obj['profile']]);
}
}
if ($taxonomyTag && !empty($taxonomyTag)) {
wp_set_post_tags($post_ID, $taxonomyTag);
}
$table_name = $wpdb->prefix . DB_TABLE_SYNC_POST;
$wpdb->insert(
$table_name, array(
'post_id' => $post_ID,
'guid' => wp_strip_all_tags($obj['guid']),
'time' => current_time('mysql')
)
);
}
/* save featured media */
if ($obj['associations']['featuremedia'] && $obj['associations']['featuremedia']['type'] == 'picture') {
$filenameQ = explode("/", $obj['associations']['featuremedia']['renditions']['original']['media']);
$filename = $filenameQ[count($filenameQ) - 1];
$fileExist = $wpdb->get_row("SELECT meta_id, post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE '%" . wp_strip_all_tags($filename) . "'");
if ($fileExist) {
set_post_thumbnail($post_ID, $fileExist->post_id);
} else {
$caption = generate_caption_image($obj['associations']['featuremedia']);
$alt = (!empty($obj['associations']['featuremedia']['body_text'])) ? wp_strip_all_tags($obj['associations']['featuremedia']['body_text']) : '';
saveAttachment($obj['associations']['featuremedia'], $post_ID, $caption, $alt);
}
} else if ($image !== null) {
$filenameQ = explode("/", $image->src);
$filename = $filenameQ[count($filenameQ) - 1];
$fileExist = $wpdb->get_row("SELECT meta_id, post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE '%" . wp_strip_all_tags($filename) . "'");
if ($fileExist) {
set_post_thumbnail($post_ID, $fileExist->post_id);
} else {
savePicture($image->src, $post_ID, $image->oldSrc, isset($obj['associations']) ? $obj['associations'] : array(), $image->alt);
}
}
} elseif ($obj['pubstatus'] == 'canceled') {
/* remove article */
$guid = wp_strip_all_tags($obj['guid']);
$sync = $wpdb->get_row("SELECT post_id FROM " . $wpdb->prefix . DB_TABLE_SYNC_POST . " WHERE guid = '" . $guid . "'");
if ($sync) {
$edit_post = array(
'ID' => $sync->post_id,
'post_status' => 'draft'
);
wp_update_post($edit_post);
}
}
}