PluginProbe
ManageWP Worker / 3.9.16
ManageWP Worker v3.9.16
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / post.class.php

post.class.php in ManageWP Worker 3.9.16, at post.class.php

378 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*************************************************************
3 *
4 * post.class.php
5 *
6 * Create remote post
7 *
8 *
9 * Copyright (c) 2011 Prelovac Media
10 * www.prelovac.com
11 **************************************************************/
12
13 class MMB_Post extends MMB_Core
14 {
15 function __construct()
16 {
17 parent::__construct();
18 }
19
20 function create($args)
21 {
22
23 /**
24 * algorithm
25 * 1. create post using wp_insert_post (insert tags also here itself)
26 * 2. use wp_create_categories() to create(not exists) and insert in the post
27 * 3. insert meta values
28 */
29
30 include_once ABSPATH . 'wp-admin/includes/taxonomy.php';
31 include_once ABSPATH . 'wp-admin/includes/image.php';
32 include_once ABSPATH . 'wp-admin/includes/file.php';
33
34 $post_struct = $args['post_data'];
35
36 $post_data = $post_struct['post_data'];
37 $new_custom = $post_struct['post_extras']['post_meta'];
38 $post_categories = explode(',', $post_struct['post_extras']['post_categories']);
39 $post_atta_img = $post_struct['post_extras']['post_atta_images'];
40 $post_upload_dir = $post_struct['post_extras']['post_upload_dir'];
41 $post_checksum = $post_struct['post_extras']['post_checksum'];
42 $post_featured_img = $post_struct['post_extras']['featured_img'];
43
44 $upload = wp_upload_dir();
45
46 // create dynamic url RegExp
47 $mwp_base_url = parse_url($post_upload_dir['url']);
48 $mwp_regexp_url = $mwp_base_url['host'] . $mwp_base_url['path'];
49 $rep = array(
50 '/',
51 '+',
52 '.',
53 ':',
54 '?'
55 );
56 $with = array(
57 '\/',
58 '\+',
59 '\.',
60 '\:',
61 '\?'
62 );
63 $mwp_regexp_url = str_replace($rep, $with, $mwp_regexp_url);
64
65 // rename all src ../wp-content/ with hostname/wp-content/
66 $mmb_dot_url = '..' . $mmb_base_url['path'];
67 $mmb_dot_url = str_replace($rep, $with, $mmb_dot_url);
68 $dot_match_count = preg_match_all('/(<a[^>]+href=\"([^"]+)\"[^>]*>)?(<\s*img.[^\/>]*src="([^"]*' . $mmb_dot_url . '[^\s]+\.(jpg|jpeg|png|gif|bmp))"[^>]*>)/ixu', $post_data['post_content'], $dot_get_urls, PREG_SET_ORDER);
69 if ($dot_match_count > 0) {
70 foreach ($dot_get_urls as $dot_url) {
71 $match_dot = '/' . str_replace($rep, $with, $dot_url[4]) . '/';
72 $replace_dot = 'http://' . $mmb_base_url['host'] . substr($dot_url[4], 2, strlen($dot_url[4]));
73 $post_data['post_content'] = preg_replace($match_dot, $replace_dot, $post_data['post_content']);
74
75 if ($dot_url[1] != '') {
76 $match_dot_a = '/' . str_replace($rep, $with, $dot_url[2]) . '/';
77 $replace_dot_a = 'http://' . $mmb_base_url['host'] . substr($dot_url[2], 2, strlen($dot_url[2]));
78 $post_data['post_content'] = preg_replace($match_dot_a, $replace_dot_a, $post_data['post_content']);
79 }
80 }
81 }
82
83
84
85 //to find all the images
86 $match_count = preg_match_all('/(<a[^>]+href=\"([^"]+)\"[^>]*>)?(<\s*img.[^\/>]*src="([^"]+' . $mmb_regexp_url . '[^\s]+\.(jpg|jpeg|png|gif|bmp))"[^>]*>)/ixu', $post_data['post_content'], $get_urls, PREG_SET_ORDER);
87 if ($match_count > 0) {
88 $attachments = array();
89 $post_content = $post_data['post_content'];
90
91 foreach ($get_urls as $get_url_k => $get_url) {
92 // unset url in attachment array
93 foreach ($post_atta_img as $atta_url_k => $atta_url_v) {
94 $match_patt_url = '/' . str_replace($rep, $with, substr($atta_url_v['src'], 0, strrpos($atta_url_v['src'], '.'))) . '/';
95 if (preg_match($match_patt_url, $get_url[4])) {
96 unset($post_atta_img[$atta_url_k]);
97 }
98 }
99
100 if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url
101 if ($get_url[1] != '') {
102 // change src url
103 $s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/';
104
105 $s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]);
106 $s_mmb_rp = $s_img_atta[0];
107 $post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content);
108 // change attachment url
109 if (preg_match('/attachment_id/i', $get_url[2])) {
110 $mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
111 $mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6];
112 $post_content = preg_replace($mmb_mp, $mmb_rp, $post_content);
113 }
114 }
115 continue;
116 }
117
118 $no_thumb = '';
119 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
120 $no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', '.' . $get_url[5], $get_url[4]);
121 } else {
122 $no_thumb = $get_url[4];
123 }
124
125 if(isset($upload['error']) && !empty($upload['error'])){
126 return array('error' => $upload['error']);
127 }
128 $file_name = basename($no_thumb);
129 $tmp_file = download_url($no_thumb);
130
131 if(is_wp_error($tmp_file)){
132 return array('error' => $tmp_file->get_error_message());
133 }
134
135 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
136 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
137 $renamed = @rename($tmp_file, $attach_upload['path']);
138 if ($renamed === true) {
139 $match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/';
140 $replace_pattern = $attach_upload['url'];
141 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
142 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
143 $match_pattern = '/' . str_replace($rep, $with, preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', '.' . $get_url[5], $get_url[4])) . '/';
144 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
145 }
146
147 $attachment = array(
148 'post_title' => $file_name,
149 'post_content' => '',
150 'post_type' => 'attachment',
151 //'post_parent' => $post_id,
152 'post_mime_type' => 'image/' . $get_url[5],
153 'guid' => $attach_upload['url']
154 );
155
156 // Save the data
157
158 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
159
160 $attachments[$attach_id] = 0;
161
162 // featured image
163 if ($post_featured_img != '') {
164 $feat_img_url = '';
165 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
166 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
167 } else {
168 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
169 }
170 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
171 if (preg_match($m_feat_url, $get_url[4])) {
172 $post_featured_img = '';
173 $attachments[$attach_id] = $attach_id;
174 }
175 }
176
177 // set $get_urls value[6] - parent atta_id
178 foreach ($get_urls as $url_k => $url_v) {
179 if ($get_url_k != $url_k) {
180 $s_get_url = '';
181 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $url_v[4])) {
182 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8);
183 } else {
184 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.'));
185 }
186 $m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/';
187 if (preg_match($m_patt_url, $get_url[4])) {
188 array_push($get_urls[$url_k], $attach_id);
189 }
190 }
191 }
192
193
194 $some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']);
195 wp_update_attachment_metadata($attach_id, $some_data);
196
197
198 // changing href of a tag
199 if ($get_url[1] != '') {
200 $mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
201 if (preg_match('/attachment_id/i', $get_url[2])) {
202 $mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id;
203 $post_content = preg_replace($mmb_mp, $mmb_rp, $post_content);
204 }
205 }
206 } else {
207 @unlink($tmp_file);
208 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
209
210 }
211 @unlink($tmp_file);
212 }
213
214
215 $post_data['post_content'] = $post_content;
216
217 }
218 if (count($post_atta_img)) {
219 foreach ($post_atta_img as $img) {
220 $file_name = basename($img['src']);
221
222 if(isset($upload['error']) && !empty($upload['error'])){
223 return array('error' => $upload['error']);
224 }
225
226 $tmp_file = download_url($img['src']);
227 if(is_wp_error($tmp_file)){
228 return array('error' => $tmp_file->get_error_message());
229 }
230
231 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
232 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
233 $renamed = @rename($tmp_file, $attach_upload['path']);
234 if ($renamed === true) {
235 $atta_ext = end(explode('.', $file_name));
236
237 $attachment = array(
238 'post_title' => $file_name,
239 'post_content' => '',
240 'post_type' => 'attachment',
241 //'post_parent' => $post_id,
242 'post_mime_type' => 'image/' . $atta_ext,
243 'guid' => $attach_upload['url']
244 );
245
246 // Save the data
247 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
248 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
249 $attachments[$attach_id] = 0;
250
251 // featured image
252 if ($post_featured_img != '') {
253 $feat_img_url = '';
254 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
255 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
256 } else {
257 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
258 }
259 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
260 if (preg_match($m_feat_url, $img['src'])) {
261 $post_featured_img = '';
262 $attachments[$attach_id] = $attach_id;
263 }
264 }
265
266 } else {
267 @unlink($tmp_file);
268 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
269 }
270 @unlink($tmp_file);
271 }
272 }
273
274 //Prepare post data and temporarily remove content filters before insert post
275 $user = $this->mmb_get_user_info( $args['username'] );
276 if($user && $user->ID){
277 $post_data['post_author'] = $user->ID;
278 }
279 remove_filter('content_save_pre', 'wp_filter_post_kses');
280 $post_id = wp_insert_post($post_data);
281
282 if (count($attachments)) {
283 foreach ($attachments as $atta_id => $featured_id) {
284 $result = wp_update_post(array(
285 'ID' => $atta_id,
286 'post_parent' => $post_id
287 ));
288 if ($featured_id > 0) {
289 $new_custom['_thumbnail_id'] = array(
290 $featured_id
291 );
292 }
293 }
294 }
295
296 // featured image
297 if ($post_featured_img != '') {
298 $file_name = basename($post_featured_img);
299 if(isset($upload['error']) && !empty($upload['error'])){
300 return array('error' => $upload['error']);
301 }
302 $tmp_file = download_url($post_featured_img);
303 if(is_wp_error($tmp_file)){
304 return array('error' => $tmp_file->get_error_message());
305 }
306 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
307 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
308 $renamed = @rename($tmp_file, $attach_upload['path']);
309 if ($renamed === true) {
310 $atta_ext = end(explode('.', $file_name));
311
312 $attachment = array(
313 'post_title' => $file_name,
314 'post_content' => '',
315 'post_type' => 'attachment',
316 'post_parent' => $post_id,
317 'post_mime_type' => 'image/' . $atta_ext,
318 'guid' => $attach_upload['url']
319 );
320
321 // Save the data
322 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
323 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
324 $new_custom['_thumbnail_id'] = array(
325 $attach_id
326 );
327 } else {
328 @unlink($tmp_file);
329 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
330 }
331 @unlink($tmp_file);
332 }
333
334 if ($post_id && is_array($post_categories)) {
335 //insert categories
336
337 $cat_ids = wp_create_categories($post_categories, $post_id);
338 }
339
340
341 //get current custom fields
342 $cur_custom = get_post_custom($post_id);
343 //check which values doesnot exists in new custom fields
344 $diff_values = array_diff_key($cur_custom, $new_custom);
345
346 if (is_array($diff_values))
347 foreach ($diff_values as $meta_key => $value) {
348 delete_post_meta($post_id, $meta_key);
349 }
350 //insert new post meta
351 foreach ($new_custom as $meta_key => $value) {
352 if (strpos($meta_key, '_mmb') === 0 || strpos($meta_key, '_edit') === 0) {
353 continue;
354 } else {
355 update_post_meta($post_id, $meta_key, $value[0]);
356 }
357 }
358 return $post_id;
359 }
360
361
362 function change_status($args)
363 {
364
365 global $wpdb;
366 $post_id = $args['post_id'];
367 $status = $args['status'];
368 $success = false;
369
370 if(in_array($status, array('draft', 'publish', 'trash'))){
371 $sql = "update ".$wpdb->prefix."posts set post_status = '$status' where ID = '$post_id'";
372 $success = $wpdb->query($sql);
373 }
374
375 return $success;
376 }
377 }
378 ?>