dragunoff

[WP] user-submitted-posts.php (User Submitted Posts)

Aug 23rd, 2011
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.02 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: User Submitted Posts
  4. Author: Jeff Starr @ Perishable Press
  5. Author URI: http://perishablepress.com/
  6. Plugin URI: http://perishablepress.com/user-submitted-posts/
  7. Description: The User Submitted Posts plugin enables your visitors to submit posts from anywhere on your site.
  8. Version: 1.0
  9.  
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  23.  
  24. */
  25.  
  26. if (!class_exists('Public_Submission_Form')) {
  27.     class Public_Submission_Form {
  28.         var $version = '1.0';
  29.         var $_post_meta_IsSubmission = 'is_submission';
  30.         var $_post_meta_Submitter    = 'user_submit_name';
  31.         var $_post_meta_SubmitterUrl = 'user_submit_url';
  32.         var $_post_meta_SubmitterEmail = 'user_submit_email'; // email
  33.         var $_post_meta_SubmitterhowToApply = 'user_submit_howto'; // howto
  34.         var $_post_meta_SubmitterIp  = 'user_submit_ip';
  35.         var $_post_meta_Image        = 'user_submit_image';
  36.         var $_post_meta_ImageInfo    = 'user_submit_image_info';
  37.         var $settings = null;
  38.        
  39.         function Public_Submission_Form() {
  40.             register_activation_hook(__FILE__, array(&$this, 'saveDefaultSettings'));
  41.             add_action('admin_init', array(&$this, 'checkForSettingsSave'));
  42.             add_action('admin_menu', array(&$this, 'addAdministrativeElements'));
  43.             // add_action('init', array(&$this, 'enqueueResources'));
  44.             add_action('parse_request', array(&$this, 'checkForPublicSubmission'));
  45.             add_action('parse_query', array(&$this, 'addSubmittedStatusClause'));
  46.             add_action('restrict_manage_posts', array(&$this, 'outputUserSubmissionLink'));
  47.             add_filter('the_author', array(&$this, 'replaceAuthor'));
  48.             // add_filter('the_author_link', array(&$this, 'replaceAuthorLink'));
  49.             add_filter('the_author', array(&$this, 'replaceAuthorLink'));
  50.             add_filter('post_stati', array(&$this, 'addNewPostStatus'));
  51.             add_shortcode('user-submitted-posts', array(&$this, 'getPublicSubmissionForm'));
  52.         }
  53.         function addAdministrativeElements() {
  54.             add_options_page(__('User Submitted Posts'), __('User Submitted Posts'), 'manage_options', 'user-submitted-posts', array(&$this, 'displaySettingsPage'));
  55.         }
  56.         function addNewPostStatus($postStati) {
  57.             $postStati['submitted'] = array(__('Submitted'), __('User submitted posts'), _n_noop('Submitted', 'Submitted'));
  58.             return $postStati;
  59.         }
  60.         function addSubmittedStatusClause($wp_query) {
  61.             global $pagenow;
  62.             if (is_admin() && $pagenow == 'edit.php' && $_GET['user_submitted'] == '1') {
  63.                 set_query_var('meta_key', $this->_post_meta_IsSubmission);
  64.                 set_query_var('meta_value', 1);
  65.                 set_query_var('post_status', 'pending');
  66.             }
  67.         }
  68.         function checkForPublicSubmission() {
  69.             if (isset($_POST['user-submitted-post']) && ! empty($_POST['user-submitted-post'])) {
  70.                 $settings = $this->getSettings();
  71.                 $title = stripslashes($_POST['user-submitted-title']);
  72.                 $content = stripslashes($_POST['user-submitted-content']);
  73.                 $authorName = stripslashes($_POST['user-submitted-name']);
  74.                 $authorUrl = stripslashes($_POST['user-submitted-url']);
  75.                 $authorEmail = stripslashes($_POST['user-submitted-email']); // email
  76.                 $howToApply = stripslashes($_POST['user-submitted-howto']); // howto
  77.                 $tags = stripslashes($_POST['user-submitted-tags']);
  78.                 $category = intval($_POST['user-submitted-category']);
  79.                 $jobtype = intval($_POST['user-submitted-jobtype']); // jobtype
  80.                 $locations = stripslashes($_POST['user-submitted-locations']); // locations
  81.                 $fileData = $_FILES['user-submitted-image'];
  82.                 $publicSubmission = $this->createPublicSubmission($title, $content, $authorName, $authorUrl, $authorEmail, $howToApply, $tags, $category, $jobtype, $locations, $fileData);
  83.  
  84.                 if (false == ($publicSubmission)) {
  85.                     $errorMessage = empty($settings['error-message']) ? __('An error occurred.  Please go back and try again.') : $settings['error-message'];
  86.                     if( !empty( $_POST[ 'redirect-override' ] ) ) {
  87.                         $redirect = stripslashes( $_POST[ 'redirect-override' ] );
  88.                         $redirect = add_query_arg( array( 'submission-error' => '1' ), $redirect );
  89.                         wp_redirect( $redirect );
  90.                         exit();
  91.                     }
  92.                     wp_die($errorMessage);
  93.                 } else {
  94.                     $redirect = empty($settings['redirect-url']) ? $_SERVER['REQUEST_URI'] : $settings['redirect-url'];
  95.                     if (! empty($_POST['redirect-override'])) {
  96.                         $redirect = stripslashes($_POST['redirect-override']);
  97.                     }
  98.                     $redirect = add_query_arg(array('success'=>1), $redirect);
  99.                     wp_redirect($redirect);
  100.                     exit();
  101.                 }
  102.             }
  103.         }
  104.         function checkForSettingsSave() {
  105.             if (isset($_POST['save-post-submission-settings']) && current_user_can('manage_options') && check_admin_referer('save-post-submission-settings')) {
  106.                 $settings = $this->getSettings();
  107.  
  108.                 $settings['author'] = get_userdata($_POST['author']) ? $_POST['author'] : $settings['author'];
  109.                 $settings['categories'] = is_array($_POST['categories']) && ! empty($_POST['categories']) ? array_unique($_POST['categories']) : array(get_option('default_category'));
  110.                 $settings['number-approved'] = is_numeric($_POST['number-approved']) ? intval($_POST['number-approved']) : - 1;
  111.  
  112.                 $settings['redirect-url'] = stripslashes($_POST['redirect-url']);
  113.                 $settings['error-message'] = stripslashes($_POST['error-message']);
  114.                
  115.                 $settings['min-images'] = is_numeric($_POST['min-images']) ? intval($_POST['min-images']) : $settings['max-images'];
  116.                 $settings['max-images'] = (is_numeric($_POST['max-images']) && ($settings['min-images'] <= $_POST['max-images'])) ? intval($_POST['max-images']) : $settings['max-images'];
  117.                
  118.                 $settings['min-image-height'] = is_numeric($_POST['min-image-height']) ? intval($_POST['min-image-height']) : $settings['min-image-height'];
  119.                 $settings['min-image-width'] = is_numeric($_POST['min-image-width']) ? intval($_POST['min-image-width']) : $settings['min-image-width'];
  120.                
  121.                 $settings['max-image-height'] = (is_numeric($_POST['max-image-height']) && ($settings['min-image-height'] <= $_POST['max-image-height'])) ? intval($_POST['max-image-height']) : $settings['max-image-height'];
  122.                 $settings['max-image-width'] = (is_numeric($_POST['max-image-width']) && ($settings['min-image-width'] <= $_POST['max-image-width'])) ? intval($_POST['max-image-width']) : $settings['max-image-width'];
  123.  
  124.                 $settings['usp_name'] = stripslashes($_POST['usp_name']);
  125.                 $settings['usp_url'] = stripslashes($_POST['usp_url']);
  126.                 $settings['usp_title'] = stripslashes($_POST['usp_title']);
  127.                 $settings['usp_tags'] = stripslashes($_POST['usp_tags']);
  128.                 $settings['usp_category'] = stripslashes($_POST['usp_category']);
  129.                 $settings['usp_content'] = stripslashes($_POST['usp_content']);
  130.                 $settings['usp_images'] = stripslashes($_POST['usp_images']);
  131.  
  132.                 $settings['upload-message'] = stripslashes($_POST['upload-message']);
  133.                 $settings['usp_form_width'] = stripslashes($_POST['usp_form_width']);
  134.  
  135.                 $this->saveSettings($settings);
  136.                 wp_redirect(admin_url('options-general.php?page=user-submitted-posts&updated=1'));
  137.             }
  138.         }
  139.         function displaySettingsPage() {
  140.             include ('views/settings.php');
  141.         }
  142.         function enqueueResources() {
  143.             wp_enqueue_script('usp_script', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/resources/user-submitted-posts.js', array('jquery'), $this->version);
  144.             wp_enqueue_style('usp_style', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/resources/user-submitted-posts.css', false, $this->version, 'screen');
  145.         }
  146.         function getPublicSubmissionForm($atts = array(), $content = null) {
  147.             if ($atts === true) {
  148.                 $redirect = $this->currentPageURL();
  149.             }
  150.             ob_start();
  151.             include (WP_PLUGIN_DIR.'/'.basename(dirname(__FILE__)).'/views/submission-form.php');
  152.             return ob_get_clean();
  153.         }
  154.         function outputUserSubmissionLink() {
  155.             global $pagenow;
  156.             if ($pagenow == 'edit.php') {
  157.                 echo '<a id="usp_admin_filter_posts" class="button-secondary" href="'.admin_url('edit.php?post_status=pending&amp;user_submitted=1').'">'.__('User Submitted Posts').'</a>';
  158.             }
  159.         }
  160.         function replaceAuthor($author) {
  161.             global $post;
  162.             $isSubmission = get_post_meta($post->ID, $this->_post_meta_IsSubmission, true);
  163.             $submissionAuthor = get_post_meta($post->ID, $this->_post_meta_Submitter, true);
  164.             if ($isSubmission && ! empty($submissionAuthor)) {
  165.                 return $submissionAuthor;
  166.             } else {
  167.                 return $author;
  168.             }
  169.         }
  170.         function replaceAuthorLink($authorLink) {
  171.             global $post;
  172.             $isSubmission = get_post_meta($post->ID, $this->_post_meta_IsSubmission, true);
  173.             $submissionAuthor = get_post_meta($post->ID, $this->_post_meta_Submitter, true);
  174.             $submissionLink = get_post_meta($post->ID, $this->_post_meta_SubmitterUrl, true);
  175.             if ($isSubmission && ! empty($submissionAuthor)) {
  176.                 if ( empty($submissionLink)) {
  177.                     return $submissionAuthor;
  178.                 } else {
  179.                     return "<a href='{$submissionLink}'>{$submissionAuthor}</a>";
  180.                 }
  181.             } else {
  182.                 return $authorLink;
  183.             }
  184.         }
  185.         function saveDefaultSettings() {
  186.             $settings = $this->getSettings();
  187.             if ( empty($settings)) {
  188.                 $currentUser = wp_get_current_user();
  189.                
  190.                 $settings = array();
  191.                 $settings['author'] = $currentUser->ID;
  192.                 $settings['categories'] = array(get_option('default_category'));
  193.                 $settings['number-approved'] = -1;
  194.                
  195.                 $settings['redirect-url'] = ''; //site_url();
  196.                 $settings['error-message'] = __('There was an error. Please ensure that you have added a title, some content, and that you have uploaded only images.');
  197.                
  198.                 $settings['min-images'] = 0;
  199.                 $settings['max-images'] = 1;
  200.                
  201.                 $settings['min-image-height'] = 0;
  202.                 $settings['min-image-width'] = 0;
  203.                
  204.                 $settings['max-image-height'] = 500;
  205.                 $settings['max-image-width'] = 500;
  206.                
  207.                 $settings['usp_name'] = 'show';
  208.                 $settings['usp_url'] = 'show';
  209.                 $settings['usp_title'] = 'show';
  210.                 $settings['usp_tags'] = 'show';
  211.                 $settings['usp_category'] = 'show';
  212.                 $settings['usp_content'] = 'show';
  213.                 $settings['usp_images'] = 'hide';
  214.  
  215.                 $settings['upload-message'] = ''; // 'Please select your image(s) to upload:';
  216.                 $settings['usp_form_width'] = '300'; // in pixels
  217.  
  218.                 $this->saveSettings($settings);
  219.             }
  220.         }
  221.         function getSettings() {
  222.             if ($this->settings === null) {
  223.                 $defaults = array();
  224.                 $this->settings = get_option('User Submitted Posts Settings', array());
  225.             }
  226.             return $this->settings;
  227.         }
  228.         function saveSettings($settings) {
  229.             if (!is_array($settings)) {
  230.                 return;
  231.             }
  232.             $this->settings = $settings;
  233.             update_option('User Submitted Posts Settings', $this->settings);
  234.         }
  235.         function createPublicSubmission($title, $content, $authorName, $authorUrl, $authorEmail, $howToApply, $tags, $category, $jobtype, $locations, $fileData) {
  236.             $settings = $this->getSettings();
  237.             $authorName = strip_tags($authorName);
  238.             $authorUrl = strip_tags($authorUrl);
  239.             $authorEmail = strip_tags($authorEmail); // email
  240.             $howToApply = strip_tags($howToApply); // howto
  241.             $authorIp = $_SERVER['REMOTE_ADDR'];
  242.  
  243.             if (!$this->validateField($title)) {
  244.                 return false;
  245.             }
  246.             if (!$this->validateField($authorName)) {
  247.                 return false;
  248.             }
  249.             if (!$this->validateField($authorEmail)) {
  250.                 return false;
  251.             }
  252.             if (!$this->validateField($content)) {
  253.                 return false;
  254.             }
  255.             if (!$this->validateField($howToApply)) {
  256.                 return false;
  257.             }
  258.             if (!$this->validateTags($tags)) {
  259.                 return false;
  260.             }
  261.            
  262.             $postData = array();
  263.             $postData['post_title'] = $title;
  264.             $postData['post_content'] = $content;
  265.             $postData['post_status'] = 'pending';
  266.             $postData['author'] = $settings['author'];
  267.             $numberApproved = $settings['number-approved'];
  268.  
  269.             if ($numberApproved < 0) {} elseif ($numberApproved == 0) {
  270.                 $postData['post_status'] = 'publish';
  271.             } else {
  272.                 $posts = get_posts(array('post_status'=>'publish', 'meta_key'=>$this->_post_meta_Submitter, 'meta_value'=>$authorName));
  273.                 $counter = 0;
  274.                 foreach ($posts as $post) {
  275.                     $submitterUrl = get_post_meta($post->ID, $this->_post_meta_SubmitterUrl, true);
  276.                     $submitterIp = get_post_meta($post->ID, $this->_post_meta_SubmitterIp, true);
  277.                     if ($submitterUrl == $authorUrl && $submitterIp == $authorIp) {
  278.                         $counter++;
  279.                     }
  280.                 }
  281.                 if ($counter >= $numberApproved) {
  282.                     $postData['post_status'] = 'publish';
  283.                 }
  284.             }
  285.             $newPost = wp_insert_post($postData);
  286.  
  287.             if ($newPost) {
  288.                 wp_set_post_tags($newPost, $tags);
  289.                 wp_set_post_categories($newPost, array($category));
  290.                 wp_set_post_terms($newPost, array($jobtype), 'jobtype'); // jobtype
  291.                 wp_set_post_terms($newPost, array($locations), 'locations'); // locations
  292.            
  293.             if (!function_exists('media_handle_upload')) {
  294.                 require_once (ABSPATH.'/wp-admin/includes/media.php');
  295.                 require_once (ABSPATH.'/wp-admin/includes/file.php');
  296.                 require_once (ABSPATH.'/wp-admin/includes/image.php');
  297.             }
  298.             $attachmentIds = array();
  299.             $imageCounter = 0;
  300.             for ($i = 0; $i < count($fileData['name']); $i++) {
  301.                 $imageInfo = getimagesize($fileData['tmp_name'][$i]);
  302.                 if (false === $imageInfo || !$this->imageIsRightSize($imageInfo[0], $imageInfo[1])) {
  303.                     continue;
  304.                 }
  305.                 $key = "public-submission-attachment-{$i}";
  306.                 $_FILES[$key] = array();
  307.                 $_FILES[$key]['name'] = $fileData['name'][$i];
  308.                 $_FILES[$key]['tmp_name'] = $fileData['tmp_name'][$i];
  309.                 $_FILES[$key]['type'] = $fileData['type'][$i];
  310.                 $_FILES[$key]['error'] = $fileData['error'][$i];
  311.                 $_FILES[$key]['size'] = $fileData['size'][$i];
  312.                 $attachmentId = media_handle_upload($key, $newPost);
  313.  
  314.                 if (!is_wp_error($attachmentId) && wp_attachment_is_image($attachmentId)) {
  315.                     $attachmentIds[] = $attachmentId;
  316.                     add_post_meta($newPost, $this->_post_meta_Image, wp_get_attachment_url($attachmentId));
  317.                     $imageCounter++;
  318.                 } else {
  319.                     wp_delete_attachment($attachmentId);
  320.                 }
  321.                 if ($imageCounter == $settings['max-images']) {
  322.                     break;
  323.                 }
  324.             }
  325.             if (count($attachmentIds) < $settings['min-images']) {
  326.                 foreach ($attachmentIds as $idToDelete) {
  327.                     wp_delete_attachment($idToDelete);
  328.                 }
  329.                 wp_delete_post($newPost);
  330.                 return false;
  331.             }
  332.             update_post_meta($newPost, $this->_post_meta_IsSubmission, true);
  333.             update_post_meta($newPost, $this->_post_meta_Submitter, htmlspecialchars($authorName));
  334.             update_post_meta($newPost, $this->_post_meta_SubmitterUrl, htmlspecialchars($authorUrl));
  335.             update_post_meta($newPost, $this->_post_meta_SubmitterEmail, htmlspecialchars($authorEmail)); // email
  336.             update_post_meta($newPost, $this->_post_meta_SubmitterhowToApply, htmlspecialchars($howToApply)); // howto
  337.             update_post_meta($newPost, $this->_post_meta_SubmitterIp, $authorIp);
  338.         }
  339.         return $newPost;
  340.     }
  341.     function imageIsRightSize($width, $height) {
  342.         $settings = $this->getSettings();
  343.         $widthFits = ($width <= intval($settings['max-image-width'])) && ($width >= $settings['min-image-width']);
  344.         $heightFits = ($height <= $settings['max-image-height']) && ($height >= $settings['min-image-height']);
  345.         return $widthFits && $heightFits;
  346.         }
  347.         function validateTags($tags) {
  348.             return true;
  349.         }
  350.         function validateField($data) {
  351.             return ! empty($data);
  352.         }
  353.         function currentPageURL() {
  354.             $pageURL = 'http';
  355.             if ($_SERVER["HTTPS"] == "on") {
  356.                 $pageURL .= "s";
  357.             }
  358.             $pageURL .= "://";
  359.             if ($_SERVER["SERVER_PORT"] != "80") {
  360.                 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  361.             } else {
  362.                 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  363.             }
  364.             return $pageURL;
  365.         }
  366.     }
  367.     $publicSubmissionForm = new Public_Submission_Form();
  368.     include ('library/template-tags.php');
  369. }
  370. ?>
Advertisement
Add Comment
Please, Sign In to add comment