1 | <?php |
||
24 | trait CheckNeedTrait { |
||
25 | |||
26 | /** |
||
27 | * Check whether an element is needed. |
||
28 | * |
||
29 | * @since 0.2.0 |
||
30 | * |
||
31 | * @param mixed $context Data about the context in which the call is made. |
||
32 | * @return boolean Whether the element is needed or not. |
||
33 | */ |
||
34 | protected function is_needed( $context = null ) { |
||
35 | |||
36 | $is_needed = $this->hasConfigKey( 'is_needed' ) |
||
37 | ? $this->getConfigKey( 'is_needed' ) |
||
38 | : true; |
||
39 | |||
40 | if ( is_callable( $is_needed ) ) { |
||
41 | return $is_needed( $context ); |
||
42 | } |
||
43 | |||
44 | return (bool) $is_needed; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Check whether the Config has a specific key. |
||
49 | * |
||
50 | * This needs to be implemented in a class that wants to use CheckNeedTrait. |
||
51 | * |
||
52 | * @since 0.2.10 |
||
53 | * |
||
54 | * @param string|array $_ List of keys. |
||
55 | * @return bool Whether the key is known. |
||
56 | */ |
||
57 | abstract protected function hasConfigKey( $_ ); |
||
58 | |||
59 | /** |
||
60 | * Get the Config value for a specific key. |
||
61 | * |
||
62 | * This needs to be implemented in a class that wants to use CheckNeedTrait. |
||
63 | * |
||
64 | * @since 0.2.10 |
||
65 | * |
||
66 | * @param string|array $_ List of keys. |
||
67 | * @return mixed Value of the key. |
||
68 | */ |
||
69 | abstract protected function getConfigKey( $_ ); |
||
70 | } |
||
71 |