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

226 lines 5.9 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
146 Syntax: <?php if (function_exists('usp_get_images')) $images = usp_get_images($size, $format, $target, $class, $number, $post_id); ?>
147 Usage: <?php if (function_exists('usp_get_images')) $images = usp_get_images(); foreach ($images as $image) echo $image; ?>
148
149 Parameters:
150 $size = image size as thumbnail, medium, large or full -> default = thumbnail
151 $format = whether to make the image a linked image -> default = image (can use image or image_link)
152 $target = whether to open linked image in new tab -> default = blank (can use blank or self)
153 $class = optional custom class name(s) -> default = none
154 $number = the number of images to display for each post -> default = 100
155 $post_id = an optional post ID to use -> default = false (uses global/current post)
156 */
157 if (!function_exists('usp_get_images')) :
158
159 function usp_get_images($size = false, $format = false, $target = false, $class = false, $number = false, $post_id = false) {
160
161 global $post;
162
163 $post_id = ($post_id && is_numeric($post_id)) ? $post_id : $post->ID;
164
165 $number = (is_numeric($number)) ? intval($number) : intval(apply_filters('usp_image_attachments', 100));
166
167 $class = ($class) ? ' '. sanitize_html_class($class) : '';
168
169 $target = ($target === 'blank') ? ' target="_blank" rel="noopener noreferrer"' : '';
170
171 $format = ($format === 'image' || $format === 'image_link') ? $format : 'image';
172
173 $size = ($size === 'thumbnail' || $size === 'medium' || $size === 'large' || $size === 'full') ? $size : 'thumbnail';
174
175 $args = array(
176 'post_status' => 'publish',
177 'post_type' => 'attachment',
178 'post_parent' => $post_id,
179 'post_status' => 'inherit',
180 'posts_per_page' => $number,
181 'fields' => 'ids'
182 );
183
184 $args = apply_filters('usp_image_attachments_args', $args);
185
186 $image_ids = get_posts($args);
187
188 $urls = array();
189
190 $i = 1;
191
192 foreach ($image_ids as $id) {
193
194 $url = wp_get_attachment_image_src($id, $size);
195
196 $original = wp_get_attachment_image_src($id, 'full');
197
198 if ($url && $original) {
199
200 if ($format === 'image' && isset($url[0])) {
201
202 $urls[] = '<img src="'. sanitize_url($url[0]) .'" class="usp-gallery-image'. $class .'" alt="">';
203
204 } elseif ($format === 'image_link' && isset($url[0]) && isset($original[0])) {
205
206 $linked_image = '<a href="'. sanitize_url($original[0]) .'" class="usp-gallery-image-link'. $class .'"'. $target .'>';
207
208 $linked_image .= '<img src="'. sanitize_url($url[0]) .'" class="usp-gallery-image" alt=""></a>';
209
210 $urls[] = $linked_image;
211
212 }
213
214 if ($i == intval($number)) break;
215
216 $i++;
217
218 }
219
220 }
221
222 return $urls;
223
224 }
225
226 endif;