Completed
Pull Request — master (#193)
by
unknown
02:22
created

Container_Validator::get_term_level()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php 
2
namespace Carbon_Fields\Container;
3
4
/**
5
* Wrapper class for container validation.
6
*/
7
class Container_Validator {
8
	
9
	/**
10
	 * Proxy method that calls appropriate method for validation
11
	 * after some preliminary checks
12
	 * 
13
	 * @param  object  $container
14
	 * @param  string  $type
15
	 * @param  string  $id  
16
	 * @return boolean
17
	 */
18
	public function is_valid_container( $container, $type, $id, $check_rest_visibilty = true ) {
19
		if ( ! $container->get_rest_visibility() && $check_rest_visibilty ) {
20
			return false;
21
		}
22
23
		if ( $container->type !== $type ) {
24
			return false;
25
		}
26
27
		$type_to_lower = strtolower( $type );
28
29
		return call_user_func( array( $this, "is_valid_{$type_to_lower}_container" ), $container, $id );
30
	}
31
32
	/**
33
	 * Validates Theme Options container
34
	 * 
35
	 * @param  object  $container
36
	 * @param  string  $id       
37
	 * @return boolean
38
	 */
39
	public function is_valid_theme_options_container( $container, $id = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
		return true;
41
	}
42
43
	/**
44
	 * Validates Post Meta container
45
	 * 
46
	 * @param  object  $container
47
	 * @param  string  $id       
48
	 * @return boolean           
49
	 */
50
	public function is_valid_post_meta_container( $container, $id ) {
51
		return $container->is_valid_save_conditions( $id );
52
	}
53
54
	/**
55
	 * Validates User Meta container
56
	 * 
57
	 * @param  object  $container 
58
	 * @param  string  $id        
59
	 * @return boolean            
60
	 */
61
	public function is_valid_user_meta_container( $container, $id ) {
62
		return $this->is_valid_post_meta_container( $container, $id );
63
	}
64
65
	/**
66
	 * Validates Term Meta container
67
	 * 
68
	 * @param  object  $container
69
	 * @param  string  $id
70
	 * @return boolean 
71
	 */
72
	public function is_valid_term_meta_container( $container, $id ) {
73
		$term = get_term( $id );
74
75
		if ( empty( $term ) || is_wp_error( $term ) ) { 
76
			return false;
77
		}
78
		
79
		$taxonomy = $term->taxonomy;
80
81
		if ( ! in_array( $taxonomy, $container->settings['taxonomy'] ) ) {
82
			return false;
83
		}
84
85
		if ( $container->settings['show_on_level'] ) { 
86
			
87
			$show_level = $container->settings['show_on_level'];
88
			$term_level = self::get_term_level( $term );
89
90
			if ( $term_level !== $show_level ) {
91
				return false;
92
			}
93
		}
94
95
		return true;
96
	}
97
98
	/**
99
	 * Validates Comment Meta container
100
	 * 
101
	 * @param  object  $container
102
	 * @param  string  $id
103
	 * @return boolean 
104
	 */
105
	public function is_valid_comment_meta_container( $container, $id = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
		return true;
107
	}
108
109
	/**
110
	 * Returns the number of parents of a taxonomy term
111
	 * 
112
	 * @param  object $term 
113
	 * @return int
114
	 */
115
	public static function get_term_level( $term ) {
116
		$ancestors = array();	
117
		while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
118
			$ancestors[] = intval( $term->parent );
119
			$term        = get_term( $term->parent );
120
		}
121
122
		return count( $ancestors ) + 1;
123
	}
124
}