Skip to content

Commit 9ad5012

Browse files
committed
Merge pull request alleyinteractive#184 from alleyinteractive/checkboxes-default-checked
Override options selected function to allow all boxes to be checked by default
2 parents 842ed0f + 3ce7b3e commit 9ad5012

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

php/class-fieldmanager-checkboxes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ public function form_element( $value = array() ) {
2828
);
2929
}
3030

31+
/**
32+
* Override function to allow all boxes to be checked by default.
33+
* @param string $current_option this option
34+
* @param array $options all valid options
35+
* @param string $attribute
36+
* @return string $attribute on match, empty on failure.
37+
*/
38+
public function option_selected( $current_option, $options, $attribute ) {
39+
if ( ( ( $options != null && !empty( $options ) ) && in_array( $current_option, $options ) ) || ( 'checked' == $this->default_value && in_array( $this->default_value, $options ) ) ) {
40+
return $attribute;
41+
} else {
42+
return '';
43+
}
44+
}
45+
3146
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
/**
4+
* Tests the Fieldmanager Checkboxes Field
5+
*
6+
* @group field
7+
* @group checkboxes
8+
*/
9+
class Test_Fieldmanager_Checkboxes_Field extends WP_UnitTestCase {
10+
public $post_id;
11+
public $post;
12+
public $custom_datasource;
13+
14+
public function setUp() {
15+
parent::setUp();
16+
Fieldmanager_Field::$debug = true;
17+
18+
$this->post_id = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_date' => '2009-07-01 00:00:00' ) );
19+
$this->post = get_post( $this->post_id );
20+
21+
$this->months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
22+
23+
$this->custom_datasource = new Fieldmanager_Datasource( array( 'options' => $this->months ) );
24+
}
25+
26+
/**
27+
* Helper which returns the post meta box HTML for a given field;
28+
*
29+
* @param object $field Some Fieldmanager_Field object.
30+
* @param array $test_data Data to save (and use when rendering)
31+
* @return string Rendered HTML
32+
*/
33+
private function _get_html_for( $field, $test_data = null ) {
34+
ob_start();
35+
$context = $field->add_meta_box( 'test meta box', $this->post );
36+
if ( $test_data ) {
37+
$context->save_to_post_meta( $this->post_id, $test_data );
38+
}
39+
$context->render_meta_box( $this->post );
40+
return ob_get_clean();
41+
}
42+
43+
private function _get_input_field_regex( $name, $value, $checked = false ) {
44+
if ( is_array( $value ) ) {
45+
$v = array_keys( $value );
46+
$v = $v[0];
47+
$label = $value[ $v ];
48+
$value = $v;
49+
} else {
50+
$label = $value;
51+
}
52+
53+
return sprintf( '#<input\s*class="fm-element"\s*type="checkbox"\s*value="%2$s"\s*name="%1$s\[\]"\s*id="fm-%1$s-\d+-%2$s"'
54+
. ( $checked ? '\s*checked' : '' ) . '\s*/>'
55+
. '\s*<label\s*for="fm-%1$s-\d+-%2$s"\s*class="fm-option-label">\s*%3$s\s*</label>#si',
56+
$name, $value, $label );
57+
}
58+
59+
public function test_basic_render() {
60+
$fm = new Fieldmanager_Checkboxes( array(
61+
'name' => 'base_field',
62+
'options' => array( 'one', 'two', 'three' ),
63+
) );
64+
65+
$html = $this->_get_html_for( $fm );
66+
67+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html );
68+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two' ), $html );
69+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html );
70+
}
71+
72+
public function test_basic_save() {
73+
$fm = new Fieldmanager_Checkboxes( array(
74+
'name' => 'base_field',
75+
'options' => array( 'one', 'two', 'three' ),
76+
) );
77+
78+
$html = $this->_get_html_for( $fm, array( 'two' ) );
79+
80+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html );
81+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two', true ), $html );
82+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html );
83+
}
84+
85+
public function test_associative_render() {
86+
$fm = new Fieldmanager_Checkboxes( array(
87+
'name' => 'base_field',
88+
'options' => array( 1 => 'one', 2 => 'two', 3 => 'three' ),
89+
) );
90+
91+
$html = $this->_get_html_for( $fm );
92+
93+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 1 => 'one' ) ), $html );
94+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 2 => 'two' ) ), $html );
95+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 3 => 'three' ) ), $html );
96+
97+
$html = $this->_get_html_for( $fm, array( 2 ) );
98+
99+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 1 => 'one' ) ), $html );
100+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 2 => 'two' ), true ), $html );
101+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 3 => 'three' ) ), $html );
102+
}
103+
104+
public function test_default_value() {
105+
$fm = new Fieldmanager_Checkboxes( array(
106+
'name' => 'base_field',
107+
'options' => array( 'one', 'two', 'three' ),
108+
'default_value' => 'two',
109+
) );
110+
111+
$html = $this->_get_html_for( $fm );
112+
113+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html );
114+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two', true ), $html );
115+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html );
116+
}
117+
118+
public function test_datasource() {
119+
$fm = new Fieldmanager_Checkboxes( array(
120+
'name' => 'base_field',
121+
'datasource' => $this->custom_datasource,
122+
) );
123+
124+
$html = $this->_get_html_for( $fm );
125+
126+
foreach ( $this->months as $month ) {
127+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month ), $html );
128+
}
129+
}
130+
131+
public function test_datasource_default_value() {
132+
$fm = new Fieldmanager_Checkboxes( array(
133+
'name' => 'base_field',
134+
'default_value' => 'February',
135+
'datasource' => $this->custom_datasource,
136+
) );
137+
138+
$html = $this->_get_html_for( $fm );
139+
140+
foreach ( $this->months as $month ) {
141+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, ( 'February' === $month ) ), $html );
142+
}
143+
}
144+
145+
public function test_datasource_save() {
146+
$fm = new Fieldmanager_Checkboxes( array(
147+
'name' => 'base_field',
148+
'datasource' => $this->custom_datasource,
149+
) );
150+
151+
$html = $this->_get_html_for( $fm, array( 'February' ) );
152+
153+
foreach ( $this->months as $month ) {
154+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, ( 'February' === $month ) ), $html );
155+
}
156+
}
157+
158+
public function test_datasource_default_value_all() {
159+
$fm = new Fieldmanager_Checkboxes( array(
160+
'name' => 'base_field',
161+
'default_value' => 'checked',
162+
'datasource' => $this->custom_datasource,
163+
) );
164+
165+
$html = $this->_get_html_for( $fm );
166+
167+
foreach ( $this->months as $month ) {
168+
$this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, true ), $html );
169+
}
170+
}
171+
172+
}

0 commit comments

Comments
 (0)