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

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