-
Notifications
You must be signed in to change notification settings - Fork 57
/
page-nth-level-location-rule.php
89 lines (79 loc) · 2.28 KB
/
page-nth-level-location-rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
ACF custom location rule : Page Level
level "1" = top level parent page
this should work on any hierarchical post type?
Works on number of ancestors
*/
if (class_exists('ACF_LOCATION')) {
class location_page_level_jh extends ACF_LOCATION {
function initialize() {
$this->name = 'page_level';
$this->label = 'Page (Post) Level';
$this->category = 'page';
} // end function initialize
static function get_operators($rule) {
$operators = array(
'!=' => 'is not equal to',
'<' => 'is less than',
'<=' => 'is less than or equal to',
'==' => 'is equal to',
'>=' => 'is greater and or equal to',
'>' => 'is greater than'
);
return $operators;
} // end static function get_operators
public function get_values($rule) {
// value indicates number of ancestors
$value_add = array(
' (Parent)',
' (Child)',
' (Grandchild)'
);
$values = array();
for ($i=0; $i<10; $i++) {
$values[$i] = strval($i+1);
if (isset($value_add[$i])) {
$values[$i] .= $value_add[$i];
}
} // end for
return $values;
} // end public function get_values
public function match($rule, $screen, $field_group) {
$match = false;
if (!isset($screen['post_id'])) {
return $match;
}
if (!isset($screen['page_parent'])) {
$ancestors = count(get_ancestors($screen['post_id'], $screen['post_type'], 'post_type'));
} else {
$ancestors = count(get_ancestors($screen['page_parent'], $screen['post_type'], 'post_type'))+1;
}
switch ($rule['operator']) {
case '!=':
$match = ($rule['value'] != $ancestors);
break;
case '<':
$match = ($rule['value'] < $ancestors);
break;
case '<=':
$match = ($rule['value'] <= $ancestors);
break;
case '==':
$match = ($rule['value'] == $ancestors);
break;
case '>=':
$match = ($rule['value'] >= $ancestors);
break;
case '>':
$match = ($rule['value'] > $ancestors);
break;
default:
// do nothing
break;
} // end switch
return $match;
} // end public function match
} // end class location_page_level_jh
acf_register_location_type('location_page_level_jh');
} // end if class exists