| 1 |
<?php |
| 2 |
//Taxopress auto terms => Auto terms all content ajax callback |
| 3 |
add_action('wp_ajax_taxopress_autoterms_content_by_ajax', 'taxopress_autoterms_content_by_ajax'); |
| 4 |
function taxopress_autoterms_content_by_ajax() |
| 5 |
{ |
| 6 |
global $wpdb, $added_post_term, $empty_term_messages; |
| 7 |
|
| 8 |
$added_post_term = []; |
| 9 |
$empty_term_messages = []; |
| 10 |
|
| 11 |
// run a quick security check |
| 12 |
check_ajax_referer('st-admin-js', 'security'); |
| 13 |
|
| 14 |
//instantiate response default value |
| 15 |
$response['status'] = 'error'; |
| 16 |
$response['message'] = ''; |
| 17 |
$response['content'] = ''; |
| 18 |
$response['total'] = 0; |
| 19 |
$response['next_start'] = 0; |
| 20 |
$response['percentage'] = ''; |
| 21 |
|
| 22 |
$autoterms = taxopress_get_autoterm_data(); |
| 23 |
$auto_term_id = !empty($_POST['taxopress_autoterm_content']['autoterm_id']) ? (int)$_POST['taxopress_autoterm_content']['autoterm_id'] : 0; |
| 24 |
|
| 25 |
$existing_terms_batches = !empty($_POST['taxopress_autoterm_content']['existing_terms_batches']) ? (int)$_POST['taxopress_autoterm_content']['existing_terms_batches'] : 0; |
| 26 |
$existing_terms_sleep = !empty($_POST['taxopress_autoterm_content']['existing_terms_sleep']) ? (int)$_POST['taxopress_autoterm_content']['existing_terms_sleep'] : 0; |
| 27 |
$limit_days = !empty($_POST['taxopress_autoterm_content']['limit_days']) ? (int)$_POST['taxopress_autoterm_content']['limit_days'] : 0; |
| 28 |
$autoterm_existing_content_exclude = !empty($_POST['taxopress_autoterm_content']['autoterm_existing_content_exclude']) ? (int)$_POST['taxopress_autoterm_content']['autoterm_existing_content_exclude'] : 0; |
| 29 |
|
| 30 |
$start_from = isset($_POST['start_from']) && (int)$_POST['start_from'] > 0 ? (int)$_POST['start_from'] : 0; |
| 31 |
$offset_start_from = max(0, $start_from); |
| 32 |
|
| 33 |
if(!current_user_can('simple_tags')){ |
| 34 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Permission denied.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 35 |
wp_send_json($response); |
| 36 |
} |
| 37 |
|
| 38 |
if ($start_from === 0) { |
| 39 |
delete_option('tmp_auto_terms_st'); |
| 40 |
} |
| 41 |
|
| 42 |
$auto_term_settings = [ |
| 43 |
'autoterm_id' => $auto_term_id, |
| 44 |
'existing_terms_batches' => $existing_terms_batches, |
| 45 |
'existing_terms_sleep' => $existing_terms_sleep, |
| 46 |
'limit_days' => $limit_days, |
| 47 |
'autoterm_existing_content_exclude' => $autoterm_existing_content_exclude, |
| 48 |
]; |
| 49 |
update_option('taxopress_autoterms_content', $auto_term_settings); |
| 50 |
|
| 51 |
if (empty($auto_term_id)) { |
| 52 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Auto Term is required, kindly add an Auto Term from Auto Term menu.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 53 |
wp_send_json($response); |
| 54 |
} elseif (empty($existing_terms_batches)) { |
| 55 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Limit per batches is required.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 56 |
wp_send_json($response); |
| 57 |
} elseif (empty($existing_terms_sleep)) { |
| 58 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Batches wait time is required.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 59 |
wp_send_json($response); |
| 60 |
} |
| 61 |
|
| 62 |
if ($auto_term_id && array_key_exists($auto_term_id, $autoterms)) { |
| 63 |
$autoterm_data = $autoterms[$auto_term_id]; |
| 64 |
$autoterm_data['existing_terms_batches'] = $existing_terms_batches; |
| 65 |
$autoterm_data['existing_terms_sleep'] = $existing_terms_sleep; |
| 66 |
$autoterm_data['limit_days'] = $limit_days; |
| 67 |
$autoterm_data['autoterm_exclude'] = $autoterm_existing_content_exclude; |
| 68 |
$autoterm_data['replace_type'] = isset($autoterm_data['existing_content_replace_type']) ? $autoterm_data['existing_content_replace_type'] : ''; |
| 69 |
$autoterm_data['terms_limit'] = !empty($autoterm_data['existing_content_terms_limit']) ? $autoterm_data['existing_content_terms_limit'] : ''; |
| 70 |
}else{ |
| 71 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Auto term settings not found', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 72 |
wp_send_json($response); |
| 73 |
} |
| 74 |
|
| 75 |
if (empty($autoterm_data['post_types'])) { |
| 76 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('The selected Auto Term setting is not enabled for any post type. Please select at least one post type in the Auto Term settings.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 77 |
wp_send_json($response); |
| 78 |
} |
| 79 |
|
| 80 |
if (empty($autoterm_data['autoterm_for_existing_content'])) { |
| 81 |
$response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('The selected Auto Term is not enabled for existing content. Please enable it in Auto Term settings.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 82 |
wp_send_json($response); |
| 83 |
} |
| 84 |
|
| 85 |
$limit = (isset($autoterm_data['existing_terms_batches']) && (int)$autoterm_data['existing_terms_batches'] > 0) ? (int)$autoterm_data['existing_terms_batches'] : 2; |
| 86 |
|
| 87 |
$sleep = (isset($autoterm_data['existing_terms_sleep']) && (int)$autoterm_data['existing_terms_sleep'] > 0) ? (int)$autoterm_data['existing_terms_sleep'] : 0; |
| 88 |
|
| 89 |
if($sleep > 0 && $start_from > 0){ |
| 90 |
sleep($sleep); |
| 91 |
} |
| 92 |
|
| 93 |
$limit_days = (int) $autoterm_data['limit_days']; |
| 94 |
$limit_days_sql = ''; |
| 95 |
if ( $limit_days > 0 ) { |
| 96 |
$limit_days_sql = 'AND post_date > "' . date( 'Y-m-d H:i:s', time() - $limit_days * 86400 ) . '"'; |
| 97 |
} |
| 98 |
|
| 99 |
$post_types = $autoterm_data['post_types']; |
| 100 |
$post_status = isset($autoterm_data['post_status']) && is_array($autoterm_data['post_status']) ? $autoterm_data['post_status'] : ['publish']; |
| 101 |
|
| 102 |
$total = isset($_POST['total']) ? (int)$_POST['total'] : 0; |
| 103 |
|
| 104 |
$post_types = array_map('sanitize_key', (array) $post_types); |
| 105 |
$post_status = array_map('sanitize_key', (array) $post_status); |
| 106 |
|
| 107 |
$post_types_placeholders = implode(',', array_fill(0, count($post_types), '%s')); |
| 108 |
$post_status_placeholders = implode(',', array_fill(0, count($post_status), '%s')); |
| 109 |
|
| 110 |
if($autoterm_existing_content_exclude > 0){ |
| 111 |
$sql = "SELECT ID, post_title, post_content FROM {$wpdb->posts} |
| 112 |
LEFT JOIN {$wpdb->postmeta} ON ( ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_taxopress_autotermed' ) |
| 113 |
WHERE post_type IN ({$post_types_placeholders}) |
| 114 |
AND {$wpdb->postmeta}.post_id IS NULL |
| 115 |
AND post_status IN ({$post_status_placeholders}) |
| 116 |
{$limit_days_sql} |
| 117 |
ORDER BY ID DESC |
| 118 |
LIMIT %d OFFSET %d"; |
| 119 |
|
| 120 |
$prepare_args = array_merge($post_types, $post_status, [$limit, $offset_start_from]); |
| 121 |
$objects = (array) $wpdb->get_results($wpdb->prepare($sql, $prepare_args)); |
| 122 |
}else{ |
| 123 |
$sql = "SELECT ID, post_title, post_content FROM {$wpdb->posts} |
| 124 |
WHERE post_type IN ({$post_types_placeholders}) |
| 125 |
AND post_status IN ({$post_status_placeholders}) |
| 126 |
{$limit_days_sql} |
| 127 |
ORDER BY ID DESC |
| 128 |
LIMIT %d OFFSET %d"; |
| 129 |
|
| 130 |
$prepare_args = array_merge($post_types, $post_status, [$limit, $offset_start_from]); |
| 131 |
$objects = (array) $wpdb->get_results($wpdb->prepare($sql, $prepare_args)); |
| 132 |
} |
| 133 |
|
| 134 |
$response_content = ''; |
| 135 |
if (!empty($objects)) { |
| 136 |
foreach ($objects as $object) { |
| 137 |
update_post_meta($object->ID, '_taxopress_autotermed', 1); |
| 138 |
$post_object = get_post($object->ID); |
| 139 |
SimpleTags_Client_Autoterms::auto_terms_post( $post_object, $autoterm_data['taxonomy'], $autoterm_data, true, 'existing_content', 'st_autoterms' ); |
| 140 |
$log_messages = !empty($empty_term_messages[$object->ID]['message']) ? $empty_term_messages[$object->ID]['message'] : []; |
| 141 |
$log_message_html = ''; |
| 142 |
if (!empty($log_messages)) { |
| 143 |
$log_message_html .= '<div class="log-message-show-button"><a href="#" class="msg-show">' . sprintf(esc_html__('Show Log Messages (%1s)', 'simple-tags'), '<strong>' . count($log_messages) . '</strong>') . '</a></div>'; |
| 144 |
$log_message_html .= '<div class="autoterm-log-message" style="display: none;"><ul>'; |
| 145 |
$log_msg_lists = array_map(function($item) { |
| 146 |
return '<li class="log-message-list">' . $item . '</li>'; |
| 147 |
}, $log_messages); |
| 148 |
$log_message_html .= join('', $log_msg_lists); |
| 149 |
$log_message_html .= '</ul></div>'; |
| 150 |
$log_message_html .= '<div class="log-message-hide-button"><a href="#" class="msg-hide" style="display: none;">' . sprintf(esc_html__('Hide Log Messages (%1s)', 'simple-tags'), '<strong>' . count($log_messages) . '</strong>') . '</a></div>'; |
| 151 |
} |
| 152 |
if (!empty($added_post_term[$object->ID])) { |
| 153 |
$tag_lists = array_map(function($item) { |
| 154 |
return '<span class="taxopress-term"><span class="term-name">' . $item . '</span></span>'; |
| 155 |
}, $added_post_term[$object->ID]); |
| 156 |
$added_terms_html = join('', $tag_lists) . $log_message_html; |
| 157 |
} else { |
| 158 |
$added_terms_html = esc_html__('No terms added.', 'simple-tags') . $log_message_html;; |
| 159 |
} |
| 160 |
$response_content .= '<li class="result-item"> |
| 161 |
<fieldset> |
| 162 |
<legend> |
| 163 |
<span class="result-title"> |
| 164 |
<a target="_blank" href="'. esc_url(get_edit_post_link($object->ID)) . '">' . esc_html($object->post_title) . '</a> |
| 165 |
</span> |
| 166 |
</legend> |
| 167 |
<div class="result-content">'. $added_terms_html .'</div> |
| 168 |
</fieldset></li>'; |
| 169 |
unset($object); |
| 170 |
} |
| 171 |
|
| 172 |
$response['status'] = 'progress'; |
| 173 |
$response['content'] = $response_content; |
| 174 |
$response['done'] = ($start_from + count($objects)); |
| 175 |
$percentage = 100; |
| 176 |
$response['notice'] = '<div class="taxopress-response-css yellow"><p>'. sprintf(esc_html__('Please leave this screen running to continue the scan. To stop the scan, close this screen or click here: %1sStop%2s | %3sPause%4s', 'simple-tags'), '<a href="#" class="terminate-autoterm-scan">', '</a>', '<a href="#" class="pause-autoterm-scan" data-pause="0" data-pause-text="'. esc_html__('Pause', 'simple-tags') .'" data-resume-text="'. esc_html__('Resume', 'simple-tags') .'">', '</a>') .'</p></div>'; |
| 177 |
$progress_message = '<div class="taxopress-response-css yellow"><p>'. sprintf(esc_html__('Progress Report: %s posts checked.', 'simple-tags'), '<strong>' . ($start_from + count($objects)) . '</strong>') .'</p></div>'; |
| 178 |
|
| 179 |
} else { |
| 180 |
|
| 181 |
$counter = (int)get_option('tmp_auto_terms_st'); |
| 182 |
delete_option('tmp_auto_terms_st'); |
| 183 |
$percentage = 100; |
| 184 |
$response['status'] = 'sucess'; |
| 185 |
$response['done'] = $total; |
| 186 |
$progress_message = '<div class="taxopress-response-css green"><p>'. sprintf(esc_html__('Completed: %s terms added from %s posts checked.', 'simple-tags'), $counter, ($start_from + count($objects))) .'</p><button type="button" class="notice-dismiss"></button></div>'; |
| 187 |
$response['message'] = $progress_message; |
| 188 |
} |
| 189 |
$response['percentage'] = $progress_message; |
| 190 |
|
| 191 |
wp_send_json($response); |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
//Taxopress search post call back |
| 197 |
add_action('wp_ajax_taxopress_post_search', 'taxopress_post_search_callback'); |
| 198 |
|
| 199 |
function taxopress_post_search_callback() |
| 200 |
{ |
| 201 |
header('Content-Type: application/javascript'); |
| 202 |
|
| 203 |
if ( |
| 204 |
empty($_GET['nonce']) |
| 205 |
|| !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-post-search') |
| 206 |
) { |
| 207 |
wp_send_json_error(null, 403); |
| 208 |
} |
| 209 |
|
| 210 |
if (!current_user_can('simple_tags')) { |
| 211 |
wp_send_json_error(null, 403); |
| 212 |
} |
| 213 |
|
| 214 |
$search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : ''; |
| 215 |
$post_type = !empty($_GET['post_types']) ? array_map('sanitize_text_field', $_GET['post_types']) : 'any'; |
| 216 |
|
| 217 |
$post_args = [ |
| 218 |
'post_type' => $post_type, |
| 219 |
'post_status' => 'publish', |
| 220 |
'posts_per_page' => apply_filters('taxopress_filter_posts_search_result_limit', 20), |
| 221 |
]; |
| 222 |
|
| 223 |
if (!empty($search)) { |
| 224 |
$post_args['s'] = $search; |
| 225 |
} |
| 226 |
|
| 227 |
$posts = get_posts($post_args); |
| 228 |
$results = []; |
| 229 |
|
| 230 |
foreach ($posts as $post) { |
| 231 |
$results[] = [ |
| 232 |
'id' => $post->ID, |
| 233 |
'text' => $post->post_title, |
| 234 |
]; |
| 235 |
} |
| 236 |
|
| 237 |
$response = [ |
| 238 |
'results' => $results, |
| 239 |
]; |
| 240 |
echo wp_json_encode($response); |
| 241 |
exit; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
//Taxopress search field call back |
| 246 |
add_action('wp_ajax_taxopress_custom_fields_search', 'taxopress_custom_fields_search_callback'); |
| 247 |
|
| 248 |
function taxopress_custom_fields_search_callback() |
| 249 |
{ |
| 250 |
global $wpdb; |
| 251 |
|
| 252 |
header('Content-Type: application/javascript'); |
| 253 |
|
| 254 |
if ( |
| 255 |
empty($_GET['nonce']) |
| 256 |
|| !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-custom-fields-search') |
| 257 |
) { |
| 258 |
wp_send_json_error(null, 403); |
| 259 |
} |
| 260 |
|
| 261 |
if (!current_user_can('simple_tags')) { |
| 262 |
wp_send_json_error(null, 403); |
| 263 |
} |
| 264 |
|
| 265 |
$search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : ''; |
| 266 |
|
| 267 |
$whereClause = ''; |
| 268 |
if (!empty($search)) { |
| 269 |
$like = '%' . $wpdb->esc_like($search) . '%'; |
| 270 |
$whereClause = $wpdb->prepare("AND meta_key LIKE %s", $like); |
| 271 |
} |
| 272 |
|
| 273 |
$queryResults = $wpdb->get_col("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE 1=1 $whereClause ORDER BY meta_key ASC LIMIT 20"); |
| 274 |
|
| 275 |
$results = []; |
| 276 |
if (!empty($queryResults)) { |
| 277 |
foreach ($queryResults as $queryResult) { |
| 278 |
$results[] = [ |
| 279 |
'id' => $queryResult, |
| 280 |
'text' => $queryResult, |
| 281 |
]; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
$response = [ |
| 286 |
'results' => $results, |
| 287 |
]; |
| 288 |
echo wp_json_encode($response); |
| 289 |
exit; |
| 290 |
} |