| 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; |
| 7 |
|
| 8 |
// run a quick security check |
| 9 |
check_ajax_referer('st-admin-js', 'security'); |
| 10 |
|
| 11 |
//instantiate response default value |
| 12 |
$response['status'] = 'error'; |
| 13 |
$response['message'] = ''; |
| 14 |
$response['content'] = ''; |
| 15 |
$response['total'] = 0; |
| 16 |
$response['next_start'] = 0; |
| 17 |
$response['percentage'] = ''; |
| 18 |
|
| 19 |
$autoterms = taxopress_get_autoterm_data(); |
| 20 |
$auto_term_id = isset($_POST['auto_term_id']) ? (int)$_POST['auto_term_id'] : 0; |
| 21 |
$start_from = ( isset($_POST['start_from']) && (int)$_POST['start_from'] > 0) ? ((int)$_POST['start_from']) : 0; |
| 22 |
$offset_start_from = ( isset($_POST['start_from']) && (int)$_POST['start_from'] > 0) ? ((int)$_POST['start_from']-1) : 0; |
| 23 |
|
| 24 |
|
| 25 |
if(!current_user_can('simple_tags')){ |
| 26 |
$response['message'] = esc_html__('Permission denied.', 'simple-tags'); |
| 27 |
wp_send_json($response); |
| 28 |
} |
| 29 |
|
| 30 |
if($auto_term_id === 0 ){ |
| 31 |
$response['message'] = esc_html__('Kindly save your auto terms settings before running this function', 'simple-tags'); |
| 32 |
wp_send_json($response); |
| 33 |
} |
| 34 |
|
| 35 |
if ($auto_term_id && array_key_exists($auto_term_id, $autoterms)) { |
| 36 |
$autoterm_data = $autoterms[$auto_term_id]; |
| 37 |
}else{ |
| 38 |
$response['message'] = esc_html__('Auto term settings not found', 'simple-tags'); |
| 39 |
wp_send_json($response); |
| 40 |
} |
| 41 |
|
| 42 |
$limit = (isset($autoterm_data['existing_terms_batches']) && (int)$autoterm_data['existing_terms_batches'] > 0) ? (int)$autoterm_data['existing_terms_batches'] : 20; |
| 43 |
|
| 44 |
$sleep = (isset($autoterm_data['existing_terms_sleep']) && (int)$autoterm_data['existing_terms_sleep'] > 0) ? (int)$autoterm_data['existing_terms_sleep'] : 0; |
| 45 |
|
| 46 |
$autoterm_existing_content_exclude = isset($autoterm_data['autoterm_existing_content_exclude']) ? (int)$autoterm_data['autoterm_existing_content_exclude'] : 0; |
| 47 |
|
| 48 |
if($sleep > 0 && $start_from > 0){ |
| 49 |
sleep($sleep); |
| 50 |
} |
| 51 |
|
| 52 |
$limit_days = (int) $autoterm_data['limit_days']; |
| 53 |
$limit_days_sql = ''; |
| 54 |
if ( $limit_days > 0 ) { |
| 55 |
$limit_days_sql = 'AND post_date > "' . date( 'Y-m-d H:i:s', time() - $limit_days * 86400 ) . '"'; |
| 56 |
} |
| 57 |
|
| 58 |
$post_types = $autoterm_data['post_types']; |
| 59 |
$post_status = isset($autoterm_data['post_status']) && is_array($autoterm_data['post_status']) ? $autoterm_data['post_status'] : ['publish']; |
| 60 |
|
| 61 |
$total = isset($_POST['total']) ? (int)$_POST['total'] : 0; |
| 62 |
|
| 63 |
if($autoterm_existing_content_exclude > 0){ |
| 64 |
$objects = (array) $wpdb->get_results("SELECT ID, post_title, post_content FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON ( ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_taxopress_autotermed' ) WHERE post_type IN ('" . implode("', '", $post_types) . "') AND {$wpdb->postmeta}.post_id IS NULL AND post_status IN ('" . implode("', '", $post_status) . "') {$limit_days_sql} ORDER BY ID DESC LIMIT {$limit} OFFSET {$offset_start_from}"); |
| 65 |
}else{ |
| 66 |
$objects = (array) $wpdb->get_results("SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE post_type IN ('" . implode("', '", $post_types) . "') AND post_status IN ('" . implode("', '", $post_status) . "') {$limit_days_sql} ORDER BY ID DESC LIMIT {$limit} OFFSET {$offset_start_from}"); |
| 67 |
} |
| 68 |
|
| 69 |
$response_content = ''; |
| 70 |
if (!empty($objects)) { |
| 71 |
foreach ($objects as $object) { |
| 72 |
update_post_meta($object->ID, '_taxopress_autotermed', 1); |
| 73 |
SimpleTags_Client_Autoterms::auto_terms_post( $object, $autoterm_data['taxonomy'], $autoterm_data, true, 'existing_content', 'st_autoterms' ); |
| 74 |
$response_content .= '<li>#' . $object->ID . ' ' . $object->post_title . '</li>'; |
| 75 |
unset($object); |
| 76 |
} |
| 77 |
$response['status'] = 'progress'; |
| 78 |
$response['content'] = $response_content; |
| 79 |
$response['done'] = ($start_from + count($objects)); |
| 80 |
$percentage = 100; |
| 81 |
$progress_message = sprintf(esc_html__('Progress report: %s posts checked.', 'simple-tags'), ($start_from + count($objects))); |
| 82 |
|
| 83 |
} else { |
| 84 |
|
| 85 |
$counter = (int)get_option('tmp_auto_terms_st'); |
| 86 |
delete_option('tmp_auto_terms_st'); |
| 87 |
$response['status'] = 'sucess'; |
| 88 |
$response['done'] = $total; |
| 89 |
$response['message'] = sprintf(esc_html__('All done! %s terms added.', 'simple-tags'), $counter); |
| 90 |
$percentage = 100; |
| 91 |
$progress_message = sprintf(esc_html__('Completed: %s posts checked.', 'simple-tags'), ($start_from + count($objects))); |
| 92 |
} |
| 93 |
$response['percentage'] = '<div class="taxopress-loader-border"><div class="taxopress-loader-green" style="width:100%;">'.$progress_message.'</div></div>'; |
| 94 |
|
| 95 |
wp_send_json($response); |
| 96 |
|
| 97 |
} |