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

1,825 lines 47.8 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, content restriction, public post, share post, user post, user submitted post, upload
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.3
13 Stable tag: 20230902
14 Version: 20230902
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 2023 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', '20230902');
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 $check_post = get_page_by_title($title, OBJECT, 'post');
1011
1012 if ($check_post && $check_post->ID) return false;
1013
1014 }
1015
1016 return true;
1017
1018 }
1019
1020
1021
1022 function usp_maybe_rotate($tmp_name, $file_local) {
1023
1024 $image_type = function_exists('exif_imagetype') ? exif_imagetype($tmp_name) : false;
1025
1026 if ($image_type === 2) {
1027
1028 $image_exif = function_exists('exif_read_data') ? @exif_read_data($tmp_name) : array(); // @ cuz PHP bug
1029
1030 if (isset($image_exif['Orientation']) && !empty($image_exif['Orientation'])) {
1031
1032 $src = imagecreatefromjpeg($tmp_name);
1033
1034 if ($src) {
1035
1036 switch ($image_exif['Orientation']) {
1037
1038 case 3: $image = imagerotate($src, 180, 0); break;
1039 case 6: $image = imagerotate($src, -90, 0); break;
1040 case 8: $image = imagerotate($src, 90, 0); break;
1041 default: $image = null; break;
1042 }
1043
1044 imagedestroy($src);
1045
1046 if ($image) {
1047
1048 ob_start();
1049 imagejpeg($image, null, 100);
1050 $file_local = ob_get_contents();
1051 ob_end_clean();
1052 imagedestroy($image);
1053
1054 }
1055 }
1056
1057 }
1058
1059 }
1060
1061 return $file_local;
1062
1063 }
1064
1065
1066
1067 function usp_random_string($length = 12) {
1068
1069 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
1070
1071 $string = substr(str_shuffle($chars), 0, $length);
1072
1073 return $string;
1074
1075 }
1076
1077
1078
1079 function usp_unique_filename($file) {
1080
1081 $parts = pathinfo($file); // e.g., // /www/htdocs/inc/image.jpg
1082
1083 $dirname = isset($parts['dirname']) ? $parts['dirname'] : ''; // /www/htdocs/inc
1084 $basename = isset($parts['basename']) ? $parts['basename'] : ''; // image.jpg
1085 $extension = isset($parts['extension']) ? $parts['extension'] : ''; // jpg
1086 $filename = isset($parts['filename']) ? $parts['filename'] : ''; // image
1087
1088 $append = '-'. usp_random_string();
1089
1090 $file = $dirname .'/'. $filename . $append .'.'. $extension;
1091
1092 $file = apply_filters('usp_unique_filename', $file, $dirname, $basename, $extension, $filename);
1093
1094 return $file;
1095
1096 }
1097
1098
1099
1100 function usp_attach_images($post_id, $newPost, $files, $file_count) {
1101
1102 global $usp_options;
1103
1104 do_action('usp_files_before', $files);
1105
1106 $attach_ids = array();
1107
1108 if ($files && $file_count > 0) {
1109
1110 usp_include_deps();
1111
1112 for ($i = 0; $i < $file_count; $i++) {
1113
1114 if (isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])) {
1115
1116 $file_local = file_get_contents($files['tmp_name'][$i]);
1117
1118 $tmp_name = $files['tmp_name'][$i];
1119
1120 } else {
1121
1122 continue;
1123
1124 }
1125
1126 if (isset($files['name'][$i]) && !empty($files['name'][$i])) {
1127
1128 $append = ($file_count > 1) ? '-'. $i : '';
1129
1130 $file_name = sanitize_file_name(basename($files['name'][$i]));
1131
1132 $parts = pathinfo($file_name);
1133
1134 $ext = isset($parts['extension']) ? $parts['extension'] : null;
1135
1136 $append = apply_filters('usp_filename_append', $append, $file_name, $ext);
1137
1138 $filename = isset($parts['filename']) ? $parts['filename'] : usp_random_string();
1139
1140 $file_name = isset($parts['filename']) ? $parts['filename'] . $append .'.'. $ext : $file_name;
1141
1142 $file_name = apply_filters('usp_file_name', $file_name, $filename, $append, $ext);
1143
1144 } else {
1145
1146 continue;
1147
1148 }
1149
1150 $file_local = usp_maybe_rotate($tmp_name, $file_local);
1151
1152 $file_path = defined('USP_UPLOAD_DIR') ? USP_UPLOAD_DIR : '/';
1153
1154 $upload_dir = apply_filters('usp_upload_directory', wp_upload_dir());
1155
1156 $wp_filetype = wp_check_filetype($file_name, null);
1157
1158 if (wp_mkdir_p($upload_dir['path'])) {
1159
1160 $file = isset($upload_dir['path']) ? $upload_dir['path'] . $file_path . $file_name : null;
1161 $guid = isset($upload_dir['url']) ? $upload_dir['url'] . $file_path . $file_name : null;
1162
1163 } else {
1164
1165 $file = isset($upload_dir['basedir']) ? $upload_dir['basedir'] . $file_path . $file_name : null;
1166 $guid = isset($upload_dir['baseurl']) ? $upload_dir['baseurl'] . $file_path . $file_name : null;
1167
1168 }
1169
1170 $file = file_exists($file) ? usp_unique_filename($file) : $file;
1171
1172 if (stripos($ext, 'php') === false) $bytes = file_put_contents($file, $file_local);
1173
1174 $file_type = isset($wp_filetype['type']) ? $wp_filetype['type'] : null;
1175
1176 $params = apply_filters('wp_handle_upload', array('file' => $file, 'url' => $guid, 'type' => $file_type));
1177
1178 $file = isset($params['file']) ? $params['file'] : $file;
1179 $guid = isset($params['url']) ? $params['url'] : $guid;
1180 $file_type = isset($params['type']) ? $params['type'] : $file_type;
1181
1182 $attachment = array(
1183 'post_mime_type' => $file_type,
1184 'post_name' => $file_name,
1185 'post_title' => $file_name,
1186 'post_status' => 'inherit',
1187 'guid' => $guid,
1188 );
1189
1190 $attachment = apply_filters('usp_insert_attachment_data', $attachment);
1191
1192 $attach_id = wp_insert_attachment($attachment, $file, $post_id);
1193
1194 if (isset($usp_options['usp_featured_images']) && $usp_options['usp_featured_images']) {
1195
1196 if (!has_post_thumbnail($post_id)) set_post_thumbnail($post_id, $attach_id);
1197
1198 }
1199
1200 $attach_data = wp_generate_attachment_metadata($attach_id, $file);
1201
1202 wp_update_attachment_metadata($attach_id, $attach_data);
1203
1204 if (!is_wp_error($attach_id) && wp_attachment_is_image($attach_id)) {
1205
1206 $attach_ids[] = $attach_id;
1207
1208 add_post_meta($post_id, 'user_submit_image', wp_get_attachment_url($attach_id));
1209
1210 } else {
1211
1212 wp_delete_attachment($attach_id);
1213
1214 wp_delete_post($post_id, true);
1215
1216 $newPost['error'][] = 'file-upload';
1217
1218 unset($newPost['id']);
1219
1220 }
1221
1222 }
1223
1224 } else {
1225
1226 if (isset($usp_options['usp_featured_image_default']) && !empty($usp_options['usp_featured_image_default'])) {
1227
1228 $default_image = attachment_url_to_postid($usp_options['usp_featured_image_default']);
1229
1230 if (!empty($default_image) && isset($usp_options['usp_featured_images']) && $usp_options['usp_featured_images']) {
1231
1232 if (!has_post_thumbnail($post_id)) set_post_thumbnail($post_id, $default_image);
1233
1234 }
1235
1236 }
1237
1238 }
1239
1240 do_action('usp_files_after', $attach_ids);
1241
1242 return $newPost;
1243
1244 }
1245
1246
1247
1248 function usp_createPublicSubmission($title, $files, $ip, $author, $url, $email, $tags, $captcha, $verify, $content, $category, $custom, $custom_2, $checkbox, $comments) {
1249
1250 global $usp_options;
1251
1252 $newPost = array('id' => null, 'error' => array());
1253
1254 $author_data = usp_get_author($author);
1255 $author = $author_data['author'];
1256 $author_id = $author_data['author_id'];
1257
1258 if (isset($author_data['error']) && !empty($author_data['error'])) {
1259
1260 $newPost['error'][] = $author_data['error'];
1261
1262 }
1263
1264 $file_data = usp_check_images($files, $newPost);
1265 $file_count = $file_data['file_count'];
1266
1267 if (isset($file_data['error']) && !empty($file_data['error'])) {
1268
1269 $newPost['error'] = array_unique(array_merge($file_data['error'], $newPost['error']));
1270
1271 }
1272
1273 $tags = is_array($tags) ? array_filter($tags) : $tags;
1274 $category = is_array($category) ? array_filter($category) : $category;
1275
1276 if (isset($usp_options['usp_title']) && ($usp_options['usp_title'] == 'show') && empty($title)) $newPost['error'][] = 'required-title';
1277 if (isset($usp_options['usp_url']) && ($usp_options['usp_url'] == 'show') && empty($url)) $newPost['error'][] = 'required-url';
1278 if (isset($usp_options['usp_tags']) && ($usp_options['usp_tags'] == 'show') && empty($tags)) $newPost['error'][] = 'required-tags';
1279 if (isset($usp_options['usp_category']) && ($usp_options['usp_category'] == 'show') && empty($category)) $newPost['error'][] = 'required-category';
1280 if (isset($usp_options['usp_content']) && ($usp_options['usp_content'] == 'show') && empty($content)) $newPost['error'][] = 'required-content';
1281 if (isset($usp_options['custom_field']) && ($usp_options['custom_field'] == 'show') && empty($custom)) $newPost['error'][] = 'required-custom';
1282 if (isset($usp_options['custom_field_2']) && ($usp_options['custom_field_2'] == 'show') && empty($custom_2)) $newPost['error'][] = 'required-custom-2';
1283
1284 if (usp_check_recaptcha_keys()) {
1285
1286 if (isset($usp_options['usp_recaptcha']) && ($usp_options['usp_recaptcha'] == 'show') && !usp_verify_recaptcha()) $newPost['error'][] = 'required-recaptcha';
1287
1288 }
1289
1290 if (isset($usp_options['usp_captcha']) && ($usp_options['usp_captcha'] == 'show') && !usp_spamQuestion($captcha)) $newPost['error'][] = 'required-captcha';
1291
1292 if (isset($usp_options['usp_email']) && ($usp_options['usp_email'] == 'show')) {
1293
1294 $email = sanitize_email($email);
1295
1296 if (!usp_validateEmail($email)) $newPost['error'][] = 'required-email';
1297
1298 }
1299
1300 if (isset($usp_options['usp_email']) && ($usp_options['usp_email'] == 'optn') && !empty($email)) {
1301
1302 $email = sanitize_email($email);
1303
1304 if (!usp_validateEmail($email)) $newPost['error'][] = 'incorrect-email';
1305
1306 }
1307
1308 if (isset($usp_options['titles_unique']) && $usp_options['titles_unique'] && !usp_check_duplicates($title)) $newPost['error'][] = 'duplicate-title';
1309 if (!empty($verify)) $newPost['error'][] = 'spam-verify';
1310
1311 if (isset($usp_options['custom_checkbox']) && !empty($usp_options['custom_checkbox']) && empty($checkbox)) $newPost['error'][] = 'required-checkbox';
1312
1313 if (isset($newPost['error']) && !empty($newPost['error'])) {
1314
1315 foreach ($newPost['error'] as $e) {
1316
1317 if (!empty($e)) {
1318
1319 unset($newPost['id']);
1320
1321 return $newPost;
1322
1323 }
1324
1325 }
1326
1327 }
1328
1329 $postData = usp_prepare_post($title, $content, $author_id, $author, $ip);
1330
1331 $new_status = (isset($postData['post_status']) && !empty($postData['post_status'])) ? sanitize_text_field($postData['post_status']) : apply_filters('usp_post_status', 'pending');
1332 $postData['post_status'] = apply_filters('usp_post_status', 'pending');
1333
1334 do_action('usp_insert_before', $postData);
1335 $newPost['id'] = wp_insert_post($postData);
1336 do_action('usp_insert_after', $newPost);
1337
1338 $post_id = isset($newPost['id']) ? $newPost['id'] : null;
1339
1340 if ($post_id && !is_wp_error($post_id)) {
1341
1342 $post = get_post($post_id);
1343
1344 $post->post_status = $new_status;
1345
1346 $post->comment_status = $comments;
1347
1348 wp_update_post($post);
1349
1350 wp_set_post_tags($post_id, apply_filters('usp_filter_tags', $tags), apply_filters('usp_append_tags', false));
1351
1352 wp_set_post_categories($post_id, apply_filters('usp_filter_cats', $category), apply_filters('usp_append_cats', false));
1353
1354 $newPost = usp_attach_images($post_id, $newPost, $files, $file_count);
1355
1356 if (isset($newPost['error']) && empty($newPost['error'])) {
1357
1358 update_post_meta($post_id, 'is_submission', true);
1359 update_post_meta($post_id, 'usp-post-id', $post_id);
1360
1361 $custom_name = isset($usp_options['custom_name']) ? $usp_options['custom_name'] : 'usp_custom_field';
1362 $custom_name_2 = isset($usp_options['custom_name_2']) ? $usp_options['custom_name_2'] : 'usp_custom_field_2';
1363
1364 $checkbox_name = isset($usp_options['custom_checkbox_name']) ? $usp_options['custom_checkbox_name'] : 'usp_custom_checkbox';
1365
1366 if (!empty($custom)) update_post_meta($post_id, $custom_name, $custom);
1367 if (!empty($custom_2)) update_post_meta($post_id, $custom_name_2, $custom_2);
1368 if (!empty($checkbox)) update_post_meta($post_id, $checkbox_name, $checkbox);
1369 if (!empty($author)) update_post_meta($post_id, 'user_submit_name', $author);
1370 if (!empty($email)) update_post_meta($post_id, 'user_submit_email', $email);
1371 if (!empty($url)) update_post_meta($post_id, 'user_submit_url', $url);
1372
1373 if (!empty($ip) && !$usp_options['disable_ip_tracking']) update_post_meta($post_id, 'user_submit_ip', $ip);
1374
1375 usp_send_mail_alert($post_id, $title, $content, $author, $email, $url, $custom, $custom_2);
1376
1377 }
1378
1379 } else {
1380
1381 $newPost['error'][] = 'post-fail';
1382
1383 }
1384
1385 return apply_filters('usp_new_post', $newPost);
1386
1387 }
1388
1389
1390
1391 function usp_include_deps() {
1392
1393 if (!function_exists('media_handle_upload')) {
1394
1395 require_once (ABSPATH .'/wp-admin/includes/media.php');
1396 require_once (ABSPATH .'/wp-admin/includes/file.php');
1397 require_once (ABSPATH .'/wp-admin/includes/image.php');
1398
1399 }
1400
1401 }
1402
1403
1404
1405 function usp_width_min($width) {
1406
1407 global $usp_options;
1408
1409 if (intval($width) < intval($usp_options['min-image-width'])) return false;
1410
1411 else return true;
1412
1413 }
1414
1415
1416
1417 function usp_width_max($width) {
1418
1419 global $usp_options;
1420
1421 if (intval($width) > intval($usp_options['max-image-width'])) return false;
1422
1423 else return true;
1424
1425 }
1426
1427
1428
1429 function usp_height_min($height) {
1430
1431 global $usp_options;
1432
1433 if (intval($height) < intval($usp_options['min-image-height'])) return false;
1434
1435 else return true;
1436
1437 }
1438
1439
1440
1441 function usp_height_max($height) {
1442
1443 global $usp_options;
1444
1445 if (intval($height) > intval($usp_options['max-image-height'])) return false;
1446
1447 else return true;
1448
1449 }
1450
1451
1452
1453 function usp_validateEmail($email) {
1454
1455 if (!is_email($email)) return false;
1456
1457 $bad_stuff = array("\r", "\n", "mime-version", "content-type", "cc:", "to:");
1458
1459 foreach ($bad_stuff as $bad) {
1460
1461 if (strpos(strtolower($email), strtolower($bad)) !== false) {
1462
1463 return false;
1464
1465 }
1466
1467 }
1468
1469 return true;
1470
1471 }
1472
1473 function usp_send_mail_alert($post_id, $title, $content, $author, $email, $url, $custom, $custom_2) {
1474
1475 global $usp_options;
1476
1477 if (isset($usp_options['usp_email_alerts']) && $usp_options['usp_email_alerts']) {
1478
1479 $blog_url = get_bloginfo('url'); // %%blog_url%%
1480 $blog_name = get_bloginfo('name'); // %%blog_name%%
1481 $post_url = get_permalink($post_id); // %%post_url%%
1482 $admin_url = admin_url(); // %%admin_url%%
1483 $post_title = $title; // %%post_title%%
1484 $post_content = $content; // %%post_content%%
1485 $post_author = $author; // %%post_author%%
1486 $user_email = $email; // %%user_email%%
1487 $user_url = $url; // %%user_url%%
1488
1489 $edit_link = usp_remote_edit_post_link($post_id); // %%edit_link%%
1490 $delete_link = usp_remote_delete_post_link($post_id); // %%delete_link%%
1491
1492 $patterns = array();
1493
1494 $patterns[0] = "/%%blog_url%%/";
1495 $patterns[1] = "/%%blog_name%%/";
1496 $patterns[2] = "/%%post_url%%/";
1497 $patterns[3] = "/%%admin_url%%/";
1498 $patterns[4] = "/%%post_title%%/";
1499 $patterns[5] = "/%%post_content%%/";
1500 $patterns[6] = "/%%post_author%%/";
1501 $patterns[7] = "/%%user_email%%/";
1502 $patterns[8] = "/%%user_url%%/";
1503 $patterns[9] = "/%%edit_link%%/";
1504 $patterns[10] = "/%%custom_field%%/";
1505 $patterns[11] = "/%%custom_field_2%%/";
1506 $patterns[12] = "/%%delete_link%%/";
1507
1508 $replacements = array();
1509
1510 $replacements[0] = $blog_url;
1511 $replacements[1] = $blog_name;
1512 $replacements[2] = $post_url;
1513 $replacements[3] = $admin_url;
1514 $replacements[4] = $post_title;
1515 $replacements[5] = $post_content;
1516 $replacements[6] = $post_author;
1517 $replacements[7] = $user_email;
1518 $replacements[8] = $user_url;
1519 $replacements[9] = $edit_link;
1520 $replacements[10] = $custom;
1521 $replacements[11] = $custom_2;
1522 $replacements[12] = $delete_link;
1523
1524 //
1525
1526 $subject_default = $blog_name .': New user-submitted post!';
1527 $subject = (isset($usp_options['email_alert_subject']) && !empty($usp_options['email_alert_subject'])) ? $usp_options['email_alert_subject'] : $subject_default;
1528 $subject = preg_replace($patterns, $replacements, $subject);
1529 $subject = apply_filters('usp_mail_subject', $subject);
1530
1531 $message_default = 'Hello, there is a new user-submitted post:'. "\r\n\n" . 'Title: '. $post_title . "\r\n\n" .'Visit Admin Area: '. $admin_url;
1532 $message = (isset($usp_options['email_alert_message']) && !empty($usp_options['email_alert_message'])) ? $usp_options['email_alert_message'] : $message_default;
1533 $message = preg_replace($patterns, $replacements, $message);
1534 $message = apply_filters('usp_mail_message', $message);
1535
1536 $html = isset($usp_options['usp_email_html']) ? $usp_options['usp_email_html'] : false;
1537 $format = $html ? 'text/html' : 'text/plain';
1538
1539 //
1540
1541 $default = get_bloginfo('admin_email');
1542
1543 $to = (isset($usp_options['usp_email_address']) && !empty($usp_options['usp_email_address'])) ? $usp_options['usp_email_address'] : $default;
1544 $from = (isset($usp_options['usp_email_from']) && !empty($usp_options['usp_email_from'])) ? $usp_options['usp_email_from'] : $to;
1545
1546 $to = explode(',', $to);
1547 $from = explode(',', $from);
1548
1549 $address = array();
1550
1551 foreach ($to as $k => $v) $address[$k]['to'] = trim($v);
1552 foreach ($from as $k => $v) $address[$k]['from'] = trim($v);
1553
1554 if (!empty($address[0])) {
1555
1556 foreach ($address as $k => $v) {
1557
1558 $address_to = (isset($v['to']) && !empty($v['to'])) ? $v['to'] : $default;
1559 $address_from = (isset($v['from']) && !empty($v['from'])) ? $v['from'] : $default;
1560
1561 $headers = 'X-Mailer: User Submitted Posts'. "\n";
1562 $headers .= 'From: '. $blog_name .' <'. $address_from .'>'. "\n";
1563 $headers .= 'Reply-To: '. $blog_name .' <'. $address_from .'>'. "\n";
1564 $headers .= 'Content-Type: '. $format .'; charset='. get_option('blog_charset', 'UTF-8') . "\n";
1565
1566 wp_mail($address_to, $subject, $message, $headers);
1567
1568 }
1569
1570 }
1571
1572 }
1573
1574 }
1575
1576
1577
1578 // Thanks to Delete Post plugin @ https://wordpress.org/plugins/delete-post/
1579
1580 function usp_remote_delete_post() {
1581
1582 if (isset($_GET['delete_post']) && isset($_GET['nonce'])) {
1583
1584 if (wp_verify_nonce($_GET['nonce'], 'delete_post_'. $_GET['delete_post'])) {
1585
1586 $post_id = intval($_GET['delete_post']);
1587
1588 $post = get_post($post_id);
1589
1590 if ($post && get_current_user_id() === (int) $post->post_author) {
1591
1592 $force = apply_filters('usp_force_delete_post', true);
1593
1594 $result = wp_delete_post($post_id, $force);
1595
1596 $result = $result ? 'true' : 'false';
1597
1598 $url = add_query_arg('usp-delete-post', $result, trailingslashit(home_url()));
1599
1600 wp_redirect($url);
1601
1602 exit;
1603
1604 }
1605
1606 }
1607
1608 }
1609
1610 }
1611 add_action('init', 'usp_remote_delete_post');
1612
1613
1614
1615 function usp_remote_delete_post_link($post_id) {
1616
1617 return add_query_arg(array('delete_post' => $post_id, 'nonce' => wp_create_nonce('delete_post_'. $post_id)), trailingslashit(home_url()));
1618
1619 }
1620
1621
1622
1623 function usp_remote_edit_post_link($post_id) {
1624
1625 return admin_url('post.php?post='. $post_id .'&action=edit');
1626
1627 }
1628
1629
1630
1631 function usp_spamQuestion($input) {
1632
1633 global $usp_options;
1634
1635 $response = $usp_options['usp_response'];
1636
1637 $response = sanitize_text_field($response);
1638
1639 if ($usp_options['usp_casing'] == false) {
1640
1641 return (strtoupper($input) == strtoupper($response));
1642
1643 } else {
1644
1645 return ($input == $response);
1646
1647 }
1648
1649 }
1650
1651
1652
1653 function usp_error_message() {
1654
1655 global $usp_options;
1656
1657 $min = $usp_options['min-images'];
1658 $max = $usp_options['max-images'];
1659
1660 if ((int) $min > 1) $min = ' ('. $min . esc_html__(' files required', 'usp') .')';
1661 else $min = ' ('. $min . esc_html__(' file required', 'usp') .')';
1662
1663 if ((int) $max > 1) $max = ' (limit: '. $max . esc_html__(' files', 'usp') .')';
1664 else $max = ' (limit: '. $max . esc_html__(' file', 'usp') .')';
1665
1666 $min_width = ' ('. $usp_options['min-image-width'] . esc_html__(' pixels', 'usp') .')';
1667 $max_width = ' ('. $usp_options['max-image-width'] . esc_html__(' pixels', 'usp') .')';
1668 $min_height = ' ('. $usp_options['min-image-height'] . esc_html__(' pixels', 'usp') .')';
1669 $max_height = ' ('. $usp_options['max-image-height'] . esc_html__(' pixels', 'usp') .')';
1670
1671 $custom_label = isset($usp_options['custom_label']) ? $usp_options['custom_label'] : __('Custom Field 1', 'usp');
1672 $custom_label_2 = isset($usp_options['custom_label_2']) ? $usp_options['custom_label_2'] : __('Custom Field 2', 'usp');
1673
1674 $checkbox_label = isset($usp_options['custom_checkbox_err']) ? $usp_options['custom_checkbox_err'] : __('Custom checkbox required', 'usp');
1675
1676 if (!empty($usp_options['error-message'])) $general_error = $usp_options['error-message'];
1677 else $general_error = esc_html__('An error occurred. Please go back and try again.', 'usp');
1678
1679 if (isset($_GET['usp-error']) && !empty($_GET['usp-error'])) {
1680
1681 $error_string = sanitize_text_field($_GET['usp-error']);
1682 $error_array = explode(',', $error_string);
1683 $error = array();
1684
1685 foreach ($error_array as $e) {
1686
1687 if ($e == 'required-login') $error[] = esc_html__('User login required', 'usp');
1688 elseif ($e == 'required-name') $error[] = esc_html__('User name required', 'usp');
1689 elseif ($e == 'required-title') $error[] = esc_html__('Post title required', 'usp');
1690 elseif ($e == 'required-url') $error[] = esc_html__('User URL required', 'usp');
1691 elseif ($e == 'required-tags') $error[] = esc_html__('Post tags required', 'usp');
1692 elseif ($e == 'required-category') $error[] = esc_html__('Post category required', 'usp');
1693 elseif ($e == 'required-content') $error[] = esc_html__('Post content required', 'usp');
1694 elseif ($e == 'required-recaptcha') $error[] = esc_html__('Correct captcha required', 'usp');
1695 elseif ($e == 'required-captcha') $error[] = esc_html__('Correct captcha required', 'usp');
1696 elseif ($e == 'required-email') $error[] = esc_html__('User email required', 'usp');
1697 elseif ($e == 'incorrect-email') $error[] = esc_html__('Please check your email and try again', 'usp');
1698 elseif ($e == 'spam-verify') $error[] = esc_html__('Non-empty value for hidden field', 'usp');
1699 elseif ($e == 'file-min') $error[] = esc_html__('Minimum number of images not met', 'usp') . $min;
1700 elseif ($e == 'file-max') $error[] = esc_html__('Maximum number of images exceeded ', 'usp') . $max;
1701 elseif ($e == 'width-min') $error[] = esc_html__('Minimum image width not met', 'usp') . $min_width;
1702 elseif ($e == 'width-max') $error[] = esc_html__('Image width exceeds maximum', 'usp') . $max_width;
1703 elseif ($e == 'height-min') $error[] = esc_html__('Minimum image height not met', 'usp') . $min_height;
1704 elseif ($e == 'height-max') $error[] = esc_html__('Image height exceeds maximum', 'usp') . $max_height;
1705 elseif ($e == 'file-type') $error[] = esc_html__('File type not allowed (please upload images only)', 'usp');
1706 elseif ($e == 'required-custom') $error[] = esc_html($custom_label) . esc_html__(' required', 'usp');
1707 elseif ($e == 'required-custom-2') $error[] = esc_html($custom_label_2) . esc_html__(' required', 'usp');
1708 elseif ($e == 'required-checkbox') $error[] = esc_html($checkbox_label);
1709
1710 // general error for file uploads, check error log for description.
1711 // check server for proper values of memory_limit, max_execution_time, max_input_time, post_max_size, upload_max_filesize
1712 elseif ($e == 'file-error') $error[] = esc_html__('File not uploaded. Please check the file and try again.', 'usp');
1713
1714 // check permissions on /uploads/ directory, check error log for the following error:
1715 // PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /wp-includes/wp-db.php
1716 elseif ($e == 'file-upload') $error[] = esc_html__('The file(s) could not be uploaded', 'usp');
1717
1718 elseif ($e == 'post-fail') $error[] = esc_html__('Post not created. Please contact the site administrator for help.', 'usp');
1719 elseif ($e == 'duplicate-title') $error[] = esc_html__('Duplicate post title. Please try again.', 'usp');
1720
1721 elseif ($e == 'error') $error[] = $general_error;
1722
1723 }
1724
1725 $output = '';
1726
1727 foreach ($error as $e) {
1728
1729 $output .= "\t\t\t".'<div class="usp-error">'. esc_html__('Error: ', 'usp') . $e .'</div>'."\n";
1730
1731 }
1732
1733 $return = '<div id="usp-error-message">'."\n". $output ."\t\t".'</div>'."\n";
1734
1735 return apply_filters('usp_error_message', $return);
1736
1737 }
1738
1739 return false;
1740
1741 }
1742
1743
1744
1745 function usp_redirect_message($content = '') {
1746
1747 global $usp_options;
1748
1749 $url = (isset($usp_options['redirect-url']) && !empty($usp_options['redirect-url'])) ? true : false;
1750
1751 $enable = (!is_admin() && (isset($_GET['usp_redirect']) && $_GET['usp_redirect'] == '1')) ? true : false;
1752
1753 $referrer = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) ? esc_url($_SERVER['HTTP_REFERER']) : false;
1754
1755 $link = $referrer ? '<p id="usp-return-form"><a href="'. $referrer .'">'. esc_html__('Return to form', 'usp') .'</a></p>' : '';
1756
1757 $link = apply_filters('usp_return_form', $link, $referrer);
1758
1759 $message = '';
1760
1761 if ($url && $enable) {
1762
1763 if (isset($_GET['success']) && $_GET['success'] == '1') {
1764
1765 $message = '<p id="usp-success-message"><strong>'. $usp_options['success-message'] .'</strong></p>'. $link;
1766
1767 } else {
1768
1769 $message = usp_error_message() . $link;
1770
1771 }
1772
1773 }
1774
1775 return $message . $content;
1776
1777 }
1778
1779
1780
1781 function usp_login_required_message() {
1782
1783 $url = apply_filters('usp_require_login_url', wp_login_url());
1784
1785 $message = '<p>'. esc_html__('Please', 'usp');
1786 $message .= ' <a href="'. esc_url($url) .'">'. esc_html__('log in', 'usp') .'</a> ';
1787 $message .= esc_html__('to submit content!', 'usp') .'</p>';
1788
1789 $message = apply_filters('usp_require_login', $message);
1790
1791 return $message;
1792
1793 }
1794
1795
1796
1797 function usp_clear_cookies() {
1798
1799 $cookies = array(
1800 'user-submitted-name',
1801 'user-submitted-email',
1802 'user-submitted-url',
1803 'user-submitted-title',
1804 'user-submitted-tags',
1805 'user-submitted-category',
1806 'user-submitted-content',
1807 'user-submitted-custom',
1808 'user-submitted-checkbox',
1809 'user-submitted-captcha'
1810 );
1811
1812 foreach ($cookies as $cookie) {
1813
1814 if (isset($_COOKIE[$cookie]) && !empty($_COOKIE[$cookie])) {
1815
1816 unset($_COOKIE[$cookie]);
1817 setcookie($cookie, '', time() - 3600, '/');
1818
1819 }
1820
1821 }
1822
1823 }
1824 add_action('wp_logout', 'usp_clear_cookies');
1825