| Conditions | 7 |
| Paths | 9 |
| Total Lines | 68 |
| 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 | }); |
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.