PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / trunk
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms vtrunk
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 3.6.2 All 177 releases
simple-tags / inc / autolinks-functions.php

autolinks-functions.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms trunk, at inc/autolinks-functions.php

428 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable 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 Autolinks option.
7 *
8 * @return mixed
9 */
10 function taxopress_get_autolink_data()
11 {
12 static $cached_data = null;
13
14 if ($cached_data === null) {
15 $cached_data = array_filter((array)apply_filters(
16 'taxopress_get_autolinks_data',
17 get_option('taxopress_autolinks', []),
18 get_current_blog_id()
19 ));
20 }
21
22 return $cached_data;
23 }
24
25 function taxopress_clear_autolink_cache()
26 {
27 $reflection = new ReflectionFunction('taxopress_get_autolink_data');
28 $reflection->getStaticVariables()['cached_data'] = null;
29 }
30
31 add_action('taxopress_autolink_updated', 'taxopress_clear_autolink_cache');
32 add_action('taxopress_autolink_deleted', 'taxopress_clear_autolink_cache');
33
34 /**
35 * Get the selected autolink from the $_POST global.
36 *
37 * @return bool|string False on no result, sanitized autolink if set.
38 * @internal
39 *
40 */
41 function taxopress_get_current_autolink()
42 {
43
44 $autolinks = false;
45
46 if (!empty($_GET) && isset($_GET['taxopress_autolinks'])) {
47 $autolinks = sanitize_text_field(wp_unslash($_GET['taxopress_autolinks']));
48 } else {
49 $autolinks = taxopress_get_autolink_data();
50 if (!empty($autolinks)) {
51 // Will return the first array key.
52 $autolinks = key($autolinks);
53 }
54 }
55
56 /**
57 * Filters the current autolink to edit.
58 *
59 * @param string $autolinks autolink slug.
60 */
61 return apply_filters('taxopress_current_autolink', $autolinks);
62 }
63
64 /**
65 * Handle the save and deletion of autolink data.
66 */
67 function taxopress_process_autolink()
68 {
69
70 if (wp_doing_ajax()) {
71 return;
72 }
73
74 if (!is_admin()) {
75 return;
76 }
77
78 if (!current_user_can('simple_tags')) {
79 return;
80 }
81
82 if (empty($_GET)) {
83 return;
84 }
85
86 if (!isset($_GET['page'])) {
87 return;
88 }
89 if ('st_autolinks' !== $_GET['page']) {
90 return;
91 }
92
93 if (isset($_GET['new_autolink'])) {
94 if ((int)$_GET['new_autolink'] === 1) {
95 add_action('admin_notices', "taxopress_autolinks_update_success_admin_notice");
96 add_filter('removable_query_args', 'taxopress_saved_autolink_filter_removable_query_args');
97 }
98 }
99
100 if (isset($_GET['deleted_autolink'])) {
101 if ((int)$_GET['deleted_autolink'] === 1) {
102 add_action('admin_notices', "taxopress_autolinks_delete_success_admin_notice");
103 add_filter('removable_query_args', 'taxopress_deleted_autolink_filter_removable_query_args');
104 }
105 }
106
107
108 if (!empty($_POST) && isset($_POST['autolink_submit'])) {
109 $result = '';
110 if (isset($_POST['autolink_submit'])) {
111 check_admin_referer(
112 'taxopress_addedit_autolink_nonce_action',
113 'taxopress_addedit_autolink_nonce_field'
114 );
115 $result = taxopress_update_autolink($_POST);
116 }
117
118 if ($result) {
119 wp_safe_redirect(
120 add_query_arg(
121 [
122 'page' => 'st_autolinks',
123 'add' => 'new_item',
124 'action' => 'edit',
125 'taxopress_autolinks' => $result,
126 'new_autolink' => 1,
127 ],
128 taxopress_admin_url('admin.php')
129 )
130 );
131
132 exit();
133 }
134 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autolink') {
135 $nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce']));
136 if (wp_verify_nonce($nonce, 'autolink-action-request-nonce')) {
137 taxopress_action_delete_autolink(sanitize_text_field(wp_unslash($_REQUEST['taxopress_autolinks'])));
138 }
139 add_filter('removable_query_args', 'taxopress_delete_autolink_filter_removable_query_args');
140 }
141 }
142
143 add_action('admin_init', 'taxopress_process_autolink', 8);
144
145
146 /**
147 * Create default autolink.
148 */
149 function taxopress_create_default_autolink()
150 {
151
152 if (wp_doing_ajax()) {
153 return;
154 }
155
156 if (!is_admin()) {
157 return;
158 }
159
160 if (!current_user_can('simple_tags')) {
161 return;
162 }
163
164 if ((int)get_option('taxopress_default_autolinks') > 0) {
165 return;
166 }
167
168 if (count(taxopress_get_autolink_data()) > 0) {
169 return;
170 }
171
172 $default = [];
173 $default['taxopress_autolink']['title'] = esc_html__('Auto link', 'simple-tags');
174 $default['taxopress_autolink']['taxonomy'] = 'post_tag';
175 $default['taxopress_autolink']['autolink_case'] = 'none';
176 $default['taxopress_autolink']['autolink_display'] = 'post_content';
177 $default['taxopress_autolink']['autolink_title_attribute'] = __('Posts tagged with %s', 'simple-tags');
178 $default['taxopress_autolink']['autolink_title_attribute_when_using_custom_url'] = __('Visit this URL for more on %s', 'simple-tags');
179 $default['taxopress_autolink']['autolink_usage_min'] = '1';
180 $default['taxopress_autolink']['auto_link_exclude'] = '';
181 $default['taxopress_autolink']['autolink_usage_max'] = '10';
182 $default['taxopress_autolink']['autolink_same_usage_max'] = '1';
183 $default['taxopress_autolink']['autolink_min_char'] = '';
184 $default['taxopress_autolink']['autolink_max_char'] = '';
185 $default['taxopress_autolink']['autolink_exclude_class'] = '';
186 $default['taxopress_autolink']['hook_priority'] = '12';
187 $default['taxopress_autolink']['embedded'] = [];
188 $default['taxopress_autolink']['html_exclusion'] = [];
189 $default['taxopress_autolink']['html_exclusion_customs'] = [];
190 $default['taxopress_autolink']['html_exclusion_customs_entry'] = [];
191 $default['taxopress_autolink']['shortcodes_exclusion'] = [];
192 $default['taxopress_autolink']['shortcodes_exclusion_entries'] = [];
193 $default['taxopress_autolink']['blocks_exclusion'] = [];
194 $default['taxopress_autolink']['blocks_exclusion_entries'] = [];
195 $default['taxopress_autolink']['unattached_terms'] = '1';
196 $default['taxopress_autolink']['ignore_case'] = '1';
197 $default['taxopress_autolink']['autolink_dom'] = '1';
198 $default['taxopress_autolink']['synonyms_link'] = '0';
199 $default['taxopress_autolink']['whole_words'] = '1';
200 $default['autolink_submit'] = 'Add Auto Links';
201 $default['cpt_tax_status'] = 'new';
202 $default['taxopress_autolink']['enable_customurl_field'] = ['post_tag', 'category'];
203 $default['taxopress_autolink']['enable_custom_urls'] = '1';
204 $default['taxopress_autolink']['archivepage'] = '1';
205 $result = taxopress_update_autolink($default);
206 update_option('taxopress_default_autolinks', $result);
207 }
208
209 add_action('admin_init', 'taxopress_create_default_autolink', 8);
210
211
212 /**
213 * Add to or update our TAXOPRESS option with new data.
214 *
215 *
216 * @param array $data Array of autolink data to update. Optional.
217 * @return bool|string False on failure, string on success.
218 * @internal
219 *
220 */
221 function taxopress_update_autolink($data = [])
222 {
223 $sanitized_data = [];
224 foreach ($data as $key => $value) {
225 if (!is_array($value)) {
226 $sanitized_data[$key] = taxopress_sanitize_text_field($value);
227 } else {
228 $new_value = [];
229 foreach ($data[$key] as $option_key => $option_value) {
230 $new_value[$option_key] = taxopress_sanitize_text_field($option_value);
231 }
232 $sanitized_data[$key] = $new_value;
233 }
234 }
235 $data = $sanitized_data;
236
237 $autolinks = taxopress_get_autolink_data();
238
239 $title = $data['taxopress_autolink']['title'];
240 $title = str_replace('"', '', htmlspecialchars_decode($title));
241 $title = htmlspecialchars($title, ENT_QUOTES);
242 $title = trim($title);
243 $data['taxopress_autolink']['title'] = stripslashes_deep($title);
244
245 //update seperate post
246 $data['taxopress_autolink']['embedded'] = isset($data['embedded']) ? $data['embedded'] : [];
247 $data['taxopress_autolink']['html_exclusion'] = isset($data['html_exclusion']) ? $data['html_exclusion'] : [];
248 $data['taxopress_autolink']['html_exclusion_customs'] = isset($data['html_exclusion_customs']) ? $data['html_exclusion_customs'] : [];
249 $data['taxopress_autolink']['html_exclusion_customs_entry'] = isset($data['html_exclusion_customs_entry']) ? array_unique($data['html_exclusion_customs_entry']) : [];
250 $data['taxopress_autolink']['shortcodes_exclusion'] = isset($data['shortcodes_exclusion']) ? array_unique($data['shortcodes_exclusion']) : [];
251 $data['taxopress_autolink']['shortcodes_exclusion_entries'] = isset($data['shortcodes_exclusion_entries']) ? array_unique($data['shortcodes_exclusion_entries']) : [];
252 $data['taxopress_autolink']['blocks_exclusion'] = isset($data['blocks_exclusion']) ? array_unique($data['blocks_exclusion']) : [];
253 $data['taxopress_autolink']['blocks_exclusion_entries'] = isset($data['blocks_exclusion_entries']) ? $data['blocks_exclusion_entries'] : [];
254 $data['taxopress_autolink']['enable_customurl_field'] = isset($data['taxopress_autolink']['enable_customurl_field']) ? $data['taxopress_autolink']['enable_customurl_field'] : [];
255
256 //update our custom checkbox value if not checked
257 if (!isset($data['taxopress_autolink']['unattached_terms'])) {
258 $data['taxopress_autolink']['unattached_terms'] = 0;
259 }
260 if (!isset($data['taxopress_autolink']['ignore_case'])) { //auto set ignore case to true
261 $data['taxopress_autolink']['ignore_case'] = 1;
262 }
263 if (!isset($data['taxopress_autolink']['autolink_dom'])) {
264 $data['taxopress_autolink']['autolink_dom'] = 0;
265 }
266 if (!isset($data['taxopress_autolink']['synonyms_link'])) {
267 $data['taxopress_autolink']['synonyms_link'] = 0;
268 }
269 if (!isset($data['taxopress_autolink']['whole_words'])) {
270 $data['taxopress_autolink']['whole_words'] = 0;
271 }
272 if (!isset($data['taxopress_autolink']['enable_customurl_field'])) {
273 $data['taxopress_autolink']['enable_customurl_field'] = isset($data['autolink_submit']) ? [] : ['post_tag', 'category'];
274 }
275
276 if (!isset($data['taxopress_autolink']['enable_custom_urls'])) {
277 $data['taxopress_autolink']['enable_custom_urls'] = 0;
278 }
279
280 if (!isset($data['taxopress_autolink']['archivepage'])) {
281 $data['taxopress_autolink']['archivepage'] = 0;
282 }
283
284
285
286 if (isset($data['edited_autolink'])) {
287 $autolink_id = $data['edited_autolink'];
288 $autolinks[$autolink_id] = $data['taxopress_autolink'];
289 $success = update_option('taxopress_autolinks', $autolinks);
290 } else {
291 $autolink_id = (int)get_option('taxopress_autolink_ids_increament') + 1;
292 $data['taxopress_autolink']['ID'] = $autolink_id;
293 $autolinks[$autolink_id] = $data['taxopress_autolink'];
294 $success = update_option('taxopress_autolinks', $autolinks);
295 $update_id = update_option('taxopress_autolink_ids_increament', $autolink_id);
296 }
297
298 return $autolink_id;
299 }
300
301 /**
302 * Successful update callback.
303 */
304 function taxopress_autolinks_update_success_admin_notice()
305 {
306 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
307 echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags'));
308 }
309
310 /**
311 * Successful deleted callback.
312 */
313 function taxopress_autolinks_delete_success_admin_notice()
314 {
315 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
316 echo taxopress_admin_notices_helper(esc_html__('Auto Links successfully deleted.', 'simple-tags'), false);
317 }
318
319 /**
320 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
321 *
322 * @link https://core.trac.wordpress.org/ticket/23367
323 *
324 * @param string[] $args Array of removable query arguments.
325 * @return string[] Updated array of removable query arguments.
326 */
327 function taxopress_saved_autolink_filter_removable_query_args(array $args)
328 {
329 return array_merge($args, [
330 'new_autolink',
331 ]);
332 }
333
334 /**
335 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
336 *
337 * @link https://core.trac.wordpress.org/ticket/23367
338 *
339 * @param string[] $args Array of removable query arguments.
340 * @return string[] Updated array of removable query arguments.
341 */
342 function taxopress_deleted_autolink_filter_removable_query_args(array $args)
343 {
344 return array_merge($args, [
345 'deleted_autolink',
346 ]);
347 }
348
349 /**
350 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
351 *
352 * @link https://core.trac.wordpress.org/ticket/23367
353 *
354 * @param string[] $args Array of removable query arguments.
355 * @return string[] Updated array of removable query arguments.
356 */
357 function taxopress_delete_autolink_filter_removable_query_args(array $args)
358 {
359 return array_merge($args, [
360 'action',
361 'taxopress_autolinks',
362 '_wpnonce',
363 ]);
364 }
365
366 /**
367 * Delete our custom autolink from the array of autolinks.
368 * @return bool|string False on failure, string on success.
369 */
370 function taxopress_action_delete_autolink($autolink_id)
371 {
372 $autolinks = taxopress_get_autolink_data();
373
374 if (array_key_exists($autolink_id, $autolinks)) {
375 unset($autolinks[$autolink_id]);
376 $success = update_option('taxopress_autolinks', $autolinks);
377 }
378
379 if (isset($success)) {
380 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
381 wp_safe_redirect(
382 add_query_arg(
383 [
384 'page' => 'st_autolinks',
385 'deleted_autolink' => 1,
386 ],
387 taxopress_admin_url('admin.php')
388 )
389 );
390 exit();
391 }
392 }
393
394 /**
395 * Get auto link for current post
396 *
397 *
398 * @return mixed
399 */
400 function taxopress_post_type_autolink_autolink()
401 {
402 global $pagenow;
403
404 $allowed_pages = ['post-new.php', 'post.php', 'page.php', 'page-new.php'];
405 if (!in_array($pagenow, $allowed_pages)) {
406 return false;
407 }
408
409 $autolinks = taxopress_get_autolink_data();
410
411 if (count($autolinks) > 0) {
412 foreach ($autolinks as $autolink) {
413 // Get option
414 $post_types = (isset($autolink['embedded']) && is_array($autolink['embedded']) && count($autolink['embedded']) > 0) ? $autolink['embedded'] : false;
415
416 if (!$post_types) {
417 continue;
418 }
419
420 if (in_array(get_post_type(), $post_types)) {
421 return $autolink;
422 }
423 }
424 }
425
426 return false;
427 }
428