| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class TimberTermGetter { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	 * @param string|array $args | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	 * @param array $maybe_args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	 * @param string $TermClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	public static function get_terms($args = null, $maybe_args = array(), $TermClass = 'TimberTerm') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		if ( is_string($maybe_args) && !strstr($maybe_args, '=') ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 			//the user is sending the $TermClass in the second argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 			$TermClass = $maybe_args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		if ( is_string($maybe_args) && strstr($maybe_args, '=') ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 			parse_str($maybe_args, $maybe_args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		if ( is_string($args) && strstr($args, '=') ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			//a string and a query string! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			$parsed = self::get_term_query_from_query_string($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			if ( is_array($maybe_args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 				$parsed->args = array_merge($parsed->args, $maybe_args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		} else if ( is_string($args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			//its just a string with a single taxonomy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$parsed = self::get_term_query_from_string($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			if ( is_array($maybe_args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 				$parsed->args = array_merge($parsed->args, $maybe_args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		} else if ( is_array($args) && TimberHelper::is_array_assoc($args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			//its an associative array, like a good ole query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$parsed = self::get_term_query_from_assoc_array($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		} else if ( is_array($args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			//its just an array of strings or IDs (hopefully) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			$parsed = self::get_term_query_from_array($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			if ( is_array($maybe_args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 				$parsed->args = array_merge($parsed->args, $maybe_args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			return self::handle_term_query($parsed->taxonomies, $parsed->args, $TermClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		} else if ( is_null($args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			return self::handle_term_query(get_taxonomies(), array(), $TermClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @param string|array $taxonomies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @param string|array $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @param string $TermClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	public static function handle_term_query($taxonomies, $args, $TermClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		if ( !isset($args['hide_empty']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			$args['hide_empty'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		if ( isset($args['term_id']) && is_int($args['term_id']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			$args['term_id'] = array($args['term_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		if ( isset($args['term_id']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$args['include'] = $args['term_id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$terms = get_terms($taxonomies, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		foreach ($terms as &$term) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			$term = new $TermClass($term->term_id, $term->taxonomy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		return $terms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @param string $query_string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	protected static function get_term_query_from_query_string($query_string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$args = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		parse_str($query_string, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$ret = self::get_term_query_from_assoc_array($args); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * @param string $taxs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	protected static function get_term_query_from_string($taxs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$ret = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$ret->args = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if ( is_string($taxs) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$taxs = array($taxs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$ret->taxonomies = self::correct_taxonomy_names($taxs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @param array $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	public static function get_term_query_from_assoc_array($args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$ret = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$ret->args = $args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		if ( isset($ret->args['tax']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			$ret->taxonomies = $ret->args['tax']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		} else if ( isset($ret->args['taxonomies']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$ret->taxonomies = $ret->args['taxonomies']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} else if ( isset($ret->args['taxs']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			$ret->taxonomies = $ret->args['taxs']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		} else if ( isset($ret->args['taxonomy']) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$ret->taxonomies = $ret->args['taxonomy']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		if ( isset($ret->taxonomies) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			if ( is_string($ret->taxonomies) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 				$ret->taxonomies = array($ret->taxonomies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			$ret->taxonomies = self::correct_taxonomy_names($ret->taxonomies); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			$ret->taxonomies = get_taxonomies(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * @param array $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @return stdClass | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	public static function get_term_query_from_array($args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		if ( is_array($args) && !empty($args) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			//okay its an array with content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			if ( is_int($args[0]) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 				return self::get_term_query_from_array_of_ids($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			} else if ( is_string($args[0]) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 				return self::get_term_query_from_array_of_strings($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @param integer[] $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	public static function get_term_query_from_array_of_ids($args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		$ret = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$ret->taxonomies = get_taxonomies(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		$ret->args['include'] = $args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * @param string[] $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @return stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	public static function get_term_query_from_array_of_strings($args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		$ret = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$ret->taxonomies = self::correct_taxonomy_names($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$ret->args = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * @param string|array $taxs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  | 	private static function correct_taxonomy_names($taxs) { | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  | 		if ( is_string($taxs) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  | 			$taxs = array($taxs); | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  | 		foreach ($taxs as &$tax) { | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  | 			if ( $tax == 'tags' || $tax == 'tag' ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  | 				$tax = 'post_tag'; | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  | 			} else if ( $tax == 'categories' ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  | 				$tax = 'category'; | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  | 		return $taxs; | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  | 
            
                        
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.