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

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