Conditions | 28 |
Paths | 1280 |
Total Lines | 140 |
Code Lines | 103 |
Lines | 8 |
Ratio | 5.71 % |
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 |
||
174 | function get_items( $args = null ) { |
||
175 | global $wpdb; |
||
176 | |||
177 | $defaults = array( |
||
178 | 'type' => 'monthly-nested', |
||
179 | 'limit' => '', |
||
180 | 'show_post_count' => false, |
||
181 | 'order' => 'DESC', |
||
182 | 'post_type' => 'post', |
||
183 | 'show_year' => false, |
||
184 | 'nested' => false |
||
185 | ); |
||
186 | |||
187 | $args = wp_parse_args($args, $defaults); |
||
188 | $post_type = $args['post_type']; |
||
189 | $order = $args['order']; |
||
190 | $nested = $args['nested']; |
||
191 | $type = $args['type']; |
||
192 | $limit = ''; |
||
193 | if ( $type == 'yearlymonthly' || $type == 'yearmonth' ) { |
||
194 | $type = 'monthly-nested'; |
||
195 | } |
||
196 | if ( $type == 'monthly-nested' ) { |
||
197 | $nested = true; |
||
198 | } |
||
199 | |||
200 | if ( !empty($args['limit']) ) { |
||
201 | $limit = absint($limit); |
||
202 | $limit = ' LIMIT ' . $limit; |
||
203 | } |
||
204 | |||
205 | $order = strtoupper($order); |
||
206 | if ( $order !== 'ASC' ) { |
||
207 | $order = 'DESC'; |
||
208 | } |
||
209 | |||
210 | // this is what will separate dates on weekly archive links |
||
211 | $archive_week_separator = '–'; |
||
212 | |||
213 | // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride |
||
214 | $archive_date_format_over_ride = 0; |
||
215 | |||
216 | // options for daily archive (only if you over-ride the general date format) |
||
217 | $archive_day_date_format = 'Y/m/d'; |
||
218 | |||
219 | // options for weekly archive (only if you over-ride the general date format) |
||
220 | $archive_week_start_date_format = 'Y/m/d'; |
||
221 | $archive_week_end_date_format = 'Y/m/d'; |
||
222 | |||
223 | if (!$archive_date_format_over_ride) { |
||
224 | $archive_day_date_format = get_option('date_format'); |
||
225 | $archive_week_start_date_format = get_option('date_format'); |
||
226 | $archive_week_end_date_format = get_option('date_format'); |
||
227 | } |
||
228 | |||
229 | $where = $wpdb->prepare('WHERE post_type = "%s" AND post_status = "publish"', $post_type); |
||
230 | $where = apply_filters('getarchives_where', $where, $args); |
||
231 | $join = apply_filters('getarchives_join', '', $args); |
||
232 | |||
233 | $output = array(); |
||
234 | $last_changed = wp_cache_get('last_changed', 'posts'); |
||
235 | if (!$last_changed) { |
||
236 | $last_changed = microtime(); |
||
237 | wp_cache_set('last_changed', $last_changed, 'posts'); |
||
238 | } |
||
239 | if ( 'monthly' == $type ) { |
||
240 | $output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit, $nested); |
||
241 | } elseif ( 'yearly' == $type ) { |
||
242 | $output = $this->get_items_yearly($args, $last_changed, $join, $where, $order, $limit); |
||
243 | } elseif ( 'monthly-nested' == $type ) { |
||
244 | $output = $this->get_items_monthly($args, $last_changed, $join, $where, $order, $limit); |
||
245 | } elseif ( 'daily' == $type ) { |
||
246 | $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit"; |
||
247 | $key = md5($query); |
||
248 | $key = "wp_get_archives:$key:$last_changed"; |
||
249 | if (!$results = wp_cache_get($key, 'posts')) { |
||
250 | $results = $wpdb->get_results($query); |
||
251 | $cache = array(); |
||
252 | $cache[$key] = $results; |
||
253 | wp_cache_set($key, $results, 'posts'); |
||
254 | } |
||
255 | if ( $results ) { |
||
256 | foreach ( (array)$results as $result ) { |
||
257 | $url = get_day_link($result->year, $result->month, $result->dayofmonth); |
||
258 | $date = sprintf('%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth); |
||
259 | $text = mysql2date($archive_day_date_format, $date); |
||
260 | $output[] = $this->get_archives_link($url, $text); |
||
261 | } |
||
262 | } |
||
263 | } elseif ( 'weekly' == $type ) { |
||
264 | $week = _wp_mysql_week('`post_date`'); |
||
265 | $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, " |
||
266 | . "count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit"; |
||
267 | $key = md5($query); |
||
268 | $key = "wp_get_archives:$key:$last_changed"; |
||
269 | View Code Duplication | if (!$results = wp_cache_get($key, 'posts')) { |
|
270 | $results = $wpdb->get_results($query); |
||
271 | wp_cache_set($key, $results, 'posts'); |
||
272 | } |
||
273 | $arc_w_last = ''; |
||
274 | if ( $results ) { |
||
275 | foreach ( (array)$results as $result ) { |
||
276 | if ( $result->week != $arc_w_last ) { |
||
277 | $arc_year = $result->yr; |
||
278 | $arc_w_last = $result->week; |
||
279 | $arc_week = get_weekstartend($result->yyyymmdd, get_option('start_of_week')); |
||
280 | $arc_week_start = date_i18n($archive_week_start_date_format, $arc_week['start']); |
||
281 | $arc_week_end = date_i18n($archive_week_end_date_format, $arc_week['end']); |
||
282 | $url = sprintf('%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week); |
||
283 | $text = $arc_week_start . $archive_week_separator . $arc_week_end; |
||
284 | $output[] = $this->get_archives_link($url, $text); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | } elseif ( 'postbypost' == $type || 'alpha' == $type ) { |
||
289 | $orderby = 'alpha' == $type ? 'post_title ASC ' : 'post_date DESC '; |
||
290 | $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; |
||
291 | $key = md5($query); |
||
292 | $key = "wp_get_archives:$key:$last_changed"; |
||
293 | View Code Duplication | if ( !$results = wp_cache_get($key, 'posts') ) { |
|
294 | $results = $wpdb->get_results($query); |
||
295 | wp_cache_set($key, $results, 'posts'); |
||
296 | } |
||
297 | if ( $results ) { |
||
298 | foreach ( (array)$results as $result ) { |
||
299 | if ($result->post_date != '0000-00-00 00:00:00') { |
||
300 | $url = get_permalink($result); |
||
301 | if ($result->post_title) { |
||
302 | /** This filter is documented in wp-includes/post-template.php */ |
||
303 | $text = strip_tags(apply_filters('the_title', $result->post_title, $result->ID)); |
||
304 | } else { |
||
305 | $text = $result->ID; |
||
306 | } |
||
307 | $output[] = $this->get_archives_link($url, $text); |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | return $output; |
||
313 | } |
||
314 | |||
316 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.