PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.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.51.0, at inc/post-tags-functions.php

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