Conditions | 38 |
Paths | > 20000 |
Total Lines | 180 |
Code Lines | 111 |
Lines | 27 |
Ratio | 15 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
117 | function get_bookmarks( $args = '' ) { |
||
118 | global $wpdb; |
||
119 | |||
120 | $defaults = array( |
||
121 | 'orderby' => 'name', 'order' => 'ASC', |
||
122 | 'limit' => -1, 'category' => '', |
||
123 | 'category_name' => '', 'hide_invisible' => 1, |
||
124 | 'show_updated' => 0, 'include' => '', |
||
125 | 'exclude' => '', 'search' => '' |
||
126 | ); |
||
127 | |||
128 | $r = wp_parse_args( $args, $defaults ); |
||
129 | |||
130 | $key = md5( serialize( $r ) ); |
||
131 | $cache = false; |
||
132 | if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { |
||
133 | if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { |
||
134 | $bookmarks = $cache[ $key ]; |
||
135 | /** |
||
136 | * Filters the returned list of bookmarks. |
||
137 | * |
||
138 | * The first time the hook is evaluated in this file, it returns the cached |
||
139 | * bookmarks list. The second evaluation returns a cached bookmarks list if the |
||
140 | * link category is passed but does not exist. The third evaluation returns |
||
141 | * the full cached results. |
||
142 | * |
||
143 | * @since 2.1.0 |
||
144 | * |
||
145 | * @see get_bookmarks() |
||
146 | * |
||
147 | * @param array $bookmarks List of the cached bookmarks. |
||
148 | * @param array $r An array of bookmark query arguments. |
||
149 | */ |
||
150 | return apply_filters( 'get_bookmarks', $bookmarks, $r ); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | if ( ! is_array( $cache ) ) { |
||
155 | $cache = array(); |
||
156 | } |
||
157 | |||
158 | $inclusions = ''; |
||
159 | if ( ! empty( $r['include'] ) ) { |
||
160 | $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include |
||
161 | $r['category'] = ''; |
||
162 | $r['category_name'] = ''; |
||
163 | $inclinks = preg_split( '/[\s,]+/', $r['include'] ); |
||
164 | if ( count( $inclinks ) ) { |
||
165 | foreach ( $inclinks as $inclink ) { |
||
166 | if ( empty( $inclusions ) ) { |
||
167 | $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' '; |
||
168 | } else { |
||
169 | $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' '; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | if (! empty( $inclusions ) ) { |
||
175 | $inclusions .= ')'; |
||
176 | } |
||
177 | |||
178 | $exclusions = ''; |
||
179 | View Code Duplication | if ( ! empty( $r['exclude'] ) ) { |
|
180 | $exlinks = preg_split( '/[\s,]+/', $r['exclude'] ); |
||
181 | if ( count( $exlinks ) ) { |
||
182 | foreach ( $exlinks as $exlink ) { |
||
183 | if ( empty( $exclusions ) ) { |
||
184 | $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' '; |
||
185 | } else { |
||
186 | $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' '; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | if ( ! empty( $exclusions ) ) { |
||
192 | $exclusions .= ')'; |
||
193 | } |
||
194 | |||
195 | if ( ! empty( $r['category_name'] ) ) { |
||
196 | if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) { |
||
197 | $r['category'] = $r['category']->term_id; |
||
198 | } else { |
||
199 | $cache[ $key ] = array(); |
||
200 | wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
||
201 | /** This filter is documented in wp-includes/bookmark.php */ |
||
202 | return apply_filters( 'get_bookmarks', array(), $r ); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | $search = ''; |
||
207 | if ( ! empty( $r['search'] ) ) { |
||
208 | $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; |
||
209 | $search = $wpdb->prepare(" AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like ); |
||
210 | } |
||
211 | |||
212 | $category_query = ''; |
||
213 | $join = ''; |
||
214 | View Code Duplication | if ( ! empty( $r['category'] ) ) { |
|
215 | $incategories = preg_split( '/[\s,]+/', $r['category'] ); |
||
216 | if ( count($incategories) ) { |
||
217 | foreach ( $incategories as $incat ) { |
||
218 | if ( empty( $category_query ) ) { |
||
219 | $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' '; |
||
220 | } else { |
||
221 | $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' '; |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | if ( ! empty( $category_query ) ) { |
||
227 | $category_query .= ") AND taxonomy = 'link_category'"; |
||
228 | $join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; |
||
229 | } |
||
230 | |||
231 | if ( $r['show_updated'] ) { |
||
232 | $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated "; |
||
233 | } else { |
||
234 | $recently_updated_test = ''; |
||
235 | } |
||
236 | |||
237 | $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
||
238 | |||
239 | $orderby = strtolower( $r['orderby'] ); |
||
240 | $length = ''; |
||
241 | switch ( $orderby ) { |
||
242 | case 'length': |
||
243 | $length = ", CHAR_LENGTH(link_name) AS length"; |
||
244 | break; |
||
245 | case 'rand': |
||
246 | $orderby = 'rand()'; |
||
247 | break; |
||
248 | case 'link_id': |
||
249 | $orderby = "$wpdb->links.link_id"; |
||
250 | break; |
||
251 | default: |
||
252 | $orderparams = array(); |
||
253 | $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); |
||
254 | foreach ( explode( ',', $orderby ) as $ordparam ) { |
||
255 | $ordparam = trim( $ordparam ); |
||
256 | |||
257 | if ( in_array( 'link_' . $ordparam, $keys ) ) { |
||
258 | $orderparams[] = 'link_' . $ordparam; |
||
259 | } elseif ( in_array( $ordparam, $keys ) ) { |
||
260 | $orderparams[] = $ordparam; |
||
261 | } |
||
262 | } |
||
263 | $orderby = implode( ',', $orderparams ); |
||
264 | } |
||
265 | |||
266 | if ( empty( $orderby ) ) { |
||
267 | $orderby = 'link_name'; |
||
268 | } |
||
269 | |||
270 | $order = strtoupper( $r['order'] ); |
||
271 | View Code Duplication | if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) { |
|
272 | $order = 'ASC'; |
||
273 | } |
||
274 | |||
275 | $visible = ''; |
||
276 | if ( $r['hide_invisible'] ) { |
||
277 | $visible = "AND link_visible = 'Y'"; |
||
278 | } |
||
279 | |||
280 | $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
||
281 | $query .= " $exclusions $inclusions $search"; |
||
282 | $query .= " ORDER BY $orderby $order"; |
||
283 | if ( $r['limit'] != -1 ) { |
||
284 | $query .= ' LIMIT ' . $r['limit']; |
||
285 | } |
||
286 | |||
287 | $results = $wpdb->get_results( $query ); |
||
288 | |||
289 | if ( 'rand()' !== $orderby ) { |
||
290 | $cache[ $key ] = $results; |
||
291 | wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
||
292 | } |
||
293 | |||
294 | /** This filter is documented in wp-includes/bookmark.php */ |
||
295 | return apply_filters( 'get_bookmarks', $results, $r ); |
||
296 | } |
||
297 | |||
421 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.