Advertisement
Guest User

Untitled

a guest
Jan 11th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <?php
  2. /* * *
  3. * Processed form data into a proper post array, uses wp_insert_post() to add post.
  4. *
  5. * @param array $pfs_data POSTed array of data from the form
  6. */
  7. require('../../../wp-load.php');
  8.  
  9. /**
  10. * Create post from form data, including uploading images
  11. * @param array $post
  12. * @param array $files
  13. * @return string success or error message.
  14. */
  15. function pfs_submit($post,$files){
  16. $pfs_options = get_option('pfs_options');
  17. $pfs_data = $post;
  18. $pfs_files = $files;
  19. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($pfs_data, true)."</pre>\n";
  20. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($pfs_files, true)."</pre>\n";
  21.  
  22. foreach($pfs_data as $key=>$value) ${$key} = $value;
  23. $imgAllowed = 0;
  24. $result = Array(
  25. 'image'=>"",
  26. 'error'=>"",
  27. 'success'=>"",
  28. 'post'=>""
  29. );
  30. $success = False;
  31. if (is_user_logged_in()) {
  32. /* play with the image */
  33. switch (True) {
  34. case (1 < count($pfs_files['image']['name'])):
  35. // multiple file upload
  36. $result['image'] = "multiple";
  37. $file = $pfs_files['image'];
  38. for ( $i = 0; $i < count($file['tmp_name']); $i++ ){
  39. if( ''!=$file['tmp_name'][$i] ){
  40. $imgAllowed = (getimagesize($file['tmp_name'][$i])) ? True : (''==$file['name'][$i]);
  41. if ($imgAllowed){
  42. $j=$i+1;
  43. $upload[$j] = wp_upload_bits($pfs_files["image"]["name"][$i], null, file_get_contents($pfs_files["image"]["tmp_name"][$i]));
  44. if (False === $upload[$j]['error']){
  45. $success[$j] = True;
  46. } else {
  47. $result['error'] = "There was an error uploading the image $j {$upload['error']}";
  48. return $result;
  49. }
  50. } else {
  51. $result['error'] = "Incorrect filetype. Only images (.gif, .png, .jpg, .jpeg) are allowed.";
  52. }
  53. }
  54. }
  55. break;
  56. case ((1 == count($pfs_files['image']['name'])) && ('' != $pfs_files['image']['name'][0]) ):
  57. // single file upload
  58. $file = $pfs_files['image'];
  59. $result['image'] = 'single';
  60. $imgAllowed = (getimagesize($file['tmp_name'][0])) ? True : (''==$file['name'][0]);
  61. if ($imgAllowed){
  62. $upload[1] = wp_upload_bits($file["name"][0], null, file_get_contents($file["tmp_name"][0]));
  63. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($upload, true)."</pre>\n";
  64. if (False === $upload[1]['error']){
  65. $success[1] = True;
  66. } else {
  67. $result['error'] = "There was an error uploading the image: {$upload[1]['error']}";
  68. return $result;
  69. }
  70. } else {
  71. $result['error'] = "Incorrect filetype. Only images (.gif, .png, .jpg, .jpeg) are allowed.";
  72. }
  73. break;
  74. default:
  75. $result['image'] = 'none';
  76. }
  77. if ('' != $result['error']) return $result;
  78. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($upload, true)."</pre>\n";
  79. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($success, true)."</pre>\n";
  80. /* manipulate $pfs_data into proper post array */
  81. if ($title != '' && $postcontent != '') {
  82. $content = $postcontent;
  83. global $user_ID;
  84. get_currentuserinfo();
  85. if (is_array($success)){
  86. foreach(array_keys($success) as $i){
  87. //$i++;
  88. $imgtag = "[!--image$i--]";
  89. if (False === strpos($content,$imgtag)) $content .= "<br />$imgtag";
  90. $content = str_replace($imgtag, "<img src='{$upload[$i]['url']}' class='pfs-image' />", $content);
  91. }
  92. } else {
  93. $imgtag = "[!--image1--]";
  94. if (False === strpos($content,$imgtag)) $content .= "<br />$imgtag";
  95. $content = str_replace($imgtag, "<img src='{$upload[1]['url']}' class='pfs-image' />", $content);
  96. }
  97. //if any [!--image#--] tags remain, they are invalid and should just be deleted.
  98. $content = preg_replace('/\[\!--image\d*--\]/','',$content);
  99. $categories = $cats;
  100. $newcats = explode(',',$newcats);
  101. foreach ($newcats as $cat) $categories[] = wp_insert_category(array('cat_name' => trim($cat), 'category_parent' => 0));
  102. $newtags = explode(',',$newtags);
  103. foreach ($newtags as $tag) {
  104. wp_create_tag(trim($tag));
  105. $tags[] = trim($tag);
  106. }
  107. $postarr = array();
  108. $postarr['post_title'] = $title;
  109. $postarr['post_content'] = $content;
  110. $postarr['comment_status'] = $pfs_options['pfs_comment_status'];
  111. $postarr['post_status'] = $pfs_options['pfs_post_status'];
  112. $postarr['post_author'] = $user_ID;
  113. $postarr['post_category'] = array(5);
  114. $postarr['tags_input'] = implode(',',$tags);
  115. $postarr['post_type'] = 'post';
  116. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($postarr, true)."</pre>\n";
  117. $post_id = wp_insert_post($postarr);
  118.  
  119.  
  120. } else {
  121. $result['error'] = __("You've left either the title or content empty.",'pfs_domain');
  122. }
  123. } else {
  124. /* TODO: translate following */
  125. $result['error'] = "You are no longer logged in. Did something happen? Try <a href='".get_bloginfo('url')."/wp-login.php'>logging in</a> again.";
  126. }
  127. return wp_redirect(get_permalink($post_id));
  128. }
  129.  
  130. if (!empty($_POST)){
  131. $pfs = pfs_submit($_POST,$_FILES);
  132. echo json_encode($pfs);
  133. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($pfs, true)."</pre>\n";
  134. } else {
  135. /* TODO: translate following */
  136. echo "You should not be seeing this page, something went wrong. <a href='".get_bloginfo('url')."'>Go home</a>?";
  137. }
  138.  
  139. //get_footer();
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement