code) ? str_replace('{term}', preg_quote($find_term), $autoterm_regex_code) : ''; if (!empty($terms_regex_code)) { if (preg_match("{$terms_regex_code}", $content)) { $terms_to_add[] = $term; } } elseif (isset($options['autoterm_word']) && (int) $options['autoterm_word'] == 1) { // Whole word ? if (preg_match("/\b" . preg_quote($find_term) . "\b/i", $content)) { $terms_to_add[] = $term; } //make exception for hashtag special character if (substr($find_term, 0, strlen('#')) === '#') { $trim_term = ltrim($find_term, '#'); if (preg_match("/\B(\#+$trim_term\b)(?!;)/i", $content)) { $terms_to_add[] = $term; } } if (isset($options['autoterm_hash']) && (int) $options['autoterm_hash'] == 1 && stristr($content, '#' . $find_term)) { $terms_to_add[] = $term; } } elseif (stristr($content, $find_term)) { $terms_to_add[] = $term; } } } } } } //Autoterm with OpenCalais if ($autoterm_use_opencalais > 0 && taxopress_is_pro_version()) { $open_calais_results = TaxoPressAiApi::get_open_calais_results($args); if (!empty($open_calais_results['results'])) { $data = $open_calais_results['results']; if (!empty($data)) { // Remove empty terms $data = array_filter($data, '_delete_empty_element'); $data = array_unique($data); foreach ((array) $data as $term) { $term = stripslashes($term); if (!is_string($term)) { continue; } $term = trim($term); if (empty($term)) { continue; } //check if term belong to the post already if (has_term($term, $taxonomy, $object)) { continue; } //exclude if name found in exclude terms if (in_array($term, $autoterm_exclude)) { continue; } //add primary term $add_terms = []; $add_terms[$term] = $term; // add term synonyms if (is_array($options) && isset($options['synonyms_term']) && (int) $options['synonyms_term'] > 0) { $term_synonyms = taxopress_get_term_synonyms($term, $taxonomy); if (!empty($term_synonyms)) { foreach ($term_synonyms as $term_synonym) { $add_terms[$term_synonym] = $term; } } } // add linked term $add_terms = taxopress_add_linked_term_options($add_terms, $term, $taxonomy, false, true); foreach ($add_terms as $find_term => $original_term) { $terms_regex_code = !empty($autoterm_regex_code) ? str_replace('{term}', preg_quote($find_term), $autoterm_regex_code) : ''; if (!empty($terms_regex_code)) { if (preg_match("{$terms_regex_code}", $content)) { $terms_to_add[] = $term; } } elseif (isset($options['autoterm_word']) && (int) $options['autoterm_word'] == 1) { // Whole word ? if (preg_match("/\b" . preg_quote($find_term) . "\b/i", $content)) { $terms_to_add[] = $term; } //make exception for hashtag special character if (substr($find_term, 0, strlen('#')) === '#') { $trim_term = ltrim($find_term, '#'); if (preg_match("/\B(\#+$trim_term\b)(?!;)/i", $content)) { $terms_to_add[] = $term; } } if (isset($options['autoterm_hash']) && (int) $options['autoterm_hash'] == 1 && stristr($content, '#' . $find_term)) { $terms_to_add[] = $term; } } elseif (stristr($content, $find_term)) { $terms_to_add[] = $term; } } } } } } // Auto term with specific auto terms list if (isset($options['specific_terms']) && isset($options['autoterm_useonly']) && (int) $options['autoterm_useonly'] === 1) { $terms = maybe_unserialize($options['specific_terms']); $terms = taxopress_change_to_array($terms); foreach ($terms as $term) { if (!is_string($term)) { continue; } $term = trim($term); if (empty($term)) { continue; } //check if term belong to the post already if (has_term($term, $taxonomy, $object)) { continue; } //exclude if name found in exclude terms if (in_array($term, $autoterm_exclude)) { continue; } //add primary term $add_terms = []; $add_terms[$term] = $term; // add term synonyms if (is_array($options) && isset($options['synonyms_term']) && (int) $options['synonyms_term'] > 0) { $term_synonyms = taxopress_get_term_synonyms($term, $taxonomy); if (!empty($term_synonyms)) { foreach ($term_synonyms as $term_synonym) { $add_terms[$term_synonym] = $term; } } } // add linked term $add_terms = taxopress_add_linked_term_options($add_terms, $term, $taxonomy, false, true); foreach ($add_terms as $find_term => $original_term) { $terms_regex_code = !empty($autoterm_regex_code) ? str_replace('{term}', preg_quote($find_term), $autoterm_regex_code) : ''; if (!empty($terms_regex_code)) { if (preg_match("{$terms_regex_code}", $content)) { $terms_to_add[] = $term; } } elseif(isset($options['autoterm_word']) && (int) $options['autoterm_word'] === 1) { // Whole word ? //if (preg_match("/\b" . preg_quote($find_term) . "\b/i", $content)) { if (preg_match("#\b" . preg_quote($find_term) . "\b#i", $content)) { $terms_to_add[] = $term; } //make exception for hashtag special character if (substr($find_term, 0, strlen('#')) === '#') { $trim_term = ltrim($find_term, '#'); if (preg_match("/\B(\#+$trim_term\b)(?!;)/i", $content)) { $terms_to_add[] = $term; } } if (isset($options['autoterm_hash']) && (int) $options['autoterm_hash'] === 1 && stristr($content, '#' . $find_term)) { $terms_to_add[] = $term; } } elseif (stristr($content, $find_term)) { $terms_to_add[] = $term; } } } unset($terms, $term); } // Auto terms with all terms if (isset($options['autoterm_useall']) && (int) $options['autoterm_useall'] === 1) { // Get all terms $terms = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT name FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s", $taxonomy)); $terms = array_unique($terms); foreach ($terms as $term) { $term = stripslashes($term); if (!is_string($term)) { continue; } $term = trim($term); if (empty($term)) { continue; } //check if term belong to the post already if (has_term($term, $taxonomy, $object)) { continue; } //exclude if name found in exclude terms if (in_array($term, $autoterm_exclude)) { continue; } //add primary term $add_terms = []; $add_terms[$term] = $term; // add term synonyms if (is_array($options) && isset($options['synonyms_term']) && (int) $options['synonyms_term'] > 0) { $term_synonyms = taxopress_get_term_synonyms($term, $taxonomy); if (!empty($term_synonyms)) { foreach ($term_synonyms as $term_synonym) { $add_terms[$term_synonym] = $term; } } } // add linked term $add_terms = taxopress_add_linked_term_options($add_terms, $term, $taxonomy, false, true); foreach ($add_terms as $find_term => $original_term) { $terms_regex_code = !empty($autoterm_regex_code) ? str_replace('{term}', preg_quote($find_term), $autoterm_regex_code) : ''; if (!empty($terms_regex_code)) { if (preg_match("{$terms_regex_code}", $content)) { $terms_to_add[] = $term; } } elseif (isset($options['autoterm_word']) && (int) $options['autoterm_word'] == 1) { // Whole word ? //if (preg_match("/\b" . preg_quote($find_term) . "\b/i", $content)) { if (preg_match("#\b" . preg_quote($find_term) . "\b#i", $content)) { $terms_to_add[] = $term; } //make exception for hashtag special character if (substr($find_term, 0, strlen('#')) === '#') { $trim_term = ltrim($find_term, '#'); if (preg_match("/\B(\#+$trim_term\b)(?!;)/i", $content)) { $terms_to_add[] = $term; } } if (isset($options['autoterm_hash']) && (int) $options['autoterm_hash'] == 1 && stristr($content, '#' . $find_term)) { $terms_to_add[] = $term; } } elseif (stristr($content, $find_term)) { $terms_to_add[] = $term; } } } } // Append terms if terms to add if (!empty($terms_to_add)) { // Remove empty and duplicate elements $terms_to_add = array_filter($terms_to_add, '_delete_empty_element'); $terms_to_add = array_unique($terms_to_add); //auto terms limit $terms_limit = isset($options['terms_limit']) ? (int) $options['terms_limit'] : 0; if ($terms_limit > 0 && count($terms_to_add) > $terms_limit) { $terms_to_add = array_slice($terms_to_add, 0, $terms_limit); } if ($counter == true) { // Increment counter $counter = ((int) get_option('tmp_auto_terms_st')) + count($terms_to_add); update_option('tmp_auto_terms_st', $counter); } // Add terms to posts wp_set_object_terms($object->ID, $terms_to_add, $taxonomy, true); // Clean cache clean_post_cache($object->ID); //update log self::update_taxopress_logs($object, $taxonomy, $options, $counter, $action, $component, $terms_to_add, 'success', 'terms_added'); return true; } else { //update log self::update_taxopress_logs($object, $taxonomy, $options, $counter, $action, $component, $terms_to_add, 'failed', 'empty_terms'); } return false; } /** * Update taxopress logs * * Known possible values * * COMPONENT: (st_autoterms) * ACTION: (existing_content, save_posts, daily_cron_schedule, hourly_cron_schedule) * STATUS: (failed, success) * STATUS MESSAGE: (invalid_option, term_only_option, empty_post_content, terms_added, empty_terms) * * @param object $object * @param string $taxonomy * @param array $options * @param boolean $counter * @param string $action * @param string $component * @param array $terms_to_add * @param string $status * @param string $status_message * * @return boolean * @author olatechpro */ public static function update_taxopress_logs($object, $taxonomy = 'post_tag', $options = array(), $counter = false, $action = 'save_posts', $component = 'st_autoterms', $terms_to_add = [], $status = 'failed', $status_message = 'not_provided') { if (get_option('taxopress_autoterms_logs_disabled') || !post_type_exists('taxopress_logs')) { return; } $insert_post_args = array( 'post_author' => get_current_user_id(), 'post_title' => $object->post_title, 'post_content' => $object->post_content, 'post_status' => 'publish', 'post_type' => 'taxopress_logs', ); $post_id = wp_insert_post($insert_post_args); update_post_meta($post_id, '_taxopress_log_post_id', $object->ID); update_post_meta($post_id, '_taxopress_log_taxonomy', $taxonomy); update_post_meta($post_id, '_taxopress_log_post_type', get_post_type($object->ID)); update_post_meta($post_id, '_taxopress_log_action', $action); update_post_meta($post_id, '_taxopress_log_component', $component); update_post_meta($post_id, '_taxopress_log_terms', implode(", ", $terms_to_add)); update_post_meta($post_id, '_taxopress_log_status', $status); update_post_meta($post_id, '_taxopress_log_status_message', $status_message); update_post_meta($post_id, '_taxopress_log_options', $options); update_post_meta($post_id, '_taxopress_log_option_id', $options['ID']); //for performance reason, delete only 1 posts if more than limit instead of querying all posts $auto_terms_logs_limit = (int) get_option('taxopress_auto_terms_logs_limit', 1000); $current_logs_counts = wp_count_posts('taxopress_logs'); $current_logs_count = isset($current_logs_counts->publish) ? $current_logs_counts->publish : 0; if (isset($current_logs_counts->publish) && (int) $current_logs_count > $auto_terms_logs_limit) { $posts = get_posts( array( 'post_type' => 'taxopress_logs', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'ID', 'order' => 'ASC', 'fields' => 'ids' ) ); if (count($posts) > 0) { foreach ($posts as $post) { wp_delete_post($post, true); } } } } }