PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20250329
User Submitted Posts – Enable Users to Submit Posts from the Front End v20250329
20260916 20260810 20260608 20230806 20230809 20230811 20230901 20230902 20230914 20231102 20240319 20240516 20240703 20241026 20250327 20250329 20251121 20251210 20260110 20260113 20260207 20260217 20260407 20260422 trunk All 59 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 20250329, at user-submitted-posts.php

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