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

422 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 /**
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['autolink_submit'] = 'Add Auto Links';
197 $default['cpt_tax_status'] = 'new';
198 $default['taxopress_autolink']['enable_customurl_field'] = ['post_tag', 'category'];
199 $default['taxopress_autolink']['enable_custom_urls'] = '1';
200 $default['taxopress_autolink']['archivepage'] = '1';
201 $result = taxopress_update_autolink($default);
202 update_option('taxopress_default_autolinks', $result);
203 }
204
205 add_action('admin_init', 'taxopress_create_default_autolink', 8);
206
207
208 /**
209 * Add to or update our TAXOPRESS option with new data.
210 *
211 *
212 * @param array $data Array of autolink data to update. Optional.
213 * @return bool|string False on failure, string on success.
214 * @internal
215 *
216 */
217 function taxopress_update_autolink($data = [])
218 {
219 $sanitized_data = [];
220 foreach ($data as $key => $value) {
221 if (!is_array($value)) {
222 $sanitized_data[$key] = taxopress_sanitize_text_field($value);
223 } else {
224 $new_value = [];
225 foreach ($data[$key] as $option_key => $option_value) {
226 $new_value[$option_key] = taxopress_sanitize_text_field($option_value);
227 }
228 $sanitized_data[$key] = $new_value;
229 }
230 }
231 $data = $sanitized_data;
232
233 $autolinks = taxopress_get_autolink_data();
234
235 $title = $data['taxopress_autolink']['title'];
236 $title = str_replace('"', '', htmlspecialchars_decode($title));
237 $title = htmlspecialchars($title, ENT_QUOTES);
238 $title = trim($title);
239 $data['taxopress_autolink']['title'] = stripslashes_deep($title);
240
241 //update seperate post
242 $data['taxopress_autolink']['embedded'] = isset($data['embedded']) ? $data['embedded'] : [];
243 $data['taxopress_autolink']['html_exclusion'] = isset($data['html_exclusion']) ? $data['html_exclusion'] : [];
244 $data['taxopress_autolink']['html_exclusion_customs'] = isset($data['html_exclusion_customs']) ? $data['html_exclusion_customs'] : [];
245 $data['taxopress_autolink']['html_exclusion_customs_entry'] = isset($data['html_exclusion_customs_entry']) ? array_unique($data['html_exclusion_customs_entry']) : [];
246 $data['taxopress_autolink']['shortcodes_exclusion'] = isset($data['shortcodes_exclusion']) ? array_unique($data['shortcodes_exclusion']) : [];
247 $data['taxopress_autolink']['shortcodes_exclusion_entries'] = isset($data['shortcodes_exclusion_entries']) ? array_unique($data['shortcodes_exclusion_entries']) : [];
248 $data['taxopress_autolink']['blocks_exclusion'] = isset($data['blocks_exclusion']) ? array_unique($data['blocks_exclusion']) : [];
249 $data['taxopress_autolink']['blocks_exclusion_entries'] = isset($data['blocks_exclusion_entries']) ? $data['blocks_exclusion_entries'] : [];
250 $data['taxopress_autolink']['enable_customurl_field'] = isset($data['taxopress_autolink']['enable_customurl_field']) ? $data['taxopress_autolink']['enable_customurl_field'] : [];
251
252 //update our custom checkbox value if not checked
253 if (!isset($data['taxopress_autolink']['unattached_terms'])) {
254 $data['taxopress_autolink']['unattached_terms'] = 0;
255 }
256 if (!isset($data['taxopress_autolink']['ignore_case'])) { //auto set ignore case to true
257 $data['taxopress_autolink']['ignore_case'] = 1;
258 }
259 if (!isset($data['taxopress_autolink']['autolink_dom'])) {
260 $data['taxopress_autolink']['autolink_dom'] = 0;
261 }
262 if (!isset($data['taxopress_autolink']['synonyms_link'])) {
263 $data['taxopress_autolink']['synonyms_link'] = 0;
264 }
265 if (!isset($data['taxopress_autolink']['enable_customurl_field'])) {
266 $data['taxopress_autolink']['enable_customurl_field'] = isset($data['autolink_submit']) ? [] : ['post_tag', 'category'];
267 }
268
269 if (!isset($data['taxopress_autolink']['enable_custom_urls'])) {
270 $data['taxopress_autolink']['enable_custom_urls'] = 0;
271 }
272
273 if (!isset($data['taxopress_autolink']['archivepage'])) {
274 $data['taxopress_autolink']['archivepage'] = 0;
275 }
276
277
278
279 if (isset($data['edited_autolink'])) {
280 $autolink_id = $data['edited_autolink'];
281 $autolinks[$autolink_id] = $data['taxopress_autolink'];
282 $success = update_option('taxopress_autolinks', $autolinks);
283 } else {
284 $autolink_id = (int)get_option('taxopress_autolink_ids_increament') + 1;
285 $data['taxopress_autolink']['ID'] = $autolink_id;
286 $autolinks[$autolink_id] = $data['taxopress_autolink'];
287 $success = update_option('taxopress_autolinks', $autolinks);
288 $update_id = update_option('taxopress_autolink_ids_increament', $autolink_id);
289 }
290
291 return $autolink_id;
292 }
293
294 /**
295 * Successful update callback.
296 */
297 function taxopress_autolinks_update_success_admin_notice()
298 {
299 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
300 echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags'));
301 }
302
303 /**
304 * Successful deleted callback.
305 */
306 function taxopress_autolinks_delete_success_admin_notice()
307 {
308 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
309 echo taxopress_admin_notices_helper(esc_html__('Auto Links successfully deleted.', 'simple-tags'), false);
310 }
311
312 /**
313 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
314 *
315 * @link https://core.trac.wordpress.org/ticket/23367
316 *
317 * @param string[] $args Array of removable query arguments.
318 * @return string[] Updated array of removable query arguments.
319 */
320 function taxopress_saved_autolink_filter_removable_query_args(array $args)
321 {
322 return array_merge($args, [
323 'new_autolink',
324 ]);
325 }
326
327 /**
328 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
329 *
330 * @link https://core.trac.wordpress.org/ticket/23367
331 *
332 * @param string[] $args Array of removable query arguments.
333 * @return string[] Updated array of removable query arguments.
334 */
335 function taxopress_deleted_autolink_filter_removable_query_args(array $args)
336 {
337 return array_merge($args, [
338 'deleted_autolink',
339 ]);
340 }
341
342 /**
343 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
344 *
345 * @link https://core.trac.wordpress.org/ticket/23367
346 *
347 * @param string[] $args Array of removable query arguments.
348 * @return string[] Updated array of removable query arguments.
349 */
350 function taxopress_delete_autolink_filter_removable_query_args(array $args)
351 {
352 return array_merge($args, [
353 'action',
354 'taxopress_autolinks',
355 '_wpnonce',
356 ]);
357 }
358
359 /**
360 * Delete our custom autolink from the array of autolinks.
361 * @return bool|string False on failure, string on success.
362 */
363 function taxopress_action_delete_autolink($autolink_id)
364 {
365 $autolinks = taxopress_get_autolink_data();
366
367 if (array_key_exists($autolink_id, $autolinks)) {
368 unset($autolinks[$autolink_id]);
369 $success = update_option('taxopress_autolinks', $autolinks);
370 }
371
372 if (isset($success)) {
373 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
374 wp_safe_redirect(
375 add_query_arg(
376 [
377 'page' => 'st_autolinks',
378 'deleted_autolink' => 1,
379 ],
380 taxopress_admin_url('admin.php')
381 )
382 );
383 exit();
384 }
385 }
386
387 /**
388 * Get auto link for current post
389 *
390 *
391 * @return mixed
392 */
393 function taxopress_post_type_autolink_autolink()
394 {
395 global $pagenow;
396
397 $allowed_pages = ['post-new.php', 'post.php', 'page.php', 'page-new.php'];
398 if (!in_array($pagenow, $allowed_pages)) {
399 return false;
400 }
401
402 $autolinks = taxopress_get_autolink_data();
403
404 if (count($autolinks) > 0) {
405 foreach ($autolinks as $autolink) {
406
407 // Get option
408 $post_types = (isset($autolink['embedded']) && is_array($autolink['embedded']) && count($autolink['embedded']) > 0) ? $autolink['embedded'] : false;
409
410 if (!$post_types) {
411 continue;
412 }
413
414 if (in_array(get_post_type(), $post_types)) {
415 return $autolink;
416 }
417 }
418 }
419
420 return false;
421 }
422