PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / addons / post_links / post.class.php

post.class.php in InfiniteWP Client trunk, at addons/post_links/post.class.php

834 lines 31.7 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 ( ! defined('ABSPATH') )
13 die();
14 class IWP_MMB_Post extends IWP_MMB_Core
15 {
16 function __construct()
17 {
18 parent::__construct();
19 }
20
21 function create($args)
22 {
23
24 //$this->_log($args);
25 global $wpdb;
26
27 /**
28 * algorithm
29 * 1. create post using wp_insert_post (insert tags also here itself)
30 * 2. use wp_create_categories() to create(not exists) and insert in the post
31 * 3. insert meta values
32 */
33
34 include_once ABSPATH . 'wp-admin/includes/taxonomy.php';
35 include_once ABSPATH . 'wp-admin/includes/image.php';
36 include_once ABSPATH . 'wp-admin/includes/file.php';
37
38 $post_struct = $args['post_data'];
39 if(function_exists('iwp_recursive_parse_args')){
40 $defaults = array(
41 'post_extras' => array(
42 'post_upload_dir' => array(),
43 'post_checksum' =>'',
44 'post_atta_images' =>''
45 ),
46 );
47 $post_struct = iwp_recursive_parse_args($post_struct,$defaults);
48 }
49
50
51
52 $post_data = $post_struct['post_data'];
53 $new_custom = $post_struct['post_extras']['post_meta'];
54 $post_categories = explode(',', $post_struct['post_extras']['post_categories']);
55 $post_atta_img = $post_struct['post_extras']['post_atta_images'];
56 $post_upload_dir = $post_struct['post_extras']['post_upload_dir'];
57 $post_checksum = $post_struct['post_extras']['post_checksum'];
58 $post_featured_img = $post_struct['post_extras']['featured_img'];
59
60 $upload = wp_upload_dir();
61
62 // create dynamic url RegExp
63 $dot_match_count = 0;
64
65 if(empty($post_upload_dir)){
66 $iwp_regexp_url = "";
67 $iwp_base_url = array();
68 $iwp_base_url['host'] = '';
69 $iwp_base_url['path'] = '';
70 }else{
71 $iwp_base_url = parse_url($post_upload_dir['url']);
72 $iwp_regexp_url = $iwp_base_url['host'] . $iwp_base_url['path'];
73 }
74 $rep = array(
75 '/',
76 '+',
77 '.',
78 ':',
79 '?'
80 );
81 $with = array(
82 '\/',
83 '\+',
84 '\.',
85 '\:',
86 '\?'
87 );
88 $iwp_regexp_url = str_replace($rep, $with, $iwp_regexp_url);
89
90 // rename all src ../wp-content/ with hostname/wp-content/
91
92 $iwp_mmb_dot_url = '..' . $iwp_base_url['path'];
93 $iwp_mmb_dot_url = str_replace($rep, $with, $iwp_mmb_dot_url);
94 $dot_match_count = preg_match_all('/(<a[^>]+href=\"([^"]+)\"[^>]*>)?(<\s*img.[^\/>]*src="([^"]*' . $iwp_mmb_dot_url . '[^\s]+\.(jpg|jpeg|png|gif|bmp))"[^>]*>)/ixu', $post_data['post_content'], $dot_get_urls, PREG_SET_ORDER);
95
96
97 if ($dot_match_count > 0) {
98 foreach ($dot_get_urls as $dot_url) {
99 $match_dot = '/' . str_replace($rep, $with, $dot_url[4]) . '/';
100 $replace_dot = 'http://' . $iwp_base_url['host'] . substr($dot_url[4], 2, strlen($dot_url[4]));
101 $post_data['post_content'] = preg_replace($match_dot, $replace_dot, $post_data['post_content']);
102
103 if ($dot_url[1] != '') {
104 $match_dot_a = '/' . str_replace($rep, $with, $dot_url[2]) . '/';
105 $replace_dot_a = 'http://' . $iwp_base_url['host'] . substr($dot_url[2], 2, strlen($dot_url[2]));
106 $post_data['post_content'] = preg_replace($match_dot_a, $replace_dot_a, $post_data['post_content']);
107 }
108 }
109 }
110
111 //to find all the images
112 $match_count = preg_match_all('/(<a[^>]+href=\"([^"]+)\"[^>]*>)?(<\s*img.[^\/>]*src="([^"]+' . $iwp_regexp_url . '[^\s]+\.(jpg|jpeg|png|gif|bmp))"[^>]*>)/ixu', $post_data['post_content'], $get_urls, PREG_SET_ORDER);
113
114 ///////////
115 //$html = $params['postContent'];
116 //
117 // $doc = new DOMDocument();
118 // @$doc->loadHTML($html);
119 //
120 // $tags = $doc->getElementsByTagName('img');
121 //
122 // foreach ($tags as $tag) {
123 // $img[]['src'] = str_replace('\"','',$tag->getAttribute('src'));
124 // }
125 /////////////////
126
127 if ($match_count > 0) {
128 $attachments = array();
129 $post_content = $post_data['post_content'];
130
131 if(!empty($get_urls) && is_array($get_urls)){
132 foreach ($get_urls as $get_url_k => $get_url) {
133 // unset url in attachment array
134 if(!empty($post_atta_img) && is_array($post_atta_img)){
135 foreach ($post_atta_img as $atta_url_k => $atta_url_v) {
136 $match_patt_url = '/' . str_replace($rep, $with, substr($atta_url_v['src'], 0, strrpos($atta_url_v['src'], '.'))) . '/';
137 if (preg_match($match_patt_url, $get_url[4])) {
138 unset($post_atta_img[$atta_url_k]);
139 }
140 }
141 }
142 $pic_from_other_site = $get_urls[$get_url_k][4];
143 /* if(strpos($pic_from_other_site,'exammple.com') === false){
144 continue;
145 }*/
146
147 if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url
148 if ($get_url[1] != '') {
149 // change src url
150 $s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/';
151
152 $s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]);
153 $s_mmb_rp = $s_img_atta[0];
154 $post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content);
155 // change attachment url
156 if (preg_match('/attachment_id/i', $get_url[2])) {
157 $iwp_mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
158 $iwp_mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6];
159 $post_content = preg_replace($iwp_mmb_mp, $iwp_mmb_rp, $post_content);
160 }
161 }
162 continue;
163 }
164
165 $no_thumb = '';
166 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
167 $no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', '.' . $get_url[5], $get_url[4]);
168 } else {
169 $no_thumb = $get_url[4];
170 }
171
172 if(isset($upload['error']) && !empty($upload['error'])){
173 return array('error' => $upload['error'], 'error_code' => 'upload_error');
174 }
175 $file_name = basename($no_thumb);
176 $tmp_file = download_url($no_thumb);
177
178 if(is_wp_error($tmp_file)){
179 return array('error' => $tmp_file->get_error_message(), 'error_code' => 'error_download_img_thumb_url');
180 }
181
182 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
183 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
184 $renamed = @rename($tmp_file, $attach_upload['path']);
185 if ($renamed === true) {
186 $match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/';
187 $replace_pattern = $attach_upload['url'];
188 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
189 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $get_url[4])) {
190 $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])) . '/';
191 $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
192 }
193
194 $attachment = array(
195 'post_title' => $file_name,
196 'post_content' => '',
197 'post_type' => 'attachment',
198 //'post_parent' => $post_id,
199 'post_mime_type' => 'image/' . $get_url[5],
200 'guid' => $attach_upload['url']
201 );
202
203 // Save the data
204
205 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
206
207 $attachments[$attach_id] = 0;
208
209 // featured image
210 if ($post_featured_img != '') {
211 $feat_img_url = '';
212 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
213 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
214 } else {
215 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
216 }
217 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
218 if (preg_match($m_feat_url, $get_url[4])) {
219 $post_featured_img = '';
220 $attachments[$attach_id] = $attach_id;
221 }
222 }
223
224 // set $get_urls value[6] - parent atta_id
225 foreach ($get_urls as $url_k => $url_v) {
226 if ($get_url_k != $url_k) {
227 $s_get_url = '';
228 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $url_v[4])) {
229 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8);
230 } else {
231 $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.'));
232 }
233 $m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/';
234 if (preg_match($m_patt_url, $get_url[4])) {
235 array_push($get_urls[$url_k], $attach_id);
236 }
237 }
238 }
239
240
241 $some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']);
242 wp_update_attachment_metadata($attach_id, $some_data);
243
244
245 //changing href of a tag
246 if ($get_url[1] != '') {
247 $iwp_mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
248 if (preg_match('/attachment_id/i', $get_url[2])) {
249 $iwp_mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id;
250 $post_content = preg_replace($iwp_mmb_mp, $iwp_mmb_rp, $post_content);
251 }
252 }
253 } else {
254 @unlink($tmp_file);
255 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.", 'error_code' =>'cannot_create_attachment_file_set_correct_permissions' );
256
257 }
258 @unlink($tmp_file);
259 }
260 }
261
262
263 $post_data['post_content'] = $post_content;
264
265 }
266 if (!empty($post_atta_img) && is_array($post_atta_img)) {
267 foreach ($post_atta_img as $img) {
268 $file_name = basename($img['src']);
269
270 if(isset($upload['error']) && !empty($upload['error'])){
271 return array('error' => $upload['error'], 'error_code' => 'upload_error_post_atta_img');
272 }
273
274 $tmp_file = download_url($img['src']);
275 if(is_wp_error($tmp_file)){
276 return array('error' => $tmp_file->get_error_message(), 'error_code' => 'download_url_wp_error_post_atta_img');
277 }
278
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 $attachments[$attach_id] = 0;
298
299 // featured image
300 if ($post_featured_img != '') {
301 $feat_img_url = '';
302 if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}$/', $post_featured_img)) {
303 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
304 } else {
305 $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
306 }
307 $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
308 if (preg_match($m_feat_url, $img['src'])) {
309 $post_featured_img = '';
310 $attachments[$attach_id] = $attach_id;
311 }
312 }
313
314 } else {
315 @unlink($tmp_file);
316 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.", 'error_code' => 'cannot_create_attachment_post_featured_img');
317 }
318 @unlink($tmp_file);
319 }
320 }
321
322 //Prepare post data and temporarily remove content filters before insert post
323 if (!empty($args['username'])) {
324 $user = $this->iwp_mmb_get_user_info( $args['username'] );
325 }else{
326 $user = false;
327 }
328 if($user && $user->ID){
329 $post_data['post_author'] = $user->ID;
330 }
331 //remove filter which can brake scripts or html
332 remove_filter('content_save_pre', 'wp_filter_post_kses');
333
334 //check for edit post
335 $post_result = 0;
336 if(isset($post_data['iwp_post_edit']) && $post_data['iwp_post_edit']){
337
338
339 if(isset($post_data['iwp_match_by']) && $post_data['iwp_match_by'] == 'title'){
340 $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_status NOT IN('inherit','auto-draft') LIMIT 1", $post_data['post_title']);
341 } else {
342 $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_status NOT IN('inherit','auto-draft') LIMIT 1", $post_data['post_name']);
343 }
344
345 $post_result = $wpdb->get_var($query);
346
347 }
348
349
350 if($post_result){
351 //update existing post
352 $post_data['ID'] = $post_result;
353 $post_id = wp_update_post($post_data);
354
355 //check for previous attachments
356 $atta_allimages =& get_children('post_type=attachment&post_parent=' . $post_id);
357 if (!empty($atta_allimages)) {
358 foreach ($atta_allimages as $image) {
359 wp_delete_attachment($image->ID);
360 }
361 }
362
363 } else {
364 if($post_data['iwp_post_edit'] && $post_data['iwp_force_publish']){
365 $post_id = wp_insert_post($post_data);
366 } elseif($post_data['iwp_post_edit'] && !$post_data['iwp_force_publish']) {
367 return array('error' => "Post not found.", 'error_code' => 'post_not_found');
368 } else {
369 $post_id = wp_insert_post($post_data);
370 }
371
372 }
373
374 if (!empty($attachments) && is_array($attachments)) {
375 foreach ($attachments as $atta_id => $featured_id) {
376 $result = wp_update_post(array(
377 'ID' => $atta_id,
378 'post_parent' => $post_id
379 ));
380 if ($featured_id > 0) {
381 $new_custom['_thumbnail_id'] = array(
382 $featured_id
383 );
384 }
385 }
386 }
387
388 // featured image
389 if ($post_featured_img != '') {
390 $file_name = basename($post_featured_img);
391 if(isset($upload['error']) && !empty($upload['error'])){
392 return array('error' => $upload['error'], 'error_code' => 'error_post_featured_img');
393 }
394 $tmp_file = download_url($post_featured_img);
395 if(is_wp_error($tmp_file)){
396 return array('error' => $tmp_file->get_error_message());
397 }
398 $attach_upload['url'] = $upload['url'] . '/' . $file_name;
399 $attach_upload['path'] = $upload['path'] . '/' . $file_name;
400 $renamed = @rename($tmp_file, $attach_upload['path']);
401 if ($renamed === true) {
402 $atta_ext = end(explode('.', $file_name));
403
404 $attachment = array(
405 'post_title' => $file_name,
406 'post_content' => '',
407 'post_type' => 'attachment',
408 'post_parent' => $post_id,
409 'post_mime_type' => 'image/' . $atta_ext,
410 'guid' => $attach_upload['url']
411 );
412
413 // Save the data
414 $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
415 wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attach_upload['path']));
416 $new_custom['_thumbnail_id'] = array(
417 $attach_id
418 );
419 } else {
420 @unlink($tmp_file);
421 return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
422 }
423 @unlink($tmp_file);
424 }
425
426 if ($post_id && is_array($post_categories)) {
427 //insert categories
428
429 $cat_ids = wp_create_categories($post_categories, $post_id);
430 }
431
432
433 //get current custom fields
434 $cur_custom = get_post_custom($post_id);
435 //check which values doesnot exists in new custom fields
436 $diff_values = array_diff_key($cur_custom, $new_custom);
437
438 if (is_array($diff_values))
439 foreach ($diff_values as $meta_key => $value) {
440 delete_post_meta($post_id, $meta_key);
441 }
442 //insert new post meta
443 foreach ($new_custom as $meta_key => $value) {
444 if (strpos($meta_key, '_mmb') === 0 || strpos($meta_key, '_edit') === 0) {
445 continue;
446 } else {
447 update_post_meta($post_id, $meta_key, $value[0]);
448 }
449 }
450 return $post_id;
451 }
452
453
454 function change_status($args)
455 {
456
457 global $wpdb;
458 $post_id = (int)$args['post_id'];
459 $status = $args['status'];
460 $success = false;
461
462 if(in_array($status, array('draft', 'publish', 'trash'))){
463 $sql = $wpdb->prepare("update ".$wpdb->base_prefix."posts set post_status = %s where ID = %d", $status, $post_id);
464 $success = $wpdb->query($sql);
465 }
466
467 return $success;
468 }
469
470 /**
471 * Function which gets posts from client depending on arguments.
472 * If FROM and TO dates are provided and range, range has bigger priority to date FROM.
473 * This means if there are less posts between FROM and TO than range provided,
474 * this function omit date from and returns last range number posts to date TO.
475 *
476 * @param array $args arguments passed to function
477 * @arg string filter_posts search phrase for post titles
478 * @arg string iwp_get_posts_date_from date in format(Y-m-d H:i:s) when posts are publishes from
479 * @arg string iwp_get_posts_date_to date in format(Y-m-d H:i:s) when posts are publishes to
480 * @arg string iwp_get_posts_range range number of returned posts
481 * @arg string iwp_get_posts_publish on or off
482 * @arg string iwp_get_posts_pending on or off
483 * @arg string iwp_get_posts_private on or off
484 * @arg string iwp_get_posts_future on or off
485 * @arg string iwp_get_posts_draft on or off
486 * @arg string iwp_get_posts_trash on or off
487 * @return array posts related to args
488 */
489 function get_posts($args){
490 global $wpdb;
491
492 $where='';
493
494 extract($args);
495
496 if(!empty($filter_posts))
497 {
498 $cus_sql= " OR ID IN(SELECT post_id FROM ".$wpdb->prefix."postmeta WHERE 1=1 AND (meta_value LIKE '%".esc_sql($filter_posts)."%'))";
499 $where.=" AND (post_title LIKE '%".esc_sql($filter_posts)."%'".$cus_sql.")";
500 }
501
502 if(!empty($iwp_get_posts_date_from) && !empty($iwp_get_posts_date_to))
503 {
504 $where.=" AND post_date BETWEEN '".esc_sql($iwp_get_posts_date_from)."' AND '".esc_sql($iwp_get_posts_date_to)."'";
505 }
506 else if(!empty($iwp_get_posts_date_from) && empty($iwp_get_posts_date_to))
507 {
508 $where.=" AND post_date >= '".esc_sql($iwp_get_posts_date_from)."'";
509 }
510 else if(empty($iwp_get_posts_date_from) && !empty($iwp_get_posts_date_to))
511 {
512 $where.=" AND post_date <= '".esc_sql($iwp_get_posts_date_to)."'";
513 }
514 $post_array=array();
515 $post_statuses = array('publish', 'pending', 'private', 'future', 'draft', 'trash');
516 foreach ($args as $checkbox => $checkbox_val)
517 {
518 $v = str_replace('iwp_get_posts_', '', $checkbox);
519 if ($checkbox_val === 'on' && in_array($v, $post_statuses, true)) {
520 $post_array[] = $v;
521 }
522 }
523 if(!empty($post_array))
524 {
525 $placeholders = implode(',', array_fill(0, count($post_array), '%s'));
526 $where .= $wpdb->prepare(" AND post_status IN ($placeholders)", $post_array);
527 }
528
529 if (!isset($iwp_get_posts_range)) {
530 $iwp_get_posts_range = '';
531 }
532
533 $range = absint($iwp_get_posts_range);
534 $limit = $range ? $wpdb->prepare(' LIMIT %d', $range) : ' LIMIT 500';
535
536 $sql_query = "$wpdb->posts WHERE post_status!='auto-draft' AND post_status!='inherit' AND (post_type='post' OR post_type = 'link_library_links') ".$where." ORDER BY post_date DESC";
537
538 $total = array();
539 $posts = array();
540 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query.$limit);
541 $user_info = $this->getUsersIDs();
542 $post_cats=$this->getPostCats();
543 $post_tags=$this->getPostCats('post_tag');
544 $total['total_num']=count($posts_info);
545
546 if($iwp_get_posts_range && !empty($iwp_get_posts_date_from) && !empty($iwp_get_posts_date_to) && $total['total_num'] < $iwp_get_posts_range) {
547
548 $sql_query = "$wpdb->posts
549 WHERE post_status!='auto-draft' AND post_status!='inherit' AND (post_type='post' OR post_type = 'link_library_links') AND post_date <= '".esc_sql($iwp_get_posts_date_to)."'
550 ORDER BY post_date DESC
551 " . $limit;
552
553 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query);
554 $total = array();
555 $total['total_num']=count($posts_info);
556 }
557
558 foreach ( $posts_info as $post_info )
559 {
560
561 $cats=array();
562 if(!empty($post_cats)){
563 foreach($post_cats[$post_info->ID] as $cat_array => $cat_array_val)
564 {
565 $cats[] = array('name' => $cat_array_val);
566 }
567 }
568
569 $tags=array();
570 if (!empty($post_tags[$post_info->ID])) {
571 foreach($post_tags[$post_info->ID] as $tag_array => $tag_array_val)
572 {
573 $tags[] = array('name' => $tag_array_val);
574 }
575 }
576
577 $posts[]=array(
578 'post_id'=>$post_info->ID,
579 'post_title'=>$post_info->post_title,
580 'post_name'=>$post_info->post_name,
581 'post_author'=>array('author_id'=>$post_info->post_author, 'author_name'=>$user_info[$post_info->post_author]),
582 'post_date'=>$post_info->post_date,
583 'post_modified'=>$post_info->post_modified,
584 'post_status'=>$post_info->post_status,
585 'post_type'=>$post_info->post_type,
586 'guid'=>$post_info->guid,
587 'post_password'=>$post_info->post_password,
588 'ping_status'=>$post_info->ping_status,
589 'comment_status'=>$post_info->comment_status,
590 'comment_count'=>$post_info->comment_count,
591 'cats'=>$cats,
592 'tags'=>$tags,
593
594 );
595 }
596
597 return array('posts' => $posts, 'total' => $total);
598 }
599
600 function delete_post($args){
601 global $wpdb;
602 if(!empty($args['post_id']) && !empty($args['action']))
603 {
604 $post_id = (int)$args['post_id'];
605 $delete_query = false;
606 if($args['action']=='delete')
607 {
608 $delete_query = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = %d", $post_id);
609 }
610 else if($args['action']=='delete_perm'){
611 $delete_query = $wpdb->prepare("DELETE FROM $wpdb->posts WHERE ID = %d", $post_id);
612 }
613 else if($args['action']=='delete_restore'){
614 $delete_query = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = %d", $post_id);
615 }
616 if(!empty($delete_query)){
617 $wpdb->query($delete_query);
618 }
619
620 return 'Post deleted.';
621 }
622 else
623 {
624 return 'No ID...';
625 }
626 }
627
628 function delete_posts($args){
629 global $wpdb;
630 extract($args);
631 $sql_format = '';
632 if(isset($deleteaction) && $deleteaction=='delete'){
633 $sql_format = "DELETE FROM $wpdb->posts WHERE ID = %d";
634 }elseif(isset($deleteaction) && $deleteaction=='trash'){
635 $sql_format = "UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = %d";
636 }elseif(isset($deleteaction) && $deleteaction=='draft'){
637 $sql_format = "UPDATE $wpdb->posts SET post_status = 'draft' WHERE ID = %d";
638 }elseif(isset($deleteaction) && $deleteaction=='publish'){
639 $sql_format = "UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = %d";
640 }
641 if(!empty($sql_format)){
642 foreach($args as $key=>$val){
643
644 if(!empty($val) && is_numeric($val))
645 {
646 $delete_query = $wpdb->prepare($sql_format, (int)$val);
647
648 $wpdb->query($delete_query);
649 }
650 }
651 }
652 return "Post deleted";
653
654 }
655
656 /**
657 * Function which gets pages from client depending on arguments.
658 * If FROM and TO dates are provided and range, range has bigger priority to date FROM.
659 * This means if there are less pages between FROM and TO than range provided,
660 * this function omit date from and returns last range number pages to date TO.
661 *
662 * @param array $args arguments passed to function
663 * @arg string filter_pages search phrase for page titles
664 * @arg string iwp_get_pages_date_from date in format(Y-m-d H:i:s) when pages are publishes from
665 * @arg string iwp_get_pages_date_to date in format(Y-m-d H:i:s) when pages are publishes to
666 * @arg string iwp_get_pages_range range number of returned pages
667 * @arg string iwp_get_pages_publish on or off
668 * @arg string iwp_get_pages_pending on or off
669 * @arg string iwp_get_pages_private on or off
670 * @arg string iwp_get_pages_future on or off
671 * @arg string iwp_get_pages_draft on or off
672 * @arg string iwp_get_pages_trash on or off
673 * @return array pages related to args
674 */
675 function get_pages($args){
676 global $wpdb;
677
678 $where='';
679 extract($args);
680
681 if(!empty($filter_pages))
682 {
683
684 $where.=" AND post_title LIKE '%".esc_sql($filter_pages)."%'";
685
686 }
687 if(!empty($iwp_get_pages_date_from) && !empty($iwp_get_pages_date_to))
688 {
689 $where.=" AND post_date BETWEEN '".esc_sql($iwp_get_pages_date_from)."' AND '".esc_sql($iwp_get_pages_date_to)."'";
690
691
692 }
693 else if(!empty($iwp_get_pages_date_from) && empty($iwp_get_pages_date_to))
694 {
695 $where.=" AND post_date >= '".esc_sql($iwp_get_pages_date_from)."'";
696
697
698 }
699 else if(empty($iwp_get_pages_date_from) && !empty($iwp_get_pages_date_to))
700 {
701 $where.=" AND post_date <= '".esc_sql($iwp_get_pages_date_to)."'";
702 }
703
704 $post_array=array();
705 $post_statuses = array('publish', 'pending', 'private', 'future', 'draft', 'trash');
706 foreach ($args as $checkbox => $checkbox_val)
707 {
708 $v = str_replace('iwp_get_pages_', '', $checkbox);
709 if ($checkbox_val === 'on' && in_array($v, $post_statuses, true)) {
710 $post_array[] = $v;
711 }
712 }
713 if(!empty($post_array))
714 {
715 $placeholders = implode(',', array_fill(0, count($post_array), '%s'));
716 $where .= $wpdb->prepare(" AND post_status IN ($placeholders)", $post_array);
717 }
718 if (!isset($iwp_get_pages_range)) {
719 $iwp_get_pages_range = '';
720 }
721
722 $range = absint($iwp_get_pages_range);
723 $limit = $range ? $wpdb->prepare(' LIMIT %d', $range) : ' LIMIT 500';
724
725
726 $sql_query = "$wpdb->posts WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='page' ".$where.' ORDER BY post_date DESC';
727
728 $total = array();
729 $posts = array();
730 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query.$limit);
731 $user_info = $this->getUsersIDs();
732 $total['total_num']=count($posts_info);
733
734 if($iwp_get_pages_range && !empty($iwp_get_pages_date_from) && !empty($iwp_get_pages_date_to) && $total['total_num'] < $iwp_get_pages_range) {
735 $sql_query = "$wpdb->posts
736 WHERE post_status!='auto-draft' AND post_status!='inherit' AND post_type='post' AND post_date <= '".esc_sql($iwp_get_pages_date_to)."'
737 ORDER BY post_date DESC
738 " . $limit;
739
740
741
742
743 $posts_info = $wpdb->get_results("SELECT * FROM ".$sql_query);
744 $total = array();
745 $total['total_num']=count($posts_info);
746 }
747
748 foreach ( $posts_info as $post_info )
749 {
750
751 $posts[]=array(
752 'post_id'=>$post_info->ID,
753 'post_title'=>$post_info->post_title,
754 'post_name'=>$post_info->post_name,
755 'post_author'=>array('author_id'=>$post_info->post_author, 'author_name'=>$user_info[$post_info->post_author]),
756 'post_date'=>$post_info->post_date,
757 'post_modified'=>$post_info->post_modified,
758 'post_status'=>$post_info->post_status,
759 'post_type'=>$post_info->post_type,
760 'guid'=>$post_info->guid,
761 'post_password'=>$post_info->post_password,
762 'ping_status'=>$post_info->ping_status,
763 'comment_status'=>$post_info->comment_status,
764 'comment_count'=>$post_info->comment_count
765
766 );
767 }
768
769 return array('pages' => $posts, 'total' => $total);
770 }
771
772 function delete_page($args){
773 global $wpdb;
774 if(!empty($args['post_id']) && !empty($args['action']))
775 {
776 $post_id = (int)$args['post_id'];
777 $delete_query = false;
778 if($args['action']=='delete')
779 {
780 $delete_query = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'trash' WHERE ID = %d", $post_id);
781 }
782 else if($args['action']=='delete_perm'){
783 $delete_query = $wpdb->prepare("DELETE FROM $wpdb->posts WHERE ID = %d", $post_id);
784 }
785 else if($args['action']=='delete_restore'){
786 $delete_query = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'publish' WHERE ID = %d", $post_id);
787 }
788 if(!empty($delete_query)){
789 $wpdb->query($delete_query);
790 }
791
792 return 'Page deleted.';
793 }
794 else
795 {
796 return 'No ID...';
797 }
798 }
799
800 function getPostCats($taxonomy = 'category')
801 {
802 global $wpdb;
803
804 $cats = $wpdb->get_results("SELECT p.ID AS post_id, $wpdb->terms.name
805 FROM $wpdb->posts AS p
806 INNER JOIN $wpdb->term_relationships ON ( p.ID = $wpdb->term_relationships.object_id )
807 INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
808 AND $wpdb->term_taxonomy.taxonomy = '".$taxonomy."' )
809 INNER JOIN $wpdb->terms ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id )");
810 $post_cats = array();
811 foreach ( $cats as $post_val )
812 {
813
814 $post_cats[$post_val->post_id][] = $post_val->name;
815 }
816
817 return $post_cats;
818 }
819
820 function getUsersIDs()
821 {
822 global $wpdb;
823 $users_authors=array();
824 $users = $wpdb->get_results("SELECT ID as user_id, display_name FROM $wpdb->users WHERE user_status=0");
825
826 foreach ( $users as $user_key=>$user_val )
827 {
828 $users_authors[$user_val->user_id] = $user_val->display_name;
829 }
830
831 return $users_authors;
832 }
833 }
834 ?>