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

Language PHP Lines 169
MD5 Hash af6ecb0fce6e8119af5f00f26f06ab62 Estimated Cost $2,332 (why?)
Repository https://bitbucket.org/Wallynm/iptb View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
class SimpleTags_Client_Autoterms extends SimpleTags_Client {
	/**
	 * Constructor
	 *
	 * @return void
	 * @author Amaury Balmer
	 */
	function SimpleTags_Client_Autoterms() {
		add_action( 'save_post', 				array(&$this, 'saveAutoTerms'), 12, 2 );
		add_action( 'post_syndicated_item', 	array(&$this, 'saveAutoTerms'), 12, 2 );
	}
	
	/**
	 * Check post/page content for auto terms
	 *
	 * @param integer $post_id
	 * @param object $object
	 * @return boolean
	 */
	function saveAutoTerms( $post_id = null, $object = null ) {
		// Get options
		$options = get_option( STAGS_OPTIONS_NAME_AUTO );
		
		// Auto terms for this CPT ?
		if ( !isset($options[$object->post_type]) || empty($options[$object->post_type]) )
			return false;
			
		// user preference for this post ?
		$meta_value = get_post_meta( $object->ID, '_exclude_autotags', true );
		if ( !empty($meta_value) )
			return false;
		
		// Loop option for find if autoterms is actived on any taxo
		$flag = false;
		foreach( $options[$object->post_type] as $taxo_name => $local_options ) {
			if ( !isset($local_options['use_auto_terms']) || $local_options['use_auto_terms'] != '1' )
				continue;
			
			$this->autoTermsPost( $object, $taxo_name, $local_options );
			$flag = true;
		}
		
		if ( $flag == true ) { // Clean cache ?
			if ( isset($object->post_type) && $object->post_type == 'page' ) {
				clean_page_cache($post_id);
			} else {
				clean_post_cache($post_id);
			}
		}
		
		return true;
	}
	
	/**
	 * Automatically tag a post/page from the database terms for the taxonomy specified
	 *
	 * @param object $object 
	 * @param string $taxonomy 
	 * @param array $options 
	 * @param boolean $counter 
	 * @return boolean
	 * @author Amaury Balmer
	 */
	function autoTermsPost( $object, $taxonomy = 'post_tag', $options = array(), $counter = false ) {
		global $wpdb;
		
		// Option exists ?
		if ( $options == false || empty($options) ) {
			return false;
		}
		
		if ( get_the_terms($object->ID, $taxonomy) != false && $options['at_empty'] == 1 ) {
			return false; // Skip post with terms, if term only empty post option is checked
		}
		
		$terms_to_add = array();
		
		// Merge title + content + excerpt to compare with terms
		$content = $object->post_content. ' ' . $object->post_title;
		if ( isset($object->post_excerpt) )
		 	$content .= ' ' . $object->post_excerpt;
		
		$content = trim(strip_tags($content));
		if ( empty($content) ) {
			return false;
		}
		
		// Auto term with specific auto terms list
		if ( isset($options['auto_list']) ) {
			$terms = (array) maybe_unserialize($options['auto_list']);
			foreach ( $terms as $term ) {
				if ( !is_string($term) && empty($term) )
				 	continue;
				
				$term = trim($term);
				
				// Whole word ?
				if ( (int) $options['only_full_word'] == 1 ) {
					if ( preg_match("/\b".$term."\b/i", $content) )
						$terms_to_add[] = $term;
				} elseif ( stristr($content, $term) ) {
					$terms_to_add[] = $term;
				}
			}
			unset($terms, $term);
		}
		
		// Auto terms with all terms
		if ( isset($options['at_all']) && $options['at_all'] == 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) && empty($term) )
				 	continue;
				
				// Whole word ?
				if ( (int) $options['only_full_word'] == 1 ) {
					$term = ' '.$term.' '; // Add space before and after !
				}
				
				if ( stristr($content, $term) ) {
					$terms_to_add[] = $term;
				}
			}
			
			// Clean memory
			$terms = array();
			unset($terms, $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);
			
			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
			if ( isset($object->post_type) && $object->post_type = 'page' ) {
				clean_page_cache($object->ID);
			} else {
				clean_post_cache($object->ID);
			}
			
			return true;
		}
		
		return false;
	}

}
?>
Back to Top