diff --git a/src/Helpers/ApiHelper.php b/src/Helpers/ApiHelper.php index 47fb03b..b3d4490 100644 --- a/src/Helpers/ApiHelper.php +++ b/src/Helpers/ApiHelper.php @@ -3,15 +3,14 @@ /** * Class that holds all api helpers used in classes. * - * @package EightshiftLibs\Helpers + * @package EightshiftFormsUtils\Helpers */ declare(strict_types=1); -namespace EightshiftForms\Helpers; +namespace EightshiftFormsUtils\Helpers; use EightshiftFormsUtils\Config\UtilsConfig; -use EightshiftFormsUtilsVendor\EightshiftLibs\Helpers\Components; /** * ApiHelper class. @@ -61,7 +60,7 @@ public static function getIntegrationApiReponseDetails( $code = $response['response']['code'] ?? 200; $body = $response['body'] ?? ''; - if (Components::isJson($body)) { + if (Helper::isJson($body)) { $body = \json_decode($body, true) ?? []; } } @@ -75,7 +74,7 @@ public static function getIntegrationApiReponseDetails( } return [ - 'integration' => Components::kebabToCamelCase($integration, '-'), + 'integration' => Helper::kebabToCamelCase($integration, '-'), 'params' => $params, 'files' => $files, 'response' => $response['response'] ?? [], diff --git a/src/Helpers/Helper.php b/src/Helpers/Helper.php index 63b57ef..3d580cf 100644 --- a/src/Helpers/Helper.php +++ b/src/Helpers/Helper.php @@ -3,12 +3,12 @@ /** * Class that holds all generic helpers. * - * @package EightshiftForms\Helpers + * @package EightshiftFormsUtils\Helpers */ declare(strict_types=1); -namespace EightshiftForms\Helpers; +namespace EightshiftFormsUtils\Helpers; use EightshiftFormsUtils\Config\UtilsConfig; use EightshiftFormsUtilsVendor\EightshiftLibs\Helpers\Components; @@ -244,7 +244,7 @@ public static function getBlockNameDetails(string $blockName): array return [ 'namespace' => $block[0] ?? '', 'name' => $blockName, - 'nameAttr' => Components::kebabToCamelCase($blockName), + 'nameAttr' => Helper::kebabToCamelCase($blockName), ]; } @@ -435,7 +435,7 @@ public static function getFormDetailsById(string $formId): array foreach ($output['fieldsOnly'] as $item) { $blockItemName = self::getBlockNameDetails($item['blockName'])['nameAttr']; - $value = $item['attrs'][Components::kebabToCamelCase("{$blockItemName}-{$blockItemName}-Name")] ?? ''; + $value = $item['attrs'][Helper::kebabToCamelCase("{$blockItemName}-{$blockItemName}-Name")] ?? ''; if (!$value) { continue; @@ -486,8 +486,8 @@ public static function getFormDetailsById(string $formId): array $name = $blockName['name']; if ($name === 'step') { - $stepCurrent = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Name")] ?? ''; - $stepLabel = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Label")] ?? ''; + $stepCurrent = $block['attrs'][Helper::kebabToCamelCase("{$name}-{$name}Name")] ?? ''; + $stepLabel = $block['attrs'][Helper::kebabToCamelCase("{$name}-{$name}Label")] ?? ''; if (!$stepLabel) { $stepLabel = $stepCurrent; @@ -504,7 +504,7 @@ public static function getFormDetailsById(string $formId): array continue; } - $itemName = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Name")] ?? ''; + $itemName = $block['attrs'][Helper::kebabToCamelCase("{$name}-{$name}Name")] ?? ''; if (!$itemName) { continue; } @@ -738,31 +738,6 @@ public static function getBlockAdditionalContentViaFilter(string $name, array $a return ''; } - /** - * Return main manifest.json file. - * - * @return array - */ - public static function getUtilsManifest(): array - { - $sep = \DIRECTORY_SEPARATOR; - $filePath = dirname(__FILE__, 2) . "{$sep}manifest.json"; - - return \json_decode(\implode(' ', (array)\file($filePath)), true); - } - - /** - * Return utils icons from manifest.json. - * - * @param string $type Type to return. - * - * @return string - */ - public static function getUtilsIcons(string $type): string - { - return self::getUtilsManifest()['icons'][Components::kebabToCamelCase($type)] ?? ''; - } - /** * Find array value by key in recursive array. * @@ -866,7 +841,7 @@ public static function getCurrentUrl(): string */ public static function removeUneceseryParamFields(array $params, array $additional = []): array { - $customFields = \array_flip(Components::flattenArray(self::getStateParams())); + $customFields = \array_flip(self::flattenArray(UtilsHelper::getStateParams())); $additional = \array_flip($additional); return \array_filter( @@ -976,76 +951,6 @@ function ($item) use ($isDeveloperModeActive) { ); } - /** - * Return selector admin enum values by name. - * - * @param string $name Name of the enum. - * - * @return string - */ - public static function getStateSelectorAdmin(string $name): string - { - return Components::getSettings()['enums']['selectorsAdmin'][$name] ?? ''; - } - - /** - * Return selector enum values by name. - * - * @param string $name Name of the enum. - * - * @return string - */ - public static function getStateSelector(string $name): string - { - return Components::getSettings()['enums']['selectors'][$name] ?? ''; - } - - /** - * Return attribute enum values by name. - * - * @param string $name Name of the enum. - * - * @return string - */ - public static function getStateAttribute(string $name): string - { - return Components::getSettings()['enums']['attrs'][$name] ?? ''; - } - - /** - * Return all params enum values. - * - * @return array - */ - public static function getStateParams(): array - { - return Components::getSettings()['enums']['params'] ?? []; - } - - /** - * Return param enum values by name. - * - * @param string $name Name of the enum. - * - * @return string - */ - public static function getStateParam(string $name): string - { - return self::getStateParams()[$name] ?? ''; - } - - /** - * Return field type internal enum values by name. - * - * @param string $name Name of the enum. - * - * @return string - */ - public static function getStateFieldType(string $name): string - { - return Components::getSettings()['enums']['typeInternal'][$name] ?? ''; - } - /** * Get the settings labels and details by type and key. * This method is used to provide the ability to translate all strings. @@ -1136,6 +1041,55 @@ public static function isDeveloperForceDisabledFieldsActive(): bool return \apply_filters(UtilsConfig::FILTER_SETTINGS_IS_DEBUG_ACTIVE, UtilsConfig::SETTINGS_DEBUG_FORCE_DISABLED_FIELDS) ?? false; } + /** + * Convert string from kebab to camel case. + * + * @param string $stringToConvert String to convert. + * @param string $separator Separator to use for conversion. + * + * @return string + */ + public static function kebabToCamelCase(string $stringToConvert, string $separator = '-'): string + { + return \lcfirst(\str_replace($separator, '', \ucwords($stringToConvert, $separator))); + } + + /** + * Check if json is valid + * + * @param string $jsonString String to check. + * + * @return bool + */ + public static function isJson(string $jsonString): bool + { + \json_decode($jsonString); + return (\json_last_error() === \JSON_ERROR_NONE); + } + + /** + * Flatten multidimensional array. + * + * @param array $arrayToFlatten Multidimensional array to flatten. + * + * @return array + */ + public static function flattenArray(array $arrayToFlatten): array + { + $output = []; + + \array_walk_recursive( + $arrayToFlatten, + function ($a) use (&$output) { + if (!empty($a)) { + $output[] = $a; + } + } + ); + + return $output; + } + /** * Get list of all full filter names build from array. * diff --git a/src/Helpers/I18nHelper.php b/src/Helpers/I18nHelper.php index 521f1f7..a0b3a63 100644 --- a/src/Helpers/I18nHelper.php +++ b/src/Helpers/I18nHelper.php @@ -3,14 +3,13 @@ /** * Class that holds all i18n helpers. * - * @package EightshiftLibs\Helpers + * @package EightshiftFormsUtils\Helpers */ declare(strict_types=1); -namespace EightshiftForms\Helpers; +namespace EightshiftFormsUtils\Helpers; -use EightshiftForms\Misc\SettingsWpml; use EightshiftFormsUtils\Config\UtilsConfig; /** diff --git a/src/Helpers/IntegrationsHelper.php b/src/Helpers/IntegrationsHelper.php index adc45e7..3ee1788 100644 --- a/src/Helpers/IntegrationsHelper.php +++ b/src/Helpers/IntegrationsHelper.php @@ -3,14 +3,14 @@ /** * Class that holds all Integration helpers. * - * @package EightshiftLibs\Helpers + * @package EightshiftFormsUtils\Helpers */ declare(strict_types=1); -namespace EightshiftForms\Helpers; +namespace EightshiftFormsUtils\Helpers; -use EightshiftForms\Helpers\SettingsHelper; +use EightshiftFormsUtils\Helpers\SettingsHelper; use EightshiftFormsUtils\Config\UtilsConfig; /** diff --git a/src/Helpers/SettingsHelper.php b/src/Helpers/SettingsHelper.php index afcf629..6d33007 100644 --- a/src/Helpers/SettingsHelper.php +++ b/src/Helpers/SettingsHelper.php @@ -3,14 +3,14 @@ /** * Class that holds all helpers for settings. * - * @package EightshiftLibs\Helpers + * @package EightshiftFormsUtils\Helpers */ declare(strict_types=1); -namespace EightshiftForms\Helpers; +namespace EightshiftFormsUtils\Helpers; -use EightshiftForms\Helpers\I18nHelper; +use EightshiftFormsUtils\Helpers\I18nHelper; use EightshiftFormsUtils\Config\UtilsConfig; /** diff --git a/src/Helpers/SettingsOutputHelper.php b/src/Helpers/SettingsOutputHelper.php index 632dd0c..cc23520 100644 --- a/src/Helpers/SettingsOutputHelper.php +++ b/src/Helpers/SettingsOutputHelper.php @@ -3,12 +3,12 @@ /** * Class that holds all Settings output helpers. * - * @package EightshiftLibs\Helpers + * @package EightshiftFormsUtils\Helpers */ declare(strict_types=1); -namespace EightshiftForms\Helpers; +namespace EightshiftFormsUtils\Helpers; use EightshiftFormsUtils\Config\UtilsConfig; @@ -216,9 +216,9 @@ public static function getTestAliConnection(string $key): array 'submitValue' => \__('Test API connection', 'eightshift-forms'), 'submitVariant' => 'outline', 'submitAttrs' => [ - Helper::getStateAttribute('testApiType') => $key, + UtilsHelper::getStateAttribute('testApiType') => $key, ], - 'additionalClass' => Helper::getStateSelectorAdmin('testApi') . ' es-submit--api-test', + 'additionalClass' => UtilsHelper::getStateSelectorAdmin('testApi') . ' es-submit--api-test', ]; } diff --git a/src/Helpers/UtilsHelper.php b/src/Helpers/UtilsHelper.php new file mode 100644 index 0000000..4f57a09 --- /dev/null +++ b/src/Helpers/UtilsHelper.php @@ -0,0 +1,100 @@ + + */ + public static function getUtilsManifest(): array + { + $sep = \DIRECTORY_SEPARATOR; + $filePath = dirname(__FILE__, 2) . "{$sep}manifest.json"; + + return \json_decode(\implode(' ', (array)\file($filePath)), true); + } + + /** + * Return utils icons from manifest.json. + * + * @param string $type Type to return. + * + * @return string + */ + public static function getUtilsIcons(string $type): string + { + return self::getUtilsManifest()['icons'][Helper::kebabToCamelCase($type)] ?? ''; + } + + /** + * Return selector admin enum values by name. + * + * @param string $name Name of the enum. + * + * @return string + */ + public static function getStateSelectorAdmin(string $name): string + { + return self::getUtilsManifest()['enums']['selectorsAdmin'][$name] ?? ''; + } + + /** + * Return selector enum values by name. + * + * @param string $name Name of the enum. + * + * @return string + */ + public static function getStateSelector(string $name): string + { + return self::getUtilsManifest()['enums']['selectors'][$name] ?? ''; + } + + /** + * Return attribute enum values by name. + * + * @param string $name Name of the enum. + * + * @return string + */ + public static function getStateAttribute(string $name): string + { + return self::getUtilsManifest()['enums']['attrs'][$name] ?? ''; + } + + /** + * Return all params enum values. + * + * @return array + */ + public static function getStateParams(): array + { + return self::getUtilsManifest()['enums']['params'] ?? []; + } + + /** + * Return param enum values by name. + * + * @param string $name Name of the enum. + * + * @return string + */ + public static function getStateParam(string $name): string + { + return self::getStateParams()[$name] ?? ''; + } +} diff --git a/src/manifest.json b/src/manifest.json index ef4f9b6..076a8b2 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -85,5 +85,142 @@ "rating": "", "emptyStateEntries": "", "entries": "" + }, + "enums": { + "selectorsAdmin": { + "singleSubmit": "js-es-block-single-submit", + "listingFilter": "js-es-admin-listing-filter", + "listingItem": "js-es-admin-listing-item", + "listingLocations": "js-es-admin-listing-locations", + "listingBulk": "js-es-admin-listing-bulk", + "listingBulkItems": "js-es-admin-listing-bulk-items", + "listingExport": "js-es-admin-listing-export", + "listingSelectAll": "js-es-admin-listing-select-all", + "cacheDelete": "js-es-cache-delete", + "migration": "js-es-migration", + "migrationOutput": "js-es-migration-output", + "transfer": "js-es-transfer", + "transferItem": "js-es-transfer-item", + "transferUpload": "js-es-transfer-upload", + "transferExisting": "js-es-transfer-existing", + "testApi": "js-es-api-test", + "manualImportApi": "js-es-manual-import-api", + "manualImportApiOutput": "js-es-manual-import-api-output", + "manualImportApiData": "js-es-manual-import-api-data", + "tabs": "js-es-tabs", + "tabsItem": "js-es-tabs-item" + }, + "selectors": { + "isActive": "es-form-is-active", + "isFilled": "es-form-is-filled", + "isLoading": "es-form-is-loading", + "isHidden": "es-form-is-hidden", + "isDisabled": "es-form-is-disabled", + "isGeoLoading": "es-form-is-geolocation-loading", + "hasError": "es-form-has-error", + "forms": "js-es-block-forms", + "form": "js-es-block-form", + "submitSingle": "js-es-block-single-submit", + "step": "js-es-block-step", + "stepProgressBar": "js-es-block-step-progress-bar", + "stepProgressBarMultiflow": "js-es-block-step-progress-bar-multiflow", + "stepSubmit": "js-es-block-step-trigger", + "error": "js-es-block-error", + "loader": "js-es-block-loader", + "globalMsg": "js-es-block-global-msg", + "group": "js-es-block-group", + "field": "js-es-block-field", + "fieldNoFormsBlock": "js-es-block-field-custom", + "rating": "js-es-block-rating" + }, + "params": { + "formId": "es-form-form-id", + "postId": "es-form-post-id", + "type": "es-form-type", + "name": "es-form-field-name", + "steps": "es-form-steps", + "settingsType": "es-form-settings-type", + "singleSubmit": "es-form-single-submit", + "storage": "es-form-storage", + "action": "es-form-action", + "actionExternal": "es-form-action-external", + "conditionalTags": "es-form-conditional-tags", + "fileId": "es-form-file-id", + "hubspotCookie": "es-form-hubspot-cookie", + "hubspotPageName": "es-form-hubspot-page-name", + "hubspotPageUrl": "es-form-hubspot-page-url", + "mailchimpTags": "es-form-mailchimp-tags", + "captcha": "es-form-captcha", + "direct": "es-form-direct", + "itemId": "es-form-item-id", + "innerId": "es-form-inner-id" + }, + "attrs": { + "formType": "data-form-type", + "formCustomName": "data-form-custom-name", + "formId": "data-form-id", + "formGeolocation": "data-form-geolocation", + + "bulkId": "data-bulk-id", + "bulkType": "data-bulk-type", + "bulkItems": "data-bulk-items", + + "stepId": "data-step-id", + "submitStepDirection": "data-step-direction", + "postId": "data-post-id", + + "fieldId": "data-field-id", + "fieldName": "data-field-name", + "fieldType": "data-field-type", + "fieldPreventSubmit": "data-field-prevent-submit", + "fieldTypeCustom": "data-type-custom", + "fieldUncheckedValue": "data-unchecked-value", + + "trackingEventName": "data-tracking-event-name", + "trackingAdditionalData": "data-tracking-additional-data", + "tracking": "data-tracking", + "cacheType": "data-cache-type", + "testApiType": "data-test-api-type", + "locationsId": "data-locations-id", + "migrationType": "data-migration-type", + "migrationExportItems": "data-migration-export-items", + "successRedirect": "data-success-redirect", + "successRedirectVariation": "data-success-redirect-variation", + "conditionalTags": "data-conditional-tags", + "typeSelector": "data-type-selector", + "actionExternal": "data-action-external", + "settingsType": "data-settings-type", + "groupSaveAsOneField": "data-group-save-as-one-field", + + "datePreviewFormat": "data-preview-format", + "dateOutputFormat": "data-output-format", + + "selectAllowSearch": "data-allow-search", + "selectIsMultiple": "data-is-multiple", + "selectPlaceholder": "data-placeholder", + "selectCustomProperties": "data-custom-properties", + "selectCountryCode": "data-country-code", + "selectCountryLabel": "data-country-label", + "selectCountryNumber": "data-country-number", + "selectOptionIsHidden": "data-option-is-hidden", + "selectId": "data-id", + "selectValue": "data-value", + + "ratingValue": "data-rating", + + "phoneSync": "data-phone-sync", + "phoneDisablePicker": "data-phone-disable-picker", + "saveAsJson": "data-save-as-json", + "downloads": "data-downloads", + "blockSsr": "data-block-ssr", + "disabledDefaultStyles": "data-disabled-default-styles", + "globalMsgHeadingSuccess": "data-msg-heading-success", + "globalMsgHeadingError": "data-msg-heading-error", + "hideCaptchaBadge": "data-hide-captcha-badge", + "reload": "data-reload", + "hubspotTypeId": "data-hubspot-type-id", + + "adminIntegrationType": "data-integration-type" + } } }