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

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