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

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