PluginProbe
ManageWP Worker / 3.9.6
ManageWP Worker v3.9.6
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.6, at post.class.php

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