PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20260217
User Submitted Posts – Enable Users to Submit Posts from the Front End v20260217
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 20260217, at user-submitted-posts.php

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