1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Images as radio buttons field class. |
7
|
|
|
*/ |
8
|
|
|
class Radio_Image_Field extends Predefined_Options_Field { |
9
|
|
|
/** |
10
|
|
|
* Check if there are callbacks and populate the options |
11
|
|
|
* Overrided to not modify the field options |
12
|
|
|
*/ |
13
|
|
|
protected function load_options() { |
14
|
|
|
if ( empty( $this->options ) ) { |
15
|
|
|
return false; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$options = $this->options; |
19
|
|
|
|
20
|
|
|
if ( is_callable( $options ) ) { |
21
|
|
|
$options = call_user_func( $options ); |
22
|
|
|
if ( ! is_array( $options ) ) { |
23
|
|
|
$options = array(); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->options = $options; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
32
|
|
|
* Will also work with a callable that returns an array. |
33
|
|
|
* Overrided to prevent losing other data in an option array |
34
|
|
|
* |
35
|
|
|
* @param array|callable $options |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function parse_options( $options ) { |
39
|
|
|
$parsed = array(); |
40
|
|
|
|
41
|
|
|
if ( is_callable( $options ) ) { |
42
|
|
|
$options = call_user_func( $options ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ( $options as $item ) { |
46
|
|
|
$tmp = $item['name']; |
47
|
|
|
$item['name'] = $item['value']; |
48
|
|
|
$item['value'] = $tmp; |
49
|
|
|
$parsed[] = $item; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $parsed; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
57
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
58
|
|
|
* |
59
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function to_json( $load ) { |
|
|
|
|
63
|
|
|
$field_data = parent::to_json( $load ); |
64
|
|
|
$this->load_options(); |
65
|
|
|
|
66
|
|
|
$field_data = array_merge( $field_data, array( |
67
|
|
|
'options' => $this->parse_options( $this->options ), |
68
|
|
|
) ); |
69
|
|
|
|
70
|
|
|
return $field_data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The main Underscore template of this field. |
75
|
|
|
*/ |
76
|
|
|
public function template() { |
77
|
|
|
?> |
78
|
|
|
<# if (_.isEmpty(options)) { #> |
79
|
|
|
<em><?php _e( 'no options', 'carbon-fields' ); ?></em> |
80
|
|
|
<# } else { #> |
81
|
|
|
<ul class="carbon-radio-list carbon-radio-image-list"> |
82
|
|
|
<# _.each(options, function(option) { #> |
83
|
|
|
<li> |
84
|
|
|
<label> |
85
|
|
|
<img class="carbon-radio-image" src="{{{ option.imgUrl }}}" title="{{{ option.name }}}"> |
86
|
|
|
<input type="radio" name="{{{ name }}}" value="{{ option.value }}" {{{ option.value == value ? 'checked="checked"' : '' }}} /> |
87
|
|
|
</label> |
88
|
|
|
</li> |
89
|
|
|
<# }) #> |
90
|
|
|
</ul> |
91
|
|
|
<# } #> |
92
|
|
|
<?php |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.