PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 33ms 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
  1<?php
  2class 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	/**
 15	 * Check post/page content for auto terms
 16	 *
 17	 * @param integer $post_id
 18	 * @param object $object
 19	 * @return boolean
 20	 */
 21	function saveAutoTerms( $post_id = null, $object = null ) {
 22		// Get options
 23		$options = get_option( STAGS_OPTIONS_NAME_AUTO );
 24		
 25		// Auto terms for this CPT ?
 26		if ( !isset($options[$object->post_type]) || empty($options[$object->post_type]) )
 27			return false;
 28			
 29		// user preference for this post ?
 30		$meta_value = get_post_meta( $object->ID, '_exclude_autotags', true );
 31		if ( !empty($meta_value) )
 32			return false;
 33		
 34		// Loop option for find if autoterms is actived on any taxo
 35		$flag = false;
 36		foreach( $options[$object->post_type] as $taxo_name => $local_options ) {
 37			if ( !isset($local_options['use_auto_terms']) || $local_options['use_auto_terms'] != '1' )
 38				continue;
 39			
 40			$this->autoTermsPost( $object, $taxo_name, $local_options );
 41			$flag = true;
 42		}
 43		
 44		if ( $flag == true ) { // Clean cache ?
 45			if ( isset($object->post_type) && $object->post_type == 'page' ) {
 46				clean_page_cache($post_id);
 47			} else {
 48				clean_post_cache($post_id);
 49			}
 50		}
 51		
 52		return true;
 53	}
 54	
 55	/**
 56	 * Automatically tag a post/page from the database terms for the taxonomy specified
 57	 *
 58	 * @param object $object 
 59	 * @param string $taxonomy 
 60	 * @param array $options 
 61	 * @param boolean $counter 
 62	 * @return boolean
 63	 * @author Amaury Balmer
 64	 */
 65	function autoTermsPost( $object, $taxonomy = 'post_tag', $options = array(), $counter = false ) {
 66		global $wpdb;
 67		
 68		// Option exists ?
 69		if ( $options == false || empty($options) ) {
 70			return false;
 71		}
 72		
 73		if ( get_the_terms($object->ID, $taxonomy) != false && $options['at_empty'] == 1 ) {
 74			return false; // Skip post with terms, if term only empty post option is checked
 75		}
 76		
 77		$terms_to_add = array();
 78		
 79		// Merge title + content + excerpt to compare with terms
 80		$content = $object->post_content. ' ' . $object->post_title;
 81		if ( isset($object->post_excerpt) )
 82		 	$content .= ' ' . $object->post_excerpt;
 83		
 84		$content = trim(strip_tags($content));
 85		if ( empty($content) ) {
 86			return false;
 87		}
 88		
 89		// Auto term with specific auto terms list
 90		if ( isset($options['auto_list']) ) {
 91			$terms = (array) maybe_unserialize($options['auto_list']);
 92			foreach ( $terms as $term ) {
 93				if ( !is_string($term) && empty($term) )
 94				 	continue;
 95				
 96				$term = trim($term);
 97				
 98				// Whole word ?
 99				if ( (int) $options['only_full_word'] == 1 ) {
100					if ( preg_match("/\b".$term."\b/i", $content) )
101						$terms_to_add[] = $term;
102				} elseif ( stristr($content, $term) ) {
103					$terms_to_add[] = $term;
104				}
105			}
106			unset($terms, $term);
107		}
108		
109		// Auto terms with all terms
110		if ( isset($options['at_all']) && $options['at_all'] == 1 ) {
111			// Get all terms
112			$terms = $wpdb->get_col( $wpdb->prepare("SELECT DISTINCT name
113				FROM {$wpdb->terms} AS t
114				INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
115				WHERE tt.taxonomy = %s", $taxonomy) );
116			
117			$terms = array_unique($terms);
118			
119			foreach ( $terms as $term ) {
120				$term = stripslashes($term);
121				
122				if ( !is_string($term) && empty($term) )
123				 	continue;
124				
125				// Whole word ?
126				if ( (int) $options['only_full_word'] == 1 ) {
127					$term = ' '.$term.' '; // Add space before and after !
128				}
129				
130				if ( stristr($content, $term) ) {
131					$terms_to_add[] = $term;
132				}
133			}
134			
135			// Clean memory
136			$terms = array();
137			unset($terms, $term);
138		}
139		
140		// Append terms if terms to add
141		if ( !empty($terms_to_add) ) {
142			// Remove empty and duplicate elements
143			$terms_to_add = array_filter($terms_to_add, '_delete_empty_element');
144			$terms_to_add = array_unique($terms_to_add);
145			
146			if ( $counter == true ) {
147				// Increment counter
148				$counter = ((int) get_option('tmp_auto_terms_st')) + count($terms_to_add);
149				update_option('tmp_auto_terms_st', $counter);
150			}
151			
152			// Add terms to posts
153			wp_set_object_terms( $object->ID, $terms_to_add, $taxonomy, true );
154			
155			// Clean cache
156			if ( isset($object->post_type) && $object->post_type = 'page' ) {
157				clean_page_cache($object->ID);
158			} else {
159				clean_post_cache($object->ID);
160			}
161			
162			return true;
163		}
164		
165		return false;
166	}
167
168}
169?>