' . $usp_plugin . ' ' . __('requires WordPress 3.3 or higher, and has been deactivated!') . '
';
$msg .= __('Please return to the ') . '' . __('WordPress Admin area') . ' ' . __('to upgrade WordPress and try again.');
wp_die($msg);
}
}
}
// add new post status
add_filter ('post_stati', 'usp_addNewPostStatus');
function usp_addNewPostStatus($postStati) {
$postStati['submitted'] = array(__('Submitted'), __('User Submitted Posts'), _n_noop('Submitted', 'Submitted'));
return $postStati;
}
// add submitted status clause
add_action ('parse_query', 'usp_addSubmittedStatusClause');
function usp_addSubmittedStatusClause($wp_query) {
global $pagenow, $usp_post_meta_IsSubmission;
if (is_admin() && $pagenow == 'edit.php' && $_GET['user_submitted'] == '1') {
set_query_var('meta_key', $usp_post_meta_IsSubmission);
set_query_var('meta_value', 1);
set_query_var('post_status', 'pending');
}
}
// check for submitted post
add_action ('parse_request', 'usp_checkForPublicSubmission');
function usp_checkForPublicSubmission() {
global $usp_options;
if (isset($_POST['user-submitted-post']) && !empty($_POST['user-submitted-post'])) {
if ($usp_options['usp_title'] == 'show') {
$title = stripslashes($_POST['user-submitted-title']);
} else {
$title = 'User Submitted Post';
}
if (stripslashes($_POST['user-submitted-name']) && !empty($_POST['user-submitted-name'])) {
$author_submit = stripslashes($_POST['user-submitted-name']);
$author_info = get_user_by('login', $author_submit);
if ($author_info) {
$authorID = $author_info->id;
$authorName = $author_submit;
} else {
$authorID = $usp_options['author'];
$authorName = $author_submit;
}
} else {
$authorID = $usp_options['author'];
$authorName = get_the_author_meta('display_name', $authorID);
}
$authorUrl = stripslashes($_POST['user-submitted-url']);
$tags = stripslashes($_POST['user-submitted-tags']);
$captcha = stripslashes($_POST['user-submitted-captcha']);
$category = intval($_POST['user-submitted-category']);
$content = stripslashes($_POST['user-submitted-content']);
$fileData = $_FILES['user-submitted-image'];
$publicSubmission = usp_createPublicSubmission($title, $content, $authorName, $authorID, $authorUrl, $tags, $category, $fileData);
if (false == ($publicSubmission)) {
$errorMessage = empty($usp_options['error-message']) ? __('An error occurred. Please go back and try again.') : $usp_options['error-message'];
if(!empty($_POST['redirect-override'])) {
$redirect = stripslashes($_POST['redirect-override']);
$redirect = remove_query_arg('success', $redirect);
$redirect = add_query_arg(array('submission-error'=>'1'), $redirect);
wp_redirect($redirect);
exit();
} else {
$redirect = stripslashes($_SERVER["REQUEST_URI"]);
$redirect = remove_query_arg('success', $redirect);
$redirect = add_query_arg(array('submission-error'=>'1'), $redirect);
wp_redirect($redirect);
exit();
}
// wp_die($errorMessage);
} else {
$redirect = empty($usp_options['redirect-url']) ? $_SERVER['REQUEST_URI'] : $usp_options['redirect-url'];
if (!empty($_POST['redirect-override'])) {
$redirect = stripslashes($_POST['redirect-override']);
}
$redirect = remove_query_arg('submission-error', $redirect);
$redirect = add_query_arg(array('success'=>1), $redirect);
wp_redirect($redirect);
exit();
}
}
}
// enqueue script and style
add_action ('init', 'usp_enqueueResources');
function usp_enqueueResources() {
global $usp_options, $usp_version;
$display_url = $usp_options['usp_display_url'];
$current_url = trailingslashit('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$current_url = remove_query_arg('submission-error', $current_url);
$current_url = remove_query_arg('success', $current_url);
if (!is_admin()) {
// style
if ($display_url !== '') {
if ($display_url == $current_url) {
if ($usp_options['usp_form_version'] == 'classic') {
wp_enqueue_style ('usp_style', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp-classic.css', false, $usp_version, 'all');
} elseif ($usp_options['usp_form_version'] == 'current') {
wp_enqueue_style ('usp_style', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp.css', false, $usp_version, 'all');
} elseif ($usp_options['usp_form_version'] == 'disable') {}
}
} else {
if ($usp_options['usp_form_version'] == 'classic') {
wp_enqueue_style ('usp_style', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp-classic.css', false, $usp_version, 'all');
} elseif ($usp_options['usp_form_version'] == 'current') {
wp_enqueue_style ('usp_style', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp.css', false, $usp_version, 'all');
} elseif ($usp_options['usp_form_version'] == 'disable') {}
}
// script
if ($display_url !== '') {
if (($display_url == $current_url) && ($usp_options['usp_include_js'] == true)) {
wp_enqueue_script ('usp_script', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp.js', array('jquery'), $usp_version);
}
} else {
if ($usp_options['usp_include_js'] == true) {
wp_enqueue_script ('usp_script', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp.js', array('jquery'), $usp_version);
}
}
}
}
// add styles to admin Edit page
add_action('admin_print_styles', 'load_custom_admin_css');
function load_custom_admin_css() {
global $usp_version, $pagenow;
if (is_admin() && $pagenow == 'edit.php') {
wp_enqueue_style('usp_style_admin', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/resources/usp-admin.css', false, $usp_version, 'all');
}
}
// shortcode
add_shortcode ('user-submitted-posts', 'usp_display_form');
function usp_display_form($atts=array(), $content=null) {
global $usp_options;
if ($atts === true) {
$redirect = usp_currentPageURL();
}
if ($usp_options['usp_form_version'] == 'classic') {
ob_start();
include (WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)) . '/views/submission-form-classic.php');
return ob_get_clean();
} else {
ob_start();
include (WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)) . '/views/submission-form.php');
return ob_get_clean();
}
}
// template tag
function user_submitted_posts() {
echo usp_display_form();
}
// add usp link
add_action ('restrict_manage_posts', 'usp_outputUserSubmissionLink');
function usp_outputUserSubmissionLink() {
global $pagenow;
if ($pagenow == 'edit.php') {
echo '' . __('User Submitted Posts') . '';
}
}
// replace author
add_filter ('the_author', 'usp_replaceAuthor');
function usp_replaceAuthor($author) {
global $post, $usp_options, $usp_post_meta_IsSubmission, $usp_post_meta_Submitter;
$isSubmission = get_post_meta($post->ID, $usp_post_meta_IsSubmission, true);
$submissionAuthor = get_post_meta($post->ID, $usp_post_meta_Submitter, true);
if ($isSubmission && !empty($submissionAuthor)) {
return $submissionAuthor;
} else {
return $author;
}
}
// create the form
function usp_createPublicSubmission($title, $content, $authorName, $authorID, $authorUrl, $tags, $category, $fileData) {
global $usp_options, $usp_post_meta_IsSubmission, $usp_post_meta_SubmitterIp, $usp_post_meta_Submitter, $usp_post_meta_SubmitterUrl, $usp_post_meta_Image;
$authorName = strip_tags($authorName);
$authorUrl = strip_tags($authorUrl);
$authorIp = $_SERVER['REMOTE_ADDR'];
$captcha = stripslashes(trim($_POST['user-submitted-captcha']));
$verify = stripslashes(trim($_POST['user-submitted-verify']));
if (!usp_validateTitle($title)) {
return false;
}
if (!usp_validateTags($tags)) {
return false;
}
if (!empty($verify)) {
return false;
}
if ($usp_options['usp_captcha'] == 'show') {
if (!usp_spam_question($captcha)) {
return false;
}
}
$postData = array();
$postData['post_title'] = $title;
$postData['post_content'] = $content;
$postData['post_status'] = 'pending';
$postData['post_author'] = $authorID;
$numberApproved = $usp_options['number-approved'];
if ($numberApproved < 0) {} elseif ($numberApproved == 0) {
$postData['post_status'] = 'publish';
} else {
$posts = get_posts(array('post_status'=>'publish', 'meta_key'=>$usp_post_meta_Submitter, 'meta_value'=>$authorName));
$counter = 0;
foreach ($posts as $post) {
$submitterUrl = get_post_meta($post->ID, $usp_post_meta_SubmitterUrl, true);
$submitterIp = get_post_meta($post->ID, $usp_post_meta_SubmitterIp, true);
if ($submitterUrl == $authorUrl && $submitterIp == $authorIp) {
$counter++;
}
}
if ($counter >= $numberApproved) {
$postData['post_status'] = 'publish';
}
}
$newPost = wp_insert_post($postData);
if ($newPost) {
wp_set_post_tags($newPost, $tags);
wp_set_post_categories($newPost, array($category));
if ($usp_options['usp_email_alerts'] == true) {
$to = $usp_options['usp_email_address'];
if ($to !== '') {
$subject = 'New user-submitted post!';
$message = 'Hey, there is a new user-submitted post waiting for you.';
wp_mail($to, $subject, $message);
}
}
if (!function_exists('media_handle_upload')) {
require_once (ABSPATH . '/wp-admin/includes/media.php');
require_once (ABSPATH . '/wp-admin/includes/file.php');
require_once (ABSPATH . '/wp-admin/includes/image.php');
}
$attachmentIds = array();
$imageCounter = 0;
for ($i = 0; $i < count($fileData['name']); $i++) {
$imageInfo = @getimagesize($fileData['tmp_name'][$i]);
if (false === $imageInfo || !usp_imageIsRightSize($imageInfo[0], $imageInfo[1])) {
continue;
}
$key = "public-submission-attachment-{$i}";
$_FILES[$key] = array();
$_FILES[$key]['name'] = $fileData['name'][$i];
$_FILES[$key]['tmp_name'] = $fileData['tmp_name'][$i];
$_FILES[$key]['type'] = $fileData['type'][$i];
$_FILES[$key]['error'] = $fileData['error'][$i];
$_FILES[$key]['size'] = $fileData['size'][$i];
$attachmentId = media_handle_upload($key, $newPost);
if (!is_wp_error($attachmentId) && wp_attachment_is_image($attachmentId)) {
$attachmentIds[] = $attachmentId;
add_post_meta($newPost, $usp_post_meta_Image, wp_get_attachment_url($attachmentId));
$imageCounter++;
} else {
wp_delete_attachment($attachmentId);
}
if ($imageCounter == $usp_options['max-images']) {
break;
}
}
if (count($attachmentIds) < $usp_options['min-images']) {
foreach ($attachmentIds as $idToDelete) {
wp_delete_attachment($idToDelete);
}
wp_delete_post($newPost);
return false;
}
update_post_meta($newPost, $usp_post_meta_IsSubmission, true);
update_post_meta($newPost, $usp_post_meta_Submitter, htmlentities($authorName, ENT_QUOTES, 'UTF-8'));
update_post_meta($newPost, $usp_post_meta_SubmitterUrl, htmlentities($authorUrl));
update_post_meta($newPost, $usp_post_meta_SubmitterIp, htmlentities($authorIp));
}
return $newPost;
}
// validate stuff
function usp_imageIsRightSize($width, $height) {
global $usp_options;
$widthFits = ($width <= intval($usp_options['max-image-width'])) && ($width >= $usp_options['min-image-width']);
$heightFits = ($height <= $usp_options['max-image-height']) && ($height >= $usp_options['min-image-height']);
return $widthFits && $heightFits;
}
function usp_validateTags($tags) {
return true;
}
function usp_validateTitle($title) {
return !empty($title);
}
// challenge question
function usp_spam_question($input) {
global $usp_options;
$response = $usp_options['usp_response'];
$response = stripslashes(trim($response));
if ($usp_options['usp_casing'] == true) {
return (strtoupper($input) == strtoupper($response));
} else {
return ($input == $response);
}
}
// current url
function usp_currentPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
// display settings link on plugin page
add_filter ('plugin_action_links', 'usp_plugin_action_links', 10, 2);
function usp_plugin_action_links($links, $file) {
global $usp_path;
if ($file == $usp_path) {
$usp_links = '' . __('Settings') .'';
array_unshift($links, $usp_links);
}
return $links;
}
// delete plugin settings
function usp_delete_plugin_options() {
delete_option('usp_options');
}
if ($usp_options['default_options'] == 1) {
register_uninstall_hook (__FILE__, 'usp_delete_plugin_options');
}
// define default settings
register_activation_hook (__FILE__, 'usp_add_defaults');
function usp_add_defaults() {
$currentUser = wp_get_current_user();
$admin_mail = get_bloginfo('admin_email');
$tmp = get_option('usp_options');
if(($tmp['default_options'] == '1') || (!is_array($tmp))) {
$arr = array(
'default_options' => 0,
'author' => $currentUser->ID,
'categories' => array(get_option('default_category')),
'number-approved' => -1,
'redirect-url' => '',
'error-message' => __('There was an error. Please ensure that you have added a title, some content, and that you have uploaded only images.'),
'min-images' => 0,
'max-images' => 1,
'min-image-height' => 0,
'min-image-width' => 0,
'max-image-height' => 1500,
'max-image-width' => 1500,
'usp_name' => 'show',
'usp_url' => 'show',
'usp_title' => 'show',
'usp_tags' => 'show',
'usp_category' => 'show',
'usp_images' => 'hide',
'upload-message' => 'Please select your image(s) to upload.',
'usp_form_width' => '300', // in pixels (not used anywhere)
'usp_question' => '1 + 1 =',
'usp_response' => '2',
'usp_casing' => 0,
'usp_captcha' => 'show',
'usp_content' => 'show',
'success-message' => 'Success! Thank you for your submission.',
'usp_form_version' => 'current',
'usp_email_alerts' => 1,
'usp_email_address' => $admin_mail,
'usp_use_author' => 0,
'usp_use_url' => 0,
'usp_use_cat' => 0,
'usp_use_cat_id' => '',
'usp_include_js' => 1,
'usp_display_url' => '',
'usp_form_content' => '',
);
update_option('usp_options', $arr);
}
}
// define style options
$usp_form_version = array(
'classic' => array(
'value' => 'classic',
'label' => __('Classic form + styles')
),
'current' => array(
'value' => 'current',
'label' => __('HTML5 form + styles')
),
'disable' => array(
'value' => 'disable',
'label' => __('Disable stylesheet')
),
);
// whitelist settings
add_action ('admin_init', 'usp_init');
function usp_init() {
register_setting('usp_plugin_options', 'usp_options', 'usp_validate_options');
}
// sanitize and validate input
function usp_validate_options($input) {
global $usp_options, $usp_form_version;
if (!isset($input['default_options'])) $input['default_options'] = null;
$input['default_options'] = ($input['default_options'] == 1 ? 1 : 0);
$input['categories'] = is_array($input['categories']) && !empty($input['categories']) ? array_unique($input['categories']) : array(get_option('default_category'));
$input['number-approved'] = is_numeric($input['number-approved']) ? intval($input['number-approved']) : - 1;
$input['min-images'] = is_numeric($input['min-images']) ? intval($input['min-images']) : $input['max-images'];
$input['max-images'] = (is_numeric($input['max-images']) && ($usp_options['min-images'] <= abs($input['max-images']))) ? intval($input['max-images']) : $usp_options['max-images'];
$input['min-image-height'] = is_numeric($input['min-image-height']) ? intval($input['min-image-height']) : $usp_options['min-image-height'];
$input['min-image-width'] = is_numeric($input['min-image-width']) ? intval($input['min-image-width']) : $usp_options['min-image-width'];
$input['max-image-height'] = (is_numeric($input['max-image-height']) && ($usp_options['min-image-height'] <= $input['max-image-height'])) ? intval($input['max-image-height']) : $usp_options['max-image-height'];
$input['max-image-width'] = (is_numeric($input['max-image-width']) && ($usp_options['min-image-width'] <= $input['max-image-width'])) ? intval($input['max-image-width']) : $usp_options['max-image-width'];
$input['author'] = wp_filter_nohtml_kses($input['author']);
$input['usp_name'] = wp_filter_nohtml_kses($input['usp_name']);
$input['usp_url'] = wp_filter_nohtml_kses($input['usp_url']);
$input['usp_title'] = wp_filter_nohtml_kses($input['usp_title']);
$input['usp_tags'] = wp_filter_nohtml_kses($input['usp_tags']);
$input['usp_category'] = wp_filter_nohtml_kses($input['usp_category']);
$input['usp_images'] = wp_filter_nohtml_kses($input['usp_images']);
$input['usp_form_width'] = wp_filter_nohtml_kses($input['usp_form_width']);
$input['usp_question'] = wp_filter_nohtml_kses($input['usp_question']);
$input['usp_answer'] = wp_filter_nohtml_kses($input['usp_answer']);
$input['usp_captcha'] = wp_filter_nohtml_kses($input['usp_captcha']);
$input['usp_content'] = wp_filter_nohtml_kses($input['usp_content']);
$input['usp_email_address'] = wp_filter_nohtml_kses($input['usp_email_address']);
$input['usp_use_cat_id'] = wp_filter_nohtml_kses($input['usp_use_cat_id']);
$input['usp_display_url'] = wp_filter_nohtml_kses($input['usp_display_url']);
$input['redirect-url'] = wp_filter_nohtml_kses($input['redirect-url']);
// dealing with kses
global $allowedposttags;
$allowed_atts = array('align'=>array(), 'class'=>array(), 'type'=>array(), 'id'=>array(), 'dir'=>array(), 'lang'=>array(), 'style'=>array(), 'xml:lang'=>array(), 'src'=>array(), 'alt'=>array());
$allowedposttags['script'] = $allowed_atts;
$allowedposttags['strong'] = $allowed_atts;
$allowedposttags['small'] = $allowed_atts;
$allowedposttags['span'] = $allowed_atts;
$allowedposttags['abbr'] = $allowed_atts;
$allowedposttags['code'] = $allowed_atts;
$allowedposttags['div'] = $allowed_atts;
$allowedposttags['img'] = $allowed_atts;
$allowedposttags['h1'] = $allowed_atts;
$allowedposttags['h2'] = $allowed_atts;
$allowedposttags['h3'] = $allowed_atts;
$allowedposttags['h4'] = $allowed_atts;
$allowedposttags['h5'] = $allowed_atts;
$allowedposttags['ol'] = $allowed_atts;
$allowedposttags['ul'] = $allowed_atts;
$allowedposttags['li'] = $allowed_atts;
$allowedposttags['em'] = $allowed_atts;
$allowedposttags['p'] = $allowed_atts;
$allowedposttags['a'] = $allowed_atts;
$input['usp_form_content'] = wp_kses_post($input['usp_form_content'], $allowedposttags);
$input['error-message'] = wp_kses_post($input['error-message'], $allowedposttags);
$input['upload-message'] = wp_kses_post($input['upload-message'], $allowedposttags);
$input['success-message'] = wp_kses_post($input['success-message'], $allowedposttags);
if (!isset($input['usp_casing'])) $input['usp_casing'] = null;
$input['usp_casing'] = ($input['usp_casing'] == 1 ? 1 : 0);
if (!isset($input['usp_form_version'])) $input['usp_form_version'] = null;
if (!array_key_exists($input['usp_form_version'], $usp_form_version)) $input['usp_form_version'] = null;
if (!isset($input['usp_email_alerts'])) $input['usp_email_alerts'] = null;
$input['usp_email_alerts'] = ($input['usp_email_alerts'] == 1 ? 1 : 0);
if (!isset($input['usp_use_author'])) $input['usp_use_author'] = null;
$input['usp_use_author'] = ($input['usp_use_author'] == 1 ? 1 : 0);
if (!isset($input['usp_use_url'])) $input['usp_use_url'] = null;
$input['usp_use_url'] = ($input['usp_use_url'] == 1 ? 1 : 0);
if (!isset($input['usp_use_cat'])) $input['usp_use_cat'] = null;
$input['usp_use_cat'] = ($input['usp_use_cat'] == 1 ? 1 : 0);
if (!isset($input['usp_include_js'])) $input['usp_include_js'] = null;
$input['usp_include_js'] = ($input['usp_include_js'] == 1 ? 1 : 0);
return $input;
}
// add the options page
add_action ('admin_menu', 'usp_add_options_page');
function usp_add_options_page() {
global $usp_plugin;
add_options_page($usp_plugin, $usp_plugin, 'manage_options', __FILE__, 'usp_render_form');
}
// create the options page
function usp_render_form() {
global $usp_plugin, $usp_options, $usp_path, $usp_homeurl, $usp_version, $usp_logo, $usp_form_version; ?>