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

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