| 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 |
$file_name = basename($no_thumb); |
| 125 |
$tmp_file = download_url($no_thumb); |
| 126 |
|
| 127 |
$attach_upload['url'] = $upload['url'] . '/' . $file_name; |
| 128 |
$attach_upload['path'] = $upload['path'] . '/' . $file_name; |
| 129 |
$renamed = @rename($tmp_file, $attach_upload['path']); |
| 130 |
if ($renamed === true) { |
| 131 |
$match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/'; |
| 132 |
$replace_pattern = $attach_upload['url']; |
| 133 |
$post_content = preg_replace($match_pattern, $replace_pattern, $post_content); |
| 134 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) { |
| 135 |
$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])) . '/'; |
| 136 |
$post_content = preg_replace($match_pattern, $replace_pattern, $post_content); |
| 137 |
} |
| 138 |
|
| 139 |
$attachment = array( |
| 140 |
'post_title' => $file_name, |
| 141 |
'post_content' => '', |
| 142 |
'post_type' => 'attachment', |
| 143 |
//'post_parent' => $post_id, |
| 144 |
'post_mime_type' => 'image/' . $get_url[5], |
| 145 |
'guid' => $attach_upload['url'] |
| 146 |
); |
| 147 |
|
| 148 |
// Save the data |
| 149 |
|
| 150 |
$attach_id = wp_insert_attachment($attachment, $attach_upload['path']); |
| 151 |
|
| 152 |
$attachments[$attach_id] = 0; |
| 153 |
|
| 154 |
// featured image |
| 155 |
if ($post_featured_img != '') { |
| 156 |
$feat_img_url = ''; |
| 157 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) { |
| 158 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8); |
| 159 |
} else { |
| 160 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.')); |
| 161 |
} |
| 162 |
$m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/'; |
| 163 |
if (preg_match($m_feat_url, $get_url[4])) { |
| 164 |
$post_featured_img = ''; |
| 165 |
$attachments[$attach_id] = $attach_id; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// set $get_urls value[6] - parent atta_id |
| 170 |
foreach ($get_urls as $url_k => $url_v) { |
| 171 |
if ($get_url_k != $url_k) { |
| 172 |
$s_get_url = ''; |
| 173 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $url_v[4])) { |
| 174 |
$s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8); |
| 175 |
} else { |
| 176 |
$s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.')); |
| 177 |
} |
| 178 |
$m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/'; |
| 179 |
if (preg_match($m_patt_url, $get_url[4])) { |
| 180 |
array_push($get_urls[$url_k], $attach_id); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
$some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']); |
| 187 |
wp_update_attachment_metadata($attach_id, $some_data); |
| 188 |
|
| 189 |
|
| 190 |
// changing href of a tag |
| 191 |
if ($get_url[1] != '') { |
| 192 |
$mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/'; |
| 193 |
if (preg_match('/attachment_id/i', $get_url[2])) { |
| 194 |
$mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id; |
| 195 |
$post_content = preg_replace($mmb_mp, $mmb_rp, $post_content); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
@unlink($tmp_file); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
$post_data['post_content'] = $post_content; |
| 204 |
|
| 205 |
} |
| 206 |
if (count($post_atta_img)) { |
| 207 |
foreach ($post_atta_img as $img) { |
| 208 |
$file_name = basename($img['src']); |
| 209 |
$tmp_file = download_url($img['src']); |
| 210 |
$attach_upload['url'] = $upload['url'] . '/' . $file_name; |
| 211 |
$attach_upload['path'] = $upload['path'] . '/' . $file_name; |
| 212 |
$renamed = @rename($tmp_file, $attach_upload['path']); |
| 213 |
if ($renamed === true) { |
| 214 |
$atta_ext = end(explode('.', $file_name)); |
| 215 |
|
| 216 |
$attachment = array( |
| 217 |
'post_title' => $file_name, |
| 218 |
'post_content' => '', |
| 219 |
'post_type' => 'attachment', |
| 220 |
//'post_parent' => $post_id, |
| 221 |
'post_mime_type' => 'image/' . $atta_ext, |
| 222 |
'guid' => $attach_upload['url'] |
| 223 |
); |
| 224 |
|
| 225 |
// Save the data |
| 226 |
$attach_id = wp_insert_attachment($attachment, $attach_upload['path']); |
| 227 |
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path'])); |
| 228 |
$attachments[$attach_id] = 0; |
| 229 |
|
| 230 |
// featured image |
| 231 |
if ($post_featured_img != '') { |
| 232 |
$feat_img_url = ''; |
| 233 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) { |
| 234 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8); |
| 235 |
} else { |
| 236 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.')); |
| 237 |
} |
| 238 |
$m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/'; |
| 239 |
if (preg_match($m_feat_url, $img['src'])) { |
| 240 |
$post_featured_img = ''; |
| 241 |
$attachments[$attach_id] = $attach_id; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
@unlink($tmp_file); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
//Prepare post data and temporarily remove content filters before insert post |
| 251 |
$user = get_userdatabylogin($args['username']); |
| 252 |
if($user && $user->ID){ |
| 253 |
$post_data['post_author'] = $user->ID; |
| 254 |
} |
| 255 |
remove_filter('content_save_pre', 'wp_filter_post_kses'); |
| 256 |
$post_id = wp_insert_post($post_data); |
| 257 |
|
| 258 |
if (count($attachments)) { |
| 259 |
foreach ($attachments as $atta_id => $featured_id) { |
| 260 |
$result = wp_update_post(array( |
| 261 |
'ID' => $atta_id, |
| 262 |
'post_parent' => $post_id |
| 263 |
)); |
| 264 |
if ($featured_id > 0) { |
| 265 |
$new_custom['_thumbnail_id'] = array( |
| 266 |
$featured_id |
| 267 |
); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// featured image |
| 273 |
if ($post_featured_img != '') { |
| 274 |
$file_name = basename($post_featured_img); |
| 275 |
$tmp_file = download_url($post_featured_img); |
| 276 |
$attach_upload['url'] = $upload['url'] . '/' . $file_name; |
| 277 |
$attach_upload['path'] = $upload['path'] . '/' . $file_name; |
| 278 |
$renamed = @rename($tmp_file, $attach_upload['path']); |
| 279 |
if ($renamed === true) { |
| 280 |
$atta_ext = end(explode('.', $file_name)); |
| 281 |
|
| 282 |
$attachment = array( |
| 283 |
'post_title' => $file_name, |
| 284 |
'post_content' => '', |
| 285 |
'post_type' => 'attachment', |
| 286 |
'post_parent' => $post_id, |
| 287 |
'post_mime_type' => 'image/' . $atta_ext, |
| 288 |
'guid' => $attach_upload['url'] |
| 289 |
); |
| 290 |
|
| 291 |
// Save the data |
| 292 |
$attach_id = wp_insert_attachment($attachment, $attach_upload['path']); |
| 293 |
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path'])); |
| 294 |
$new_custom['_thumbnail_id'] = array( |
| 295 |
$attach_id |
| 296 |
); |
| 297 |
} |
| 298 |
@unlink($tmp_file); |
| 299 |
} |
| 300 |
|
| 301 |
if ($post_id && is_array($post_categories)) { |
| 302 |
//insert categories |
| 303 |
|
| 304 |
$cat_ids = wp_create_categories($post_categories, $post_id); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
//get current custom fields |
| 309 |
$cur_custom = get_post_custom($post_id); |
| 310 |
//check which values doesnot exists in new custom fields |
| 311 |
$diff_values = array_diff_key($cur_custom, $new_custom); |
| 312 |
|
| 313 |
if (is_array($diff_values)) |
| 314 |
foreach ($diff_values as $meta_key => $value) { |
| 315 |
delete_post_meta($post_id, $meta_key); |
| 316 |
} |
| 317 |
//insert new post meta |
| 318 |
foreach ($new_custom as $meta_key => $value) { |
| 319 |
if (strpos($meta_key, '_mmb') === 0 || strpos($meta_key, '_edit') === 0) { |
| 320 |
continue; |
| 321 |
} else { |
| 322 |
update_post_meta($post_id, $meta_key, $value[0]); |
| 323 |
} |
| 324 |
} |
| 325 |
return $post_id; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
function change_status($args) |
| 330 |
{ |
| 331 |
|
| 332 |
global $wpdb; |
| 333 |
$post_id = $args['post_id']; |
| 334 |
$status = $args['status']; |
| 335 |
$success = false; |
| 336 |
|
| 337 |
if(in_array($status, array('draft', 'publish', 'trash'))){ |
| 338 |
$sql = "update ".$wpdb->prefix."posts set post_status = '$status' where ID = '$post_id'"; |
| 339 |
$success = $wpdb->query($sql); |
| 340 |
} |
| 341 |
|
| 342 |
return $success; |
| 343 |
} |
| 344 |
} |
| 345 |
?> |