Skip to content

Commit 76afb40

Browse files
jakejackson1claude
andcommitted
REST API: differentiate between checkbox and toggle boolean settings
Toggle settings serialise to 'Yes'/'No' while checkbox settings serialise to '1'/'' to match how each field type is stored. Rename the unreleased schema format string 'yes_no' -> 'yes-no' for consistency with the existing 'hex-color' format, and add a dedicated 'checkbox' format with its own prepare/response handling. Adds PHPUnit coverage for both formats. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d3af32f commit 76afb40

3 files changed

Lines changed: 106 additions & 6 deletions

File tree

src/Helper/Helper_Options_Fields.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function get_registered_fields() {
347347
'desc2' => esc_html__( 'Add rules to dynamically enable or disable the PDF. When disabled, PDFs do not show up in the admin area, cannot be viewed, and will not be attached to notifications.', 'gravity-pdf' ),
348348
'schema' => [
349349
'type' => 'boolean',
350-
'format' => 'yes_no',
350+
'format' => 'yes-no',
351351
],
352352
],
353353

@@ -645,7 +645,7 @@ public function get_registered_fields() {
645645
'description' => __( 'Force the PDF to be temporarily saved to the filesystem during form submission (deprecated). Use the gfpdf_post_save_pdf hook instead.', 'gravity-pdf' ),
646646
'default' => false,
647647
'required' => false,
648-
'format' => 'yes_no',
648+
'format' => 'yes-no',
649649
'arg_options' => [
650650
'sanitize_callback' => 'rest_sanitize_request_arg',
651651
],

src/Rest/Rest_Form_Settings.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,15 @@ protected function prepare_item_for_database( $request ) {
837837
}
838838

839839
/* Handle Toggle values */
840-
if ( $this->has_property_type( 'boolean', $property['type'] ) && ( $property['format'] ?? '' ) === 'yes_no' ) {
840+
if ( $this->has_property_type( 'boolean', $property['type'] ) && ( $property['format'] ?? '' ) === 'yes-no' ) {
841841
$value = $value === true ? 'Yes' : 'No';
842842
}
843843

844+
/* Handle checkbox values */
845+
if ( $this->has_property_type( 'boolean', $property['type'] ) && ( $property['format'] ?? '' ) === 'checkbox' ) {
846+
$value = $value === true ? '1' : '';
847+
}
848+
844849
$prepared_pdf[ $id ] = $value;
845850
}
846851

@@ -1092,12 +1097,17 @@ protected function get_section_schema( $settings, $group ) {
10921097
break;
10931098

10941099
case 'checkbox':
1100+
$schema[ $id ]['type'] = 'boolean';
1101+
$schema[ $id ]['format'] = 'checkbox';
1102+
$schema[ $id ]['default'] = in_array( $default, [ 'Yes', '1', 1, 'true', true ], true );
1103+
$schema[ $id ]['arg_options']['sanitize_callback'] = 'rest_sanitize_request_arg';
1104+
break;
1105+
10951106
case 'toggle':
10961107
$schema[ $id ]['type'] = 'boolean';
1097-
$schema[ $id ]['format'] = 'yes_no';
1108+
$schema[ $id ]['format'] = 'yes-no';
10981109
$schema[ $id ]['default'] = in_array( $default, [ 'Yes', '1', 'true', true ], true );
10991110
$schema[ $id ]['arg_options']['sanitize_callback'] = 'rest_sanitize_request_arg';
1100-
11011111
break;
11021112
}
11031113

tests/phpunit/integration/Rest/Test_Rest_Form_Settings.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ public function test_get_item_schema() {
822822
$this->assertContains( 'landscape', $args['orientation']['enum'] );
823823

824824
$this->assertSame( 'boolean', $args['rtl']['type'] );
825-
$this->assertSame( 'yes_no', $args['rtl']['format'] );
825+
$this->assertSame( 'yes-no', $args['rtl']['format'] );
826826

827827
$this->assertContains( 'Standard', $args['format']['enum'] );
828828
$this->assertContains( 'PDFX1A', $args['format']['enum'] );
@@ -1213,4 +1213,94 @@ public function test_get_schema_for_template() {
12131213
$this->assertArrayNotHasKey( 'notification', $schema['properties'] );
12141214
$this->assertArrayNotHasKey( 'conditionalLogic', $schema['properties'] );
12151215
}
1216+
1217+
/**
1218+
* Register an additional checkbox settings field so the REST schema generates a checkbox-format boolean.
1219+
*
1220+
* @param array $fields The existing advanced settings fields.
1221+
*
1222+
* @return array
1223+
*/
1224+
public function add_checkbox_field( $fields ) {
1225+
$fields['my_checkbox'] = [
1226+
'id' => 'my_checkbox',
1227+
'name' => 'My Checkbox',
1228+
'type' => 'checkbox',
1229+
'std' => false,
1230+
];
1231+
1232+
return $fields;
1233+
}
1234+
1235+
/**
1236+
* Toggle and checkbox boolean fields must expose distinct schema formats so the API can serialise them
1237+
* back to their respective storage values ('Yes'/'No' vs '1'/'').
1238+
*/
1239+
public function test_checkbox_and_toggle_schema_format() {
1240+
add_filter( 'gfpdf_form_settings_advanced', [ $this, 'add_checkbox_field' ] );
1241+
1242+
$schema = $this->api->get_item_schema();
1243+
$args = $schema['properties'];
1244+
1245+
/* Toggle fields are flagged with the 'yes-no' format */
1246+
$this->assertSame( 'boolean', $args['rtl']['type'] );
1247+
$this->assertSame( 'yes-no', $args['rtl']['format'] );
1248+
1249+
/* Checkbox fields are flagged with the 'checkbox' format */
1250+
$this->assertSame( 'boolean', $args['my_checkbox']['type'] );
1251+
$this->assertSame( 'checkbox', $args['my_checkbox']['format'] );
1252+
$this->assertFalse( $args['my_checkbox']['default'] );
1253+
}
1254+
1255+
/**
1256+
* A checkbox field stores '1' when enabled and '' when disabled, whereas a toggle field stores 'Yes'/'No'.
1257+
*/
1258+
public function test_checkbox_value_round_trip() {
1259+
add_filter( 'gfpdf_form_settings_advanced', [ $this, 'add_checkbox_field' ] );
1260+
1261+
/* Re-register the routes so the dispatched schema picks up the new checkbox field */
1262+
$this->api->register_routes();
1263+
1264+
wp_set_current_user( self::$admin_id );
1265+
1266+
/* Enabled checkbox is stored as '1', toggle as 'Yes' */
1267+
$request = new WP_REST_Request( 'POST', '/gravity-pdf/v1/form/' . $this->form_id );
1268+
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
1269+
$request->set_body_params( [
1270+
'name' => 'Label',
1271+
'template' => 'rubix',
1272+
'rtl' => true,
1273+
'my_checkbox' => true,
1274+
] );
1275+
1276+
$response = rest_get_server()->dispatch( $request );
1277+
$data = $response->get_data();
1278+
1279+
$this->assertSame( 201, $response->get_status() );
1280+
$this->assertTrue( $data['my_checkbox'] );
1281+
1282+
$pdf = \GPDFAPI::get_pdf( $this->form_id, $data['id'] );
1283+
$this->assertSame( '1', $pdf['my_checkbox'] );
1284+
$this->assertSame( 'Yes', $pdf['rtl'] );
1285+
1286+
/* Disabled checkbox is stored as '', toggle as 'No' */
1287+
$request = new WP_REST_Request( 'POST', '/gravity-pdf/v1/form/' . $this->form_id );
1288+
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
1289+
$request->set_body_params( [
1290+
'name' => 'Label',
1291+
'template' => 'rubix',
1292+
'rtl' => false,
1293+
'my_checkbox' => false,
1294+
] );
1295+
1296+
$response = rest_get_server()->dispatch( $request );
1297+
$data = $response->get_data();
1298+
1299+
$this->assertSame( 201, $response->get_status() );
1300+
$this->assertFalse( $data['my_checkbox'] );
1301+
1302+
$pdf = \GPDFAPI::get_pdf( $this->form_id, $data['id'] );
1303+
$this->assertSame( '', $pdf['my_checkbox'] );
1304+
$this->assertSame( 'No', $pdf['rtl'] );
1305+
}
12161306
}

0 commit comments

Comments
 (0)