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

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

377 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Fetch our TAXOPRESS SuggestTerms option.
4 *
5 * @return mixed
6 */
7 function taxopress_get_suggestterm_data()
8 {
9 return array_filter((array)apply_filters('taxopress_get_suggestterm_data', get_option('taxopress_suggestterms', []),
10 get_current_blog_id()));
11 }
12
13 /**
14 * Get the selected suggestterm from the $_POST global.
15 *
16 * @return bool|string False on no result, sanitized suggestterm if set.
17 * @internal
18 *
19 */
20 function taxopress_get_current_suggestterm()
21 {
22
23 $suggestterms = false;
24
25 if (!empty($_GET) && isset($_GET['taxopress_suggestterms'])) {
26 $suggestterms = sanitize_text_field($_GET['taxopress_suggestterms']);
27 } else {
28 $suggestterms = taxopress_get_suggestterm_data();
29 if (!empty($suggestterms)) {
30 // Will return the first array key.
31 $suggestterms = key($suggestterms);
32 }
33 }
34
35 /**
36 * Filters the current suggestterm to edit.
37 *
38 * @param string $suggestterms suggestterm slug.
39 */
40 return apply_filters('taxopress_current_suggestterm', $suggestterms);
41 }
42
43 /**
44 * Handle the save and deletion of suggestterm data.
45 */
46 function taxopress_process_suggestterm()
47 {
48
49 if (wp_doing_ajax()) {
50 return;
51 }
52
53 if (!is_admin()) {
54 return;
55 }
56
57 if(!current_user_can('simple_tags')){
58 return;
59 }
60
61 if (empty($_GET)) {
62 return;
63 }
64
65 if (!isset($_GET['page'])) {
66 return;
67 }
68 if ('st_suggestterms' !== $_GET['page']) {
69 return;
70 }
71
72 if (isset($_GET['new_suggestterm'])) {
73 if ((int)$_GET['new_suggestterm'] === 1) {
74 add_action('admin_notices', "taxopress_suggestterms_update_success_admin_notice");
75 add_filter('removable_query_args', 'taxopress_saved_suggestterm_filter_removable_query_args');
76 }
77 }
78
79 if (isset($_GET['deleted_suggestterm'])) {
80 if ((int)$_GET['deleted_suggestterm'] === 1) {
81 add_action('admin_notices', "taxopress_suggestterms_delete_success_admin_notice");
82 add_filter('removable_query_args', 'taxopress_deleted_suggestterm_filter_removable_query_args');
83 }
84 }
85
86
87 if (!empty($_POST) && isset($_POST['suggestterm_submit'])) {
88 $result = '';
89 if (isset($_POST['suggestterm_submit'])) {
90 check_admin_referer('taxopress_addedit_suggestterm_nonce_action',
91 'taxopress_addedit_suggestterm_nonce_field');
92 $result = taxopress_update_suggestterm($_POST);
93 }
94
95 if ($result) {
96 wp_safe_redirect(
97 add_query_arg(
98 [
99 'page' => 'st_suggestterms',
100 'add' => 'new_item',
101 'action' => 'edit',
102 'taxopress_suggestterms' => $result,
103 'new_suggestterm' => 1,
104 ],
105 taxopress_admin_url('admin.php')
106 )
107 );
108
109 exit();
110 }
111 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-suggestterm') {
112 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
113 if (wp_verify_nonce($nonce, 'suggestterm-action-request-nonce')) {
114 taxopress_action_delete_suggestterm(sanitize_text_field($_REQUEST['taxopress_suggestterms']));
115 }
116 add_filter('removable_query_args', 'taxopress_delete_suggestterm_filter_removable_query_args');
117 }
118 }
119
120 add_action('admin_init', 'taxopress_process_suggestterm', 8);
121
122
123 /**
124 * Create default suggestterm.
125 */
126 function taxopress_create_default_suggestterm()
127 {
128
129 if (wp_doing_ajax()) {
130 return;
131 }
132
133 if (!is_admin()) {
134 return;
135 }
136
137 if(!current_user_can('simple_tags')){
138 return;
139 }
140
141 if ((int)get_option('taxopress_default_suggestterms') > 0) {
142 return;
143 }
144
145 if (count(taxopress_get_suggestterm_data()) > 0) {
146 return;
147 }
148
149
150 $default = [];
151 $default['taxopress_suggestterm']['title'] = 'Suggest Term';
152 $default['taxopress_suggestterm']['taxonomy'] = 'post_tag';
153 $default['post_types'] = ['post'];
154 $default['taxopress_suggestterm']['number'] = '100';
155 $default['taxopress_suggestterm']['orderby'] = 'count';
156 $default['taxopress_suggestterm']['order'] = 'desc';
157 $default['taxopress_suggestterm']['enable_existing_terms'] = '1';
158 $default['taxopress_suggestterm']['suggest_term_use_local'] = '1';
159 $default['taxopress_suggestterm']['suggest_term_use_dandelion'] = '0';
160 $default['taxopress_suggestterm']['suggest_term_use_opencalais'] = '0';
161 $default['taxopress_suggestterm']['terms_opencalais_key'] = SimpleTags_Plugin::get_option_value( 'opencalais_key' );
162 $default['taxopress_suggestterm']['terms_datatxt_access_token'] = SimpleTags_Plugin::get_option_value( 'datatxt_access_token' );
163 $default['taxopress_suggestterm']['terms_datatxt_min_confidence'] = SimpleTags_Plugin::get_option_value( 'datatxt_min_confidence' );
164 $result = taxopress_update_suggestterm($default);
165
166 update_option('taxopress_default_suggestterms', $result);
167 }
168
169 add_action('admin_init', 'taxopress_create_default_suggestterm', 8);
170
171
172 /**
173 * Add to or update our TAXOPRESS option with new data.
174 *
175 *
176 * @param array $data Array of suggestterm data to update. Optional.
177 * @return bool|string False on failure, string on success.
178 * @internal
179 *
180 */
181 function taxopress_update_suggestterm($data = [])
182 {
183 foreach ($data as $key => $value) {
184
185 if (is_string($value)) {
186 $data[$key] = sanitize_text_field($value);
187 } else {
188 array_map('sanitize_text_field', $data[$key]);
189 }
190 }
191
192 $suggestterms = taxopress_get_suggestterm_data();
193
194 $title = $data['taxopress_suggestterm']['title'];
195 $title = str_replace('"', '', htmlspecialchars_decode($title));
196 $title = htmlspecialchars($title, ENT_QUOTES);
197 $title = trim($title);
198 $data['taxopress_suggestterm']['title'] = stripslashes_deep($title);
199
200
201 //update other post post
202 $data['taxopress_suggestterm']['post_types'] = isset($data['post_types']) ? $data['post_types'] : [];
203
204 //update our custom checkbox value if not checked
205 if (!isset($data['taxopress_suggestterm']['enable_existing_terms'])) {
206 $data['taxopress_suggestterm']['enable_existing_terms'] = 0;
207 }
208 if (!isset($data['taxopress_suggestterm']['suggest_term_use_local'])) {
209 $data['taxopress_suggestterm']['suggest_term_use_local'] = 0;
210 }
211 if (!isset($data['taxopress_suggestterm']['suggest_term_use_dandelion'])) {
212 $data['taxopress_suggestterm']['suggest_term_use_dandelion'] = 0;
213 }
214 if (!isset($data['taxopress_suggestterm']['suggest_term_use_opencalais'])) {
215 $data['taxopress_suggestterm']['suggest_term_use_opencalais'] = 0;
216 }
217
218 if (isset($data['edited_suggestterm'])) {
219 $suggestterm_id = $data['edited_suggestterm'];
220 $suggestterms[$suggestterm_id] = $data['taxopress_suggestterm'];
221 $success = update_option('taxopress_suggestterms', $suggestterms);
222 //return 'update_success';
223 } else {
224 $suggestterm_id = (int)get_option('taxopress_suggestterm_ids_increament') + 1;
225 $data['taxopress_suggestterm']['ID'] = $suggestterm_id;
226 $suggestterms[$suggestterm_id] = $data['taxopress_suggestterm'];
227 $success = update_option('taxopress_suggestterms', $suggestterms);
228 $update_id = update_option('taxopress_suggestterm_ids_increament', $suggestterm_id);
229 //return 'add_success';
230 }
231
232 return $suggestterm_id;
233 }
234
235 /**
236 * Successful update callback.
237 */
238 function taxopress_suggestterms_update_success_admin_notice()
239 {
240 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
241 echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags'));
242 }
243
244 /**
245 * Successful deleted callback.
246 */
247 function taxopress_suggestterms_delete_success_admin_notice()
248 {
249 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
250 echo taxopress_admin_notices_helper(esc_html__('Suggest Terms successfully deleted.', 'simple-tags'), false);
251 }
252
253 /**
254 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
255 *
256 * @link https://core.trac.wordpress.org/ticket/23367
257 *
258 * @param string[] $args Array of removable query arguments.
259 * @return string[] Updated array of removable query arguments.
260 */
261 function taxopress_saved_suggestterm_filter_removable_query_args(array $args)
262 {
263 return array_merge($args, [
264 'new_suggestterm',
265 ]);
266 }
267
268 /**
269 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
270 *
271 * @link https://core.trac.wordpress.org/ticket/23367
272 *
273 * @param string[] $args Array of removable query arguments.
274 * @return string[] Updated array of removable query arguments.
275 */
276 function taxopress_deleted_suggestterm_filter_removable_query_args(array $args)
277 {
278 return array_merge($args, [
279 'deleted_suggestterm',
280 ]);
281 }
282
283 /**
284 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
285 *
286 * @link https://core.trac.wordpress.org/ticket/23367
287 *
288 * @param string[] $args Array of removable query arguments.
289 * @return string[] Updated array of removable query arguments.
290 */
291 function taxopress_delete_suggestterm_filter_removable_query_args(array $args)
292 {
293 return array_merge($args, [
294 'action',
295 'taxopress_suggestterms',
296 '_wpnonce',
297 ]);
298 }
299
300 /**
301 * Delete our custom suggestterm from the array of suggestterms.
302 * @return bool|string False on failure, string on success.
303 */
304 function taxopress_action_delete_suggestterm($suggestterm_id)
305 {
306 $suggestterms = taxopress_get_suggestterm_data();
307
308 if (array_key_exists($suggestterm_id, $suggestterms)) {
309 unset($suggestterms[$suggestterm_id]);
310 $success = update_option('taxopress_suggestterms', $suggestterms);
311 }
312
313 if (isset($success)) {
314 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
315 wp_safe_redirect(
316 add_query_arg(
317 [
318 'page' => 'st_suggestterms',
319 'deleted_suggestterm' => 1,
320 ],
321 taxopress_admin_url('admin.php')
322 )
323 );
324 exit();
325 }
326 }
327
328 /**
329 * Get click tags for current post
330 *
331 *
332 * @return mixed
333 */
334 function taxopress_current_post_suggest_terms($source = false, $local_check = false)
335 {
336 global $pagenow;
337
338 $allowed_pages = ['post-new.php', 'post.php', 'page.php', 'page-new.php'];
339 if(!in_array($pagenow, $allowed_pages)){
340 return false;
341 }
342
343 $suggested_terms = taxopress_get_suggestterm_data();
344
345 if (count($suggested_terms) > 0) {
346 foreach ($suggested_terms as $suggested_term) {
347
348 // Get option
349 $post_types = (isset($suggested_term['post_types']) && is_array($suggested_term['post_types']) && count($suggested_term['post_types']) > 0) ? $suggested_term['post_types'] : false;
350
351 if (!$post_types) {
352 continue;
353 }
354
355 $enable_existing_terms = isset($suggested_term['enable_existing_terms']) ? (int)$suggested_term['enable_existing_terms'] : 0;
356 $suggest_term_use_local = isset($suggested_term['suggest_term_use_local']) ? (int)$suggested_term['suggest_term_use_local'] : 0;
357 $suggest_term_use_dandelion = isset($suggested_term['suggest_term_use_dandelion']) ? (int)$suggested_term['suggest_term_use_dandelion'] : 0;
358 $suggest_term_use_opencalais = isset($suggested_term['suggest_term_use_opencalais']) ? (int)$suggested_term['suggest_term_use_opencalais'] : 0;
359
360 if($source && $source !== 'existing_terms' && $suggest_term_use_local === 0 && $suggest_term_use_dandelion === 0 && $suggest_term_use_opencalais === 0){
361 continue;
362 }
363
364 if($source && $source === 'existing_terms' && $enable_existing_terms === 0){
365 continue;
366 }
367
368 if (in_array(get_post_type(), $post_types)) {
369 return $suggested_term;
370 }
371
372 }
373 }
374
375 return false;
376 }
377 ?>