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

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