/**
  * Migrate all terms of SEO SearchTerm Tagging 2 on database into postmeta table
  *
  * @since 1.1
  *
  */
 public function migrate_stt2_terms()
 {
     if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'heartbeat-nonce')) {
         do_action('stt2extat_notice', $code = 9, $error = true, $add_setting_error = false);
         wp_die();
     }
     if (!session_id() && !headers_sent()) {
         session_start();
     }
     $location = 'options-general.php?page=stt2extat';
     $result = wp_cache_get('stt2extat_migrate_stt2');
     if (false == $result) {
         global $wpdb;
         $sql = "SELECT * FROM " . $wpdb->prefix . "stt2_meta ;";
         $result = $wpdb->get_results($sql);
         wp_cache_set('stt2extat_migrate_stt2', $result, 900);
     }
     if (false != $result) {
         $data = array();
         foreach ($result as $r) {
             if ('any' != stt2extat_post_type() && !in_array(get_post_type($r->post_id), stt2extat_post_type())) {
                 continue;
             }
             $ignore = get_option('stt2extat_check_relevant_terms');
             if ('1' == $ignore) {
                 $relevant = stt2extat_get_relevant_post($r->post_id, sanitize_text_field($r->meta_value), false, true);
                 if (!$relevant) {
                     continue;
                 }
             }
             $obj = new StdClass();
             $obj->post_id = $r->post_id;
             $obj->count = $r->meta_count;
             $obj->post_modified = strtotime($r->last_modified);
             $data[$r->meta_value] = $obj;
         }
         if (0 == count($data)) {
             $location = add_query_arg(array('error' => true, 'message' => 6), $location);
             wp_die($location);
         }
         global $stt2extat_data;
         $new = array();
         foreach ($data as $key => $val) {
             $stt2extat_data->last_id = $stt2extat_data->last_id + 1;
             do_action('stt2extat_nopriv_update_post_meta', $key, $val->post_id, null, $stt2extat_data->last_id, $stt2extat_data->terms, null, $val->count, $val->post_modified);
         }
         $location = add_query_arg(array('error' => false, 'message' => 15), $location);
     } else {
         do_action('stt2extat_notice', $code = 6, $error = true, $add_setting_error = false);
         wp_die();
     }
     wp_die($location);
 }
/**
 * populate terms of relevant posts
 * @deprecated see stt2extat_search_relevant_post_callback
 * 
 * @since 1.0.0
 *
 * sanitize $_POST and $_REQUEST and other variable
 *
 * @since 1.0.3
 *
 * patch get_the_excerpt
 *
 * @since 1.0.9
 *
 */
function stt2extat_search_relevant_ajax()
{
    $response = array('what' => 'nonce', 'action' => 'search_relevant', 'id' => new WP_Error('notice', 'error'), 'data' => __('You do not have permission to do that.', 'stt2extat'));
    if (!isset($_REQUEST['_wpnonce'], $_POST['post_ID'], $_POST['s'], $_POST['ignore']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'heartbeat-nonce')) {
        wp_send_json($response);
    }
    global $stt2extat_settings, $stt2extat_data;
    $q = sanitize_text_field($_POST['s']);
    $post_id = absint($_POST['post_ID']);
    if ('' == $q) {
        $response['what'] = 'disallow';
        $response['id'] = new WP_Error('notice', 'Terms Disallow');
        wp_send_json($response);
    }
    $ignore = wp_validate_boolean($_POST['ignore']);
    $query = stt2extat_get_relevant_post($post_id, $q, $ignore);
    $post = $query->post;
    $excerpt = get_the_excerpt();
    $excerpt = apply_filters('get_the_excerpt', $excerpt, $q, $post_id);
    $data = $stt2extat_data->terms;
    if (stt2extat_in_stopwords($q)) {
        $post = get_post($post_id);
        $what = 'stopwords';
        $msg = __('include in filter word(s), can not be added!.', 'stt2extat');
        $id = new WP_Error('notice', $q . ' ' . $msg);
    } elseif (isset($post->ID)) {
        if (isset($data[$q])) {
            $what = 'exist';
            $post_id = $data[$q]->post_id;
            $id = new WP_Error('notice', 'Terms Exists ( Relevant )');
        } elseif (3 < mb_strlen($q) && $stt2extat_settings['max_char'] >= mb_strlen($q)) {
            $what = 'relevant';
            $id = new WP_Error('notice', 'Terms Relevant');
        } else {
            $what = 'disallow';
            $id = new WP_Error('notice', 'Terms Disallow');
        }
    } else {
        $post = get_post($post_id);
        if (isset($data[$q])) {
            $what = 'existirrelevant';
            $post_id = $data[$q]->post_id;
            $id = new WP_Error('notice', 'Terms Exists ( Irrelevant )');
        } elseif (3 < mb_strlen($q) && $stt2extat_settings['max_char'] >= mb_strlen($q)) {
            $what = 'irrelevant';
            $id = new WP_Error('notice', 'Terms Irrelevant');
        } else {
            $what = 'disallow';
            $id = new WP_Error('notice', 'Terms Disallow');
        }
    }
    $data = array('title' => sanitize_text_field($q), 'link' => get_permalink($post_id), 'excerpt' => $excerpt, 'content' => wp_strip_all_tags($post->post_content));
    $response = array('what' => sanitize_key($what), 'action' => 'search_relevant', 'id' => $id, 'data' => $data);
    return wp_send_json($response);
}