| 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; |
| 22 |
|
| 23 |
|
| 24 |
if($auto_term_id === 0 ){ |
| 25 |
$response['message'] = __('Kindly save your auto terms settings before running this function', 'simple-tags'); |
| 26 |
wp_send_json($response); |
| 27 |
} |
| 28 |
|
| 29 |
if ($auto_term_id && array_key_exists($auto_term_id, $autoterms)) { |
| 30 |
$autoterm_data = $autoterms[$auto_term_id]; |
| 31 |
}else{ |
| 32 |
$response['message'] = __('Auto term settings not found', 'simple-tags'); |
| 33 |
wp_send_json($response); |
| 34 |
} |
| 35 |
|
| 36 |
$post_types = $autoterm_data['post_types']; |
| 37 |
|
| 38 |
|
| 39 |
$total = isset($_POST['total']) ? (int)$_POST['total'] : $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('" . implode( "', '", $post_types ) . "') AND post_status = 'publish'" ); |
| 40 |
$response['total'] = $total; |
| 41 |
$objects = (array) $wpdb->get_results("SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE post_type IN ('" . implode( "', '", $post_types ) . "') AND post_status = 'publish' ORDER BY ID DESC LIMIT {$start_from}, 20"); |
| 42 |
|
| 43 |
$response_content = ''; |
| 44 |
if (!empty($objects)) { |
| 45 |
foreach ($objects as $object) { |
| 46 |
SimpleTags_Client_Autoterms::auto_terms_post( $object, $autoterm_data['taxonomy'], $autoterm_data, true ); |
| 47 |
$response_content .= '<li>#' . $object->ID . ' ' . $object->post_title . '</li>'; |
| 48 |
unset($object); |
| 49 |
} |
| 50 |
$response['status'] = 'progress'; |
| 51 |
$response['content'] = $response_content; |
| 52 |
$response['done'] = ($start_from + count($objects)); |
| 53 |
$percentage = round((($start_from + count($objects))/$total)*100); |
| 54 |
|
| 55 |
} else { |
| 56 |
|
| 57 |
$counter = (int)get_option('tmp_auto_terms_st'); |
| 58 |
delete_option('tmp_auto_terms_st'); |
| 59 |
$response['status'] = 'sucess'; |
| 60 |
$response['done'] = $total; |
| 61 |
$response['message'] = sprintf(__('All done! %s terms added.', 'simple-tags'), $counter); |
| 62 |
$percentage = 100; |
| 63 |
} |
| 64 |
$response['percentage'] = '<div class="taxopress-loader-border"><div class="taxopress-loader-green" style="width:'.$percentage.'%;">'.$percentage.'%</div></div>'; |
| 65 |
|
| 66 |
wp_send_json($response); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
?> |