| 1 |
<?php |
| 2 |
|
| 3 |
class SimpleTags_Client_Autoterms { |
| 4 |
/** |
| 5 |
* Constructor |
| 6 |
* |
| 7 |
* @return void |
| 8 |
* @author WebFactory Ltd |
| 9 |
*/ |
| 10 |
public function __construct() { |
| 11 |
if ( 1 === (int) SimpleTags_Plugin::get_option_value( 'active_auto_terms' ) ) { |
| 12 |
add_action( 'save_post', array( __CLASS__, 'save_post' ), 12, 2 ); |
| 13 |
add_action( 'post_syndicated_item', array( __CLASS__, 'save_post' ), 12, 2 ); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Check post/page content for auto terms |
| 19 |
* |
| 20 |
* @param integer $post_id |
| 21 |
* @param object $object |
| 22 |
* |
| 23 |
* @return boolean |
| 24 |
*/ |
| 25 |
public static function save_post( $post_id = null, $object = null ) { |
| 26 |
|
| 27 |
// Get options |
| 28 |
$options = get_option( STAGS_OPTIONS_NAME_AUTO ); |
| 29 |
|
| 30 |
// user preference for this post ? |
| 31 |
$meta_value = isset($_POST['exclude_autotags']) ? sanitize_text_field($_POST['exclude_autotags']) : false; |
| 32 |
if ( $meta_value ) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
if(!is_object($object)){ |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
if(!isset($object->post_type)){ |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Loop option for find if autoterms is actived on any taxonomy and post type |
| 45 |
$current_post_type = $object->post_type; |
| 46 |
$current_post_status = $object->post_status; |
| 47 |
$autoterms = taxopress_get_autoterm_data(); |
| 48 |
$flag = false; |
| 49 |
foreach($autoterms as $autoterm_key => $autoterm_data){ |
| 50 |
$eligible_post_status = isset($autoterm_data['post_status']) && is_array($autoterm_data['post_status']) ? $autoterm_data['post_status'] : ['publish']; |
| 51 |
$eligible_post_types = isset($autoterm_data['post_types']) && is_array($autoterm_data['post_types']) ? $autoterm_data['post_types'] : []; |
| 52 |
$eligible_post_types = array_filter($eligible_post_types); |
| 53 |
|
| 54 |
if(count($eligible_post_types) === 0){ |
| 55 |
break; |
| 56 |
} |
| 57 |
if(!in_array($current_post_type, $eligible_post_types)){ |
| 58 |
continue; |
| 59 |
} |
| 60 |
if(!in_array($current_post_status, $eligible_post_status)){ |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
self::auto_terms_post( $object, $autoterm_data['taxonomy'], $autoterm_data ); |
| 65 |
$flag = true; |
| 66 |
} |
| 67 |
|
| 68 |
if ( $flag == true ) { // Clean cache ? |
| 69 |
clean_post_cache( $post_id ); |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Automatically tag a post/page from the database terms for the taxonomy specified |
| 77 |
* |
| 78 |
* @param object $object |
| 79 |
* @param string $taxonomy |
| 80 |
* @param array $options |
| 81 |
* @param boolean $counter |
| 82 |
* |
| 83 |
* @return boolean |
| 84 |
* @author WebFactory Ltd |
| 85 |
*/ |
| 86 |
public static function auto_terms_post( $object, $taxonomy = 'post_tag', $options = array(), $counter = false ) { |
| 87 |
global $wpdb; |
| 88 |
|
| 89 |
// Option exists ? |
| 90 |
if ( $options == false || empty( $options ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
if ( get_the_terms( $object->ID, $taxonomy ) != false && (int)$options['autoterm_target'] === 1 ) { |
| 95 |
return false; // Skip post with terms, if term only empty post option is checked |
| 96 |
} |
| 97 |
|
| 98 |
$autoterm_exclude = isset($options['autoterm_exclude']) ? taxopress_change_to_array($options['autoterm_exclude']) : []; |
| 99 |
|
| 100 |
$terms_to_add = array(); |
| 101 |
|
| 102 |
|
| 103 |
if( isset($options['autoterm_from']) && $options['autoterm_from'] === 'post_title' ){ |
| 104 |
$content = $object->post_title; |
| 105 |
}elseif( isset($options['autoterm_from']) && $options['autoterm_from'] === 'post_content' ){ |
| 106 |
$content = $object->post_content; |
| 107 |
}else{ |
| 108 |
$content = $object->post_content . ' ' . $object->post_title; |
| 109 |
} |
| 110 |
|
| 111 |
if ( isset( $object->post_excerpt ) ) { |
| 112 |
$content .= ' ' . $object->post_excerpt; |
| 113 |
} |
| 114 |
|
| 115 |
$content = trim( strip_tags( $content ) ); |
| 116 |
if ( empty( $content ) ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$autoterm_use_dandelion = isset($options['autoterm_use_dandelion']) ? (int)$options['autoterm_use_dandelion'] : 0; |
| 121 |
$autoterm_use_opencalais = isset($options['autoterm_use_opencalais']) ? (int)$options['autoterm_use_opencalais'] : 0; |
| 122 |
|
| 123 |
//Autoterm with Dandelion |
| 124 |
if ( $autoterm_use_dandelion > 0 && $options['terms_datatxt_access_token'] !== '' ) { |
| 125 |
$request_ws_args = array(); |
| 126 |
$request_ws_args['text'] = $content; |
| 127 |
// Custom confidence ? |
| 128 |
$request_ws_args['min_confidence'] = 0.6; |
| 129 |
if ( $options['terms_datatxt_min_confidence'] != "" ) { |
| 130 |
$request_ws_args['min_confidence'] = $options['terms_datatxt_min_confidence']; |
| 131 |
} |
| 132 |
$request_ws_args['token'] = $options['terms_datatxt_access_token']; |
| 133 |
// Build params |
| 134 |
$response = wp_remote_post( 'https://api.dandelion.eu/datatxt/nex/v1', array( |
| 135 |
'user-agent' => 'WordPress simple-tags', |
| 136 |
'body' => $request_ws_args |
| 137 |
) ); |
| 138 |
$data = false; |
| 139 |
if ( ! is_wp_error( $response ) && $response != null ) { |
| 140 |
if ( wp_remote_retrieve_response_code( $response ) == 200 ) { |
| 141 |
$data = wp_remote_retrieve_body( $response ); |
| 142 |
} |
| 143 |
} |
| 144 |
if($data){ |
| 145 |
$data = json_decode( $data ); |
| 146 |
$data = is_object($data) ? $data->annotations : ''; |
| 147 |
if ( !empty( $data ) ) { |
| 148 |
foreach ( (array) $data as $term ) { |
| 149 |
$term = $term->title; |
| 150 |
$term = stripslashes( $term ); |
| 151 |
|
| 152 |
if ( ! is_string( $term ) ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$term = trim( $term ); |
| 157 |
if ( empty( $term ) ) { |
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
//check if term belong to the post already |
| 162 |
if(has_term( $term, $taxonomy, $object )){ |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
//exclude if name found in exclude terms |
| 167 |
if(in_array($term, $autoterm_exclude)){ |
| 168 |
continue; |
| 169 |
} |
| 170 |
|
| 171 |
// Whole word ? |
| 172 |
if ( isset( $options['autoterm_word'] ) && (int) $options['autoterm_word'] == 1 ) { |
| 173 |
if(strpos($content, ' '.$term.' ') !== FALSE) |
| 174 |
{ |
| 175 |
$terms_to_add[] = $term; |
| 176 |
} |
| 177 |
|
| 178 |
//make exception for hashtag special character |
| 179 |
if (substr($term, 0, strlen('#')) === '#') { |
| 180 |
$trim_term = ltrim($term, '#'); |
| 181 |
if ( preg_match( "/\B(\#+$trim_term\b)(?!;)/i", $content ) ) { |
| 182 |
$terms_to_add[] = $term; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( isset( $options['autoterm_hash'] ) && (int) $options['autoterm_hash'] == 1 && stristr( $content, '#' . $term ) ) { |
| 187 |
$terms_to_add[] = $term; |
| 188 |
} |
| 189 |
} elseif ( stristr( $content, $term ) ) { |
| 190 |
$terms_to_add[] = $term; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
//Autoterm with OpenCalais |
| 198 |
if ( $autoterm_use_opencalais > 0 && $options['terms_opencalais_key'] !== '' ) { |
| 199 |
$response = wp_remote_post( 'https://api-eit.refinitiv.com/permid/calais', array( |
| 200 |
'timeout' => 30, |
| 201 |
'headers' => array( |
| 202 |
'X-AG-Access-Token' => $options['terms_opencalais_key'], |
| 203 |
'Content-Type' => 'text/html', |
| 204 |
'outputFormat' => 'application/json' |
| 205 |
), |
| 206 |
'body' => $content |
| 207 |
) ); |
| 208 |
$data = false; |
| 209 |
if ( ! is_wp_error( $response ) && $response != null ) { |
| 210 |
if ( wp_remote_retrieve_response_code( $response ) == 200 ) { |
| 211 |
$data_raw = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 212 |
$data = array(); |
| 213 |
if ( isset( $data_raw ) && is_array( $data_raw ) ) { |
| 214 |
foreach ( $data_raw as $_data_raw ) { |
| 215 |
if ( isset( $_data_raw['_typeGroup'] ) && $_data_raw['_typeGroup'] == 'socialTag' ) { |
| 216 |
$data[] = $_data_raw['name']; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
if($data){ |
| 223 |
if (!empty( $data )){ |
| 224 |
// Remove empty terms |
| 225 |
$data = array_filter( $data, '_delete_empty_element' ); |
| 226 |
$data = array_unique( $data ); |
| 227 |
|
| 228 |
foreach ( (array) $data as $term ) { |
| 229 |
$term = stripslashes( $term ); |
| 230 |
|
| 231 |
if ( ! is_string( $term ) ) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
$term = trim( $term ); |
| 236 |
if ( empty( $term ) ) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
//check if term belong to the post already |
| 241 |
if(has_term( $term, $taxonomy, $object )){ |
| 242 |
continue; |
| 243 |
} |
| 244 |
|
| 245 |
//exclude if name found in exclude terms |
| 246 |
if(in_array($term, $autoterm_exclude)){ |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
// Whole word ? |
| 251 |
if ( isset( $options['autoterm_word'] ) && (int) $options['autoterm_word'] == 1 ) { |
| 252 |
if(strpos($content, ' '.$term.' ') !== FALSE) |
| 253 |
{ |
| 254 |
$terms_to_add[] = $term; |
| 255 |
} |
| 256 |
|
| 257 |
//make exception for hashtag special character |
| 258 |
if (substr($term, 0, strlen('#')) === '#') { |
| 259 |
$trim_term = ltrim($term, '#'); |
| 260 |
if ( preg_match( "/\B(\#+$trim_term\b)(?!;)/i", $content ) ) { |
| 261 |
$terms_to_add[] = $term; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if ( isset( $options['autoterm_hash'] ) && (int) $options['autoterm_hash'] == 1 && stristr( $content, '#' . $term ) ) { |
| 266 |
$terms_to_add[] = $term; |
| 267 |
} |
| 268 |
} elseif ( stristr( $content, $term ) ) { |
| 269 |
$terms_to_add[] = $term; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
// Auto term with specific auto terms list |
| 278 |
if ( isset( $options['specific_terms'] ) && isset( $options['autoterm_useonly'] ) && (int)$options['autoterm_useonly'] === 1 ) { |
| 279 |
$terms = maybe_unserialize( $options['specific_terms'] ); |
| 280 |
$terms = taxopress_change_to_array($terms); |
| 281 |
foreach ( $terms as $term ) { |
| 282 |
if ( ! is_string( $term ) ) { |
| 283 |
continue; |
| 284 |
} |
| 285 |
|
| 286 |
$term = trim( $term ); |
| 287 |
if ( empty( $term ) ) { |
| 288 |
continue; |
| 289 |
} |
| 290 |
|
| 291 |
//check if term belong to the post already |
| 292 |
if(has_term( $term, $taxonomy, $object )){ |
| 293 |
continue; |
| 294 |
} |
| 295 |
|
| 296 |
//exclude if name found in exclude terms |
| 297 |
if(in_array($term, $autoterm_exclude)){ |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
// Whole word ? |
| 302 |
if ( isset( $options['autoterm_word'] ) && (int)$options['autoterm_word'] === 1 ) { |
| 303 |
if(strpos($content, ' '.$term.' ') !== FALSE) |
| 304 |
{ |
| 305 |
$terms_to_add[] = $term; |
| 306 |
} |
| 307 |
|
| 308 |
//make exception for hashtag special character |
| 309 |
if (substr($term, 0, strlen('#')) === '#') { |
| 310 |
$trim_term = ltrim($term, '#'); |
| 311 |
if ( preg_match( "/\B(\#+$trim_term\b)(?!;)/i", $content ) ) { |
| 312 |
$terms_to_add[] = $term; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if ( isset( $options['autoterm_hash'] ) && (int)$options['autoterm_hash'] === 1 && stristr( $content, '#' . $term ) ) { |
| 317 |
$terms_to_add[] = $term; |
| 318 |
} |
| 319 |
} elseif ( stristr( $content, $term ) ) { |
| 320 |
$terms_to_add[] = $term; |
| 321 |
} |
| 322 |
} |
| 323 |
unset( $terms, $term ); |
| 324 |
} |
| 325 |
|
| 326 |
// Auto terms with all terms |
| 327 |
if ( isset( $options['autoterm_useall'] ) && (int)$options['autoterm_useall'] === 1 ) { |
| 328 |
// Get all terms |
| 329 |
$terms = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT name |
| 330 |
FROM {$wpdb->terms} AS t |
| 331 |
INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id |
| 332 |
WHERE tt.taxonomy = %s", $taxonomy ) ); |
| 333 |
|
| 334 |
$terms = array_unique( $terms ); |
| 335 |
|
| 336 |
foreach ( $terms as $term ) { |
| 337 |
$term = stripslashes( $term ); |
| 338 |
|
| 339 |
if ( ! is_string( $term ) ) { |
| 340 |
continue; |
| 341 |
} |
| 342 |
|
| 343 |
$term = trim( $term ); |
| 344 |
if ( empty( $term ) ) { |
| 345 |
continue; |
| 346 |
} |
| 347 |
|
| 348 |
//check if term belong to the post already |
| 349 |
if(has_term( $term, $taxonomy, $object )){ |
| 350 |
continue; |
| 351 |
} |
| 352 |
|
| 353 |
//exclude if name found in exclude terms |
| 354 |
if(in_array($term, $autoterm_exclude)){ |
| 355 |
continue; |
| 356 |
} |
| 357 |
|
| 358 |
// Whole word ? |
| 359 |
if ( isset( $options['autoterm_word'] ) && (int) $options['autoterm_word'] == 1 ) { |
| 360 |
if(strpos($content, ' '.$term.' ') !== FALSE) |
| 361 |
{ |
| 362 |
$terms_to_add[] = $term; |
| 363 |
} |
| 364 |
|
| 365 |
//make exception for hashtag special character |
| 366 |
if (substr($term, 0, strlen('#')) === '#') { |
| 367 |
$trim_term = ltrim($term, '#'); |
| 368 |
if ( preg_match( "/\B(\#+$trim_term\b)(?!;)/i", $content ) ) { |
| 369 |
$terms_to_add[] = $term; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if ( isset( $options['autoterm_hash'] ) && (int) $options['autoterm_hash'] == 1 && stristr( $content, '#' . $term ) ) { |
| 374 |
$terms_to_add[] = $term; |
| 375 |
} |
| 376 |
} elseif ( stristr( $content, $term ) ) { |
| 377 |
$terms_to_add[] = $term; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// Append terms if terms to add |
| 383 |
if ( ! empty( $terms_to_add ) ) { |
| 384 |
// Remove empty and duplicate elements |
| 385 |
$terms_to_add = array_filter( $terms_to_add, '_delete_empty_element' ); |
| 386 |
$terms_to_add = array_unique( $terms_to_add ); |
| 387 |
|
| 388 |
//auto terms limit |
| 389 |
$terms_limit = isset($options['terms_limit']) ? (int)$options['terms_limit'] : 0; |
| 390 |
if($terms_limit > 0 && count($terms_to_add) > $terms_limit){ |
| 391 |
$terms_to_add = array_slice($terms_to_add, 0, $terms_limit); |
| 392 |
} |
| 393 |
|
| 394 |
if ( $counter == true ) { |
| 395 |
// Increment counter |
| 396 |
$counter = ( (int) get_option( 'tmp_auto_terms_st' ) ) + count( $terms_to_add ); |
| 397 |
update_option( 'tmp_auto_terms_st', $counter ); |
| 398 |
} |
| 399 |
|
| 400 |
// Add terms to posts |
| 401 |
wp_set_object_terms( $object->ID, $terms_to_add, $taxonomy, true ); |
| 402 |
|
| 403 |
// Clean cache |
| 404 |
clean_post_cache( $object->ID ); |
| 405 |
|
| 406 |
return true; |
| 407 |
} |
| 408 |
|
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|