PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.50.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.50.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 / autolinks-functions.php

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

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