Conditions | 7 |
Paths | 9 |
Total Lines | 68 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
26 | public function advanced_custom_search($where, &$wp_query) |
||
27 | { |
||
28 | global $wpdb; |
||
29 | |||
30 | if (empty($where)) { |
||
31 | return $where; |
||
32 | } |
||
33 | |||
34 | // get search expression |
||
35 | $terms = $wp_query->query_vars['s']; |
||
36 | |||
37 | // explode search expression to get search terms |
||
38 | $exploded = explode(' ', $terms); |
||
39 | |||
40 | if ($exploded === false || count($exploded) == 0) { |
||
41 | $exploded = array(0 => $terms); |
||
42 | } |
||
43 | |||
44 | // reset search in order to rebuilt it as we whish |
||
45 | $where = ''; |
||
46 | |||
47 | // get searcheable_acf, a list of advanced custom fields you want to search content in |
||
48 | $list_searcheable_acf = $this->list_searcheable_acf; |
||
49 | |||
50 | foreach ($exploded as $tag) { |
||
51 | $where .= " |
||
52 | AND ( |
||
53 | (wp_posts.post_title LIKE '%$tag%') |
||
54 | OR (wp_posts.post_content LIKE '%$tag%') |
||
55 | OR EXISTS ( |
||
56 | SELECT * FROM wp_postmeta |
||
57 | WHERE post_id = wp_posts.ID |
||
58 | AND ("; |
||
59 | |||
60 | foreach ($list_searcheable_acf as $searcheable_acf) { |
||
61 | if ($searcheable_acf == $list_searcheable_acf[0]) { |
||
62 | $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
||
63 | } else { |
||
64 | $where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; |
||
65 | } |
||
66 | |||
67 | $where .= ") |
||
68 | ) |
||
69 | OR EXISTS ( |
||
70 | SELECT * FROM wp_comments |
||
71 | WHERE comment_post_ID = wp_posts.ID |
||
72 | AND comment_content LIKE '%$tag%' |
||
73 | ) |
||
74 | OR EXISTS ( |
||
75 | SELECT * FROM wp_terms |
||
76 | INNER JOIN wp_term_taxonomy |
||
77 | ON wp_term_taxonomy.term_id = wp_terms.term_id |
||
78 | INNER JOIN wp_term_relationships |
||
79 | ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id |
||
80 | WHERE ( |
||
81 | taxonomy = 'post_tag' |
||
82 | OR taxonomy = 'category' |
||
83 | OR taxonomy = 'myCustomTax' |
||
84 | ) |
||
85 | AND object_id = wp_posts.ID |
||
86 | AND wp_terms.name LIKE '%$tag%' |
||
87 | ) |
||
88 | )"; |
||
89 | } |
||
90 | |||
91 | return $where; |
||
92 | } |
||
93 | } |
||
94 | } |
||
98 | }); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.