| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Squiz.PHP.CommentedOutCode.Found,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.SlowDBQuery.slow_db_query_meta_query,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 Autoterms option. |
| 7 |
* |
| 8 |
* @return mixed |
| 9 |
*/ |
| 10 |
function taxopress_get_autoterm_data() |
| 11 |
{ |
| 12 |
return array_filter((array)apply_filters( |
| 13 |
'taxopress_get_autoterm_data', |
| 14 |
get_option('taxopress_autoterms', []), |
| 15 |
get_current_blog_id() |
| 16 |
)); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Fetch our TAXOPRESS Autoterms content option. |
| 21 |
* |
| 22 |
* @return mixed |
| 23 |
*/ |
| 24 |
function taxopress_get_autoterms_content_data() |
| 25 |
{ |
| 26 |
return array_filter((array)apply_filters( |
| 27 |
'taxopress_get_autoterm_content_data', |
| 28 |
get_option('taxopress_autoterms_content', []), |
| 29 |
get_current_blog_id() |
| 30 |
)); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Fetch our TAXOPRESS Autoterms schedule option. |
| 35 |
* |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
function taxopress_get_autoterms_schedule_data() |
| 39 |
{ |
| 40 |
return array_filter((array)apply_filters( |
| 41 |
'taxopress_get_autoterm_schedule_data', |
| 42 |
get_option('taxopress_autoterms_schedule', []), |
| 43 |
get_current_blog_id() |
| 44 |
)); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the selected autoterm from the $_POST global. |
| 49 |
* |
| 50 |
* @return bool|string False on no result, sanitized autoterm if set. |
| 51 |
* @internal |
| 52 |
* |
| 53 |
*/ |
| 54 |
function taxopress_get_current_autoterm() |
| 55 |
{ |
| 56 |
|
| 57 |
$autoterms = false; |
| 58 |
|
| 59 |
if (!empty($_GET) && isset($_GET['taxopress_autoterms'])) { |
| 60 |
$autoterms = sanitize_text_field(wp_unslash($_GET['taxopress_autoterms'])); |
| 61 |
} else { |
| 62 |
$autoterms = taxopress_get_autoterm_data(); |
| 63 |
if (!empty($autoterms)) { |
| 64 |
// Will return the first array key. |
| 65 |
$autoterms = key($autoterms); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Filters the current autoterm to edit. |
| 71 |
* |
| 72 |
* @param string $autoterms autoterm slug. |
| 73 |
*/ |
| 74 |
return apply_filters('taxopress_current_autoterm', $autoterms); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get auto terms for current post |
| 79 |
* |
| 80 |
* |
| 81 |
* @return mixed |
| 82 |
*/ |
| 83 |
function taxopress_post_type_autoterms() |
| 84 |
{ |
| 85 |
global $pagenow; |
| 86 |
|
| 87 |
$allowed_pages = ['post-new.php', 'post.php', 'page.php', 'page-new.php']; |
| 88 |
if (!in_array($pagenow, $allowed_pages)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
$autoterms = taxopress_get_autoterm_data(); |
| 93 |
|
| 94 |
if (count($autoterms) > 0) { |
| 95 |
foreach ($autoterms as $autoterm) { |
| 96 |
$post_types = (isset($autoterm['post_types']) && is_array($autoterm['post_types']) && count($autoterm['post_types']) > 0) ? $autoterm['post_types'] : false; |
| 97 |
|
| 98 |
if (!$post_types) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if (in_array(get_post_type(), $post_types)) { |
| 103 |
return $autoterm; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Handle the save and deletion of autoterm data. |
| 113 |
*/ |
| 114 |
function taxopress_process_autoterm() |
| 115 |
{ |
| 116 |
|
| 117 |
if (wp_doing_ajax()) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (!is_admin()) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
if (!current_user_can('simple_tags')) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (empty($_GET)) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if (!isset($_GET['page'])) { |
| 134 |
return; |
| 135 |
} |
| 136 |
if (!in_array($_GET['page'], ['st_autoterms', 'st_autoterms_schedule'])) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if (isset($_GET['new_autoterm'])) { |
| 141 |
if ((int)$_GET['new_autoterm'] === 1) { |
| 142 |
add_action('admin_notices', "taxopress_autoterms_update_success_admin_notice"); |
| 143 |
add_filter('removable_query_args', 'taxopress_saved_autoterm_filter_removable_query_args'); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (isset($_GET['deleted_autoterm'])) { |
| 148 |
if ((int)$_GET['deleted_autoterm'] === 1) { |
| 149 |
add_action('admin_notices', "taxopress_autoterms_delete_success_admin_notice"); |
| 150 |
add_filter('removable_query_args', 'taxopress_deleted_autoterm_filter_removable_query_args'); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
if (!empty($_POST) && isset($_POST['autoterm_submit'])) { |
| 156 |
$result = ''; |
| 157 |
if (isset($_POST['autoterm_submit'])) { |
| 158 |
check_admin_referer( |
| 159 |
'taxopress_addedit_autoterm_nonce_action', |
| 160 |
'taxopress_addedit_autoterm_nonce_field' |
| 161 |
); |
| 162 |
$result = taxopress_update_autoterm($_POST); |
| 163 |
} |
| 164 |
|
| 165 |
if ($result) { |
| 166 |
wp_safe_redirect( |
| 167 |
add_query_arg( |
| 168 |
[ |
| 169 |
'page' => 'st_autoterms', |
| 170 |
'add' => 'new_item', |
| 171 |
'action' => 'edit', |
| 172 |
'taxopress_autoterms' => $result, |
| 173 |
'new_autoterm' => 1, |
| 174 |
], |
| 175 |
taxopress_admin_url('admin.php') |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
exit(); |
| 180 |
} |
| 181 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm') { |
| 182 |
$nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); |
| 183 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 184 |
taxopress_action_delete_autoterm(sanitize_text_field(wp_unslash($_REQUEST['taxopress_autoterms']))); |
| 185 |
add_action('admin_notices', "taxopress_autoterms_delete_autoterm_admin_notice"); |
| 186 |
} |
| 187 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_filter_removable_query_args'); |
| 188 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm-log') { |
| 189 |
$nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); |
| 190 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 191 |
wp_delete_post((int)$_REQUEST['taxopress_autoterms_log'], true); |
| 192 |
add_action('admin_notices', "taxopress_autoterms_delete_autoterm_log_admin_notice"); |
| 193 |
} |
| 194 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args'); |
| 195 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm-logs') { |
| 196 |
$nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); |
| 197 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 198 |
global $wpdb; |
| 199 |
$result = $wpdb->query( |
| 200 |
$wpdb->prepare( |
| 201 |
" |
| 202 |
DELETE posts, pt, pm |
| 203 |
FROM {$wpdb->prefix}posts posts |
| 204 |
LEFT JOIN {$wpdb->prefix}term_relationships pt ON pt.object_id = posts.ID |
| 205 |
LEFT JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = posts.ID |
| 206 |
WHERE posts.post_type = %s |
| 207 |
", |
| 208 |
'taxopress_logs' |
| 209 |
) |
| 210 |
); |
| 211 |
add_action('admin_notices', "taxopress_autoterms_delete_autoterm_logs_admin_notice"); |
| 212 |
} |
| 213 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args'); |
| 214 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-enable-autoterm-logs') { |
| 215 |
$nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); |
| 216 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 217 |
delete_option('taxopress_autoterms_logs_disabled'); |
| 218 |
add_action('admin_notices', "taxopress_autoterms_enable_log_admin_notice"); |
| 219 |
} |
| 220 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args'); |
| 221 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-disable-autoterm-logs') { |
| 222 |
$nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); |
| 223 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 224 |
update_option('taxopress_autoterms_logs_disabled', 1); |
| 225 |
add_action('admin_notices', "taxopress_autoterms_disable_log_admin_notice"); |
| 226 |
} |
| 227 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args'); |
| 228 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-update-autoterm-limit') { |
| 229 |
$nonce = sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])); |
| 230 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 231 |
$limit = (int)$_REQUEST['limit']; |
| 232 |
if ($limit > 0) { |
| 233 |
update_option('taxopress_auto_terms_logs_limit', $limit); |
| 234 |
add_action('admin_notices', "taxopress_autoterms_limit_updated_admin_notice"); |
| 235 |
} else { |
| 236 |
add_action('admin_notices', "taxopress_autoterms_limit_invalid_admin_notice"); |
| 237 |
} |
| 238 |
} |
| 239 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args'); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
add_action('admin_init', 'taxopress_process_autoterm', 8); |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Create default autoterm. |
| 248 |
*/ |
| 249 |
function taxopress_create_default_autoterm() |
| 250 |
{ |
| 251 |
|
| 252 |
if (wp_doing_ajax()) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
if (!is_admin()) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
if (!current_user_can('simple_tags')) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if ((int)get_option('taxopress_default_autoterms') > 0) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
if (count(taxopress_get_autoterm_data()) > 0) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
//create default or export previous |
| 273 |
$create_default = true; |
| 274 |
|
| 275 |
$options = get_option(STAGS_OPTIONS_NAME_AUTO); |
| 276 |
if (is_array($options) && count($options) > 0) { |
| 277 |
foreach ($options as $options_post_type => $options_taxdata) { |
| 278 |
if (is_array($options_taxdata) && count($options_taxdata) > 0) { |
| 279 |
$create_default = false; |
| 280 |
foreach ($options_taxdata as $options_taxonomy => $options_taxonomy_data) { |
| 281 |
$taxonomy = get_taxonomy($options_taxonomy); |
| 282 |
$default = []; |
| 283 |
$default['taxopress_autoterm']['autoterm_from'] = 'posts'; |
| 284 |
$default['taxopress_autoterm']['title'] = '' . (is_object($taxonomy) ? $taxonomy->labels->name : $options_taxonomy) . ' ' . ucwords($options_post_type) . ' Auto term'; |
| 285 |
$default['taxopress_autoterm']['taxonomy'] = $options_taxonomy; |
| 286 |
$default['post_types'] = []; |
| 287 |
$default['post_status'] = ['publish']; |
| 288 |
$default['taxopress_autoterm']['autoterm_useall'] = isset($options_taxonomy_data['at_all']) ? $options_taxonomy_data['at_all'] : 0; |
| 289 |
$default['taxopress_autoterm']['autoterm_useonly'] = isset($options_taxonomy_data['at_all_no']) ? $options_taxonomy_data['at_all_no'] : 0; |
| 290 |
$default['taxopress_autoterm']['autoterm_target'] = isset($options_taxonomy_data['at_empty']) ? $options_taxonomy_data['at_empty'] : 0; |
| 291 |
$default['taxopress_autoterm']['autoterm_word'] = isset($options_taxonomy_data['only_full_word']) ? $options_taxonomy_data['only_full_word'] : 0; |
| 292 |
$default['taxopress_autoterm']['autoterm_hash'] = isset($options_taxonomy_data['allow_hashtag_format']) ? $options_taxonomy_data['allow_hashtag_format'] : 0; |
| 293 |
$default['specific_terms'] = isset($options_taxonomy_data['auto_list']) ? taxopress_sanitize_specific_terms($options_taxonomy_data['auto_list']) : []; |
| 294 |
$default['taxopress_autoterm']['terms_limit'] = '5'; |
| 295 |
$default['taxopress_autoterm']['synonyms_term'] = 1; |
| 296 |
|
| 297 |
$result = taxopress_update_autoterm($default); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
if ($create_default) { |
| 304 |
$default = []; |
| 305 |
$default['taxopress_autoterm']['title'] = esc_html__('Auto term', 'simple-tags'); |
| 306 |
$default['taxopress_autoterm']['taxonomy'] = 'post_tag'; |
| 307 |
$default['post_types'] = []; |
| 308 |
$default['post_status'] = ['publish']; |
| 309 |
$default['taxopress_autoterm']['autoterm_from'] = 'posts'; |
| 310 |
$default['taxopress_autoterm']['autoterm_use_taxonomy'] = '1'; |
| 311 |
$default['taxopress_autoterm']['autoterm_useall'] = '1'; |
| 312 |
$default['taxopress_autoterm']['autoterm_useonly'] = '0'; |
| 313 |
$default['taxopress_autoterm']['autoterm_target'] = '0'; |
| 314 |
$default['taxopress_autoterm']['autoterm_word'] = '0'; |
| 315 |
$default['taxopress_autoterm']['autoterm_hash'] = '0'; |
| 316 |
$default['taxopress_autoterm']['terms_limit'] = '5'; |
| 317 |
$default['taxopress_autoterm']['synonyms_term'] = 1; |
| 318 |
$default['taxopress_autoterm']['autoterm_for_post'] = '1'; |
| 319 |
$default['taxopress_autoterm']['autoterm_for_schedule'] = '1'; |
| 320 |
$default['taxopress_autoterm']['autoterm_for_existing_content'] = '1'; |
| 321 |
$default['taxopress_autoterm']['autoterm_for_metaboxes'] = '1'; |
| 322 |
$default['taxopress_autoterm']['schedule_terms_limit'] = '5'; |
| 323 |
$default['taxopress_autoterm']['schedule_autoterm_target'] = '0'; |
| 324 |
$default['taxopress_autoterm']['schedule_autoterm_word'] = '0'; |
| 325 |
$default['taxopress_autoterm']['schedule_autoterm_hash'] = '0'; |
| 326 |
$default['taxopress_autoterm']['existing_content_terms_limit'] = '5'; |
| 327 |
$default['specific_terms'] = []; |
| 328 |
$default['find_in_customs_entries'] = []; |
| 329 |
$default['find_in_custom_fields_custom_items'] = []; |
| 330 |
$default['find_in_taxonomies_custom_items'] = []; |
| 331 |
$result = taxopress_update_autoterm($default); |
| 332 |
} |
| 333 |
update_option('taxopress_default_autoterms', $result); |
| 334 |
} |
| 335 |
|
| 336 |
add_action('admin_init', 'taxopress_create_default_autoterm', 8); |
| 337 |
|
| 338 |
|
| 339 |
/** |
| 340 |
* Normalize Auto Terms' specific-term setting without accepting serialized data. |
| 341 |
* |
| 342 |
* @param mixed $terms Specific terms from a request or saved option. |
| 343 |
* @return array |
| 344 |
*/ |
| 345 |
function taxopress_sanitize_specific_terms($terms) |
| 346 |
{ |
| 347 |
if (!is_array($terms)) { |
| 348 |
if (!is_scalar($terms)) { |
| 349 |
return []; |
| 350 |
} |
| 351 |
|
| 352 |
$terms = wp_unslash((string) $terms); |
| 353 |
if (is_serialized($terms)) { |
| 354 |
return []; |
| 355 |
} |
| 356 |
|
| 357 |
$terms = taxopress_change_to_array($terms); |
| 358 |
} |
| 359 |
|
| 360 |
$sanitized_terms = []; |
| 361 |
foreach ($terms as $term) { |
| 362 |
if (!is_scalar($term)) { |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
$term = sanitize_text_field(wp_unslash((string) $term)); |
| 367 |
if ($term !== '') { |
| 368 |
$sanitized_terms[] = $term; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return array_values(array_unique($sanitized_terms)); |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Add to or update our TAXOPRESS option with new data. |
| 377 |
* |
| 378 |
* |
| 379 |
* @param array $data Array of autoterm data to update. Optional. |
| 380 |
* @return bool|string False on failure, string on success. |
| 381 |
* @internal |
| 382 |
* |
| 383 |
*/ |
| 384 |
function taxopress_update_autoterm($data = []) |
| 385 |
{ |
| 386 |
$sanitized_data = []; |
| 387 |
foreach ($data as $key => $value) { |
| 388 |
if ($key === 'specific_terms') { |
| 389 |
$sanitized_data[$key] = taxopress_sanitize_specific_terms($value); |
| 390 |
} elseif (!is_array($value)) { |
| 391 |
$sanitized_data[$key] = taxopress_sanitize_text_field($value); |
| 392 |
} else { |
| 393 |
$new_value = []; |
| 394 |
foreach ($data[$key] as $option_key => $option_value) { |
| 395 |
if ($option_key === 'terms_regex_code') { |
| 396 |
$regex_pattern = sanitize_text_field($option_value); |
| 397 |
if (!empty($regex_pattern) && preg_match($regex_pattern, '') !== false) { |
| 398 |
$new_value[$option_key] = $regex_pattern; |
| 399 |
} else { |
| 400 |
$new_value[$option_key] = '';//invalid expression |
| 401 |
} |
| 402 |
} elseif ($option_key === 'post_types' || $option_key === 'post_status') { |
| 403 |
$new_value[$option_key] = taxopress_sanitize_post_type_status($option_value); |
| 404 |
} else { |
| 405 |
$new_value[$option_key] = taxopress_sanitize_text_field($option_value); |
| 406 |
} |
| 407 |
} |
| 408 |
$sanitized_data[$key] = $new_value; |
| 409 |
} |
| 410 |
} |
| 411 |
$data = $sanitized_data; |
| 412 |
|
| 413 |
$autoterms = taxopress_get_autoterm_data(); |
| 414 |
|
| 415 |
// API credentials are write-only and may only be changed by plugin administrators. |
| 416 |
// A blank submitted value intentionally preserves the existing secret. |
| 417 |
$protected_fields = [ |
| 418 |
'open_ai_api_key', |
| 419 |
'ibm_watson_api_key', |
| 420 |
'dandelion_api_token', |
| 421 |
'open_calais_api_key', |
| 422 |
'ibm_watson_api_url', |
| 423 |
]; |
| 424 |
$edited_autoterm = isset($data['edited_autoterm']) ? (int) $data['edited_autoterm'] : 0; |
| 425 |
$existing_autoterm = $edited_autoterm && isset($autoterms[$edited_autoterm]) |
| 426 |
? $autoterms[$edited_autoterm] |
| 427 |
: []; |
| 428 |
|
| 429 |
foreach ($protected_fields as $protected_field) { |
| 430 |
$submitted_value = isset($data['taxopress_autoterm'][$protected_field]) |
| 431 |
? trim($data['taxopress_autoterm'][$protected_field]) |
| 432 |
: ''; |
| 433 |
|
| 434 |
if (!current_user_can('admin_simple_tags') || $submitted_value === '') { |
| 435 |
if (isset($existing_autoterm[$protected_field])) { |
| 436 |
$data['taxopress_autoterm'][$protected_field] = $existing_autoterm[$protected_field]; |
| 437 |
} else { |
| 438 |
unset($data['taxopress_autoterm'][$protected_field]); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
$title = $data['taxopress_autoterm']['title']; |
| 444 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 445 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 446 |
$title = trim($title); |
| 447 |
$data['taxopress_autoterm']['title'] = stripslashes_deep($title); |
| 448 |
|
| 449 |
|
| 450 |
//update other post post |
| 451 |
$data['taxopress_autoterm']['specific_terms'] = isset($data['specific_terms']) ? $data['specific_terms'] : []; |
| 452 |
$data['taxopress_autoterm']['post_types'] = isset($data['post_types']) ? $data['post_types'] : []; |
| 453 |
$data['taxopress_autoterm']['post_status'] = isset($data['post_status']) ? $data['post_status'] : []; |
| 454 |
|
| 455 |
$data['taxopress_autoterm']['html_exclusion'] = isset($data['html_exclusion']) ? $data['html_exclusion'] : []; |
| 456 |
$data['taxopress_autoterm']['html_exclusion_customs'] = isset($data['html_exclusion_customs']) ? $data['html_exclusion_customs'] : []; |
| 457 |
$data['taxopress_autoterm']['html_exclusion_customs_entry'] = isset($data['html_exclusion_customs_entry']) ? array_unique($data['html_exclusion_customs_entry']) : []; |
| 458 |
|
| 459 |
$data['taxopress_autoterm']['find_in_customs_entries'] = isset($data['find_in_customs_entries']) ? $data['find_in_customs_entries'] : []; |
| 460 |
$data['taxopress_autoterm']['find_in_custom_fields_custom_items'] = isset($data['find_in_custom_fields_custom_items']) ? $data['find_in_custom_fields_custom_items'] : []; |
| 461 |
$data['taxopress_autoterm']['find_in_taxonomies_custom_items'] = isset($data['find_in_taxonomies_custom_items']) ? $data['find_in_taxonomies_custom_items'] : []; |
| 462 |
|
| 463 |
|
| 464 |
//update our custom checkbox value if not checked |
| 465 |
if (!isset($data['taxopress_autoterm']['autoterm_useall'])) { |
| 466 |
$data['taxopress_autoterm']['autoterm_useall'] = 0; |
| 467 |
} |
| 468 |
if (!isset($data['taxopress_autoterm']['autoterm_useonly'])) { |
| 469 |
$data['taxopress_autoterm']['autoterm_useonly'] = 0; |
| 470 |
} |
| 471 |
if (!isset($data['taxopress_autoterm']['autoterm_target'])) { |
| 472 |
$data['taxopress_autoterm']['autoterm_target'] = 0; |
| 473 |
} |
| 474 |
if (!isset($data['taxopress_autoterm']['autoterm_word'])) { |
| 475 |
$data['taxopress_autoterm']['autoterm_word'] = 0; |
| 476 |
} |
| 477 |
if (!isset($data['taxopress_autoterm']['autoterm_hash'])) { |
| 478 |
$data['taxopress_autoterm']['autoterm_hash'] = 0; |
| 479 |
} |
| 480 |
if (!isset($data['taxopress_autoterm']['synonyms_term'])) { |
| 481 |
$data['taxopress_autoterm']['synonyms_term'] = 0; |
| 482 |
} |
| 483 |
if (!isset($data['taxopress_autoterm']['autoterm_for_post'])) { |
| 484 |
$data['taxopress_autoterm']['autoterm_for_post'] = 0; |
| 485 |
} |
| 486 |
if (!isset($data['taxopress_autoterm']['autoterm_for_schedule'])) { |
| 487 |
$data['taxopress_autoterm']['autoterm_for_schedule'] = 0; |
| 488 |
} |
| 489 |
if (!isset($data['taxopress_autoterm']['autoterm_for_existing_content'])) { |
| 490 |
$data['taxopress_autoterm']['autoterm_for_existing_content'] = 0; |
| 491 |
} |
| 492 |
if (!isset($data['taxopress_autoterm']['autoterm_for_metaboxes'])) { |
| 493 |
$data['taxopress_autoterm']['autoterm_for_metaboxes'] = 0; |
| 494 |
} |
| 495 |
if (!isset($data['taxopress_autoterm']['schedule_autoterm_target'])) { |
| 496 |
$data['taxopress_autoterm']['schedule_autoterm_target'] = 0; |
| 497 |
} |
| 498 |
if (!isset($data['taxopress_autoterm']['schedule_autoterm_word'])) { |
| 499 |
$data['taxopress_autoterm']['schedule_autoterm_word'] = 0; |
| 500 |
} |
| 501 |
if (!isset($data['taxopress_autoterm']['schedule_autoterm_hash'])) { |
| 502 |
$data['taxopress_autoterm']['schedule_autoterm_hash'] = 0; |
| 503 |
} |
| 504 |
|
| 505 |
if (isset($data['edited_autoterm'])) { |
| 506 |
$autoterm_id = $data['edited_autoterm']; |
| 507 |
$autoterms[$autoterm_id] = $data['taxopress_autoterm']; |
| 508 |
$success = update_option('taxopress_autoterms', $autoterms); |
| 509 |
//return 'update_success'; |
| 510 |
} else { |
| 511 |
$autoterm_id = (int)get_option('taxopress_autoterm_ids_increament') + 1; |
| 512 |
$data['taxopress_autoterm']['ID'] = $autoterm_id; |
| 513 |
$autoterms[$autoterm_id] = $data['taxopress_autoterm']; |
| 514 |
$success = update_option('taxopress_autoterms', $autoterms); |
| 515 |
$update_id = update_option('taxopress_autoterm_ids_increament', $autoterm_id); |
| 516 |
//return 'add_success'; |
| 517 |
} |
| 518 |
|
| 519 |
return $autoterm_id; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Successful update callback. |
| 524 |
*/ |
| 525 |
function taxopress_autoterms_update_success_admin_notice() |
| 526 |
{ |
| 527 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 528 |
echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags')); |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Successful deleted callback. |
| 533 |
*/ |
| 534 |
function taxopress_autoterms_delete_success_admin_notice() |
| 535 |
{ |
| 536 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 537 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms successfully deleted.', 'simple-tags'), false); |
| 538 |
} |
| 539 |
|
| 540 |
function taxopress_autoterms_enable_log_admin_notice() |
| 541 |
{ |
| 542 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 543 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms logs enabled successfully.', 'simple-tags')); |
| 544 |
} |
| 545 |
|
| 546 |
function taxopress_autoterms_disable_log_admin_notice() |
| 547 |
{ |
| 548 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 549 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms logs disabled successfully.', 'simple-tags')); |
| 550 |
} |
| 551 |
|
| 552 |
function taxopress_autoterms_delete_autoterm_admin_notice() |
| 553 |
{ |
| 554 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 555 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms deleted successfully.', 'simple-tags')); |
| 556 |
} |
| 557 |
|
| 558 |
function taxopress_autoterms_delete_autoterm_log_admin_notice() |
| 559 |
{ |
| 560 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 561 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms log deleted successfully.', 'simple-tags')); |
| 562 |
} |
| 563 |
|
| 564 |
function taxopress_autoterms_delete_autoterm_logs_admin_notice() |
| 565 |
{ |
| 566 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 567 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms logs deleted successfully.', 'simple-tags')); |
| 568 |
} |
| 569 |
|
| 570 |
function taxopress_autoterms_limit_updated_admin_notice() |
| 571 |
{ |
| 572 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 573 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms limit updated successfully.', 'simple-tags')); |
| 574 |
} |
| 575 |
|
| 576 |
function taxopress_autoterms_limit_invalid_admin_notice() |
| 577 |
{ |
| 578 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 579 |
echo taxopress_admin_notices_helper(esc_html__('Auto Terms limit must be greater than 0.', 'simple-tags'), false); |
| 580 |
} |
| 581 |
|
| 582 |
/** |
| 583 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 584 |
* |
| 585 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 586 |
* |
| 587 |
* @param string[] $args Array of removable query arguments. |
| 588 |
* @return string[] Updated array of removable query arguments. |
| 589 |
*/ |
| 590 |
function taxopress_saved_autoterm_filter_removable_query_args(array $args) |
| 591 |
{ |
| 592 |
return array_merge($args, [ |
| 593 |
'new_autoterm', |
| 594 |
]); |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 599 |
* |
| 600 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 601 |
* |
| 602 |
* @param string[] $args Array of removable query arguments. |
| 603 |
* @return string[] Updated array of removable query arguments. |
| 604 |
*/ |
| 605 |
function taxopress_deleted_autoterm_filter_removable_query_args(array $args) |
| 606 |
{ |
| 607 |
return array_merge($args, [ |
| 608 |
'deleted_autoterm', |
| 609 |
]); |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 614 |
* |
| 615 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 616 |
* |
| 617 |
* @param string[] $args Array of removable query arguments. |
| 618 |
* @return string[] Updated array of removable query arguments. |
| 619 |
*/ |
| 620 |
function taxopress_delete_autoterm_filter_removable_query_args(array $args) |
| 621 |
{ |
| 622 |
return array_merge($args, [ |
| 623 |
'action', |
| 624 |
'taxopress_autoterms', |
| 625 |
'_wpnonce', |
| 626 |
]); |
| 627 |
} |
| 628 |
|
| 629 |
/** |
| 630 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 631 |
* |
| 632 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 633 |
* |
| 634 |
* @param string[] $args Array of removable query arguments. |
| 635 |
* @return string[] Updated array of removable query arguments. |
| 636 |
*/ |
| 637 |
function taxopress_delete_autoterm_log_filter_removable_query_args(array $args) |
| 638 |
{ |
| 639 |
return array_merge($args, [ |
| 640 |
'action', |
| 641 |
'taxopress_autoterms_log', |
| 642 |
'_wpnonce', |
| 643 |
]); |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Delete our custom autoterm from the array of autoterms. |
| 648 |
* @return bool|string False on failure, string on success. |
| 649 |
*/ |
| 650 |
function taxopress_action_delete_autoterm($autoterm_id) |
| 651 |
{ |
| 652 |
$autoterms = taxopress_get_autoterm_data(); |
| 653 |
|
| 654 |
if (array_key_exists($autoterm_id, $autoterms)) { |
| 655 |
unset($autoterms[$autoterm_id]); |
| 656 |
$success = update_option('taxopress_autoterms', $autoterms); |
| 657 |
} |
| 658 |
|
| 659 |
if (isset($success)) { |
| 660 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 661 |
wp_safe_redirect( |
| 662 |
add_query_arg( |
| 663 |
[ |
| 664 |
'page' => 'st_autoterms', |
| 665 |
'deleted_autoterm' => 1, |
| 666 |
], |
| 667 |
taxopress_admin_url('admin.php') |
| 668 |
) |
| 669 |
); |
| 670 |
exit(); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Get auto terms logs data |
| 676 |
* |
| 677 |
*/ |
| 678 |
function taxopress_autoterms_logs_data($per_page = 20, $current_page = 1, $orderby = 'ID', $order = 'desc', $schedule_query = false) |
| 679 |
{ |
| 680 |
|
| 681 |
$meta_query[] = array('relation' => 'AND'); |
| 682 |
$meta_query[] = array( |
| 683 |
'key' => '_taxopress_log_component', |
| 684 |
'value' => 'st_autoterms', |
| 685 |
); |
| 686 |
|
| 687 |
/** |
| 688 |
* Custom filter handler |
| 689 |
*/ |
| 690 |
$custom_filters = [ |
| 691 |
'log_source_filter' => '_taxopress_log_action', |
| 692 |
'log_filter_post_type' => '_taxopress_log_post_type', |
| 693 |
'log_filter_taxonomy' => '_taxopress_log_taxonomy', |
| 694 |
'log_filter_status_message' => '_taxopress_log_status_message', |
| 695 |
'log_filter_settings' => '_taxopress_log_option_id' |
| 696 |
]; |
| 697 |
foreach ($custom_filters as $filter => $option) { |
| 698 |
if (!empty($_REQUEST[$filter])) { |
| 699 |
$meta_query[] = array( |
| 700 |
'key' => sanitize_key($option), |
| 701 |
'value' => sanitize_text_field(wp_unslash($_REQUEST[$filter])), |
| 702 |
); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
if ($schedule_query) { |
| 707 |
$meta_query[] = array( |
| 708 |
'key' => '_taxopress_log_action', |
| 709 |
'value' => ['daily_cron_schedule', 'hourly_cron_schedule', 'weekly_cron_schedule'], |
| 710 |
'compare' => 'IN' |
| 711 |
); |
| 712 |
} |
| 713 |
|
| 714 |
$logs_arg = array( |
| 715 |
'post_type' => 'taxopress_logs', |
| 716 |
'post_status' => 'publish', |
| 717 |
'paged' => $current_page, |
| 718 |
'posts_per_page' => $per_page, |
| 719 |
'meta_query' => $meta_query, |
| 720 |
'orderby' => $orderby, |
| 721 |
'order' => $order, |
| 722 |
); |
| 723 |
|
| 724 |
/** |
| 725 |
* Handle search |
| 726 |
*/ |
| 727 |
if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field(wp_unslash($_REQUEST['s']))) { |
| 728 |
$logs_arg['s'] = $search; |
| 729 |
} |
| 730 |
|
| 731 |
$logs = new WP_Query($logs_arg); |
| 732 |
|
| 733 |
return ['posts' => $logs->posts, 'counts' => $logs->found_posts]; |
| 734 |
} |
| 735 |
|