PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/simple-tags/inc/class.client.autoterms.php

https://bitbucket.org/Wallynm/iptb
PHP | 169 lines | 99 code | 30 blank | 40 comment | 41 complexity | af6ecb0fce6e8119af5f00f26f06ab62 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.0
  1. <?php
  2. class SimpleTags_Client_Autoterms extends SimpleTags_Client {
  3. /**
  4. * Constructor
  5. *
  6. * @return void
  7. * @author Amaury Balmer
  8. */
  9. function SimpleTags_Client_Autoterms() {
  10. add_action( 'save_post', array(&$this, 'saveAutoTerms'), 12, 2 );
  11. add_action( 'post_syndicated_item', array(&$this, 'saveAutoTerms'), 12, 2 );
  12. }
  13. /**
  14. * Check post/page content for auto terms
  15. *
  16. * @param integer $post_id
  17. * @param object $object
  18. * @return boolean
  19. */
  20. function saveAutoTerms( $post_id = null, $object = null ) {
  21. // Get options
  22. $options = get_option( STAGS_OPTIONS_NAME_AUTO );
  23. // Auto terms for this CPT ?
  24. if ( !isset($options[$object->post_type]) || empty($options[$object->post_type]) )
  25. return false;
  26. // user preference for this post ?
  27. $meta_value = get_post_meta( $object->ID, '_exclude_autotags', true );
  28. if ( !empty($meta_value) )
  29. return false;
  30. // Loop option for find if autoterms is actived on any taxo
  31. $flag = false;
  32. foreach( $options[$object->post_type] as $taxo_name => $local_options ) {
  33. if ( !isset($local_options['use_auto_terms']) || $local_options['use_auto_terms'] != '1' )
  34. continue;
  35. $this->autoTermsPost( $object, $taxo_name, $local_options );
  36. $flag = true;
  37. }
  38. if ( $flag == true ) { // Clean cache ?
  39. if ( isset($object->post_type) && $object->post_type == 'page' ) {
  40. clean_page_cache($post_id);
  41. } else {
  42. clean_post_cache($post_id);
  43. }
  44. }
  45. return true;
  46. }
  47. /**
  48. * Automatically tag a post/page from the database terms for the taxonomy specified
  49. *
  50. * @param object $object
  51. * @param string $taxonomy
  52. * @param array $options
  53. * @param boolean $counter
  54. * @return boolean
  55. * @author Amaury Balmer
  56. */
  57. function autoTermsPost( $object, $taxonomy = 'post_tag', $options = array(), $counter = false ) {
  58. global $wpdb;
  59. // Option exists ?
  60. if ( $options == false || empty($options) ) {
  61. return false;
  62. }
  63. if ( get_the_terms($object->ID, $taxonomy) != false && $options['at_empty'] == 1 ) {
  64. return false; // Skip post with terms, if term only empty post option is checked
  65. }
  66. $terms_to_add = array();
  67. // Merge title + content + excerpt to compare with terms
  68. $content = $object->post_content. ' ' . $object->post_title;
  69. if ( isset($object->post_excerpt) )
  70. $content .= ' ' . $object->post_excerpt;
  71. $content = trim(strip_tags($content));
  72. if ( empty($content) ) {
  73. return false;
  74. }
  75. // Auto term with specific auto terms list
  76. if ( isset($options['auto_list']) ) {
  77. $terms = (array) maybe_unserialize($options['auto_list']);
  78. foreach ( $terms as $term ) {
  79. if ( !is_string($term) && empty($term) )
  80. continue;
  81. $term = trim($term);
  82. // Whole word ?
  83. if ( (int) $options['only_full_word'] == 1 ) {
  84. if ( preg_match("/\b".$term."\b/i", $content) )
  85. $terms_to_add[] = $term;
  86. } elseif ( stristr($content, $term) ) {
  87. $terms_to_add[] = $term;
  88. }
  89. }
  90. unset($terms, $term);
  91. }
  92. // Auto terms with all terms
  93. if ( isset($options['at_all']) && $options['at_all'] == 1 ) {
  94. // Get all terms
  95. $terms = $wpdb->get_col( $wpdb->prepare("SELECT DISTINCT name
  96. FROM {$wpdb->terms} AS t
  97. INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
  98. WHERE tt.taxonomy = %s", $taxonomy) );
  99. $terms = array_unique($terms);
  100. foreach ( $terms as $term ) {
  101. $term = stripslashes($term);
  102. if ( !is_string($term) && empty($term) )
  103. continue;
  104. // Whole word ?
  105. if ( (int) $options['only_full_word'] == 1 ) {
  106. $term = ' '.$term.' '; // Add space before and after !
  107. }
  108. if ( stristr($content, $term) ) {
  109. $terms_to_add[] = $term;
  110. }
  111. }
  112. // Clean memory
  113. $terms = array();
  114. unset($terms, $term);
  115. }
  116. // Append terms if terms to add
  117. if ( !empty($terms_to_add) ) {
  118. // Remove empty and duplicate elements
  119. $terms_to_add = array_filter($terms_to_add, '_delete_empty_element');
  120. $terms_to_add = array_unique($terms_to_add);
  121. if ( $counter == true ) {
  122. // Increment counter
  123. $counter = ((int) get_option('tmp_auto_terms_st')) + count($terms_to_add);
  124. update_option('tmp_auto_terms_st', $counter);
  125. }
  126. // Add terms to posts
  127. wp_set_object_terms( $object->ID, $terms_to_add, $taxonomy, true );
  128. // Clean cache
  129. if ( isset($object->post_type) && $object->post_type = 'page' ) {
  130. clean_page_cache($object->ID);
  131. } else {
  132. clean_post_cache($object->ID);
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. }
  139. ?>