PluginProbe
User Submitted Posts – Enable Users to Submit Posts from the Front End / 20230809
User Submitted Posts – Enable Users to Submit Posts from the Front End v20230809
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 / library / template-tags.php

template-tags.php in User Submitted Posts – Enable Users to Submit Posts from the Front End 20230809, at library/template-tags.php

231 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // User Submitted Posts - Template Tags
2
3 if (!defined('ABSPATH')) die();
4
5
6
7 /*
8 Returns a boolean value indicating whether the specified post is a public submission
9 Usage: <?php if (function_exists('usp_is_public_submission')) usp_is_public_submission(); ?>
10 */
11 function usp_is_public_submission($postId = false) {
12
13 global $post;
14
15 if (false === $postId) {
16
17 if ($post) $postId = $post->ID;
18
19 }
20
21 if (get_post_meta($postId, 'is_submission', true) == true) {
22
23 return true;
24
25 }
26
27 return false;
28
29 }
30
31
32
33 /*
34 Returns an array of URLs for the specified post image
35 Usage: <?php $images = usp_get_post_images(); foreach ($images as $image) { echo $image; } ?>
36 */
37 function usp_get_post_images($postId = false) {
38
39 global $post;
40
41 if (false === $postId) {
42
43 if ($post) $postId = $post->ID;
44
45 }
46
47 if (usp_is_public_submission($postId)) {
48
49 return get_post_meta($postId, 'user_submit_image');
50
51 }
52
53 return array();
54
55 }
56
57
58
59 /*
60 Prints the URLs for all post attachments.
61 Usage: <?php if (function_exists('usp_post_attachments')) usp_post_attachments(); ?>
62 Syntax: <?php if (function_exists('usp_post_attachments')) usp_post_attachments($size, $beforeUrl, $afterUrl, $numberImages, $postId); ?>
63 Parameters:
64 $size = image size as thumbnail, medium, large or full -> default = full
65 $beforeUrl = text/markup displayed before the image URL -> default = <img src="
66 $afterUrl = text/markup displayed after the image URL -> default = " />
67 $numberImages = the number of images to display for each post -> default = false (display all)
68 $postId = an optional post ID to use -> default = uses global post
69 */
70 function usp_post_attachments($size = 'full', $beforeUrl = '<img src="', $afterUrl = '" />', $numberImages = false, $postId = false) {
71
72 global $post;
73
74 if (false === $postId) {
75
76 if ($post) $postId = $post->ID;
77
78 }
79
80 if (false === $numberImages || !is_numeric($numberImages)) {
81
82 $numberImages = 99;
83
84 }
85
86 $args = array(
87 'post_type' => 'attachment',
88 'post_parent' => $postId,
89 'post_status' => 'inherit',
90 'numberposts' => $numberImages
91 );
92
93 $attachments = get_posts($args);
94
95 foreach ($attachments as $attachment) {
96
97 $info = wp_get_attachment_image_src($attachment->ID, $size);
98
99 echo $beforeUrl . $info[0] . $afterUrl;
100
101 }
102
103 }
104
105
106
107 /*
108 For public-submitted posts, this tag displays the author's name as a link (if URL provided) or plain text (if URL not provided)
109 For normal posts, this tag displays the author's name as a link to their author's post page
110 Usage: <?php if (function_exists('usp_author_link')) usp_author_link(); ?>
111 */
112 function usp_author_link() {
113
114 global $post;
115
116 $isSubmission = get_post_meta($post->ID, 'is_submission', true);
117 $submissionAuthor = get_post_meta($post->ID, 'user_submit_name', true);
118 $submissionLink = get_post_meta($post->ID, 'user_submit_url', true);
119
120 if ($isSubmission && !empty($submissionAuthor)) {
121
122 if (empty($submissionLink)) {
123
124 echo '<span class="usp-author-link">' . $submissionAuthor . '</span>';
125
126 } else {
127
128 echo '<span class="usp-author-link"><a href="' . $submissionLink . '">' . $submissionAuthor . '</a></span>';
129
130 }
131
132 } else {
133
134 the_author_posts_link();
135
136 }
137
138 }
139
140
141
142 /*
143 Function: usp_get_images()
144 Returns an array of image URLs, wrapped in optional HTML
145 Syntax: <?php if (function_exists('usp_get_images')) $images = usp_get_images($size, $before, $after, $number, $postId); ?>
146 Usage: <?php if (function_exists('usp_get_images')) $images = usp_get_images(); foreach ($images as $image) echo $image; ?>
147
148 Parameters:
149 $size = image size as thumbnail, medium, large or full -> default = thumbnail
150 $before = text/markup displayed before the image URL -> default = {img src="
151 $after = text/markup displayed after the image URL -> default = " /}
152 $number = the number of images to display for each post -> default = false (display all)
153 $postId = an optional post ID to use -> default = false (uses global/current post)
154
155 Notes:
156 For $before/$after parameters, use curly brackets instead of angle brackets, for example:
157 usp_get_images('thumbnail', '{img src="', '" /}'); // results in each image URL wrapped like: <img src="[image URL]" />
158
159 For $before/$after parameters, use %%url%% to get the URL of the full-size image, for example:
160 usp_get_images('thumbnail', '{a href="%%url%%"}{img src="', '" /}{/a}'); // outputs for each image: <a href="[full-size image URL]"><img src="[image URL]" /></a>
161 */
162 if (!function_exists('usp_get_images')) :
163
164 function usp_get_images($size = false, $before = false, $after = false, $number = false, $postId = false) {
165
166 global $post;
167
168 if (false === $postId || !is_numeric($postId)) $postId = $post->ID;
169 if (false === $number || !is_numeric($number)) $number = apply_filters('usp_image_attachments', 100);
170 if (false === $size) $size = 'thumbnail';
171 if (false === $before) $before = '{img src="';
172 if (false === $after) $after = '" /}';
173
174 $args = compact('before', 'after');
175
176 $new = array();
177
178 foreach ($args as $key => $value) {
179
180 $value = str_replace("{", "<", $value);
181 $value = str_replace("}", ">", $value);
182
183 if (isset($value)) $new[$key] = $value;
184
185 }
186
187 $args = array(
188 'post_status' => 'publish',
189 'post_type' => 'attachment',
190 'post_parent' => $postId,
191 'post_status' => 'inherit',
192 'posts_per_page' => $number,
193 'fields' => 'ids'
194 );
195
196 $args = apply_filters('usp_image_attachments_args', $args);
197
198 $image_ids = get_posts($args);
199
200 $urls = array(); $i = 1;
201
202 foreach ($image_ids as $id) {
203
204 $url = wp_get_attachment_image_src($id, $size);
205
206 $original = wp_get_attachment_image_src($id, 'full');
207
208 if ($url !== false && $original !== false) {
209
210 $before = isset($new['before']) ? $new['before'] : '';
211 $after = isset($new['after']) ? $new['after'] : '';
212
213 $before = str_replace("%%url%%", $original[0], $before);
214 $after = str_replace("%%url%%", $original[0], $after);
215
216 $urls[] = isset($url[0]) ? $before . $url[0] . $after : '';
217
218 if ($i == intval($number)) break;
219
220 $i++;
221
222 }
223
224 }
225
226 return $urls;
227
228 }
229
230 endif;
231