PluginProbe
ManageWP Worker / 3.8.1
ManageWP Worker v3.8.1
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.8.1, at post.class.php

350 lines 15.5 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 * algorithm
24 * 1. create post using wp_insert_post (insert tags also here itself)
25 * 2. use wp_create_categories() to create(not exists) and insert in the post
26 * 3. insert meta values
27 */
28
29 include_once ABSPATH . 'wp-admin/includes/taxonomy.php';
30 include_once ABSPATH . 'wp-admin/includes/image.php';
31
32 $post_struct = $args['post_data'];
33
34 $post_data = $post_struct['post_data'];
35 $new_custom = $post_struct['post_extras']['post_meta'];
36 $post_categories = explode(',', $post_struct['post_extras']['post_categories']);
37 $post_atta_img = $post_struct['post_extras']['post_atta_images'];
38 $post_upload_dir = $post_struct['post_extras']['post_upload_dir'];
39 $post_checksum = $post_struct['post_extras']['post_checksum'];
40 $post_featured_img = $post_struct['post_extras']['featured_img'];
41
42 $upload = wp_upload_dir();
43
44 // create dynamic url RegExp
45 $mwp_base_url = parse_url($post_upload_dir['url']);
46 $mwp_regexp_url = $mwp_base_url['host'] . $mwp_base_url['path'];
47 $rep = array(
48 '/',
49 '+',
50 '.',
51 ':',
52 '?'
53 );
54 $with = array(
55 '\/',
56 '\+',
57 '\.',
58 '\:',
59 '\?'
60 );
61 $mwp_regexp_url = str_replace($rep, $with, $mwp_regexp_url);
62
63 // rename all src ../wp-content/ with hostname/wp-content/
64 $mmb_dot_url = '..' . $mmb_base_url['path'];
65 $mmb_dot_url = str_replace($rep, $with, $mmb_dot_url);
66 $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);
67 if ($dot_match_count > 0) {
68 foreach ($dot_get_urls as $dot_url) {
69 $match_dot = '/' . str_replace($rep, $with, $dot_url[4]) . '/';
70 $replace_dot = 'http://' . $mmb_base_url['host'] . substr($dot_url[4], 2, strlen($dot_url[4]));
71 $post_data['post_content'] = preg_replace($match_dot, $replace_dot, $post_data['post_content']);
72
73 if ($dot_url[1] != '') {
74 $match_dot_a = '/' . str_replace($rep, $with, $dot_url[2]) . '/';
75 $replace_dot_a = 'http://' . $mmb_base_url['host'] . substr($dot_url[2], 2, strlen($dot_url[2]));
76 $post_data['post_content'] = preg_replace($match_dot_a, $replace_dot_a, $post_data['post_content']);
77 }
78 }
79 }
80
81
82
83 //to find all the images
84 $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);
85 if ($match_count > 0) {
86 $attachments = array();
87 $post_content = $post_data['post_content'];
88
89 foreach ($get_urls as $get_url_k => $get_url) {
90 // unset url in attachment array
91 foreach ($post_atta_img as $atta_url_k => $atta_url_v) {
92 $match_patt_url = '/' . str_replace($rep, $with, substr($atta_url_v['src'], 0, strrpos($atta_url_v['src'], '.'))) . '/';
93 if (preg_match($match_patt_url, $get_url[4])) {
94 unset($post_atta_img[$atta_url_k]);
95 }
96 }
97
98 if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url
99 if ($get_url[1] != '') {
100 // change src url
101 $s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/';
102
103 $s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]);
104 $s_mmb_rp = $s_img_atta[0];
105 $post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content);
106 // change attachment url
107 if (preg_match('/attachment_id/i', $get_url[2])) {
108 $mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
109 $mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6];
110 $post_content = preg_replace($mmb_mp, $mmb_rp, $post_content);
111 }
112 }
113 continue;
114 }
115
116 $no_thumb = '';
117 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
118 $no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', '.' . $get_url[5], $get_url[4]);
119 } else {
120 $no_thumb = $get_url[4];
121 }
122 $file_name = basename($no_thumb);
123
124 //$tmp_file = $upload['path'].'/tempfile.tmp';
125 $tmp_file = $this->mmb_download_url($no_thumb, $upload['path'] . '/tempfile'.md5(time()).'.tmp');
126 //$tmp_file = download_url($no_thumb);
127
128 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
129 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
130 $renamed = @rename($tmp_file, $attach_upload['path']);
131 if ($renamed === true) {
132 $match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/';
133 $replace_pattern = $attach_upload['url'];
134 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
135 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
136 $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])) . '/';
137 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
138 }
139
140 $attachment = array(
141 'post_title' => $file_name,
142 'post_content' => '',
143 'post_type' => 'attachment',
144 //'post_parent' => $post_id,
145 'post_mime_type' => 'image/' . $get_url[5],
146 'guid' => $attach_upload['url']
147 );
148
149 // Save the data
150
151 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
152
153 $attachments[$attach_id] = 0;
154
155 // featured image
156 if ($post_featured_img != '') {
157 $feat_img_url = '';
158 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
159 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
160 } else {
161 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
162 }
163 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
164 if (preg_match($m_feat_url, $get_url[4])) {
165 $post_featured_img = '';
166 $attachments[$attach_id] = $attach_id;
167 }
168 }
169
170 // set $get_urls value[6] - parent atta_id
171 foreach ($get_urls as $url_k => $url_v) {
172 if ($get_url_k != $url_k) {
173 $s_get_url = '';
174 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $url_v[4])) {
175 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8);
176 } else {
177 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.'));
178 }
179 $m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/';
180 if (preg_match($m_patt_url, $get_url[4])) {
181 array_push($get_urls[$url_k], $attach_id);
182 }
183 }
184 }
185
186
187 $some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']);
188 wp_update_attachment_metadata($attach_id, $some_data);
189
190
191 // changing href of a tag
192 if ($get_url[1] != '') {
193 $mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
194 if (preg_match('/attachment_id/i', $get_url[2])) {
195 $mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id;
196 $post_content = preg_replace($mmb_mp, $mmb_rp, $post_content);
197 }
198 }
199 }
200 @unlink($tmp_file);
201 }
202
203
204 $post_data['post_content'] = $post_content;
205
206 }
207 if (count($post_atta_img)) {
208 foreach ($post_atta_img as $img) {
209 $file_name = basename($img['src']);
210
211
212 $tmp_file = $this->mmb_download_url($img['src'], $upload['path'] . '/tempfile.tmp');
213
214 //$tmp_file = download_url($img['src']);
215
216 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
217 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
218 $renamed = @rename($tmp_file, $attach_upload['path']);
219 if ($renamed === true) {
220 $atta_ext = end(explode('.', $file_name));
221
222 $attachment = array(
223 'post_title' => $file_name,
224 'post_content' => '',
225 'post_type' => 'attachment',
226 //'post_parent' => $post_id,
227 'post_mime_type' => 'image/' . $atta_ext,
228 'guid' => $attach_upload['url']
229 );
230
231 // Save the data
232 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
233 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
234 $attachments[$attach_id] = 0;
235
236 // featured image
237 if ($post_featured_img != '') {
238 $feat_img_url = '';
239 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
240 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
241 } else {
242 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
243 }
244 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
245 if (preg_match($m_feat_url, $img['src'])) {
246 $post_featured_img = '';
247 $attachments[$attach_id] = $attach_id;
248 }
249 }
250
251 }
252 @unlink($tmp_file);
253 }
254 }
255
256
257 // create post
258 $post_id = wp_insert_post($post_data);
259
260 if (count($attachments)) {
261 foreach ($attachments as $atta_id => $featured_id) {
262 $result = wp_update_post(array(
263 'ID' => $atta_id,
264 'post_parent' => $post_id
265 ));
266 if ($featured_id > 0) {
267 $new_custom['_thumbnail_id'] = array(
268 $featured_id
269 );
270 }
271 }
272 }
273
274 // featured image
275 if ($post_featured_img != '') {
276 $file_name = basename($post_featured_img);
277 //$tmp_file = download_url($post_featured_img);
278 $tmp_file = $this->mmb_download_url($no_thumb, $upload['path'] . '/tempfile_feat.tmp');
279 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
280 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
281 $renamed = @rename($tmp_file, $attach_upload['path']);
282 if ($renamed === true) {
283 $atta_ext = end(explode('.', $file_name));
284
285 $attachment = array(
286 'post_title' => $file_name,
287 'post_content' => '',
288 'post_type' => 'attachment',
289 'post_parent' => $post_id,
290 'post_mime_type' => 'image/' . $atta_ext,
291 'guid' => $attach_upload['url']
292 );
293
294 // Save the data
295 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
296 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
297 $new_custom['_thumbnail_id'] = array(
298 $attach_id
299 );
300 }
301 @unlink($tmp_file);
302 }
303
304 if ($post_id && is_array($post_categories)) {
305 //insert categories
306
307 $cat_ids = wp_create_categories($post_categories, $post_id);
308 }
309
310
311 //get current custom fields
312 $cur_custom = get_post_custom($post_id);
313 //check which values doesnot exists in new custom fields
314 $diff_values = array_diff_key($cur_custom, $new_custom);
315
316 if (is_array($diff_values))
317 foreach ($diff_values as $meta_key => $value) {
318 delete_post_meta($post_id, $meta_key);
319 }
320 //insert new post meta
321 foreach ($new_custom as $meta_key => $value) {
322 if (strpos($meta_key, '_mmb') === 0 || strpos($meta_key, '_edit') === 0) {
323 continue;
324 } else {
325 update_post_meta($post_id, $meta_key, $value[0]);
326 }
327 }
328
329
330 return $post_id;
331
332
333 }
334
335 /**
336 * Aleternate function for WordPress download_url()
337 */
338 function mmb_download_url($url, $file_name)
339 {
340 $destination = fopen($file_name, 'wb');
341 $source = @fopen($url, "r");
342 while ($a = fread($source, 1024)) {
343 $ret = fwrite($destination, $a);
344 }
345 fclose($source);
346 fclose($destination);
347 return $file_name;
348 }
349 }
350 ?>