Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Differentiate between checkbox and toggles #1575

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Helper/Helper_Options_Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function get_registered_fields() {
'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-forms-pdf-extended' ),
'schema' => [
'type' => 'boolean',
'format' => 'yes_no',
'format' => 'yes-no',
],
],

Expand Down Expand Up @@ -636,7 +636,7 @@ public function get_registered_fields() {
'description' => __( 'Force the PDF to be temporarily saved to the filesystem during form submission (deprecated). Use the gfpdf_post_save_pdf hook instead.', 'gravity-forms-pdf-extended' ),
'default' => false,
'required' => false,
'format' => 'yes_no',
'format' => 'yes-no',
'arg_options' => [
'sanitize_callback' => 'rest_sanitize_request_arg',
],
Expand Down
15 changes: 13 additions & 2 deletions src/Rest/Rest_Form_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,15 @@
}

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

/* Handle checkbox values */
if ( $this->has_property_type( 'boolean', $property['type'] ) && ( $property['format'] ?? '' ) === 'checkbox' ) {
$value = $value === true ? '1' : '';

Check warning on line 846 in src/Rest/Rest_Form_Settings.php

View check run for this annotation

Codecov / codecov/patch

src/Rest/Rest_Form_Settings.php#L846

Added line #L846 was not covered by tests
}

$prepared_pdf[ $id ] = $value;
}

Expand Down Expand Up @@ -1092,9 +1097,15 @@
break;

case 'checkbox':
$schema[ $id ]['type'] = 'boolean';
$schema[ $id ]['format'] = 'checkbox';
$schema[ $id ]['default'] = in_array( $default, [ 'Yes', '1', 1, 'true', true ], true );
$schema[ $id ]['arg_options']['sanitize_callback'] = 'rest_sanitize_request_arg';
break;

Check failure on line 1104 in src/Rest/Rest_Form_Settings.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Terminating statement must be indented to the same level as the CASE body

Check warning on line 1104 in src/Rest/Rest_Form_Settings.php

View check run for this annotation

Codecov / codecov/patch

src/Rest/Rest_Form_Settings.php#L1100-L1104

Added lines #L1100 - L1104 were not covered by tests

case 'toggle':
$schema[ $id ]['type'] = 'boolean';
$schema[ $id ]['format'] = 'yes_no';
$schema[ $id ]['format'] = 'yes-no';
$schema[ $id ]['default'] = in_array( $default, [ 'Yes', '1', 'true', true ], true );
$schema[ $id ]['arg_options']['sanitize_callback'] = 'rest_sanitize_request_arg';

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/unit-tests/Rest/Test_Rest_Form_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public function test_get_item_schema() {
$this->assertContains( 'landscape', $args['orientation']['enum'] );

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

$this->assertContains( 'Standard', $args['format']['enum'] );
$this->assertContains( 'PDFX1A', $args['format']['enum'] );
Expand Down
Loading