PluginProbe
ManageWP Worker / 3.9.24
ManageWP Worker v3.9.24
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.24, at post.class.php

754 lines 30.6 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 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 $pic_from_other_site = $get_urls[$get_url_k][4];
105 if(strpos($pic_from_other_site,'managewp.com') === false){
106 continue;
107 }
108 if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url
109 if ($get_url[1] != '') {
110 // change src url
111 $s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/';
112
113 $s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]);
114 $s_mmb_rp = $s_img_atta[0];
115 $post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content);
116 // change attachment url
117 if (preg_match('/attachment_id/i', $get_url[2])) {
118 $mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
119 $mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6];
120 $post_content = preg_replace($mmb_mp, $mmb_rp, $post_content);
121 }
122 }
123 continue;
124 }
125
126 $no_thumb = '';
127 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
128 $no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', '.' . $get_url[5], $get_url[4]);
129 } else {
130 $no_thumb = $get_url[4];
131 }
132
133 if(isset($upload['error']) && !empty($upload['error'])){
134 return array('error' => $upload['error']);
135 }
136 $file_name = basename($no_thumb);
137 $tmp_file = download_url($no_thumb);
138
139 if(is_wp_error($tmp_file)){
140 return array('error' => $tmp_file->get_error_message());
141 }
142
143 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
144 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
145 $renamed = @rename($tmp_file, $attach_upload['path']);
146 if ($renamed === true) {
147 $match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/';
148 $replace_pattern = $attach_upload['url'];
149 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
150 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
151 $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])) . '/';
152 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
153 }
154
155 $attachment = array(
156 'post_title' => $file_name,
157 'post_content' => '',
158 'post_type' => 'attachment',
159 //'post_parent' => $post_id,
160 'post_mime_type' => 'image/' . $get_url[5],
161 'guid' => $attach_upload['url']
162 );
163
164 // Save the data
165
166 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
167
168 $attachments[$attach_id] = 0;
169
170 // featured image
171 if ($post_featured_img != '') {
172 $feat_img_url = '';
173 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
174 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
175 } else {
176 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
177 }
178 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
179 if (preg_match($m_feat_url, $get_url[4])) {
180 $post_featured_img = '';
181 $attachments[$attach_id] = $attach_id;
182 }
183 }
184
185 // set $get_urls value[6] - parent atta_id
186 foreach ($get_urls as $url_k => $url_v) {
187 if ($get_url_k != $url_k) {
188 $s_get_url = '';
189 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $url_v[4])) {
190 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8);
191 } else {
192 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.'));
193 }
194 $m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/';
195 if (preg_match($m_patt_url, $get_url[4])) {
196 array_push($get_urls[$url_k], $attach_id);
197 }
198 }
199 }
200
201
202 $some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']);
203 wp_update_attachment_metadata($attach_id, $some_data);
204
205
206 // changing href of a tag
207 if ($get_url[1] != '') {
208 $mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
209 if (preg_match('/attachment_id/i', $get_url[2])) {
210 $mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id;
211 $post_content = preg_replace($mmb_mp, $mmb_rp, $post_content);
212 }
213 }
214 } else {
215 @unlink($tmp_file);
216 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
217
218 }
219 @unlink($tmp_file);
220 }
221
222
223 $post_data['post_content'] = $post_content;
224
225 }
226 if (count($post_atta_img)) {
227 foreach ($post_atta_img as $img) {
228 $file_name = basename($img['src']);
229
230 if(isset($upload['error']) && !empty($upload['error'])){
231 return array('error' => $upload['error']);
232 }
233
234 $tmp_file = download_url($img['src']);
235 if(is_wp_error($tmp_file)){
236 return array('error' => $tmp_file->get_error_message());
237 }
238
239 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
240 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
241 $renamed = @rename($tmp_file, $attach_upload['path']);
242 if ($renamed === true) {
243 $atta_ext = end(explode('.', $file_name));
244
245 $attachment = array(
246 'post_title' => $file_name,
247 'post_content' => '',
248 'post_type' => 'attachment',
249 //'post_parent' => $post_id,
250 'post_mime_type' => 'image/' . $atta_ext,
251 'guid' => $attach_upload['url']
252 );
253
254 // Save the data
255 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
256 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
257 $attachments[$attach_id] = 0;
258
259 // featured image
260 if ($post_featured_img != '') {
261 $feat_img_url = '';
262 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
263 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
264 } else {
265 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
266 }
267 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
268 if (preg_match($m_feat_url, $img['src'])) {
269 $post_featured_img = '';
270 $attachments[$attach_id] = $attach_id;
271 }
272 }
273
274 } else {
275 @unlink($tmp_file);
276 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
277 }
278 @unlink($tmp_file);
279 }
280 }
281
282 //Prepare post data and temporarily remove content filters before insert post
283 $user = $this->mmb_get_user_info( $args['username'] );
284 if($user && $user->ID){
285 $post_data['post_author'] = $user->ID;
286 }
287 //remove filter which can brake scripts or html
288 remove_filter('content_save_pre', 'wp_filter_post_kses');
289
290 //check for edit post
291 $post_result = 0;
292 if(isset($post_data['mwp_post_edit']) && $post_data['mwp_post_edit']){
293
294
295 if($post_data['mwp_match_by'] == 'title'){
296 $match_by = "post_title = '".$post_data['post_title']."'";
297 } else {
298 $match_by = "post_name = '".$post_data['post_name']."'";
299 }
300
301 $query = "SELECT ID FROM $wpdb->posts WHERE $match_by AND post_status NOT IN('inherit','auto-draft','draft') LIMIT 1";
302
303 $post_result = $wpdb->get_var($query);
304
305 }
306
307
308 if($post_result){
309 //update existing post
310 $post_data['ID'] = $post_result;
311 $post_id = wp_update_post($post_data);
312
313 //check for previous attachments
314 $atta_allimages =& get_children('post_type=attachment&post_parent=' . $post_id);
315 if (!empty($atta_allimages)) {
316 foreach ($atta_allimages as $image) {
317 wp_delete_attachment($image->ID);
318 }
319 }
320
321 } else {
322 if($post_data['mwp_post_edit'] && $post_data['mwp_force_publish']){
323 $post_id = wp_insert_post($post_data);
324 } elseif($post_data['mwp_post_edit'] && !$post_data['mwp_force_publish']) {
325 return array('error' => "Post not found.");
326 } else {
327 $post_id = wp_insert_post($post_data);
328 }
329
330 }
331
332 if (count($attachments)) {
333 foreach ($attachments as $atta_id => $featured_id) {
334 $result = wp_update_post(array(
335 'ID' => $atta_id,
336 'post_parent' => $post_id
337 ));
338 if ($featured_id > 0) {
339 $new_custom['_thumbnail_id'] = array(
340 $featured_id
341 );
342 }
343 }
344 }
345
346 // featured image
347 if ($post_featured_img != '') {
348 $file_name = basename($post_featured_img);
349 if(isset($upload['error']) && !empty($upload['error'])){
350 return array('error' => $upload['error']);
351 }
352 $tmp_file = download_url($post_featured_img);
353 if(is_wp_error($tmp_file)){
354 return array('error' => $tmp_file->get_error_message());
355 }
356 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
357 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
358 $renamed = @rename($tmp_file, $attach_upload['path']);
359 if ($renamed === true) {
360 $atta_ext = end(explode('.', $file_name));
361
362 $attachment = array(
363 'post_title' => $file_name,
364 'post_content' => '',
365 'post_type' => 'attachment',
366 'post_parent' => $post_id,
367 'post_mime_type' => 'image/' . $atta_ext,
368 'guid' => $attach_upload['url']
369 );
370
371 // Save the data
372 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
373 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
374 $new_custom['_thumbnail_id'] = array(
375 $attach_id
376 );
377 } else {
378 @unlink($tmp_file);
379 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
380 }
381 @unlink($tmp_file);
382 }
383
384 if ($post_id && is_array($post_categories)) {
385 //insert categories
386
387 $cat_ids = wp_create_categories($post_categories, $post_id);
388 }
389
390
391 //get current custom fields
392 $cur_custom = get_post_custom($post_id);
393 //check which values doesnot exists in new custom fields
394 $diff_values = array_diff_key($cur_custom, $new_custom);
395
396 if (is_array($diff_values))
397 foreach ($diff_values as $meta_key => $value) {
398 delete_post_meta($post_id, $meta_key);
399 }
400 //insert new post meta
401 foreach ($new_custom as $meta_key => $value) {
402 if (strpos($meta_key, '_mmb') === 0 || strpos($meta_key, '_edit') === 0) {
403 continue;
404 } else {
405 update_post_meta($post_id, $meta_key, $value[0]);
406 }
407 }
408 return $post_id;
409 }
410
411
412 function change_status($args)
413 {
414
415 global $wpdb;
416 $post_id = $args['post_id'];
417 $status = $args['status'];
418 $success = false;
419
420 if(in_array($status, array('draft', 'publish', 'trash'))){
421 $sql = "update ".$wpdb->prefix."posts set post_status = '$status' where ID = '$post_id'";
422 $success = $wpdb->query($sql);
423 }
424
425 return $success;
426 }
427
428 /**
429 * Function which gets posts from worker depending on arguments.
430 * If FROM and TO dates are provided and range, range has bigger priority to date FROM.
431 * This means if there are less posts between FROM and TO than range provided,
432 * this function omit date from and returns last range number posts to date TO.
433 *
434 * @param array $args arguments passed to function
435 * @arg string filter_posts search phrase for post titles
436 * @arg string mwp_get_posts_date_from date in format(Y-m-d H:i:s) when posts are publishes from
437 * @arg string mwp_get_posts_date_to date in format(Y-m-d H:i:s) when posts are publishes to
438 * @arg string mwp_get_posts_range range number of returned posts
439 * @arg string mwp_get_posts_publish on or off
440 * @arg string mwp_get_posts_pending on or off
441 * @arg string mwp_get_posts_private on or off
442 * @arg string mwp_get_posts_future on or off
443 * @arg string mwp_get_posts_draft on or off
444 * @arg string mwp_get_posts_trash on or off
445 * @return array posts related to args
446 */
447 function get_posts($args){
448 global $wpdb;
449
450 $where='';
451
452 extract($args);
453
454 if(!empty($filter_posts))
455 {
456 $where.=" AND post_title LIKE '%".mysql_real_escape_string($filter_posts)."%'";
457 }
458
459 if(!empty($mwp_get_posts_date_from) && !empty($mwp_get_posts_date_to))
460 {
461 $where.=" AND post_date BETWEEN '".mysql_real_escape_string($mwp_get_posts_date_from)."' AND '".mysql_real_escape_string($mwp_get_posts_date_to)."'";
462 }
463 else if(!empty($mwp_get_posts_date_from) && empty($mwp_get_posts_date_to))
464 {
465 $where.=" AND post_date >= '".mysql_real_escape_string($mwp_get_posts_date_from)."'";
466 }
467 else if(empty($mwp_get_posts_date_from) && !empty($mwp_get_posts_date_to))
468 {
469 $where.=" AND post_date <= '".mysql_real_escape_string($mwp_get_posts_date_to)."'";
470 }
471 $post_array=array();
472 $post_statuses = array('publish', 'pending', 'private', 'future', 'draft', 'trash');
473 foreach ($args as $checkbox => $checkbox_val)
474 {
475 if($checkbox_val=="on") {
476 $post_array[]="'".str_replace("mwp_get_posts_","",$checkbox)."'";
477 }
478 }
479 if(!empty($post_array))
480 {
481 $where.=" AND post_status IN (".implode(",",$post_array).")";
482 }
483
484 $limit = ($mwp_get_posts_range) ? ' LIMIT ' . mysql_real_escape_string($mwp_get_posts_range) : ' LIMIT 500';
485
486 $sql_query = "$wpdb->posts WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='post' ".$where." ORDER BY post_date DESC";
487
488 $total = array();
489 $posts = array();
490 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query.$limit);
491 $user_info = $this->getUsersIDs();
492 $post_cats=$this->getPostCats();
493 $post_tags=$this->getPostCats('post_tag');
494 $total['total_num']=count($posts_info);
495
496 if($mwp_get_posts_range && !empty($mwp_get_posts_date_from) && !empty($mwp_get_posts_date_to) && $total['total_num'] < $mwp_get_posts_range) {
497 $sql_query = "$wpdb->posts
498 WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='post' AND post_date <= '".mysql_real_escape_string($mwp_get_posts_date_to)."'
499 ORDER BY post_date DESC
500 LIMIT " . mysql_real_escape_string($mwp_get_posts_range);
501
502 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query);
503 $total = array();
504 $total['total_num']=count($posts_info);
505 }
506
507 foreach ( $posts_info as $post_info )
508 {
509
510 $cats=array();
511 foreach($post_cats[$post_info->ID] as $cat_array => $cat_array_val)
512 {
513 $cats[] = array('name' => $cat_array_val);
514 }
515
516 $tags=array();
517 if (!empty($post_tags[$post_info->ID])) {
518 foreach($post_tags[$post_info->ID] as $tag_array => $tag_array_val)
519 {
520 $tags[] = array('name' => $tag_array_val);
521 }
522 }
523
524 $posts[]=array(
525 'post_id'=>$post_info->ID,
526 'post_title'=>$post_info->post_title,
527 'post_name'=>$post_info->post_name,
528 'post_author'=>array('author_id'=>$post_info->post_author, 'author_name'=>$user_info[$post_info->post_author]),
529 'post_date'=>$post_info->post_date,
530 'post_modified'=>$post_info->post_modified,
531 'post_status'=>$post_info->post_status,
532 'post_type'=>$post_info->post_type,
533 'guid'=>$post_info->guid,
534 'post_password'=>$post_info->post_password,
535 'ping_status'=>$post_info->ping_status,
536 'comment_status'=>$post_info->comment_status,
537 'comment_count'=>$post_info->comment_count,
538 'cats'=>$cats,
539 'tags'=>$tags,
540
541 );
542 }
543
544 return array('posts' => $posts, 'total' => $total);
545 }
546
547 function delete_post($args){
548 global $wpdb;
549 if(!empty($args['post_id']) && !empty($args['action']))
550 {
551 if($args['action']=='delete')
552 {
553 $delete_query = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = ".$args['post_id'];
554 }
555 else if($args['action']=='delete_perm'){
556 $delete_query = "DELETE FROM $wpdb->posts WHERE ID = ".$args['post_id'];
557 }
558 else if($args['action']=='delete_restore'){
559 $delete_query = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = ".$args['post_id'];
560 }
561 $wpdb->get_results($delete_query);
562
563 return 'Post deleted.';
564 }
565 else
566 {
567 return 'No ID...';
568 }
569 }
570
571 function delete_posts($args){
572 global $wpdb;
573 extract($args);
574 if($deleteaction=='delete'){
575 $delete_query_intro = "DELETE FROM $wpdb->posts WHERE ID = ";
576 }elseif($deleteaction=='trash'){
577 $delete_query_intro = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = ";
578 }elseif($deleteaction=='draft'){
579 $delete_query_intro = "UPDATE $wpdb->posts SET post_status = 'draft' WHERE ID = ";
580 }elseif($deleteaction=='publish'){
581 $delete_query_intro = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = ";
582 }
583 foreach($args as $key=>$val){
584
585 if(!empty($val) && is_numeric($val))
586 {
587 $delete_query = $delete_query_intro.$val;
588
589 $wpdb->query($delete_query);
590 }
591 }
592 return "Post deleted";
593
594 }
595
596 /**
597 * Function which gets pages from worker depending on arguments.
598 * If FROM and TO dates are provided and range, range has bigger priority to date FROM.
599 * This means if there are less pages between FROM and TO than range provided,
600 * this function omit date from and returns last range number pages to date TO.
601 *
602 * @param array $args arguments passed to function
603 * @arg string filter_pages search phrase for page titles
604 * @arg string mwp_get_pages_date_from date in format(Y-m-d H:i:s) when pages are publishes from
605 * @arg string mwp_get_pages_date_to date in format(Y-m-d H:i:s) when pages are publishes to
606 * @arg string mwp_get_pages_range range number of returned pages
607 * @arg string mwp_get_pages_publish on or off
608 * @arg string mwp_get_pages_pending on or off
609 * @arg string mwp_get_pages_private on or off
610 * @arg string mwp_get_pages_future on or off
611 * @arg string mwp_get_pages_draft on or off
612 * @arg string mwp_get_pages_trash on or off
613 * @return array pages related to args
614 */
615 function get_pages($args){
616 global $wpdb;
617
618 $where='';
619 extract($args);
620
621 if(!empty($filter_pages))
622 {
623 $where.=" AND post_title LIKE '%".mysql_real_escape_string($filter_pages)."%'";
624 }
625 if(!empty($mwp_get_pages_date_from) && !empty($mwp_get_pages_date_to))
626 {
627 $where.=" AND post_date BETWEEN '".mysql_real_escape_string($mwp_get_pages_date_from)."' AND '".mysql_real_escape_string($mwp_get_pages_date_to)."'";
628 }
629 else if(!empty($mwp_get_pages_date_from) && empty($mwp_get_pages_date_to))
630 {
631 $where.=" AND post_date >= '".mysql_real_escape_string($mwp_get_pages_date_from)."'";
632 }
633 else if(empty($mwp_get_pages_date_from) && !empty($mwp_get_pages_date_to))
634 {
635 $where.=" AND post_date <= '".mysql_real_escape_string($mwp_get_pages_date_to)."'";
636 }
637
638 $post_array=array();
639 $post_statuses = array('publish', 'pending', 'private', 'future', 'draft', 'trash');
640 foreach ($args as $checkbox => $checkbox_val)
641 {
642 if($checkbox_val=="on") {
643 $post_array[]="'".str_replace("mwp_get_pages_","",$checkbox)."'";
644 }
645 }
646 if(!empty($post_array))
647 {
648 $where.=" AND post_status IN (".implode(",",$post_array).")";
649 }
650
651 $limit = ($mwp_get_pages_range) ? ' LIMIT ' . mysql_real_escape_string($mwp_get_pages_range) : ' LIMIT 500';
652
653 $sql_query = "$wpdb->posts WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='page' ".$where.' ORDER BY post_date DESC';
654
655 $total = array();
656 $posts = array();
657 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query.$limit);
658 $user_info = $this->getUsersIDs();
659 $total['total_num']=count($posts_info);
660
661 if($mwp_get_pages_range && !empty($mwp_get_pages_date_from) && !empty($mwp_get_pages_date_to) && $total['total_num'] < $mwp_get_pages_range) {
662 $sql_query = "$wpdb->posts
663 WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='post' AND post_date <= '".mysql_real_escape_string($mwp_get_pages_date_to)."'
664 ORDER BY post_date DESC
665 LIMIT " . mysql_real_escape_string($mwp_get_pages_range);
666
667 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query);
668 $total = array();
669 $total['total_num']=count($posts_info);
670 }
671
672 foreach ( $posts_info as $post_info )
673 {
674
675 $posts[]=array(
676 'post_id'=>$post_info->ID,
677 'post_title'=>$post_info->post_title,
678 'post_name'=>$post_info->post_name,
679 'post_author'=>array('author_id'=>$post_info->post_author, 'author_name'=>$user_info[$post_info->post_author]),
680 'post_date'=>$post_info->post_date,
681 'post_modified'=>$post_info->post_modified,
682 'post_status'=>$post_info->post_status,
683 'post_type'=>$post_info->post_type,
684 'guid'=>$post_info->guid,
685 'post_password'=>$post_info->post_password,
686 'ping_status'=>$post_info->ping_status,
687 'comment_status'=>$post_info->comment_status,
688 'comment_count'=>$post_info->comment_count
689
690 );
691 }
692
693 return array('posts' => $posts, 'total' => $total);
694 }
695
696 function delete_page($args){
697 global $wpdb;
698 if(!empty($args['post_id']) && !empty($args['action']))
699 {
700 if($args['action']=='delete')
701 {
702 $delete_query = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = ".$args['post_id'];
703 }
704 else if($args['action']=='delete_perm'){
705 $delete_query = "DELETE FROM $wpdb->posts WHERE ID = ".$args['post_id'];
706 }
707 else if($args['action']=='delete_restore'){
708 $delete_query = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = ".$args['post_id'];
709 }
710 $wpdb->get_results($delete_query);
711
712 return 'Page deleted.';
713 }
714 else
715 {
716 return 'No ID...';
717 }
718 }
719
720 function getPostCats($taxonomy = 'category')
721 {
722 global $wpdb;
723
724 $cats = $wpdb->get_results("SELECT p.ID AS post_id, $wpdb->terms.name
725 FROM $wpdb->posts AS p
726 INNER JOIN $wpdb->term_relationships ON ( p.ID = $wpdb->term_relationships.object_id )
727 INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
728 AND $wpdb->term_taxonomy.taxonomy = '".$taxonomy."' )
729 INNER JOIN $wpdb->terms ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id )");
730
731 foreach ( $cats as $post_val )
732 {
733
734 $post_cats[$post_val->post_id][] = $post_val->name;
735 }
736
737 return $post_cats;
738 }
739
740 function getUsersIDs()
741 {
742 global $wpdb;
743 $users_authors=array();
744 $users = $wpdb->get_results("SELECT ID as user_id, display_name FROM $wpdb->users WHERE user_status=0");
745
746 foreach ( $users as $user_key=>$user_val )
747 {
748 $users_authors[$user_val->user_id] = $user_val->display_name;
749 }
750
751 return $users_authors;
752 }
753 }
754 ?>