Skip to content

Commit

Permalink
misc fixes (#457)
Browse files Browse the repository at this point in the history
* misc fixes

* Update src/Validation/SettingsValidation.php

Co-authored-by: Mario Borna Mjertan <bor.mbm@gmail.com>

---------

Co-authored-by: Mario Borna Mjertan <bor.mbm@gmail.com>
  • Loading branch information
iruzevic and mbmjertan authored Nov 13, 2024
1 parent d335649 commit c52d39e
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 89 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/bundle-artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [ 'latest' ]
node: [ '23.1' ]
php: [ '8.2' ]
steps:
- name: Bundle artifact
Expand All @@ -23,3 +23,4 @@ jobs:
PROJECT_PATH: '.'
REPO_NAME: 'eightshift-forms'
PHP_VERSION: ${{ matrix.php }}
NODE_VERSION: ${{ matrix.node }}
3 changes: 2 additions & 1 deletion .github/workflows/bundle-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [ 'latest' ]
node: [ '23.1' ]
php: [ '8.2' ]
steps:
- name: Bundle release
Expand All @@ -25,3 +25,4 @@ jobs:
REPO_NAME: 'eightshift-forms'
WORDPRESS_GH_ACTIONS: ${{ secrets.WORDPRESS_GH_ACTIONS }}
PHP_VERSION: ${{ matrix.php }}
NODE_VERSION: ${{ matrix.node }}
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [ 'latest' ]
php: [ '8.2', '8.3' ]
node: [ '23.1' ]
steps:
- name: Lint assets
uses: infinum/eightshift-deploy-actions-public/.github/actions/lint-assets-forms@main
uses: infinum/eightshift-deploy-actions-public/.github/actions/lint-assets@main
with:
NODE_VERSION: ${{ matrix.node }}
PHP_VERSION: ${{ matrix.php }}
PROJECT_PATH: '.'

phpstan:
Expand All @@ -31,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '8.2', '8.3' ]
php: [ '8.3' ]
steps:
- name: PHP Static code analysis
uses: infinum/eightshift-deploy-actions-public/.github/actions/lint-php-static@main
Expand All @@ -45,7 +43,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '8.2', '8.3' ]
php: [ '8.3' ]
steps:
- name: PHP check coding standards
uses: infinum/eightshift-deploy-actions-public/.github/actions/lint-php@main
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a
### Added

- New Integration for `CorvusPay`.
- New helper to get forms usage locations - `getFormUsageLocation`.

### Fixed

- Broken conditional tags for none form fields.

## [5.1.10]

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"require": {
"php": ">=8.2",
"erusev/parsedown": "^1.7.4",
"infinum/eightshift-forms-utils": "^3.0.4"
"infinum/eightshift-forms-utils": "^3.0.5"
},
"autoload": {
"psr-4": {
Expand All @@ -67,11 +67,11 @@
},
"scripts": {
"test:types": "@php ./vendor/bin/phpstan analyze",
"test:standards": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --runtime-set testVersion 8.2-",
"test:standards": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --runtime-set testVersion 8.3-",
"test:unit": "@php ./vendor/bin/pest --group=unit",
"test:unit-report": "@php ./vendor/bin/pest --group=unit --log-junit tests/coverage/report.xml",
"test:coverage": "@php ./vendor/bin/pest --group=unit --coverage",
"standards:fix": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --runtime-set testVersion 8.2-",
"standards:fix": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --runtime-set testVersion 8.3-",
"test": [
"@test:standards",
"@test:types",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Blocks/custom/field/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Helpers::props('field', $attributes),
[
'selectorClass' => 'field',
'fieldFieldIsNoneFormBlock' => true,
'fieldFieldConditionalTags' => Helpers::render(
'conditional-tags',
Helpers::props('conditionalTags', $attributes)
Expand Down
4 changes: 3 additions & 1 deletion src/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ public function updateFormsBlockOutput(array $blocks, array $attributes, array $
*/
public function checkFormsBlockShouldRender(bool $output, array $attributes, array $manifest): bool
{
$loggedInOnlyForm = UtilsSettingsHelper::isOptionCheckboxChecked(SettingsValidation::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, SettingsValidation::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY);
$formId = Helpers::checkAttr('formsFormPostId', $attributes, $manifest);

$loggedInOnlyForm = UtilsSettingsHelper::isSettingCheckboxChecked(SettingsValidation::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, SettingsValidation::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, $formId);

if ($loggedInOnlyForm && !\is_user_logged_in()) {
return false;
Expand Down
18 changes: 15 additions & 3 deletions src/Helpers/publicHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ function esFormRenderForm(string $formId, array $attributes = []): string
/**
* Get entry by ID.
*
* @param string $id Entry Id.
* @param string $entryId Entry Id.
*
* @return array<string, mixed>
*/
function esFormGetEntry(string $id): array
function esFormGetEntry(string $entryId): array
{
return EntriesHelper::getEntry($id);
return EntriesHelper::getEntry($entryId);
}

/**
Expand All @@ -143,3 +143,15 @@ function getFormsGetCountryList(): array
{
return (new Countries())->getCountriesDataSet();
}

/**
* Get form usage location.
*
* @param string $formId Form ID.
*
* @return array<int, mixed>
*/
function getFormUsageLocation(string $formId): array
{
return UtilsGeneralHelper::getBlockLocations($formId);
}
1 change: 1 addition & 0 deletions src/Labels/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ private function getValidationLabels(): array
'validationSecurity' => \__('You have made too many requests in a short time. Please slow down and try again.', 'eightshift-forms'),
'validationMissingMandatoryParams' => \__('This form is malformed or not configured correctly. Please get in touch with the website administrator to resolve this issue.', 'eightshift-forms'),
'validationSubmitOnce' => \__('This form can be submitted only once.', 'eightshift-forms'),
'validationSubmitLoggedIn' => \__('This form can be submitted only by logged in users.', 'eightshift-forms'),
];
}

Expand Down
17 changes: 14 additions & 3 deletions src/Rest/Routes/AbstractFormSubmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,25 @@ public function routeCallback(WP_REST_Request $request)
}
}

// Validate form submit once.
if ($this->validator->isValicationSubmitOnce($formDetails[UtilsConfig::FD_FORM_ID] ?? '')) {
// Validate submit only logged in.
if ($this->validator->validateSubmitOnlyLoggedIn($formDetails[UtilsConfig::FD_FORM_ID] ?? '')) {
// Validate submit only logged in.
return \rest_ensure_response(
UtilsApiHelper::getApiErrorPublicOutput(
$this->labels->getLabel('validationSubmitOnce', $formDetails[UtilsConfig::FD_FORM_ID] ?? ''),
$this->labels->getLabel('validationSubmitLoggedIn', $formDetails[UtilsConfig::FD_FORM_ID] ?? ''),
)
);
} else {
// Validate submit only once.
if ($this->validator->validateSubmitOnlyOnce($formDetails[UtilsConfig::FD_FORM_ID] ?? '')) {
return \rest_ensure_response(
UtilsApiHelper::getApiErrorPublicOutput(
$this->labels->getLabel('validationSubmitOnce', $formDetails[UtilsConfig::FD_FORM_ID] ?? ''),
)
);
}
}

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private function prepareParams(array $mapParams, array $params, string $formId):
}

$output['store_id'] = UtilsSettingsHelper::getSettingValue(SettingsCorvus::SETTINGS_CORVUS_STORE_ID, $formId);
$output['version'] = '1.4';
$output['version'] = '1.4'; // Corvus API version.
$output['language'] = UtilsSettingsHelper::getSettingValue(SettingsCorvus::SETTINGS_CORVUS_LANG_KEY, $formId);
$output['require_complete'] = (string) UtilsSettingsHelper::isSettingCheckboxChecked(SettingsCorvus::SETTINGS_CORVUS_REQ_COMPLETE_KEY, SettingsCorvus::SETTINGS_CORVUS_REQ_COMPLETE_KEY, $formId);
$output['currency'] = UtilsSettingsHelper::getSettingValue(SettingsCorvus::SETTINGS_CORVUS_CURRENCY_KEY, $formId);
Expand Down
113 changes: 63 additions & 50 deletions src/Validation/SettingsValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class SettingsValidation implements UtilsSettingGlobalInterface, UtilsSettingInt
*/
public const SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY = 'validation-use-submit-once';

/**
* Validation use submit only logged in key.
*/
public const SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY = 'validation-use-submit-only-logged-in';

/**
* Validation use only logged in key.
*/
Expand Down Expand Up @@ -96,14 +101,6 @@ public function register(): void
*/
public function getSettingsData(string $formId): array
{
$output = [
[
'component' => 'intro',
// translators: %s will be replaced with the settings link.
'introSubtitle' => \sprintf(\__('In these settings, you can change all validation success messages. Global validation options can be configured in the <a href="%s" target="_blank" rel="noopener noreferrer">global settings.</a>', 'eightshift-forms'), UtilsGeneralHelper::getSettingsGlobalPageUrl(self::SETTINGS_TYPE_KEY)),
],
];

$formType = UtilsGeneralHelper::getFormTypeById($formId);

if (!$formType) {
Expand All @@ -112,6 +109,8 @@ public function getSettingsData(string $formId): array

$key = "{$formType}Success";

$loggedInSubmit = UtilsSettingsHelper::isSettingCheckboxChecked(self::SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY, self::SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY, $formId);

return [
UtilsSettingsOutputHelper::getIntro(self::SETTINGS_TYPE_KEY),
[
Expand All @@ -130,6 +129,62 @@ public function getSettingsData(string $formId): array
],
],
],
[
'component' => 'tab',
'tabLabel' => \__('Users', 'eightshift-forms'),
'tabContent' => [
[
'component' => 'checkboxes',
'checkboxesFieldLabel' => '',
'checkboxesName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY),
'checkboxesContent' => [
[
'component' => 'checkbox',
'checkboxLabel' => \__('Show only to logged in users', 'eightshift-forms'),
'checkboxHelp' => \__('The form will be accessible only to users who are logged in.', 'eightshift-forms'),
'checkboxIsChecked' => UtilsSettingsHelper::isSettingCheckboxChecked(self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, $formId),
'checkboxValue' => self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
]
]
],
[
'component' => 'checkboxes',
'checkboxesFieldLabel' => '',
'checkboxesName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY),
'checkboxesContent' => [
[
'component' => 'checkbox',
'checkboxLabel' => \__('Allow only logged in users to submit', 'eightshift-forms'),
'checkboxHelp' => \__('If enabled, only logged in users can submit the form.', 'eightshift-forms'),
'checkboxIsChecked' => UtilsSettingsHelper::isSettingCheckboxChecked(self::SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY, self::SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY, $formId),
'checkboxValue' => self::SETTINGS_VALIDATION_USE_SUBMIT_ONLY_LOGGED_IN_KEY,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
]
]
],
...($loggedInSubmit ? [
[
'component' => 'checkboxes',
'checkboxesFieldLabel' => '',
'checkboxesName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY),
'checkboxesContent' => [
[
'component' => 'checkbox',
'checkboxLabel' => \__('Use single submit per user', 'eightshift-forms'),
'checkboxHelp' => \__('If enabled, each logged in user can submit the form only once.', 'eightshift-forms'),
'checkboxIsChecked' => UtilsSettingsHelper::isSettingCheckboxChecked(self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY, self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY, $formId),
'checkboxValue' => self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
],
],
],
] : []),
],
],
],
],
];
Expand Down Expand Up @@ -170,8 +225,6 @@ public function getSettingsGlobalData(): array
];
}

$loggedInForm = UtilsSettingsHelper::isOptionCheckboxChecked(self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY);

return [
UtilsSettingsOutputHelper::getIntro(self::SETTINGS_TYPE_KEY),
[
Expand Down Expand Up @@ -220,46 +273,6 @@ public function getSettingsGlobalData(): array
],
],
],
[
'component' => 'tab',
'tabLabel' => \__('Users', 'eightshift-forms'),
'tabContent' => [
[
'component' => 'checkboxes',
'checkboxesFieldLabel' => '',
'checkboxesName' => UtilsSettingsHelper::getOptionName(self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY),
'checkboxesContent' => [
[
'component' => 'checkbox',
'checkboxLabel' => \__('Show only to logged in users', 'eightshift-forms'),
'checkboxHelp' => \__('The form will be accessible only to users who are logged in.', 'eightshift-forms'),
'checkboxIsChecked' => UtilsSettingsHelper::isOptionCheckboxChecked(self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY, self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY),
'checkboxValue' => self::SETTINGS_VALIDATION_USE_ONLY_LOGGED_IN_KEY,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
]
]
],
...($loggedInForm ? [
[
'component' => 'checkboxes',
'checkboxesFieldLabel' => '',
'checkboxesName' => UtilsSettingsHelper::getOptionName(self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY),
'checkboxesContent' => [
[
'component' => 'checkbox',
'checkboxLabel' => \__('Use single submit per user', 'eightshift-forms'),
'checkboxHelp' => \__('If enabled, each user can submit the form only once.', 'eightshift-forms'),
'checkboxIsChecked' => UtilsSettingsHelper::isOptionCheckboxChecked(self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY, self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY),
'checkboxValue' => self::SETTINGS_VALIDATION_USE_SUBMIT_ONCE_KEY,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
]
]
],
] : []),
],
],
[
'component' => 'tab',
'tabLabel' => \__('Messages', 'eightshift-forms'),
Expand Down
Loading

0 comments on commit c52d39e

Please sign in to comment.