PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20230809
User Submitted Posts – Enable Users to Submit Posts from the Front End v20230809
20260810 20260608 20230806 20230809 20230811 20230901 20230902 20230914 20231102 20240319 20240516 20240703 20241026 20250327 20250329 20251121 20251210 20260110 20260113 20260207 20260217 20260407 20260422 trunk 20170326 All 58 releases
user-submitted-posts / user-submitted-posts.php

user-submitted-posts.php in User Submitted Posts – Enable Users to Submit Posts from the Front End 20230809, at user-submitted-posts.php

1,814 lines 47.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: User Submitted Posts
4 Plugin URI: https://perishablepress.com/user-submitted-posts/
5 Description: Enables your visitors to submit posts and images from anywhere on your site.
6 Tags: frontend post, submit post, guest post, visitor post, content restriction, public post, share post, user post, user submitted post, upload
7 Author: Jeff Starr
8 Author URI: https://plugin-planet.com/
9 Donate link: https://monzillamedia.com/donate.html
10 Contributors: specialk
11 Requires at least: 4.6
12 Tested up to: 6.3
13 Stable tag: 20230809
14 Version: 20230809
15 Requires PHP: 5.6.20
16 Text Domain: usp
17 Domain Path: /languages
18 License: GPL v2 or later
19 */
20
21 /*
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License
24 as published by the Free Software Foundation; either version
25 2 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 with this program. If not, visit: https://www.gnu.org/licenses/
34
35 Copyright 2023 Monzilla Media. All rights reserved.
36 */
37
38 if (!defined('ABSPATH')) die();
39
40 if (!defined('USP_WP_VERSION')) define('USP_WP_VERSION', '4.6');
41 if (!defined('USP_VERSION')) define('USP_VERSION', '20230809');
42 if (!defined('USP_PLUGIN')) define('USP_PLUGIN', esc_html__('User Submitted Posts', 'usp'));
43 if (!defined('USP_FILE')) define('USP_FILE', plugin_basename(__FILE__));
44 if (!defined('USP_PATH')) define('USP_PATH', plugin_dir_path(__FILE__));
45 if (!defined('USP_URL')) define('USP_URL', plugin_dir_url (__FILE__));
46
47 $usp_options = get_option('usp_options');
48
49 require_once('library/core-functions.php');
50 require_once('library/form-functions.php');
51 require_once('library/enqueue-scripts.php');
52 require_once('library/plugin-display.php');
53 require_once('library/plugin-settings.php');
54 require_once('library/shortcode-access.php');
55 require_once('library/shortcode-login.php');
56 require_once('library/shortcode-misc.php');
57 require_once('library/template-tags.php');
58
59 register_activation_hook(__FILE__, 'usp_add_defaults');
60 register_activation_hook(__FILE__, 'usp_dismiss_notice_activate');
61
62 if (isset($usp_options['default_options']) && $usp_options['default_options'] == 1) {
63
64 register_deactivation_hook(__FILE__, 'usp_delete_plugin_options');
65
66 }
67
68 //
69
70 function usp_i18n_init() {
71
72 $domain = 'usp';
73
74 $locale = apply_filters('usp_locale', get_locale(), $domain);
75
76 $dir = trailingslashit(WP_LANG_DIR);
77
78 $file = $domain .'-'. $locale .'.mo';
79
80 $path_1 = $dir . $file;
81
82 $path_2 = $dir . $domain .'/'. $file;
83
84 $path_3 = $dir .'plugins/'. $file;
85
86 $path_4 = $dir .'plugins/'. $domain .'/'. $file;
87
88 $paths = array($path_1, $path_2, $path_3, $path_4);
89
90 foreach ($paths as $path) {
91
92 if ($loaded = load_textdomain($domain, $path)) {
93
94 return $loaded;
95
96 } else {
97
98 return load_plugin_textdomain($domain, false, dirname(USP_FILE) .'/languages/');
99
100 }
101
102 }
103
104 }
105 add_action('init', 'usp_i18n_init');
106
107
108
109 function usp_require_wp_version() {
110
111 $wp_version = get_bloginfo('version');
112
113 if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
114
115 if (version_compare($wp_version, USP_WP_VERSION, '<')) {
116
117 if (is_plugin_active(USP_FILE)) {
118
119 deactivate_plugins(USP_FILE);
120
121 $msg = '<strong>'. USP_PLUGIN .'</strong> ';
122 $msg .= esc_html__('requires WordPress ', 'usp') . USP_WP_VERSION;
123 $msg .= esc_html__(' or higher, and has been deactivated! ', 'usp');
124 $msg .= esc_html__('Please return to the', 'usp') .' <a href="'. admin_url() .'">';
125 $msg .= esc_html__('WordPress Admin Area', 'usp') .'</a> ';
126 $msg .= esc_html__('to upgrade WordPress and try again.', 'usp');
127
128 wp_die($msg);
129
130 }
131
132 }
133
134 }
135
136 }
137 add_action('admin_init', 'usp_require_wp_version');
138
139
140
141 if (!current_theme_supports('post-thumbnails')) {
142
143 if (isset($usp_options['usp_featured_images']) && $usp_options['usp_featured_images']) {
144
145 add_theme_support('post-thumbnails');
146
147 }
148
149 }
150
151
152
153 if (isset($usp_options['enable_shortcodes']) && $usp_options['enable_shortcodes']) {
154
155 // add_filter('the_content', 'do_shortcode', 10);
156 add_filter('widget_text', 'do_shortcode', 10);
157
158 }
159
160
161
162 function usp_check_required($field) {
163
164 global $usp_options;
165
166 if ($usp_options[$field] === 'show') return true;
167
168 else return false;
169
170 }
171
172
173
174 function usp_get_date_time() {
175
176 $date_format = get_option('date_format');
177
178 $time_format = get_option('time_format');
179
180 if (function_exists('current_datetime')) {
181
182 $format = $date_format .' \@ '. $time_format;
183
184 $date = current_datetime()->format($format);
185
186 } else {
187
188 $date = date_i18n($date_format, current_time('timestamp')) .' \@ '. date_i18n($time_format, current_time('timestamp'));
189
190 }
191
192 return apply_filters('usp_date_time', $date);
193
194 }
195
196
197
198 function usp_get_default_title() {
199
200 $date = usp_get_date_time();
201
202 $title = esc_html__('User Submitted Post', 'usp');
203
204 $title = apply_filters('usp_default_title', $title, $date);
205
206 return $title;
207
208 }
209
210
211
212 function usp_get_submitted_title() {
213
214 global $usp_options;
215
216 $option = isset($usp_options['usp_title']) ? $usp_options['usp_title'] : null;
217
218 $title = usp_get_default_title();
219
220 $allow_tags = apply_filters('usp_title_tags_allow', false);
221 $allowed_tags = apply_filters('usp_title_tags_allowed', '<em><i><strong><b>');
222
223 if (isset($_POST['user-submitted-title'])) {
224
225 $title = $allow_tags ? strip_tags($_POST['user-submitted-title'], $allowed_tags) : sanitize_text_field($_POST['user-submitted-title']);
226
227 }
228
229 if ($option === 'optn' && empty($title)) $title = usp_get_default_title();
230
231 return $title;
232
233 }
234
235
236
237 function usp_get_custom_field() {
238
239 global $usp_options;
240
241 $name = isset($usp_options['custom_name']) ? $usp_options['custom_name'] : '';
242
243 $custom = isset($_POST[$name]) ? usp_sanitize_content($_POST[$name]) : '';
244
245 return $custom;
246
247 }
248
249
250
251 function usp_get_custom_field_2() {
252
253 global $usp_options;
254
255 $name = isset($usp_options['custom_name_2']) ? $usp_options['custom_name_2'] : '';
256
257 $custom = isset($_POST[$name]) ? usp_sanitize_content($_POST[$name]) : '';
258
259 return $custom;
260
261 }
262
263
264
265 function usp_get_custom_checkbox() {
266
267 global $usp_options;
268
269 $name = isset($usp_options['custom_checkbox_name']) ? $usp_options['custom_checkbox_name'] : '';
270
271 $custom = isset($_POST[$name]) ? usp_sanitize_content($_POST[$name]) : '';
272
273 return $custom;
274
275 }
276
277
278
279 function usp_get_comment_status() {
280
281 global $usp_options;
282
283 $post_type = isset($usp_options['usp_post_type']) ? $usp_options['usp_post_type'] : 'post';
284
285 $post_type = apply_filters('usp_post_type', $post_type);
286
287 $default = get_default_comment_status($post_type);
288
289 return isset($_POST['user-submitted-comments']) ? 'closed' : $default;
290
291 }
292
293
294
295 function usp_get_submitted_category() {
296
297 $category = isset($_POST['user-submitted-category']) ? $_POST['user-submitted-category'] : '';
298
299 if (is_array($category)) {
300
301 $cats = array();
302
303 foreach ($category as $cat) $cats[] = sanitize_text_field($cat);
304
305 } else {
306
307 if (strpos($category, ',') !== false) {
308
309 $cats = array_map('trim', explode(',', $category));
310
311 } else {
312
313 $cats = sanitize_text_field($category);
314
315 }
316
317 }
318
319 return $cats;
320
321 }
322
323
324
325 function usp_get_submitted_tags() {
326
327 $submitted_tags = isset($_POST['user-submitted-tags']) ? $_POST['user-submitted-tags'] : '';
328
329 $tags = array();
330
331 if (is_array($submitted_tags)) {
332
333 foreach ($submitted_tags as $tag) $tags[] = sanitize_text_field($tag);
334
335 } else {
336
337 if (strpos($submitted_tags, ',') !== false) {
338
339 $tag_array = array_map('trim', explode(',', $submitted_tags));
340
341 foreach ($tag_array as $tag) $tags[] = sanitize_text_field($tag);
342
343 } else {
344
345 $tags[] = sanitize_text_field($submitted_tags);
346
347 }
348
349 }
350
351 return $tags;
352
353 }
354
355
356
357 function usp_get_ip_address() {
358
359 if (isset($_SERVER)) {
360
361 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
362 $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
363
364 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
365 $ip_address = $_SERVER['HTTP_CLIENT_IP'];
366
367 } else {
368 $ip_address = $_SERVER['REMOTE_ADDR'];
369
370 }
371
372 } else {
373
374 if (getenv('HTTP_X_FORWARDED_FOR')) {
375 $ip_address = getenv('HTTP_X_FORWARDED_FOR');
376
377 } elseif (getenv('HTTP_CLIENT_IP')) {
378 $ip_address = getenv('HTTP_CLIENT_IP');
379
380 } else {
381 $ip_address = getenv('REMOTE_ADDR');
382
383 }
384
385 }
386
387 return sanitize_text_field($ip_address);
388
389 }
390
391
392
393 function usp_checkForPublicSubmission() {
394
395 global $usp_options;
396
397 $is_submitted = (isset($_POST['usp-nonce']) && wp_verify_nonce($_POST['usp-nonce'], 'usp-nonce')) ? true : false;
398
399 $is_allowed = apply_filters('usp_check_if_allowed', true);
400
401 if ($is_submitted && $is_allowed) {
402
403 $title = usp_get_submitted_title();
404
405 $ip = usp_get_ip_address();
406
407 $custom = usp_get_custom_field();
408
409 $custom_2 = usp_get_custom_field_2();
410
411 $checkbox = usp_get_custom_checkbox();
412
413 $comments = usp_get_comment_status();
414
415 $category = usp_get_submitted_category();
416
417 $tags = usp_get_submitted_tags();
418
419 $files = isset($_FILES['user-submitted-image']) ? $_FILES['user-submitted-image'] : array();
420
421 $author = isset($_POST['user-submitted-name']) ? sanitize_text_field($_POST['user-submitted-name']) : '';
422 $url = isset($_POST['user-submitted-url']) ? esc_url($_POST['user-submitted-url']) : '';
423 $email = isset($_POST['user-submitted-email']) ? sanitize_text_field($_POST['user-submitted-email']) : '';
424 $captcha = isset($_POST['user-submitted-captcha']) ? sanitize_text_field($_POST['user-submitted-captcha']) : '';
425 $verify = isset($_POST['user-submitted-verify']) ? sanitize_text_field($_POST['user-submitted-verify']) : '';
426 $content = isset($_POST['user-submitted-content']) ? usp_sanitize_content($_POST['user-submitted-content']) : '';
427
428 $result = usp_createPublicSubmission($title, $files, $ip, $author, $url, $email, $tags, $captcha, $verify, $content, $category, $custom, $custom_2, $checkbox, $comments);
429
430 $post_id = false;
431
432 if (isset($result['id'])) {
433
434 $post_id = $result['id'];
435
436 /* Polylang plugin */
437 if (function_exists('pll_set_post_language') && function_exists('pll_default_language')) {
438
439 $default_or_current = 'default';
440 $default_or_current = apply_filters('usp_pll_set_post_language', $default_or_current);
441
442 if ($default_or_current === 'default') {
443
444 pll_set_post_language($post_id, pll_default_language());
445
446 } else {
447
448 pll_set_post_language($post_id, pll_current_language());
449
450 }
451
452 }
453 /* Polylang plugin */
454
455 }
456
457 $error = false;
458
459 if (isset($result['error']) && !empty($result['error'])) $error = array_filter(array_unique($result['error']));
460
461 if ($error) {
462
463 $e = implode(',', $error);
464 $e = trim($e, ',');
465
466 } else {
467
468 $e = 'error';
469
470 }
471
472 if ($post_id) {
473
474 if (!empty($_POST['redirect-override'])) {
475
476 $redirect = $_POST['redirect-override'];
477
478 $redirect = remove_query_arg(array('usp-error'), $redirect);
479 $redirect = add_query_arg(array('usp_redirect' => '1', 'success' => 1, 'post_id' => $post_id), $redirect);
480
481 } else {
482
483 $redirect = $_SERVER['REQUEST_URI'];
484
485 $redirect = remove_query_arg(array('usp-error'), $redirect);
486 $redirect = add_query_arg(array('success' => 1, 'post_id' => $post_id), $redirect);
487
488 }
489
490 do_action('usp_submit_success', $redirect);
491
492 } else {
493
494 $redirect = $_SERVER['REQUEST_URI'];
495
496 $redirect = remove_query_arg(array('success', 'post_id', 'usp-error'), $redirect);
497 $redirect = add_query_arg(array('usp-error' => $e), $redirect);
498
499 do_action('usp_submit_error', $redirect);
500
501 }
502
503 wp_redirect(esc_url_raw($redirect));
504
505 exit();
506
507 }
508
509 }
510 add_action('parse_request', 'usp_checkForPublicSubmission', 1);
511
512
513
514 function usp_check_recaptcha_keys() {
515
516 global $usp_options;
517
518 $public = isset($usp_options['recaptcha_public']) ? $usp_options['recaptcha_public'] : '';
519 $private = isset($usp_options['recaptcha_private']) ? $usp_options['recaptcha_private'] : '';
520
521 if (empty($public) || empty($private)) return false;
522
523 return true;
524
525 }
526
527
528
529 function usp_verify_recaptcha() {
530
531 global $usp_options;
532
533 $private = isset($usp_options['recaptcha_private']) ? $usp_options['recaptcha_private'] : '';
534 $version = isset($usp_options['recaptcha_version']) ? $usp_options['recaptcha_version'] : 2;
535
536 if (!usp_check_recaptcha_keys()) return false;
537
538 if ($version == 3) {
539
540 $response = isset($_POST['recaptcha_response']) ? $_POST['recaptcha_response'] : null;
541
542 $recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='. $private .'&response='. $response);
543 $recaptcha = json_decode($recaptcha);
544
545 $score = apply_filters('usp_recaptcha_score', 0.5);
546
547 return (($recaptcha->success == true) && ($recaptcha->score >= $score)) ? true : false;
548
549 } else {
550
551 if (isset($_POST['g-recaptcha-response'])) return require_once(USP_PATH .'recaptcha/connect.php');
552
553 return false;
554
555 }
556
557 }
558
559
560
561 function usp_sanitize_content($content) {
562
563 $allowed_tags = wp_kses_allowed_html('post');
564
565 $allowed_tags['style'] = array('types' => array());
566
567 $allowed_tags = apply_filters('usp_content_allowed', $allowed_tags);
568
569 $patterns = array('/target="_blank"/i', "/target='_blank'/i");
570
571 $patterns = apply_filters('usp_content_patterns', $patterns);
572
573 $replacements = array('', '');
574
575 $replacements = apply_filters('usp_content_replacements', $replacements);
576
577 $content = wp_kses(stripslashes($content), $allowed_tags);
578
579 $content = preg_replace($patterns, $replacements, $content);
580
581 return $content;
582
583 }
584
585
586
587 function usp_add_meta_box() {
588
589 global $post;
590
591 if (usp_is_public_submission()) {
592
593 $screens = array('post', 'page');
594 $screens = apply_filters('usp_meta_box_post_types', $screens);
595
596 $name = get_post_meta($post->ID, 'user_submit_name', true);
597 $email = get_post_meta($post->ID, 'user_submit_email', true);
598 $url = get_post_meta($post->ID, 'user_submit_url', true);
599 $ip = get_post_meta($post->ID, 'user_submit_ip', true);
600
601 if (!empty($name) || !empty($email) || !empty($url) || !empty($ip)) {
602
603 foreach ($screens as $screen) {
604
605 add_meta_box('usp_section_id', esc_html__('User Submitted Post Info', 'usp'), 'usp_meta_box_callback', $screen, 'normal');
606
607 }
608
609 }
610
611 }
612
613 }
614 add_action('add_meta_boxes', 'usp_add_meta_box');
615
616
617
618 function usp_meta_box_callback($post) {
619
620 global $usp_options;
621
622 if (usp_is_public_submission()) {
623
624 wp_nonce_field('usp_meta_box_nonce', 'usp_meta_box_nonce');
625
626 $name = get_post_meta($post->ID, 'user_submit_name', true);
627 $email = get_post_meta($post->ID, 'user_submit_email', true);
628 $url = get_post_meta($post->ID, 'user_submit_url', true);
629 $ip = get_post_meta($post->ID, 'user_submit_ip', true);
630
631 if (!empty($name) || !empty($email) || !empty($url) || !empty($ip)) {
632
633 echo '<ul style="margin-left:24px;list-style:square outside;">';
634
635 if (!empty($name)) echo '<li>'. esc_html__('Submitter Name: ', 'usp') . $name .'</li>';
636 if (!empty($email)) echo '<li>'. esc_html__('Submitter Email: ', 'usp') . $email .'</li>';
637 if (!empty($url)) echo '<li>'. esc_html__('Submitter URL: ', 'usp') . $url .'</li>';
638 if (!empty($ip) && !$usp_options['disable_ip_tracking']) echo '<li>'. esc_html__('Submitter IP: ', 'usp') . $ip .'</li>';
639
640 echo '</ul>';
641
642 }
643
644 }
645
646 }
647
648
649
650 function usp_display_form() {
651
652 global $usp_options;
653
654 $default = USP_PATH .'views/submission-form.php';
655
656 $custom = get_stylesheet_directory() .'/usp/submission-form.php';
657
658 ob_start();
659
660 if ($usp_options['usp_form_version'] === 'custom' && file_exists($custom)) include($custom);
661
662 else include($default);
663
664 return apply_filters('usp_form_shortcode', ob_get_clean());
665
666 }
667 add_shortcode ('user-submitted-posts', 'usp_display_form');
668
669
670
671 function user_submitted_posts() {
672
673 echo usp_display_form();
674
675 }
676
677
678
679 function usp_outputUserSubmissionLink() {
680
681 global $pagenow, $usp_options;
682
683 $screen_post_type = usp_get_current_screen_post_type();
684
685 $post_type = isset($usp_options['usp_post_type']) ? $usp_options['usp_post_type'] : 'post';
686
687 $current = $screen_post_type ? $screen_post_type : 'post';
688
689 if ($pagenow === 'edit.php' && $post_type === $current) {
690
691 $link = '<a id="usp-admin-filter" class="button" ';
692 $link .= 'href="'. admin_url('edit.php?post_type='. $current .'&user_submitted=1') .'" ';
693 $link .= 'title="'. esc_attr__('Show USP Posts', 'usp') .'">';
694 $link .= esc_html__('USP', 'usp') .'</a>';
695
696 $link = apply_filters('usp_filter_posts_link', $link, $current);
697
698 echo $link;
699
700 }
701
702 }
703 add_action ('restrict_manage_posts', 'usp_outputUserSubmissionLink');
704
705
706
707 function usp_addSubmittedStatusClause($wp_query) {
708
709 global $pagenow;
710
711 if (is_admin() && $pagenow == 'edit.php' && isset($_GET['user_submitted'])) {
712
713 if ($_GET['user_submitted'] === '1') {
714
715 set_query_var('meta_key', 'is_submission');
716 set_query_var('meta_value', 1);
717
718 } elseif ($_GET['user_submitted'] === '0') {
719
720 $meta_query = array(
721 'meta_query' =>
722 array(
723 'key' => 'is_submission',
724 'compare' => 'NOT EXISTS',
725 'value' => '',
726 )
727 );
728
729 $wp_query->set('meta_query', $meta_query);
730
731 }
732
733 }
734
735 }
736 add_action ('parse_query', 'usp_addSubmittedStatusClause');
737
738
739
740 function usp_replaceAuthor($author) {
741
742 global $post, $usp_options;
743
744 if ($post && is_object($post) && property_exists($post, 'ID')) {
745
746 $disable = isset($usp_options['disable_author']) ? $usp_options['disable_author'] : false;
747
748 $isSubmission = get_post_meta($post->ID, 'is_submission', true);
749 $submissionAuthor = get_post_meta($post->ID, 'user_submit_name', true);
750
751 if (!$disable && $isSubmission && !empty($submissionAuthor)) $author = $submissionAuthor;
752
753 }
754
755 return apply_filters('usp_post_author', $author);
756
757 }
758 add_filter('the_author', 'usp_replaceAuthor');
759
760
761
762 function usp_get_author($author) {
763
764 global $usp_options;
765
766 $error = false;
767
768 $author_id = $usp_options['author'];
769
770 if (!empty($author)) {
771
772 if ($usp_options['usp_use_author']) {
773
774 $author_info = get_user_by('login', $author);
775
776 if ($author_info) {
777
778 $author_id = $author_info->ID;
779
780 $author = get_the_author_meta('display_name', $author_id);
781
782 }
783
784 }
785
786 } else {
787
788 if ($usp_options['usp_name'] == 'show') {
789
790 $error = 'required-name';
791
792 } else {
793
794 $author = get_the_author_meta('display_name', $author_id);
795
796 }
797
798 }
799
800 $author_data = array('author' => $author, 'author_id' => $author_id, 'error' => $error);
801
802 return $author_data;
803
804 }
805
806
807
808 if (!function_exists('exif_imagetype')) {
809
810 function exif_imagetype($filename) {
811
812 if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
813
814 return $type;
815
816 }
817
818 return false;
819
820 }
821
822 }
823
824
825
826 function usp_check_images($files, $newPost) {
827
828 global $usp_options;
829
830 $error = array(); $file_count = 0;
831
832 $name = isset($files['name']) ? array_filter($files['name']) : false;
833 $temp = isset($files['tmp_name']) ? array_filter($files['tmp_name']) : false;
834 $errr = isset($files['error']) ? array_filter($files['error']) : false;
835
836 if ($usp_options['usp_images'] == 'show') {
837
838 if (!empty($temp)) {
839
840 foreach ($temp as $key => $value) if (is_uploaded_file($value)) $file_count++;
841
842 }
843
844 if (!empty($errr)) {
845
846 foreach ($errr as $key => $value) {
847
848 if (!empty($name) && $value > 0) {
849
850 error_log('WP Plugin USP: File error message '. $value .'. Info @ https://bit.ly/2uTJc4D', 0);
851
852 $error[] = 'file-error';
853
854 }
855
856 }
857
858 }
859
860 if ($file_count < $usp_options['min-images']) $error[] = 'file-min';
861 if ($file_count > $usp_options['max-images']) $error[] = 'file-max';
862
863 for ($i = 0; $i < $file_count; $i++) {
864
865 $image = @getimagesize($temp[$i]);
866
867 if (false === $image) {
868
869 $error[] = 'file-type';
870
871 break;
872
873 } else {
874
875 if (isset($temp[$i]) && !exif_imagetype($temp[$i])) {
876
877 $error[] = 'file-type';
878
879 break;
880
881 }
882
883 if (isset($image[0]) && !usp_width_min($image[0])) {
884
885 $error[] = 'width-min';
886
887 break;
888
889 }
890
891 if (isset($image[0]) && !usp_width_max($image[0])) {
892
893 $error[] = 'width-max';
894
895 break;
896
897 }
898
899 if (isset($image[1]) && !usp_height_min($image[1])) {
900
901 $error[] = 'height-min';
902
903 break;
904
905 }
906
907 if (isset($image[1]) && !usp_height_max($image[1])) {
908
909 $error[] = 'height-max';
910
911 break;
912
913 }
914
915 if (isset($errr[$i]) && $errr[$i] > 0) {
916
917 error_log('WP Plugin USP: File error message '. $errr[$i] .'. Info @ https://bit.ly/2uTJc4D', 0);
918
919 $error[] = 'file-error';
920
921 break;
922
923 }
924
925 }
926
927 }
928
929 }
930
931 $file_data = array('error' => $error, 'file_count' => $file_count);
932
933 return $file_data;
934
935 }
936
937
938
939 function usp_prepare_post($title, $content, $author_id, $author, $ip) {
940
941 global $usp_options;
942
943 $postData = array();
944 $postData['post_title'] = $title;
945 $postData['post_content'] = $content;
946 $postData['post_author'] = $author_id;
947 $postData['post_status'] = apply_filters('usp_post_status', 'pending');
948 $postData['post_name'] = sanitize_title($title);
949
950 $postType = isset($usp_options['usp_post_type']) ? $usp_options['usp_post_type'] : 'post';
951
952 $postData['post_type'] = apply_filters('usp_post_type', $postType);
953
954 $numberApproved = $usp_options['number-approved'];
955
956 if ($numberApproved == 0) {
957
958 $postData['post_status'] = apply_filters('usp_post_publish', 'publish');
959
960 } elseif ($numberApproved == -1) {
961
962 $postData['post_status'] = apply_filters('usp_post_moderate', 'pending');
963
964 } elseif ($numberApproved == -2) {
965
966 $postData['post_status'] = apply_filters('usp_post_draft', 'draft');
967
968 } else {
969
970 $posts = get_posts(array('post_status' => 'publish', 'meta_key' => 'user_submit_name', 'meta_value' => $author));
971
972 $counter = 0;
973
974 foreach ($posts as $post) {
975
976 $submitterName = get_post_meta($post->ID, 'user_submit_name', true);
977 $submitterIp = get_post_meta($post->ID, 'user_submit_ip', true);
978
979 if ($submitterName == $author && $submitterIp == $ip) $counter++;
980
981 }
982
983 if ($counter >= $numberApproved) $postData['post_status'] = apply_filters('usp_post_approve', 'publish');
984
985 }
986
987 return apply_filters('usp_post_data', $postData);
988
989 }
990
991
992
993 function usp_check_duplicates($title) {
994
995 global $usp_options;
996
997 if ($usp_options['titles_unique']) {
998
999 $check_post = get_page_by_title($title, OBJECT, 'post');
1000
1001 if ($check_post && $check_post->ID) return false;
1002
1003 }
1004
1005 return true;
1006
1007 }
1008
1009
1010
1011 function usp_maybe_rotate($tmp_name, $file_local) {
1012
1013 $image_type = function_exists('exif_imagetype') ? exif_imagetype($tmp_name) : false;
1014
1015 if ($image_type === 2) {
1016
1017 $image_exif = function_exists('exif_read_data') ? @exif_read_data($tmp_name) : array(); // @ cuz PHP bug
1018
1019 if (isset($image_exif['Orientation']) && !empty($image_exif['Orientation'])) {
1020
1021 $src = imagecreatefromjpeg($tmp_name);
1022
1023 if ($src) {
1024
1025 switch ($image_exif['Orientation']) {
1026
1027 case 3: $image = imagerotate($src, 180, 0); break;
1028 case 6: $image = imagerotate($src, -90, 0); break;
1029 case 8: $image = imagerotate($src, 90, 0); break;
1030 default: $image = null; break;
1031 }
1032
1033 imagedestroy($src);
1034
1035 if ($image) {
1036
1037 ob_start();
1038 imagejpeg($image, null, 100);
1039 $file_local = ob_get_contents();
1040 ob_end_clean();
1041 imagedestroy($image);
1042
1043 }
1044 }
1045
1046 }
1047
1048 }
1049
1050 return $file_local;
1051
1052 }
1053
1054
1055
1056 function usp_random_string($length = 12) {
1057
1058 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1059
1060 $string = substr(str_shuffle($chars), 0, $length);
1061
1062 return $string;
1063
1064 }
1065
1066
1067
1068 function usp_unique_filename($file) {
1069
1070 $parts = pathinfo($file); // e.g., // /www/htdocs/inc/image.jpg
1071
1072 $dirname = isset($parts['dirname']) ? $parts['dirname'] : ''; // /www/htdocs/inc
1073 $basename = isset($parts['basename']) ? $parts['basename'] : ''; // image.jpg
1074 $extension = isset($parts['extension']) ? $parts['extension'] : ''; // jpg
1075 $filename = isset($parts['filename']) ? $parts['filename'] : ''; // image
1076
1077 $append = '-'. usp_random_string();
1078
1079 $file = $dirname .'/'. $filename . $append .'.'. $extension;
1080
1081 $file = apply_filters('usp_unique_filename', $file, $dirname, $basename, $extension, $filename);
1082
1083 return $file;
1084
1085 }
1086
1087
1088
1089 function usp_attach_images($post_id, $newPost, $files, $file_count) {
1090
1091 global $usp_options;
1092
1093 do_action('usp_files_before', $files);
1094
1095 $attach_ids = array();
1096
1097 if ($files && $file_count > 0) {
1098
1099 usp_include_deps();
1100
1101 for ($i = 0; $i < $file_count; $i++) {
1102
1103 if (isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])) {
1104
1105 $file_local = file_get_contents($files['tmp_name'][$i]);
1106
1107 $tmp_name = $files['tmp_name'][$i];
1108
1109 } else {
1110
1111 continue;
1112
1113 }
1114
1115 if (isset($files['name'][$i]) && !empty($files['name'][$i])) {
1116
1117 $append = ($file_count > 1) ? '-'. $i : '';
1118
1119 $file_name = sanitize_file_name(basename($files['name'][$i]));
1120
1121 $parts = pathinfo($file_name);
1122
1123 $ext = isset($parts['extension']) ? $parts['extension'] : null;
1124
1125 $append = apply_filters('usp_filename_append', $append, $file_name, $ext);
1126
1127 $filename = isset($parts['filename']) ? $parts['filename'] : usp_random_string();
1128
1129 $file_name = isset($parts['filename']) ? $parts['filename'] . $append .'.'. $ext : $file_name;
1130
1131 $file_name = apply_filters('usp_file_name', $file_name, $filename, $append, $ext);
1132
1133 } else {
1134
1135 continue;
1136
1137 }
1138
1139 $file_local = usp_maybe_rotate($tmp_name, $file_local);
1140
1141 $file_path = defined('USP_UPLOAD_DIR') ? USP_UPLOAD_DIR : '/';
1142
1143 $upload_dir = apply_filters('usp_upload_directory', wp_upload_dir());
1144
1145 $wp_filetype = wp_check_filetype($file_name, null);
1146
1147 if (wp_mkdir_p($upload_dir['path'])) {
1148
1149 $file = isset($upload_dir['path']) ? $upload_dir['path'] . $file_path . $file_name : null;
1150 $guid = isset($upload_dir['url']) ? $upload_dir['url'] . $file_path . $file_name : null;
1151
1152 } else {
1153
1154 $file = isset($upload_dir['basedir']) ? $upload_dir['basedir'] . $file_path . $file_name : null;
1155 $guid = isset($upload_dir['baseurl']) ? $upload_dir['baseurl'] . $file_path . $file_name : null;
1156
1157 }
1158
1159 $file = file_exists($file) ? usp_unique_filename($file) : $file;
1160
1161 if (stripos($ext, 'php') === false) $bytes = file_put_contents($file, $file_local);
1162
1163 $file_type = isset($wp_filetype['type']) ? $wp_filetype['type'] : null;
1164
1165 $params = apply_filters('wp_handle_upload', array('file' => $file, 'url' => $guid, 'type' => $file_type));
1166
1167 $file = isset($params['file']) ? $params['file'] : $file;
1168 $guid = isset($params['url']) ? $params['url'] : $guid;
1169 $file_type = isset($params['type']) ? $params['type'] : $file_type;
1170
1171 $attachment = array(
1172 'post_mime_type' => $file_type,
1173 'post_name' => $file_name,
1174 'post_title' => $file_name,
1175 'post_status' => 'inherit',
1176 'guid' => $guid,
1177 );
1178
1179 $attachment = apply_filters('usp_insert_attachment_data', $attachment);
1180
1181 $attach_id = wp_insert_attachment($attachment, $file, $post_id);
1182
1183 if (isset($usp_options['usp_featured_images']) && $usp_options['usp_featured_images']) {
1184
1185 if (!has_post_thumbnail($post_id)) set_post_thumbnail($post_id, $attach_id);
1186
1187 }
1188
1189 $attach_data = wp_generate_attachment_metadata($attach_id, $file);
1190
1191 wp_update_attachment_metadata($attach_id, $attach_data);
1192
1193 if (!is_wp_error($attach_id) && wp_attachment_is_image($attach_id)) {
1194
1195 $attach_ids[] = $attach_id;
1196
1197 add_post_meta($post_id, 'user_submit_image', wp_get_attachment_url($attach_id));
1198
1199 } else {
1200
1201 wp_delete_attachment($attach_id);
1202
1203 wp_delete_post($post_id, true);
1204
1205 $newPost['error'][] = 'file-upload';
1206
1207 unset($newPost['id']);
1208
1209 }
1210
1211 }
1212
1213 } else {
1214
1215 if (isset($usp_options['usp_featured_image_default']) && !empty($usp_options['usp_featured_image_default'])) {
1216
1217 $default_image = attachment_url_to_postid($usp_options['usp_featured_image_default']);
1218
1219 if (!empty($default_image) && isset($usp_options['usp_featured_images']) && $usp_options['usp_featured_images']) {
1220
1221 if (!has_post_thumbnail($post_id)) set_post_thumbnail($post_id, $default_image);
1222
1223 }
1224
1225 }
1226
1227 }
1228
1229 do_action('usp_files_after', $attach_ids);
1230
1231 return $newPost;
1232
1233 }
1234
1235
1236
1237 function usp_createPublicSubmission($title, $files, $ip, $author, $url, $email, $tags, $captcha, $verify, $content, $category, $custom, $custom_2, $checkbox, $comments) {
1238
1239 global $usp_options;
1240
1241 $newPost = array('id' => null, 'error' => array());
1242
1243 $author_data = usp_get_author($author);
1244 $author = $author_data['author'];
1245 $author_id = $author_data['author_id'];
1246
1247 if (isset($author_data['error']) && !empty($author_data['error'])) {
1248
1249 $newPost['error'][] = $author_data['error'];
1250
1251 }
1252
1253 $file_data = usp_check_images($files, $newPost);
1254 $file_count = $file_data['file_count'];
1255
1256 if (isset($file_data['error']) && !empty($file_data['error'])) {
1257
1258 $newPost['error'] = array_unique(array_merge($file_data['error'], $newPost['error']));
1259
1260 }
1261
1262 $tags = is_array($tags) ? array_filter($tags) : $tags;
1263 $category = is_array($category) ? array_filter($category) : $category;
1264
1265 if (isset($usp_options['usp_title']) && ($usp_options['usp_title'] == 'show') && empty($title)) $newPost['error'][] = 'required-title';
1266 if (isset($usp_options['usp_url']) && ($usp_options['usp_url'] == 'show') && empty($url)) $newPost['error'][] = 'required-url';
1267 if (isset($usp_options['usp_tags']) && ($usp_options['usp_tags'] == 'show') && empty($tags)) $newPost['error'][] = 'required-tags';
1268 if (isset($usp_options['usp_category']) && ($usp_options['usp_category'] == 'show') && empty($category)) $newPost['error'][] = 'required-category';
1269 if (isset($usp_options['usp_content']) && ($usp_options['usp_content'] == 'show') && empty($content)) $newPost['error'][] = 'required-content';
1270 if (isset($usp_options['custom_field']) && ($usp_options['custom_field'] == 'show') && empty($custom)) $newPost['error'][] = 'required-custom';
1271 if (isset($usp_options['custom_field_2']) && ($usp_options['custom_field_2'] == 'show') && empty($custom_2)) $newPost['error'][] = 'required-custom-2';
1272
1273 if (usp_check_recaptcha_keys()) {
1274
1275 if (isset($usp_options['usp_recaptcha']) && ($usp_options['usp_recaptcha'] == 'show') && !usp_verify_recaptcha()) $newPost['error'][] = 'required-recaptcha';
1276
1277 }
1278
1279 if (isset($usp_options['usp_captcha']) && ($usp_options['usp_captcha'] == 'show') && !usp_spamQuestion($captcha)) $newPost['error'][] = 'required-captcha';
1280
1281 if (isset($usp_options['usp_email']) && ($usp_options['usp_email'] == 'show')) {
1282
1283 $email = sanitize_email($email);
1284
1285 if (!usp_validateEmail($email)) $newPost['error'][] = 'required-email';
1286
1287 }
1288
1289 if (isset($usp_options['usp_email']) && ($usp_options['usp_email'] == 'optn') && !empty($email)) {
1290
1291 $email = sanitize_email($email);
1292
1293 if (!usp_validateEmail($email)) $newPost['error'][] = 'incorrect-email';
1294
1295 }
1296
1297 if (isset($usp_options['titles_unique']) && $usp_options['titles_unique'] && !usp_check_duplicates($title)) $newPost['error'][] = 'duplicate-title';
1298 if (!empty($verify)) $newPost['error'][] = 'spam-verify';
1299
1300 if (isset($usp_options['custom_checkbox']) && !empty($usp_options['custom_checkbox']) && empty($checkbox)) $newPost['error'][] = 'required-checkbox';
1301
1302 if (isset($newPost['error']) && !empty($newPost['error'])) {
1303
1304 foreach ($newPost['error'] as $e) {
1305
1306 if (!empty($e)) {
1307
1308 unset($newPost['id']);
1309
1310 return $newPost;
1311
1312 }
1313
1314 }
1315
1316 }
1317
1318 $postData = usp_prepare_post($title, $content, $author_id, $author, $ip);
1319
1320 $new_status = (isset($postData['post_status']) && !empty($postData['post_status'])) ? sanitize_text_field($postData['post_status']) : apply_filters('usp_post_status', 'pending');
1321 $postData['post_status'] = apply_filters('usp_post_status', 'pending');
1322
1323 do_action('usp_insert_before', $postData);
1324 $newPost['id'] = wp_insert_post($postData);
1325 do_action('usp_insert_after', $newPost);
1326
1327 $post_id = isset($newPost['id']) ? $newPost['id'] : null;
1328
1329 if ($post_id && !is_wp_error($post_id)) {
1330
1331 $post = get_post($post_id);
1332
1333 $post->post_status = $new_status;
1334
1335 $post->comment_status = $comments;
1336
1337 wp_update_post($post);
1338
1339 wp_set_post_tags($post_id, apply_filters('usp_filter_tags', $tags), apply_filters('usp_append_tags', false));
1340
1341 wp_set_post_categories($post_id, apply_filters('usp_filter_cats', $category), apply_filters('usp_append_cats', false));
1342
1343 $newPost = usp_attach_images($post_id, $newPost, $files, $file_count);
1344
1345 if (isset($newPost['error']) && empty($newPost['error'])) {
1346
1347 update_post_meta($post_id, 'is_submission', true);
1348 update_post_meta($post_id, 'usp-post-id', $post_id);
1349
1350 $custom_name = isset($usp_options['custom_name']) ? $usp_options['custom_name'] : 'usp_custom_field';
1351 $custom_name_2 = isset($usp_options['custom_name_2']) ? $usp_options['custom_name_2'] : 'usp_custom_field_2';
1352
1353 $checkbox_name = isset($usp_options['custom_checkbox_name']) ? $usp_options['custom_checkbox_name'] : 'usp_custom_checkbox';
1354
1355 if (!empty($custom)) update_post_meta($post_id, $custom_name, $custom);
1356 if (!empty($custom_2)) update_post_meta($post_id, $custom_name_2, $custom_2);
1357 if (!empty($checkbox)) update_post_meta($post_id, $checkbox_name, $checkbox);
1358 if (!empty($author)) update_post_meta($post_id, 'user_submit_name', $author);
1359 if (!empty($email)) update_post_meta($post_id, 'user_submit_email', $email);
1360 if (!empty($url)) update_post_meta($post_id, 'user_submit_url', $url);
1361
1362 if (!empty($ip) && !$usp_options['disable_ip_tracking']) update_post_meta($post_id, 'user_submit_ip', $ip);
1363
1364 usp_send_mail_alert($post_id, $title, $content, $author, $email, $url, $custom, $custom_2);
1365
1366 }
1367
1368 } else {
1369
1370 $newPost['error'][] = 'post-fail';
1371
1372 }
1373
1374 return apply_filters('usp_new_post', $newPost);
1375
1376 }
1377
1378
1379
1380 function usp_include_deps() {
1381
1382 if (!function_exists('media_handle_upload')) {
1383
1384 require_once (ABSPATH .'/wp-admin/includes/media.php');
1385 require_once (ABSPATH .'/wp-admin/includes/file.php');
1386 require_once (ABSPATH .'/wp-admin/includes/image.php');
1387
1388 }
1389
1390 }
1391
1392
1393
1394 function usp_width_min($width) {
1395
1396 global $usp_options;
1397
1398 if (intval($width) < intval($usp_options['min-image-width'])) return false;
1399
1400 else return true;
1401
1402 }
1403
1404
1405
1406 function usp_width_max($width) {
1407
1408 global $usp_options;
1409
1410 if (intval($width) > intval($usp_options['max-image-width'])) return false;
1411
1412 else return true;
1413
1414 }
1415
1416
1417
1418 function usp_height_min($height) {
1419
1420 global $usp_options;
1421
1422 if (intval($height) < intval($usp_options['min-image-height'])) return false;
1423
1424 else return true;
1425
1426 }
1427
1428
1429
1430 function usp_height_max($height) {
1431
1432 global $usp_options;
1433
1434 if (intval($height) > intval($usp_options['max-image-height'])) return false;
1435
1436 else return true;
1437
1438 }
1439
1440
1441
1442 function usp_validateEmail($email) {
1443
1444 if (!is_email($email)) return false;
1445
1446 $bad_stuff = array("\r", "\n", "mime-version", "content-type", "cc:", "to:");
1447
1448 foreach ($bad_stuff as $bad) {
1449
1450 if (strpos(strtolower($email), strtolower($bad)) !== false) {
1451
1452 return false;
1453
1454 }
1455
1456 }
1457
1458 return true;
1459
1460 }
1461
1462 function usp_send_mail_alert($post_id, $title, $content, $author, $email, $url, $custom, $custom_2) {
1463
1464 global $usp_options;
1465
1466 if (isset($usp_options['usp_email_alerts']) && $usp_options['usp_email_alerts']) {
1467
1468 $blog_url = get_bloginfo('url'); // %%blog_url%%
1469 $blog_name = get_bloginfo('name'); // %%blog_name%%
1470 $post_url = get_permalink($post_id); // %%post_url%%
1471 $admin_url = admin_url(); // %%admin_url%%
1472 $post_title = $title; // %%post_title%%
1473 $post_content = $content; // %%post_content%%
1474 $post_author = $author; // %%post_author%%
1475 $user_email = $email; // %%user_email%%
1476 $user_url = $url; // %%user_url%%
1477
1478 $edit_link = usp_remote_edit_post_link($post_id); // %%edit_link%%
1479 $delete_link = usp_remote_delete_post_link($post_id); // %%delete_link%%
1480
1481 $patterns = array();
1482
1483 $patterns[0] = "/%%blog_url%%/";
1484 $patterns[1] = "/%%blog_name%%/";
1485 $patterns[2] = "/%%post_url%%/";
1486 $patterns[3] = "/%%admin_url%%/";
1487 $patterns[4] = "/%%post_title%%/";
1488 $patterns[5] = "/%%post_content%%/";
1489 $patterns[6] = "/%%post_author%%/";
1490 $patterns[7] = "/%%user_email%%/";
1491 $patterns[8] = "/%%user_url%%/";
1492 $patterns[9] = "/%%edit_link%%/";
1493 $patterns[10] = "/%%custom_field%%/";
1494 $patterns[11] = "/%%custom_field_2%%/";
1495 $patterns[12] = "/%%delete_link%%/";
1496
1497 $replacements = array();
1498
1499 $replacements[0] = $blog_url;
1500 $replacements[1] = $blog_name;
1501 $replacements[2] = $post_url;
1502 $replacements[3] = $admin_url;
1503 $replacements[4] = $post_title;
1504 $replacements[5] = $post_content;
1505 $replacements[6] = $post_author;
1506 $replacements[7] = $user_email;
1507 $replacements[8] = $user_url;
1508 $replacements[9] = $edit_link;
1509 $replacements[10] = $custom;
1510 $replacements[11] = $custom_2;
1511 $replacements[12] = $delete_link;
1512
1513 //
1514
1515 $subject_default = $blog_name .': New user-submitted post!';
1516 $subject = (isset($usp_options['email_alert_subject']) && !empty($usp_options['email_alert_subject'])) ? $usp_options['email_alert_subject'] : $subject_default;
1517 $subject = preg_replace($patterns, $replacements, $subject);
1518 $subject = apply_filters('usp_mail_subject', $subject);
1519
1520 $message_default = 'Hello, there is a new user-submitted post:'. "\r\n\n" . 'Title: '. $post_title . "\r\n\n" .'Visit Admin Area: '. $admin_url;
1521 $message = (isset($usp_options['email_alert_message']) && !empty($usp_options['email_alert_message'])) ? $usp_options['email_alert_message'] : $message_default;
1522 $message = preg_replace($patterns, $replacements, $message);
1523 $message = apply_filters('usp_mail_message', $message);
1524
1525 $html = isset($usp_options['usp_email_html']) ? $usp_options['usp_email_html'] : false;
1526 $format = $html ? 'text/html' : 'text/plain';
1527
1528 //
1529
1530 $default = get_bloginfo('admin_email');
1531
1532 $to = (isset($usp_options['usp_email_address']) && !empty($usp_options['usp_email_address'])) ? $usp_options['usp_email_address'] : $default;
1533 $from = (isset($usp_options['usp_email_from']) && !empty($usp_options['usp_email_from'])) ? $usp_options['usp_email_from'] : $to;
1534
1535 $to = explode(',', $to);
1536 $from = explode(',', $from);
1537
1538 $address = array();
1539
1540 foreach ($to as $k => $v) $address[$k]['to'] = trim($v);
1541 foreach ($from as $k => $v) $address[$k]['from'] = trim($v);
1542
1543 if (!empty($address[0])) {
1544
1545 foreach ($address as $k => $v) {
1546
1547 $address_to = (isset($v['to']) && !empty($v['to'])) ? $v['to'] : $default;
1548 $address_from = (isset($v['from']) && !empty($v['from'])) ? $v['from'] : $default;
1549
1550 $headers = 'X-Mailer: User Submitted Posts'. "\n";
1551 $headers .= 'From: '. $blog_name .' <'. $address_from .'>'. "\n";
1552 $headers .= 'Reply-To: '. $blog_name .' <'. $address_from .'>'. "\n";
1553 $headers .= 'Content-Type: '. $format .'; charset='. get_option('blog_charset', 'UTF-8') . "\n";
1554
1555 wp_mail($address_to, $subject, $message, $headers);
1556
1557 }
1558
1559 }
1560
1561 }
1562
1563 }
1564
1565
1566
1567 // Thanks to Delete Post plugin @ https://wordpress.org/plugins/delete-post/
1568
1569 function usp_remote_delete_post() {
1570
1571 if (isset($_GET['delete_post']) && isset($_GET['nonce'])) {
1572
1573 if (wp_verify_nonce($_GET['nonce'], 'delete_post_'. $_GET['delete_post'])) {
1574
1575 $post_id = intval($_GET['delete_post']);
1576
1577 $post = get_post($post_id);
1578
1579 if ($post && get_current_user_id() === (int) $post->post_author) {
1580
1581 $force = apply_filters('usp_force_delete_post', true);
1582
1583 $result = wp_delete_post($post_id, $force);
1584
1585 $result = $result ? 'true' : 'false';
1586
1587 $url = add_query_arg('usp-delete-post', $result, trailingslashit(home_url()));
1588
1589 wp_redirect($url);
1590
1591 exit;
1592
1593 }
1594
1595 }
1596
1597 }
1598
1599 }
1600 add_action('init', 'usp_remote_delete_post');
1601
1602
1603
1604 function usp_remote_delete_post_link($post_id) {
1605
1606 return add_query_arg(array('delete_post' => $post_id, 'nonce' => wp_create_nonce('delete_post_'. $post_id)), trailingslashit(home_url()));
1607
1608 }
1609
1610
1611
1612 function usp_remote_edit_post_link($post_id) {
1613
1614 return admin_url('post.php?post='. $post_id .'&action=edit');
1615
1616 }
1617
1618
1619
1620 function usp_spamQuestion($input) {
1621
1622 global $usp_options;
1623
1624 $response = $usp_options['usp_response'];
1625
1626 $response = sanitize_text_field($response);
1627
1628 if ($usp_options['usp_casing'] == false) {
1629
1630 return (strtoupper($input) == strtoupper($response));
1631
1632 } else {
1633
1634 return ($input == $response);
1635
1636 }
1637
1638 }
1639
1640
1641
1642 function usp_error_message() {
1643
1644 global $usp_options;
1645
1646 $min = $usp_options['min-images'];
1647 $max = $usp_options['max-images'];
1648
1649 if ((int) $min > 1) $min = ' ('. $min . esc_html__(' files required', 'usp') .')';
1650 else $min = ' ('. $min . esc_html__(' file required', 'usp') .')';
1651
1652 if ((int) $max > 1) $max = ' (limit: '. $max . esc_html__(' files', 'usp') .')';
1653 else $max = ' (limit: '. $max . esc_html__(' file', 'usp') .')';
1654
1655 $min_width = ' ('. $usp_options['min-image-width'] . esc_html__(' pixels', 'usp') .')';
1656 $max_width = ' ('. $usp_options['max-image-width'] . esc_html__(' pixels', 'usp') .')';
1657 $min_height = ' ('. $usp_options['min-image-height'] . esc_html__(' pixels', 'usp') .')';
1658 $max_height = ' ('. $usp_options['max-image-height'] . esc_html__(' pixels', 'usp') .')';
1659
1660 $custom_label = isset($usp_options['custom_label']) ? $usp_options['custom_label'] : __('Custom Field 1', 'usp');
1661 $custom_label_2 = isset($usp_options['custom_label_2']) ? $usp_options['custom_label_2'] : __('Custom Field 2', 'usp');
1662
1663 $checkbox_label = isset($usp_options['custom_checkbox_err']) ? $usp_options['custom_checkbox_err'] : __('Custom checkbox required', 'usp');
1664
1665 if (!empty($usp_options['error-message'])) $general_error = $usp_options['error-message'];
1666 else $general_error = esc_html__('An error occurred. Please go back and try again.', 'usp');
1667
1668 if (isset($_GET['usp-error']) && !empty($_GET['usp-error'])) {
1669
1670 $error_string = sanitize_text_field($_GET['usp-error']);
1671 $error_array = explode(',', $error_string);
1672 $error = array();
1673
1674 foreach ($error_array as $e) {
1675
1676 if ($e == 'required-login') $error[] = esc_html__('User login required', 'usp');
1677 elseif ($e == 'required-name') $error[] = esc_html__('User name required', 'usp');
1678 elseif ($e == 'required-title') $error[] = esc_html__('Post title required', 'usp');
1679 elseif ($e == 'required-url') $error[] = esc_html__('User URL required', 'usp');
1680 elseif ($e == 'required-tags') $error[] = esc_html__('Post tags required', 'usp');
1681 elseif ($e == 'required-category') $error[] = esc_html__('Post category required', 'usp');
1682 elseif ($e == 'required-content') $error[] = esc_html__('Post content required', 'usp');
1683 elseif ($e == 'required-recaptcha') $error[] = esc_html__('Correct captcha required', 'usp');
1684 elseif ($e == 'required-captcha') $error[] = esc_html__('Correct captcha required', 'usp');
1685 elseif ($e == 'required-email') $error[] = esc_html__('User email required', 'usp');
1686 elseif ($e == 'incorrect-email') $error[] = esc_html__('Please check your email and try again', 'usp');
1687 elseif ($e == 'spam-verify') $error[] = esc_html__('Non-empty value for hidden field', 'usp');
1688 elseif ($e == 'file-min') $error[] = esc_html__('Minimum number of images not met', 'usp') . $min;
1689 elseif ($e == 'file-max') $error[] = esc_html__('Maximum number of images exceeded ', 'usp') . $max;
1690 elseif ($e == 'width-min') $error[] = esc_html__('Minimum image width not met', 'usp') . $min_width;
1691 elseif ($e == 'width-max') $error[] = esc_html__('Image width exceeds maximum', 'usp') . $max_width;
1692 elseif ($e == 'height-min') $error[] = esc_html__('Minimum image height not met', 'usp') . $min_height;
1693 elseif ($e == 'height-max') $error[] = esc_html__('Image height exceeds maximum', 'usp') . $max_height;
1694 elseif ($e == 'file-type') $error[] = esc_html__('File type not allowed (please upload images only)', 'usp');
1695 elseif ($e == 'required-custom') $error[] = esc_html($custom_label) . esc_html__(' required', 'usp');
1696 elseif ($e == 'required-custom-2') $error[] = esc_html($custom_label_2) . esc_html__(' required', 'usp');
1697 elseif ($e == 'required-checkbox') $error[] = esc_html($checkbox_label);
1698
1699 // general error for file uploads, check error log for description.
1700 // check server for proper values of memory_limit, max_execution_time, max_input_time, post_max_size, upload_max_filesize
1701 elseif ($e == 'file-error') $error[] = esc_html__('File not uploaded. Please check the file and try again.', 'usp');
1702
1703 // check permissions on /uploads/ directory, check error log for the following error:
1704 // PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /wp-includes/wp-db.php
1705 elseif ($e == 'file-upload') $error[] = esc_html__('The file(s) could not be uploaded', 'usp');
1706
1707 elseif ($e == 'post-fail') $error[] = esc_html__('Post not created. Please contact the site administrator for help.', 'usp');
1708 elseif ($e == 'duplicate-title') $error[] = esc_html__('Duplicate post title. Please try again.', 'usp');
1709
1710 elseif ($e == 'error') $error[] = $general_error;
1711
1712 }
1713
1714 $output = '';
1715
1716 foreach ($error as $e) {
1717
1718 $output .= "\t\t\t".'<div class="usp-error">'. esc_html__('Error: ', 'usp') . $e .'</div>'."\n";
1719
1720 }
1721
1722 $return = '<div id="usp-error-message">'."\n". $output ."\t\t".'</div>'."\n";
1723
1724 return apply_filters('usp_error_message', $return);
1725
1726 }
1727
1728 return false;
1729
1730 }
1731
1732
1733
1734 function usp_redirect_message($content = '') {
1735
1736 global $usp_options;
1737
1738 $url = (isset($usp_options['redirect-url']) && !empty($usp_options['redirect-url'])) ? true : false;
1739
1740 $enable = (!is_admin() && (isset($_GET['usp_redirect']) && $_GET['usp_redirect'] == '1')) ? true : false;
1741
1742 $referrer = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) ? esc_url($_SERVER['HTTP_REFERER']) : false;
1743
1744 $link = $referrer ? '<p id="usp-return-form"><a href="'. $referrer .'">'. esc_html__('Return to form', 'usp') .'</a></p>' : '';
1745
1746 $link = apply_filters('usp_return_form', $link, $referrer);
1747
1748 $message = '';
1749
1750 if ($url && $enable) {
1751
1752 if (isset($_GET['success']) && $_GET['success'] == '1') {
1753
1754 $message = '<p id="usp-success-message"><strong>'. $usp_options['success-message'] .'</strong></p>'. $link;
1755
1756 } else {
1757
1758 $message = usp_error_message() . $link;
1759
1760 }
1761
1762 }
1763
1764 return $message . $content;
1765
1766 }
1767
1768
1769
1770 function usp_login_required_message() {
1771
1772 $url = apply_filters('usp_require_login_url', wp_login_url());
1773
1774 $message = '<p>'. esc_html__('Please', 'usp');
1775 $message .= ' <a href="'. esc_url($url) .'">'. esc_html__('log in', 'usp') .'</a> ';
1776 $message .= esc_html__('to submit content!', 'usp') .'</p>';
1777
1778 $message = apply_filters('usp_require_login', $message);
1779
1780 return $message;
1781
1782 }
1783
1784
1785
1786 function usp_clear_cookies() {
1787
1788 $cookies = array(
1789 'user-submitted-name',
1790 'user-submitted-email',
1791 'user-submitted-url',
1792 'user-submitted-title',
1793 'user-submitted-tags',
1794 'user-submitted-category',
1795 'user-submitted-content',
1796 'user-submitted-custom',
1797 'user-submitted-checkbox',
1798 'user-submitted-captcha'
1799 );
1800
1801 foreach ($cookies as $cookie) {
1802
1803 if (isset($_COOKIE[$cookie]) && !empty($_COOKIE[$cookie])) {
1804
1805 unset($_COOKIE[$cookie]);
1806 setcookie($cookie, '', time() - 3600, '/');
1807
1808 }
1809
1810 }
1811
1812 }
1813 add_action('wp_logout', 'usp_clear_cookies');
1814