| 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 |
if(basename($_SERVER['SCRIPT_FILENAME']) == "post.class.php"): |
| 13 |
exit; |
| 14 |
endif; |
| 15 |
class MMB_Post extends MMB_Core |
| 16 |
{ |
| 17 |
function __construct() |
| 18 |
{ |
| 19 |
parent::__construct(); |
| 20 |
} |
| 21 |
|
| 22 |
function create($args) |
| 23 |
{ |
| 24 |
|
| 25 |
//$this->_log($args); |
| 26 |
global $wpdb; |
| 27 |
|
| 28 |
/** |
| 29 |
* algorithm |
| 30 |
* 1. create post using wp_insert_post (insert tags also here itself) |
| 31 |
* 2. use wp_create_categories() to create(not exists) and insert in the post |
| 32 |
* 3. insert meta values |
| 33 |
*/ |
| 34 |
|
| 35 |
include_once ABSPATH . 'wp-admin/includes/taxonomy.php'; |
| 36 |
include_once ABSPATH . 'wp-admin/includes/image.php'; |
| 37 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 38 |
|
| 39 |
$post_struct = $args['post_data']; |
| 40 |
|
| 41 |
$post_data = $post_struct['post_data']; |
| 42 |
$new_custom = $post_struct['post_extras']['post_meta']; |
| 43 |
$post_categories = explode(',', $post_struct['post_extras']['post_categories']); |
| 44 |
$post_atta_img = $post_struct['post_extras']['post_atta_images']; |
| 45 |
$post_upload_dir = $post_struct['post_extras']['post_upload_dir']; |
| 46 |
$post_checksum = $post_struct['post_extras']['post_checksum']; |
| 47 |
$post_featured_img = $post_struct['post_extras']['featured_img']; |
| 48 |
|
| 49 |
$upload = wp_upload_dir(); |
| 50 |
|
| 51 |
// create dynamic url RegExp |
| 52 |
$mwp_base_url = parse_url($post_upload_dir['url']); |
| 53 |
$mwp_regexp_url = $mwp_base_url['host'] . $mwp_base_url['path']; |
| 54 |
$rep = array( |
| 55 |
'/', |
| 56 |
'+', |
| 57 |
'.', |
| 58 |
':', |
| 59 |
'?' |
| 60 |
); |
| 61 |
$with = array( |
| 62 |
'\/', |
| 63 |
'\+', |
| 64 |
'\.', |
| 65 |
'\:', |
| 66 |
'\?' |
| 67 |
); |
| 68 |
$mwp_regexp_url = str_replace($rep, $with, $mwp_regexp_url); |
| 69 |
|
| 70 |
// rename all src ../wp-content/ with hostname/wp-content/ |
| 71 |
$mmb_dot_url = '..' . $mmb_base_url['path']; |
| 72 |
$mmb_dot_url = str_replace($rep, $with, $mmb_dot_url); |
| 73 |
$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); |
| 74 |
if ($dot_match_count > 0) { |
| 75 |
foreach ($dot_get_urls as $dot_url) { |
| 76 |
$match_dot = '/' . str_replace($rep, $with, $dot_url[4]) . '/'; |
| 77 |
$replace_dot = 'http://' . $mmb_base_url['host'] . substr($dot_url[4], 2, strlen($dot_url[4])); |
| 78 |
$post_data['post_content'] = preg_replace($match_dot, $replace_dot, $post_data['post_content']); |
| 79 |
|
| 80 |
if ($dot_url[1] != '') { |
| 81 |
$match_dot_a = '/' . str_replace($rep, $with, $dot_url[2]) . '/'; |
| 82 |
$replace_dot_a = 'http://' . $mmb_base_url['host'] . substr($dot_url[2], 2, strlen($dot_url[2])); |
| 83 |
$post_data['post_content'] = preg_replace($match_dot_a, $replace_dot_a, $post_data['post_content']); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
//to find all the images |
| 91 |
$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); |
| 92 |
if ($match_count > 0) { |
| 93 |
$attachments = array(); |
| 94 |
$post_content = $post_data['post_content']; |
| 95 |
|
| 96 |
foreach ($get_urls as $get_url_k => $get_url) { |
| 97 |
// unset url in attachment array |
| 98 |
foreach ($post_atta_img as $atta_url_k => $atta_url_v) { |
| 99 |
$match_patt_url = '/' . str_replace($rep, $with, substr($atta_url_v['src'], 0, strrpos($atta_url_v['src'], '.'))) . '/'; |
| 100 |
if (preg_match($match_patt_url, $get_url[4])) { |
| 101 |
unset($post_atta_img[$atta_url_k]); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url |
| 106 |
if ($get_url[1] != '') { |
| 107 |
// change src url |
| 108 |
$s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/'; |
| 109 |
|
| 110 |
$s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]); |
| 111 |
$s_mmb_rp = $s_img_atta[0]; |
| 112 |
$post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content); |
| 113 |
// change attachment url |
| 114 |
if (preg_match('/attachment_id/i', $get_url[2])) { |
| 115 |
$mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/'; |
| 116 |
$mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6]; |
| 117 |
$post_content = preg_replace($mmb_mp, $mmb_rp, $post_content); |
| 118 |
} |
| 119 |
} |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
$no_thumb = ''; |
| 124 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) { |
| 125 |
$no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', '.' . $get_url[5], $get_url[4]); |
| 126 |
} else { |
| 127 |
$no_thumb = $get_url[4]; |
| 128 |
} |
| 129 |
|
| 130 |
if(isset($upload['error']) && !empty($upload['error'])){ |
| 131 |
return array('error' => $upload['error']); |
| 132 |
} |
| 133 |
$file_name = basename($no_thumb); |
| 134 |
$tmp_file = download_url($no_thumb); |
| 135 |
|
| 136 |
if(is_wp_error($tmp_file)){ |
| 137 |
return array('error' => $tmp_file->get_error_message()); |
| 138 |
} |
| 139 |
|
| 140 |
$attach_upload['url'] = $upload['url'] . '/' . $file_name; |
| 141 |
$attach_upload['path'] = $upload['path'] . '/' . $file_name; |
| 142 |
$renamed = @rename($tmp_file, $attach_upload['path']); |
| 143 |
if ($renamed === true) { |
| 144 |
$match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/'; |
| 145 |
$replace_pattern = $attach_upload['url']; |
| 146 |
$post_content = preg_replace($match_pattern, $replace_pattern, $post_content); |
| 147 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) { |
| 148 |
$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])) . '/'; |
| 149 |
$post_content = preg_replace($match_pattern, $replace_pattern, $post_content); |
| 150 |
} |
| 151 |
|
| 152 |
$attachment = array( |
| 153 |
'post_title' => $file_name, |
| 154 |
'post_content' => '', |
| 155 |
'post_type' => 'attachment', |
| 156 |
//'post_parent' => $post_id, |
| 157 |
'post_mime_type' => 'image/' . $get_url[5], |
| 158 |
'guid' => $attach_upload['url'] |
| 159 |
); |
| 160 |
|
| 161 |
// Save the data |
| 162 |
|
| 163 |
$attach_id = wp_insert_attachment($attachment, $attach_upload['path']); |
| 164 |
|
| 165 |
$attachments[$attach_id] = 0; |
| 166 |
|
| 167 |
// featured image |
| 168 |
if ($post_featured_img != '') { |
| 169 |
$feat_img_url = ''; |
| 170 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) { |
| 171 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8); |
| 172 |
} else { |
| 173 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.')); |
| 174 |
} |
| 175 |
$m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/'; |
| 176 |
if (preg_match($m_feat_url, $get_url[4])) { |
| 177 |
$post_featured_img = ''; |
| 178 |
$attachments[$attach_id] = $attach_id; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// set $get_urls value[6] - parent atta_id |
| 183 |
foreach ($get_urls as $url_k => $url_v) { |
| 184 |
if ($get_url_k != $url_k) { |
| 185 |
$s_get_url = ''; |
| 186 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $url_v[4])) { |
| 187 |
$s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8); |
| 188 |
} else { |
| 189 |
$s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.')); |
| 190 |
} |
| 191 |
$m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/'; |
| 192 |
if (preg_match($m_patt_url, $get_url[4])) { |
| 193 |
array_push($get_urls[$url_k], $attach_id); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
$some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']); |
| 200 |
wp_update_attachment_metadata($attach_id, $some_data); |
| 201 |
|
| 202 |
|
| 203 |
// changing href of a tag |
| 204 |
if ($get_url[1] != '') { |
| 205 |
$mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/'; |
| 206 |
if (preg_match('/attachment_id/i', $get_url[2])) { |
| 207 |
$mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id; |
| 208 |
$post_content = preg_replace($mmb_mp, $mmb_rp, $post_content); |
| 209 |
} |
| 210 |
} |
| 211 |
} else { |
| 212 |
@unlink($tmp_file); |
| 213 |
return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions."); |
| 214 |
|
| 215 |
} |
| 216 |
@unlink($tmp_file); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
$post_data['post_content'] = $post_content; |
| 221 |
|
| 222 |
} |
| 223 |
if (count($post_atta_img)) { |
| 224 |
foreach ($post_atta_img as $img) { |
| 225 |
$file_name = basename($img['src']); |
| 226 |
|
| 227 |
if(isset($upload['error']) && !empty($upload['error'])){ |
| 228 |
return array('error' => $upload['error']); |
| 229 |
} |
| 230 |
|
| 231 |
$tmp_file = download_url($img['src']); |
| 232 |
if(is_wp_error($tmp_file)){ |
| 233 |
return array('error' => $tmp_file->get_error_message()); |
| 234 |
} |
| 235 |
|
| 236 |
$attach_upload['url'] = $upload['url'] . '/' . $file_name; |
| 237 |
$attach_upload['path'] = $upload['path'] . '/' . $file_name; |
| 238 |
$renamed = @rename($tmp_file, $attach_upload['path']); |
| 239 |
if ($renamed === true) { |
| 240 |
$atta_ext = end(explode('.', $file_name)); |
| 241 |
|
| 242 |
$attachment = array( |
| 243 |
'post_title' => $file_name, |
| 244 |
'post_content' => '', |
| 245 |
'post_type' => 'attachment', |
| 246 |
//'post_parent' => $post_id, |
| 247 |
'post_mime_type' => 'image/' . $atta_ext, |
| 248 |
'guid' => $attach_upload['url'] |
| 249 |
); |
| 250 |
|
| 251 |
// Save the data |
| 252 |
$attach_id = wp_insert_attachment($attachment, $attach_upload['path']); |
| 253 |
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path'])); |
| 254 |
$attachments[$attach_id] = 0; |
| 255 |
|
| 256 |
// featured image |
| 257 |
if ($post_featured_img != '') { |
| 258 |
$feat_img_url = ''; |
| 259 |
if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) { |
| 260 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8); |
| 261 |
} else { |
| 262 |
$feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.')); |
| 263 |
} |
| 264 |
$m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/'; |
| 265 |
if (preg_match($m_feat_url, $img['src'])) { |
| 266 |
$post_featured_img = ''; |
| 267 |
$attachments[$attach_id] = $attach_id; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
} else { |
| 272 |
@unlink($tmp_file); |
| 273 |
return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions."); |
| 274 |
} |
| 275 |
@unlink($tmp_file); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
//Prepare post data and temporarily remove content filters before insert post |
| 280 |
$user = $this->mmb_get_user_info( $args['username'] ); |
| 281 |
if($user && $user->ID){ |
| 282 |
$post_data['post_author'] = $user->ID; |
| 283 |
} |
| 284 |
//remove filter which can brake scripts or html |
| 285 |
remove_filter('content_save_pre', 'wp_filter_post_kses'); |
| 286 |
|
| 287 |
//check for edit post |
| 288 |
$post_result = 0; |
| 289 |
if(isset($post_data['mwp_post_edit']) && $post_data['mwp_post_edit']){ |
| 290 |
|
| 291 |
|
| 292 |
if($post_data['mwp_match_by'] == 'title'){ |
| 293 |
$match_by = "post_title = '".$post_data['post_title']."'"; |
| 294 |
} else { |
| 295 |
$match_by = "post_name = '".$post_data['post_name']."'"; |
| 296 |
} |
| 297 |
|
| 298 |
$query = "SELECT ID FROM $wpdb->posts WHERE $match_by AND post_status NOT IN('inherit','auto-draft','draft') LIMIT 1"; |
| 299 |
|
| 300 |
$post_result = $wpdb->get_var($query); |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
if($post_result){ |
| 306 |
//update existing post |
| 307 |
$post_data['ID'] = $post_result; |
| 308 |
$post_id = wp_update_post($post_data); |
| 309 |
|
| 310 |
//check for previous attachments |
| 311 |
$atta_allimages =& get_children('post_type=attachment&post_parent=' . $post_id); |
| 312 |
if (!empty($atta_allimages)) { |
| 313 |
foreach ($atta_allimages as $image) { |
| 314 |
wp_delete_attachment($image->ID); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
} else { |
| 319 |
if($post_data['mwp_post_edit'] && $post_data['mwp_force_publish']){ |
| 320 |
$post_id = wp_insert_post($post_data); |
| 321 |
} elseif($post_data['mwp_post_edit'] && !$post_data['mwp_force_publish']) { |
| 322 |
return array('error' => "Post not found."); |
| 323 |
} else { |
| 324 |
$post_id = wp_insert_post($post_data); |
| 325 |
} |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
if (count($attachments)) { |
| 330 |
foreach ($attachments as $atta_id => $featured_id) { |
| 331 |
$result = wp_update_post(array( |
| 332 |
'ID' => $atta_id, |
| 333 |
'post_parent' => $post_id |
| 334 |
)); |
| 335 |
if ($featured_id > 0) { |
| 336 |
$new_custom['_thumbnail_id'] = array( |
| 337 |
$featured_id |
| 338 |
); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
// featured image |
| 344 |
if ($post_featured_img != '') { |
| 345 |
$file_name = basename($post_featured_img); |
| 346 |
if(isset($upload['error']) && !empty($upload['error'])){ |
| 347 |
return array('error' => $upload['error']); |
| 348 |
} |
| 349 |
$tmp_file = download_url($post_featured_img); |
| 350 |
if(is_wp_error($tmp_file)){ |
| 351 |
return array('error' => $tmp_file->get_error_message()); |
| 352 |
} |
| 353 |
$attach_upload['url'] = $upload['url'] . '/' . $file_name; |
| 354 |
$attach_upload['path'] = $upload['path'] . '/' . $file_name; |
| 355 |
$renamed = @rename($tmp_file, $attach_upload['path']); |
| 356 |
if ($renamed === true) { |
| 357 |
$atta_ext = end(explode('.', $file_name)); |
| 358 |
|
| 359 |
$attachment = array( |
| 360 |
'post_title' => $file_name, |
| 361 |
'post_content' => '', |
| 362 |
'post_type' => 'attachment', |
| 363 |
'post_parent' => $post_id, |
| 364 |
'post_mime_type' => 'image/' . $atta_ext, |
| 365 |
'guid' => $attach_upload['url'] |
| 366 |
); |
| 367 |
|
| 368 |
// Save the data |
| 369 |
$attach_id = wp_insert_attachment($attachment, $attach_upload['path']); |
| 370 |
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path'])); |
| 371 |
$new_custom['_thumbnail_id'] = array( |
| 372 |
$attach_id |
| 373 |
); |
| 374 |
} else { |
| 375 |
@unlink($tmp_file); |
| 376 |
return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions."); |
| 377 |
} |
| 378 |
@unlink($tmp_file); |
| 379 |
} |
| 380 |
|
| 381 |
if ($post_id && is_array($post_categories)) { |
| 382 |
//insert categories |
| 383 |
|
| 384 |
$cat_ids = wp_create_categories($post_categories, $post_id); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
//get current custom fields |
| 389 |
$cur_custom = get_post_custom($post_id); |
| 390 |
//check which values doesnot exists in new custom fields |
| 391 |
$diff_values = array_diff_key($cur_custom, $new_custom); |
| 392 |
|
| 393 |
if (is_array($diff_values)) |
| 394 |
foreach ($diff_values as $meta_key => $value) { |
| 395 |
delete_post_meta($post_id, $meta_key); |
| 396 |
} |
| 397 |
//insert new post meta |
| 398 |
foreach ($new_custom as $meta_key => $value) { |
| 399 |
if (strpos($meta_key, '_mmb') === 0 || strpos($meta_key, '_edit') === 0) { |
| 400 |
continue; |
| 401 |
} else { |
| 402 |
update_post_meta($post_id, $meta_key, $value[0]); |
| 403 |
} |
| 404 |
} |
| 405 |
return $post_id; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
function change_status($args) |
| 410 |
{ |
| 411 |
|
| 412 |
global $wpdb; |
| 413 |
$post_id = $args['post_id']; |
| 414 |
$status = $args['status']; |
| 415 |
$success = false; |
| 416 |
|
| 417 |
if(in_array($status, array('draft', 'publish', 'trash'))){ |
| 418 |
$sql = "update ".$wpdb->prefix."posts set post_status = '$status' where ID = '$post_id'"; |
| 419 |
$success = $wpdb->query($sql); |
| 420 |
} |
| 421 |
|
| 422 |
return $success; |
| 423 |
} |
| 424 |
|
| 425 |
function get_posts($args){ |
| 426 |
global $wpdb; |
| 427 |
|
| 428 |
$where=''; |
| 429 |
|
| 430 |
extract($args); |
| 431 |
|
| 432 |
if(!empty($filter_posts)) |
| 433 |
{ |
| 434 |
$where.=" AND post_title LIKE '%".mysql_real_escape_string($filter_posts)."%'"; |
| 435 |
} |
| 436 |
|
| 437 |
if(!empty($mwp_get_posts_date_from) && !empty($mwp_get_posts_date_to)) |
| 438 |
{ |
| 439 |
$where.=" AND post_date BETWEEN '".mysql_real_escape_string($mwp_get_posts_date_from)."' AND '".mysql_real_escape_string($mwp_get_posts_date_to)."'"; |
| 440 |
} |
| 441 |
else if(!empty($mwp_get_posts_date_from) && empty($mwp_get_posts_date_to)) |
| 442 |
{ |
| 443 |
$where.=" AND post_date >= '".mysql_real_escape_string($mwp_get_posts_date_from)."'"; |
| 444 |
} |
| 445 |
else if(empty($mwp_get_posts_date_from) && !empty($mwp_get_posts_date_to)) |
| 446 |
{ |
| 447 |
$where.=" AND post_date <= '".mysql_real_escape_string($mwp_get_posts_date_to)."'"; |
| 448 |
} |
| 449 |
$post_array=array(); |
| 450 |
$post_statuses = array('publish', 'pending', 'private', 'future', 'draft', 'trash'); |
| 451 |
foreach ($args as $checkbox => $checkbox_val) |
| 452 |
{ |
| 453 |
if($checkbox_val=="on") { |
| 454 |
$post_array[]="'".str_replace("mwp_get_posts_","",$checkbox)."'"; |
| 455 |
} |
| 456 |
} |
| 457 |
if(!empty($post_array)) |
| 458 |
{ |
| 459 |
$where.=" AND post_status IN (".implode(",",$post_array).")"; |
| 460 |
} |
| 461 |
|
| 462 |
$sql_query = "$wpdb->posts WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='post' ".$where." ORDER BY post_date DESC"; |
| 463 |
|
| 464 |
$total = array(); |
| 465 |
$user_info = $this->getUsersIDs(); |
| 466 |
$post_cats=$this->getPostCats(); |
| 467 |
$post_tags=$this->getPostCats('post_tag'); |
| 468 |
$posts_total = $wpdb->get_results("SELECT count(*) as total_posts FROM ".$sql_query); |
| 469 |
$total['total_num']=$posts_total[0]->total_posts; |
| 470 |
|
| 471 |
$posts = array(); |
| 472 |
$posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query." LIMIT 500"); |
| 473 |
|
| 474 |
foreach ( $posts_info as $post_info ) |
| 475 |
{ |
| 476 |
|
| 477 |
$cats=array(); |
| 478 |
foreach($post_cats[$post_info->ID] as $cat_array => $cat_array_val) |
| 479 |
{ |
| 480 |
$cats[] = array('name' => $cat_array_val); |
| 481 |
} |
| 482 |
|
| 483 |
$tags=array(); |
| 484 |
foreach($post_tags[$post_info->ID] as $tag_array => $tag_array_val) |
| 485 |
{ |
| 486 |
$tags[] = array('name' => $tag_array_val); |
| 487 |
} |
| 488 |
|
| 489 |
$posts[]=array( |
| 490 |
'post_id'=>$post_info->ID, |
| 491 |
'post_title'=>$post_info->post_title, |
| 492 |
'post_name'=>$post_info->post_name, |
| 493 |
'post_author'=>array('author_id'=>$post_info->post_author, 'author_name'=>$user_info[$post_info->post_author]), |
| 494 |
'post_date'=>$post_info->post_date, |
| 495 |
'post_modified'=>$post_info->post_modified, |
| 496 |
'post_status'=>$post_info->post_status, |
| 497 |
'post_type'=>$post_info->post_type, |
| 498 |
'guid'=>$post_info->guid, |
| 499 |
'post_password'=>$post_info->post_password, |
| 500 |
'ping_status'=>$post_info->ping_status, |
| 501 |
'comment_status'=>$post_info->comment_status, |
| 502 |
'comment_count'=>$post_info->comment_count, |
| 503 |
'cats'=>$cats, |
| 504 |
'tags'=>$tags, |
| 505 |
|
| 506 |
); |
| 507 |
} |
| 508 |
|
| 509 |
return array('posts' => $posts, 'total' => $total); |
| 510 |
} |
| 511 |
|
| 512 |
function delete_post($args){ |
| 513 |
global $wpdb; |
| 514 |
if(!empty($args['post_id']) && !empty($args['action'])) |
| 515 |
{ |
| 516 |
if($args['action']=='delete') |
| 517 |
{ |
| 518 |
$delete_query = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = ".$args['post_id']; |
| 519 |
} |
| 520 |
else if($args['action']=='delete_perm'){ |
| 521 |
$delete_query = "DELETE FROM $wpdb->posts WHERE ID = ".$args['post_id']; |
| 522 |
} |
| 523 |
else if($args['action']=='delete_restore'){ |
| 524 |
$delete_query = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = ".$args['post_id']; |
| 525 |
} |
| 526 |
$wpdb->get_results($delete_query); |
| 527 |
|
| 528 |
return 'Post deleted.'; |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
return 'No ID...'; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
function delete_posts($args){ |
| 537 |
global $wpdb; |
| 538 |
extract($args); |
| 539 |
if($deleteaction=='delete'){ |
| 540 |
$delete_query_intro = "DELETE FROM $wpdb->posts WHERE ID = "; |
| 541 |
}elseif($deleteaction=='trash'){ |
| 542 |
$delete_query_intro = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = "; |
| 543 |
}elseif($deleteaction=='draft'){ |
| 544 |
$delete_query_intro = "UPDATE $wpdb->posts SET post_status = 'draft' WHERE ID = "; |
| 545 |
}elseif($deleteaction=='publish'){ |
| 546 |
$delete_query_intro = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = "; |
| 547 |
} |
| 548 |
foreach($args as $key=>$val){ |
| 549 |
|
| 550 |
if(!empty($val) && is_numeric($val)) |
| 551 |
{ |
| 552 |
$delete_query = $delete_query_intro.$val; |
| 553 |
|
| 554 |
$wpdb->query($delete_query); |
| 555 |
} |
| 556 |
} |
| 557 |
return "Post deleted"; |
| 558 |
|
| 559 |
} |
| 560 |
|
| 561 |
function get_pages($args){ |
| 562 |
global $wpdb; |
| 563 |
|
| 564 |
$where=''; |
| 565 |
extract($args); |
| 566 |
|
| 567 |
if(!empty($filter_pages)) |
| 568 |
{ |
| 569 |
$where.=" AND post_title LIKE '%".mysql_real_escape_string($filter_pages)."%'"; |
| 570 |
} |
| 571 |
if(!empty($mwp_get_pages_date_from) && !empty($mwp_get_pages_date_to)) |
| 572 |
{ |
| 573 |
$where.=" AND post_date BETWEEN '".mysql_real_escape_string($mwp_get_pages_date_from)."' AND '".mysql_real_escape_string($mwp_get_pages_date_to)."'"; |
| 574 |
} |
| 575 |
else if(!empty($mwp_get_pages_date_from) && empty($mwp_get_pages_date_to)) |
| 576 |
{ |
| 577 |
$where.=" AND post_date >= '".mysql_real_escape_string($mwp_get_pages_date_from)."'"; |
| 578 |
} |
| 579 |
else if(empty($mwp_get_pages_date_from) && !empty($mwp_get_pages_date_to)) |
| 580 |
{ |
| 581 |
$where.=" AND post_date <= '".mysql_real_escape_string($mwp_get_pages_date_to)."'"; |
| 582 |
} |
| 583 |
|
| 584 |
$post_array=array(); |
| 585 |
$post_statuses = array('publish', 'pending', 'private', 'future', 'draft', 'trash'); |
| 586 |
foreach ($args as $checkbox => $checkbox_val) |
| 587 |
{ |
| 588 |
if($checkbox_val=="on") { |
| 589 |
$post_array[]="'".str_replace("mwp_get_pages_","",$checkbox)."'"; |
| 590 |
} |
| 591 |
} |
| 592 |
if(!empty($post_array)) |
| 593 |
{ |
| 594 |
$where.=" AND post_status IN (".implode(",",$post_array).")"; |
| 595 |
} |
| 596 |
|
| 597 |
$sql_query = "$wpdb->posts WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='page' ".$where.' ORDER BY post_date DESC'; |
| 598 |
|
| 599 |
$total = array(); |
| 600 |
$user_info = $this->getUsersIDs(); |
| 601 |
$posts_total = $wpdb->get_results("SELECT count(*) as total_posts FROM ".$sql_query); |
| 602 |
$total['total_num']=$posts_total[0]->total_posts; |
| 603 |
|
| 604 |
$posts = array(); |
| 605 |
$posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query." LIMIT 500"); |
| 606 |
|
| 607 |
foreach ( $posts_info as $post_info ) |
| 608 |
{ |
| 609 |
|
| 610 |
$posts[]=array( |
| 611 |
'post_id'=>$post_info->ID, |
| 612 |
'post_title'=>$post_info->post_title, |
| 613 |
'post_name'=>$post_info->post_name, |
| 614 |
'post_author'=>array('author_id'=>$post_info->post_author, 'author_name'=>$user_info[$post_info->post_author]), |
| 615 |
'post_date'=>$post_info->post_date, |
| 616 |
'post_modified'=>$post_info->post_modified, |
| 617 |
'post_status'=>$post_info->post_status, |
| 618 |
'post_type'=>$post_info->post_type, |
| 619 |
'guid'=>$post_info->guid, |
| 620 |
'post_password'=>$post_info->post_password, |
| 621 |
'ping_status'=>$post_info->ping_status, |
| 622 |
'comment_status'=>$post_info->comment_status, |
| 623 |
'comment_count'=>$post_info->comment_count |
| 624 |
|
| 625 |
); |
| 626 |
} |
| 627 |
|
| 628 |
return array('posts' => $posts, 'total' => $total); |
| 629 |
} |
| 630 |
|
| 631 |
function delete_page($args){ |
| 632 |
global $wpdb; |
| 633 |
if(!empty($args['post_id']) && !empty($args['action'])) |
| 634 |
{ |
| 635 |
if($args['action']=='delete') |
| 636 |
{ |
| 637 |
$delete_query = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = ".$args['post_id']; |
| 638 |
} |
| 639 |
else if($args['action']=='delete_perm'){ |
| 640 |
$delete_query = "DELETE FROM $wpdb->posts WHERE ID = ".$args['post_id']; |
| 641 |
} |
| 642 |
else if($args['action']=='delete_restore'){ |
| 643 |
$delete_query = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = ".$args['post_id']; |
| 644 |
} |
| 645 |
$wpdb->get_results($delete_query); |
| 646 |
|
| 647 |
return 'Page deleted.'; |
| 648 |
} |
| 649 |
else |
| 650 |
{ |
| 651 |
return 'No ID...'; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
function getPostCats($taxonomy = 'category') |
| 656 |
{ |
| 657 |
global $wpdb; |
| 658 |
|
| 659 |
$cats = $wpdb->get_results("SELECT p.ID AS post_id, $wpdb->terms.name |
| 660 |
FROM $wpdb->posts AS p |
| 661 |
INNER JOIN $wpdb->term_relationships ON ( p.ID = $wpdb->term_relationships.object_id ) |
| 662 |
INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id |
| 663 |
AND $wpdb->term_taxonomy.taxonomy = '".$taxonomy."' ) |
| 664 |
INNER JOIN $wpdb->terms ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id )"); |
| 665 |
|
| 666 |
foreach ( $cats as $post_val ) |
| 667 |
{ |
| 668 |
|
| 669 |
$post_cats[$post_val->post_id][] = $post_val->name; |
| 670 |
} |
| 671 |
|
| 672 |
return $post_cats; |
| 673 |
} |
| 674 |
|
| 675 |
function getUsersIDs() |
| 676 |
{ |
| 677 |
global $wpdb; |
| 678 |
$users_authors=array(); |
| 679 |
$users = $wpdb->get_results("SELECT ID as user_id, display_name FROM $wpdb->users WHERE user_status=0"); |
| 680 |
|
| 681 |
foreach ( $users as $user_key=>$user_val ) |
| 682 |
{ |
| 683 |
$users_authors[$user_val->user_id] = $user_val->display_name; |
| 684 |
} |
| 685 |
|
| 686 |
return $users_authors; |
| 687 |
} |
| 688 |
} |
| 689 |
?> |