1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sanderdekroon\Parlant\Compiler; |
4
|
|
|
|
5
|
|
|
use Sanderdekroon\Parlant\Builder\NestedTaxonomy; |
6
|
|
|
|
7
|
|
|
trait CompilesTaxonomies |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Compile the meta query to valid query arguments |
12
|
|
|
* @param array $wheres |
13
|
|
|
*/ |
14
|
4 |
|
protected function compileWhereTaxonomies($wheres) |
15
|
|
|
{ |
16
|
4 |
|
if (empty($wheres) || !is_array($wheres)) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
4 |
|
$compiled = []; |
21
|
|
|
|
22
|
4 |
|
foreach ($wheres as $where) { |
23
|
4 |
|
if ($where instanceof NestedTaxonomy) { |
24
|
1 |
|
$compiled[] = $this->compileNestedTaxonomy($where); |
25
|
1 |
|
continue; |
26
|
|
|
} |
27
|
|
|
|
28
|
4 |
|
$compiled[] = $this->prepareTaxonomyArguments($where); |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
$this->addArgument('tax_query', $compiled, true); |
|
|
|
|
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Complie nested meta query arguments to valid query arguments. If a meta query |
36
|
|
|
* contains another nested meta, we'll resolve that recursively. |
37
|
|
|
* @param NestedTaxonomy $nestedMeta |
38
|
|
|
* @param integer $level |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
1 |
|
protected function compileNestedTaxonomy($nestedTaxonomy, $level = 2) |
42
|
|
|
{ |
43
|
1 |
|
$query = $nestedTaxonomy->getQuery(); |
44
|
1 |
|
foreach ($query as $key => $taxonomy) { |
45
|
1 |
|
if ($taxonomy instanceof NestedTaxonomy) { |
46
|
|
|
$query[$key] = $this->compileNestedTaxonomy($taxonomy, $level++); |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$query[$key] = $this->prepareTaxonomyArguments($taxonomy); |
51
|
|
|
} |
52
|
1 |
|
$query['relation'] = $nestedTaxonomy->getRelation(); |
53
|
|
|
|
54
|
1 |
|
return $query; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Format the supplied arguments to WordPress arguments |
59
|
|
|
* @param array $meta |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
4 |
|
private function prepareTaxonomyArguments($taxonomy) |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
4 |
|
'taxonomy' => $taxonomy['taxonomy'], |
66
|
4 |
|
'field' => $taxonomy['field'], |
67
|
4 |
|
'terms' => $taxonomy['value'], |
68
|
4 |
|
'include_children' => $taxonomy['includeChildren'], |
69
|
4 |
|
'operator' => $taxonomy['operator'], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Compile the relation(s) between meta queries and add it to the arguments. |
75
|
|
|
* @param array $relations The index of the array is used as the level. |
76
|
|
|
*/ |
77
|
4 |
|
protected function compileWhereTaxonomyRelation($relations) |
78
|
|
|
{ |
79
|
4 |
|
return $this->addArgument('tax_query', ['relation' => reset($relations)], true); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected abstract function addArgument(); |
83
|
|
|
} |
84
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.