PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.6.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.6.0
3.54.0 3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 All 178 releases
simple-tags / inc / post-tags-functions.php

post-tags-functions.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.6.0, at inc/post-tags-functions.php

397 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Fetch our TAXOPRESS Terms Display option.
4 *
5 * @return mixed
6 */
7 function taxopress_get_posttags_data()
8 {
9 return array_filter((array)apply_filters('taxopress_get_posttags_data', get_option('taxopress_posttagss', []),
10 get_current_blog_id()));
11 }
12
13 /**
14 * Get the selected posttags from the $_POST global.
15 *
16 * @return bool|string False on no result, sanitized posttags if set.
17 * @internal
18 *
19 */
20 function taxopress_get_current_posttags()
21 {
22
23 $posttagss = false;
24
25 if (!empty($_GET) && isset($_GET['taxopress_posttags'])) {
26 $posttagss = sanitize_text_field($_GET['taxopress_posttags']);
27 } else {
28 $posttagss = taxopress_get_posttags_data();
29 if (!empty($posttagss)) {
30 // Will return the first array key.
31 $posttagss = key($posttagss);
32 }
33 }
34
35 /**
36 * Filters the current posttags to edit.
37 *
38 * @param string $posttagss posttags slug.
39 */
40 return apply_filters('taxopress_current_posttags', $posttagss);
41 }
42
43
44 /**
45 * Handle the save and deletion of posttags data.
46 */
47 function taxopress_process_posttags()
48 {
49
50 if (wp_doing_ajax()) {
51 return;
52 }
53
54 if (!is_admin()) {
55 return;
56 }
57
58 if (empty($_GET)) {
59 return;
60 }
61
62 if (!isset($_GET['page'])) {
63 return;
64 }
65 if ('st_post_tags' !== $_GET['page']) {
66 return;
67 }
68
69 if(!current_user_can('simple_tags')){
70 return;
71 }
72
73 if (isset($_GET['new_posttags'])) {
74 if ((int)$_GET['new_posttags'] === 1) {
75 add_action('admin_notices', "taxopress_posttags_update_success_admin_notice");
76 add_filter('removable_query_args', 'taxopress_saved_posttags_filter_removable_query_args');
77 }
78 }
79
80 if (isset($_GET['deleted_posttags'])) {
81 if ((int)$_GET['deleted_posttags'] === 1) {
82 add_action('admin_notices', "taxopress_posttags_delete_success_admin_notice");
83 add_filter('removable_query_args', 'taxopress_deleted_posttags_filter_removable_query_args');
84 }
85 }
86
87
88 if (!empty($_POST) && isset($_POST['posttags_submit'])) {
89 $result = '';
90 if (isset($_POST['posttags_submit'])) {
91 check_admin_referer('taxopress_addedit_posttags_nonce_action', 'taxopress_addedit_posttags_nonce_field');
92 $result = taxopress_update_posttags($_POST);
93 }
94
95 if ($result) {
96 wp_safe_redirect(
97 add_query_arg(
98 [
99 'page' => 'st_post_tags',
100 'add' => 'new_item',
101 'action' => 'edit',
102 'taxopress_posttags' => $result,
103 'new_posttags' => 1,
104 ],
105 taxopress_admin_url('admin.php')
106 )
107 );
108
109 exit();
110 }
111 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-posttags') {
112 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
113 if (wp_verify_nonce($nonce, 'posttags-action-request-nonce')) {
114 taxopress_action_delete_posttags(sanitize_text_field($_REQUEST['taxopress_posttags']));
115 }
116 add_filter('removable_query_args', 'taxopress_delete_posttags_filter_removable_query_args');
117 }
118 }
119
120
121 /**
122 * Create default post tags.
123 */
124 function taxopress_create_default_post_tags()
125 {
126
127 if (wp_doing_ajax()) {
128 return;
129 }
130
131 if (!is_admin()) {
132 return;
133 }
134
135 if ((int)get_option('taxopress_default_posttagss') > 0) {
136 return;
137 }
138
139 if (count(taxopress_get_posttags_data()) > 0) {
140 return;
141 }
142
143 if(!current_user_can('simple_tags')){
144 return;
145 }
146
147 // Get options
148 $options = SimpleTags_Plugin::get_option();
149
150 // Default values
151 $defaults = [
152 'before' => __('Tags: ', 'simple-tags'),
153 'separator' => ', ',
154 'after' => '<br />',
155 'xformat' => __('<a href="%tag_link%" title="%tag_name_attribute%" %tag_rel%>%tag_name%</a>', 'simple-tags'),
156 'notagtext' => __('No tag for this post.', 'simple-tags'),
157 'number' => 0,
158 'format' => '',
159 ];
160 // Get values in DB
161 $defaults['before'] = $options['tt_before'];
162 $defaults['separator'] = $options['tt_separator'];
163 $defaults['after'] = $options['tt_after'];
164 $defaults['xformat'] = $options['tt_xformat'];
165 $defaults['notagtext'] = $options['tt_notagstext'];
166 $defaults['number'] = (int)$options['tt_number'];
167
168 $post_tags_default = [];
169 $post_tags_default['taxopress_post_tags']['title'] = 'Terms for Current Post';
170 $post_tags_default['taxopress_post_tags']['taxonomy'] = 'post_tag';
171 $post_tags_default['taxopress_post_tags']['embedded'] = [];
172 $post_tags_default['taxopress_post_tags']['number'] = $defaults['number'];
173 $post_tags_default['taxopress_post_tags']['separator'] = $defaults['separator'];
174 $post_tags_default['taxopress_post_tags']['after'] = $defaults['after'];
175 $post_tags_default['taxopress_post_tags']['before'] = $defaults['before'];
176 $post_tags_default['taxopress_post_tags']['notagtext'] = $defaults['notagtext'];
177 $post_tags_default['taxopress_post_tags']['xformat'] = $defaults['xformat'];
178 $result = taxopress_update_posttags($post_tags_default);
179 update_option('taxopress_default_posttagss', $result);
180 }
181
182
183 /**
184 * Successful update callback.
185 */
186 function taxopress_posttags_update_success_admin_notice()
187 {
188 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
189 echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags'));
190 }
191
192 /**
193 * Successful deleted callback.
194 */
195 function taxopress_posttags_delete_success_admin_notice()
196 {
197 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
198 echo taxopress_admin_notices_helper(esc_html__('Shortcode entry successfully deleted.', 'simple-tags'), false);
199 }
200
201 /**
202 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
203 *
204 * @link https://core.trac.wordpress.org/ticket/23367
205 *
206 * @param string[] $args Array of removable query arguments.
207 * @return string[] Updated array of removable query arguments.
208 */
209 function taxopress_saved_posttags_filter_removable_query_args(array $args)
210 {
211 return array_merge($args, [
212 'new_posttags',
213 ]);
214 }
215
216 /**
217 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
218 *
219 * @link https://core.trac.wordpress.org/ticket/23367
220 *
221 * @param string[] $args Array of removable query arguments.
222 * @return string[] Updated array of removable query arguments.
223 */
224 function taxopress_deleted_posttags_filter_removable_query_args(array $args)
225 {
226 return array_merge($args, [
227 'deleted_posttags',
228 ]);
229 }
230
231 /**
232 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
233 *
234 * @link https://core.trac.wordpress.org/ticket/23367
235 *
236 * @param string[] $args Array of removable query arguments.
237 * @return string[] Updated array of removable query arguments.
238 */
239 function taxopress_delete_posttags_filter_removable_query_args(array $args)
240 {
241 return array_merge($args, [
242 'action',
243 'taxopress_posttags',
244 '_wpnonce',
245 ]);
246 }
247
248 /**
249 * Add to or update our TAXOPRESS option with new data.
250 *
251 *
252 * @param array $data Array of posttags data to update. Optional.
253 * @return bool|string False on failure, string on success.
254 * @internal
255 *
256 */
257 function taxopress_update_posttags($data = [])
258 {
259 foreach ($data as $key => $value) {
260
261 if (is_string($value)) {
262 $data[$key] = sanitize_text_field($value);
263 } else {
264 array_map('sanitize_text_field', $data[$key]);
265 }
266 }
267
268 $posttagss = taxopress_get_posttags_data();
269
270 $title = $data['taxopress_post_tags']['title'];
271 $title = str_replace('"', '', htmlspecialchars_decode($title));
272 $title = htmlspecialchars($title, ENT_QUOTES);
273 $title = trim($title);
274 $data['taxopress_post_tags']['title'] = stripslashes_deep($title);
275
276 $xformat = $data['taxopress_post_tags']['xformat'];
277 $data['taxopress_post_tags']['xformat'] = stripslashes_deep($xformat);
278 $data['taxopress_post_tags']['embedded'] = isset($data['embedded']) ? $data['embedded'] : [];
279
280 if (isset($data['edited_posttags'])) {
281 $posttags_id = $data['edited_posttags'];
282 $posttagss[$posttags_id] = $data['taxopress_post_tags'];
283 $success = update_option('taxopress_posttagss', $posttagss);
284 //return 'update_success';
285 } else {
286 $posttags_id = (int)get_option('taxopress_posttags_ids_increament') + 1;
287 $data['taxopress_post_tags']['ID'] = $posttags_id;
288 $posttagss[$posttags_id] = $data['taxopress_post_tags'];
289 $success = update_option('taxopress_posttagss', $posttagss);
290 $update_id = update_option('taxopress_posttags_ids_increament', $posttags_id);
291 //return 'add_success';
292 }
293
294 return $posttags_id;
295
296 }
297
298
299 /**
300 * Delete our custom taxonomy from the array of taxonomies.
301 * @return bool|string False on failure, string on success.
302 */
303 function taxopress_action_delete_posttags($posttags_id)
304 {
305 $posttagss = taxopress_get_posttags_data();
306
307 if (array_key_exists($posttags_id, $posttagss)) {
308 unset($posttagss[$posttags_id]);
309 $success = update_option('taxopress_posttagss', $posttagss);
310 }
311
312 if (isset($success)) {
313 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
314 wp_safe_redirect(
315 add_query_arg(
316 [
317 'page' => 'st_post_tags',
318 'deleted_posttags' => 1,
319 ],
320 taxopress_admin_url('admin.php')
321 )
322 );
323 exit();
324 }
325 }
326
327 function taxopress_posttags_shortcode($atts)
328 {
329 extract(shortcode_atts([
330 'id' => 0
331 ], $atts));
332
333 $posttags_id = $id;
334 $posttagss = taxopress_get_posttags_data();
335
336 ob_start();
337 if (array_key_exists($posttags_id, $posttagss)) {
338 $posttags_arg = build_query($posttagss[$posttags_id]);
339
340 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
341 echo SimpleTags_Client_PostTags::extendedPostTags($posttags_arg);
342
343 } else {
344 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
345 echo esc_html__('Invalid post terms ID.', 'simple-tags');
346 }
347
348 $html = ob_get_clean();
349
350 return $html;
351
352
353 }
354
355 /**
356 * Auto add current tags post to post content
357 *
358 * @param string $content
359 *
360 * @return string
361 */
362 function taxopress_posttags_the_content($content = '')
363 {
364
365 $post_tags = taxopress_get_posttags_data();
366
367 if (count($post_tags) > 0) {
368 foreach ($post_tags as $post_tag) {
369
370 // Get option
371 $embedded = (isset($post_tag['embedded']) && is_array($post_tag['embedded']) && count($post_tag['embedded']) > 0) ? $post_tag['embedded'] : false;
372
373 if (!$embedded) {
374 continue;
375 }
376
377 $marker = false;
378 if (is_feed() && in_array('feed', $embedded)) {
379 $marker = true;
380 }elseif (is_home() && in_array('homeonly', $embedded)) {
381 $marker = true;
382 }elseif (is_feed() && in_array('blogonly', $embedded)) {
383 $marker = true;
384 }elseif (is_singular() && in_array('singleonly', $embedded)) {
385 $marker = true;
386 }elseif (is_singular() && in_array(get_post_type(), $embedded)) {
387 $marker = true;
388 }
389 if (true === $marker) {
390 $posttags_arg = build_query($post_tag);
391 $content .= SimpleTags_Client_PostTags::extendedPostTags($posttags_arg);
392 }
393 }
394 }
395
396 return $content;
397 }