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

Libs 8.0.0 release #7

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.

This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/).

## [2.0.0]

### Updated
- `eightshift-forms-utils` to version `2.0.0`.
- `@infinum/eightshift-libs` to version `8.0.0`.

### Removed
- `MAIN_PLUGIN_MANIFEST_ITEM_HOOK_NAME` constant.
- `getDataManifest`, `getDataManifestRaw`, `getProjectVersion`, `getCountrySelectList` functions.
- `src/Manifest/UtilsManifest.php` class.

### Changed
- `getDataManifestPath` helper now supports only `$path` param.
- `camelToSnakeCase`, `kebabToSnakeCase`, `recursiveFind`, `getCurrentUrl`, `cleanPageUrl` are now used from the `@infinum/eightshift-libs` package.

## [1.3.5]

### Fixed
Expand Down Expand Up @@ -136,6 +151,7 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a

- Initial production release.

[2.0.0]: https://github.com/infinum/eightshift-forms-utils/compare/1.3.5...2.0.0
[1.3.5]: https://github.com/infinum/eightshift-forms-utils/compare/1.3.4...1.3.5
[1.3.4]: https://github.com/infinum/eightshift-forms-utils/compare/1.3.3...1.3.4
[1.3.3]: https://github.com/infinum/eightshift-forms-utils/compare/1.3.2...1.3.3
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@
"php-stubs/wordpress-stubs": "^6.3",
"szepeviktor/phpstan-wordpress": "^1.3"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/infinum/eightshift-libs.git"
}
],
"require": {
"php": "^7.4 || >=8.0",
"infinum/eightshift-libs": "^7.1.2"
"infinum/eightshift-libs": "dev-feature/optimizations"
dingo-d marked this conversation as resolved.
Show resolved Hide resolved
},
"suggest": {
"ext-pcov": "* || This extension is used for code coverage generation. Use either pcov, or xdebug, but not both.",
Expand Down
215 changes: 136 additions & 79 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Config/UtilsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ class UtilsConfig
// ------------------------------------------------------------------

/**
* Main plugin manifest item hook name.
* Main plugin manifest cache name.
*
* @var string
*/
public const MAIN_PLUGIN_MANIFEST_ITEM_HOOK_NAME = 'es-forms-manifest-item';
public const MAIN_PLUGIN_MANIFEST_CACHE_NAME = 'es_forms';

// ------------------------------------------------------------------
// DEVELOPER
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Helpers/UtilsApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace EightshiftFormsUtils\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftLibs\Helpers\Components;
use EightshiftLibs\Helpers\Helpers;

/**
* UtilsApiHelper class.
Expand Down Expand Up @@ -61,7 +61,7 @@ public static function getIntegrationApiReponseDetails(
$code = $response['response']['code'] ?? UtilsConfig::API_RESPONSE_CODE_SUCCESS;
$body = $response['body'] ?? '';

if (Components::isJson($body)) {
if (Helpers::isJson($body)) {
$body = \json_decode($body, true) ?? [];
}
}
Expand Down
48 changes: 4 additions & 44 deletions src/Helpers/UtilsDataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,22 @@

namespace EightshiftFormsUtils\Helpers;

use EightshiftLibs\Helpers\Components;
use EightshiftLibs\Helpers\Helpers;

/**
* Class UtilsDataHelper
*/
final class UtilsDataHelper
{
/**
* Return files from data folder.
*
* @param string $type Folder name.
* @param string $file File name with ext.
*
* @return array<mixed>
*/
public static function getDataManifest(string $type, string $file = 'manifest.json'): array
{
$path = self::getDataManifestRaw($type, $file);

if ($path) {
return \json_decode($path, true);
}

return [];
}

/**
* Return files from data folder in raw format.
*
* @param string $type Folder name.
* @param string $file File name with ext.
*
* @return string
*/
public static function getDataManifestRaw(string $type, string $file = 'manifest.json'): string
{
$path = self::getDataManifestPath($type, $file);

if (\file_exists($path)) {
return \file_get_contents($path); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
}

return '';
}

/**
* Return files full path.
*
* @param string $type Folder name.
* @param string $file File name with ext.
* @param string $path Path to the file.
*
* @return string
*/
public static function getDataManifestPath(string $type, string $file = 'manifest.json'): string
public static function getDataManifestPath(string $path): string
{
$sep = \DIRECTORY_SEPARATOR;
return Components::getProjectPaths('srcPath') . "vendor-prefixed/infinum/eightshift-forms-utils{$sep}src{$sep}Data{$sep}{$type}{$sep}{$file}";
return Helpers::joinPaths([Helpers::getProjectPaths('pluginRoot'), 'vendor-prefixed', 'infinum', 'eightshift-forms-utils', 'src', 'Data', $path]);
}
}
117 changes: 7 additions & 110 deletions src/Helpers/UtilsGeneralHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,13 @@
namespace EightshiftFormsUtils\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftLibs\Helpers\Components;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use EightshiftLibs\Helpers\Helpers;

/**
* UtilsGeneralHelper class.
*/
final class UtilsGeneralHelper
{
/**
* Method that returns project version.
*
* Generally used for versioning asset handlers while enqueueing them.
*
* @return string
*/
public static function getProjectVersion(): string
{
if (!\function_exists('get_plugin_data')) {
require_once(\ABSPATH . 'wp-admin/includes/plugin.php');
}

$details = \get_plugin_data(Components::getProjectPaths('cliOutput') . \DIRECTORY_SEPARATOR . UtilsConfig::MAIN_PLUGIN_FILE_NAME);

return isset($details['Version']) ? (string) $details['Version'] : '1.0.0';
}

/**
* Method that returns listing page url.
*
Expand Down Expand Up @@ -242,34 +222,10 @@ public static function getBlockNameDetails(string $blockName): array
return [
'namespace' => $block[0] ?? '',
'name' => $blockName,
'nameAttr' => Components::kebabToCamelCase($blockName),
'nameAttr' => Helpers::kebabToCamelCase($blockName),
];
}

/**
* Convert camel to snake case
*
* @param string $input Name to change.
*
* @return string
*/
public static function camelToSnakeCase($input): string
{
return \strtolower((string) \preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
}

/**
* Convert string from kebab to snake case.
*
* @param string $stringToConvert String to convert.
*
* @return string
*/
public static function kebabToSnakeCase(string $stringToConvert): string
{
return \str_replace('-', '_', $stringToConvert);
}

/**
* Output the form type used by checking the post_content and extracting the block used for the integration.
*
Expand Down Expand Up @@ -431,7 +387,7 @@ public static function getFormDetails(string $formId): array
foreach ($output[UtilsConfig::FD_FIELDS_ONLY] as $item) {
$blockItemName = self::getBlockNameDetails($item['blockName'])['nameAttr'];

$value = $item['attrs'][Components::kebabToCamelCase("{$blockItemName}-{$blockItemName}-Name")] ?? '';
$value = $item['attrs'][Helpers::kebabToCamelCase("{$blockItemName}-{$blockItemName}-Name")] ?? '';

if (!$value) {
continue;
Expand Down Expand Up @@ -482,8 +438,8 @@ public static function getFormDetails(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'][Helpers::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
$stepLabel = $block['attrs'][Helpers::kebabToCamelCase("{$name}-{$name}Label")] ?? '';

if (!$stepLabel) {
$stepLabel = $stepCurrent;
Expand All @@ -500,7 +456,7 @@ public static function getFormDetails(string $formId): array
continue;
}

$itemName = $block['attrs'][Components::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
$itemName = $block['attrs'][Helpers::kebabToCamelCase("{$name}-{$name}Name")] ?? '';
if (!$itemName) {
continue;
}
Expand Down Expand Up @@ -621,16 +577,6 @@ public static function prepareGenericParamsOutput(array $params, array $exclude
return $output;
}

/**
* Return counries filtered by some key for multiple usages.
*
* @return array<int, array<int, string>>
*/
public static function getCountrySelectList(): array
{
return UtilsDataHelper::getDataManifest('country');
}

/**
* Output additional content from filter by block.
* Limited to front page only.
Expand Down Expand Up @@ -659,29 +605,6 @@ public static function getBlockAdditionalContentViaFilter(string $name, array $a
return '';
}

/**
* Find array value by key in recursive array.
*
* @param array<mixed> $array Array to find.
* @param string $needle Key name to find.
*
* @return array<int, string>
*/
public static function recursiveFind(array $array, string $needle): array
{
$iterator = new RecursiveArrayIterator($array);
$recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
$aHitList = [];

foreach ($recursive as $key => $value) {
if ($key === $needle) {
\array_push($aHitList, $value);
}
}

return $aHitList;
}

/**
* Output select options ass array from html string.
*
Expand Down Expand Up @@ -738,20 +661,6 @@ public static function isBlockEditor(): bool
return $currentScreen->is_block_editor();
}

/**
* Get current url with params.
*
* @return string
*/
public static function getCurrentUrl(): string
{
$port = isset($_SERVER['HTTPS']) ? \sanitize_text_field(\wp_unslash($_SERVER['HTTPS'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$host = isset($_SERVER['HTTP_HOST']) ? \sanitize_text_field(\wp_unslash($_SERVER['HTTP_HOST'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$request = isset($_SERVER['REQUEST_URI']) ? \sanitize_text_field(\wp_unslash($_SERVER['REQUEST_URI'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

return ($port ? "https" : "http") . "://{$host}{$request}";
}

/**
* Remove unecesery custom params.
*
Expand All @@ -762,7 +671,7 @@ public static function getCurrentUrl(): string
*/
public static function removeUneceseryParamFields(array $params, array $additional = []): array
{
$customFields = \array_flip(Components::flattenArray(UtilsHelper::getStateParams()));
$customFields = \array_flip(Helpers::flattenArray(UtilsHelper::getStateParams()));
$additional = \array_flip($additional);

return \array_filter(
Expand Down Expand Up @@ -793,18 +702,6 @@ public static function canIntegrationUseSync(string $integrationName): bool
return isset(\apply_filters(UtilsConfig::FILTER_SETTINGS_DATA, [])[$integrationName]['fields']);
}

/**
* Clean url from query params.
*
* @param string $url Url to clean.
*
* @return string
*/
public static function cleanPageUrl(string $url): string
{
return \preg_replace('/\\?.*/', '', $url);
}

/**
* Return all posts where form is assigned.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/UtilsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace EightshiftFormsUtils\Helpers;

use EightshiftLibs\Helpers\Components;
use EightshiftLibs\Helpers\Helpers;

/**
* UtilsHelper class.
Expand Down Expand Up @@ -39,7 +39,7 @@ public static function getUtilsManifest(): array
*/
public static function getUtilsIcons(string $type): string
{
return self::getUtilsManifest()['icons'][Components::kebabToCamelCase($type)] ?? '';
return self::getUtilsManifest()['icons'][Helpers::kebabToCamelCase($type)] ?? '';
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Helpers/UtilsHooksHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EightshiftFormsUtils\Helpers;

use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftLibs\Helpers\Helpers;

/**
* Class UtilsHooksHelper
Expand Down Expand Up @@ -78,7 +79,7 @@ private static function getHookName(array $names, string $cacheName, string $lab
// List of all keys provided for the filter name.
$names = \array_map(
function ($item) {
return UtilsGeneralHelper::kebabToSnakeCase(UtilsGeneralHelper::camelToSnakeCase($item));
return Helpers::kebabToSnakeCase(Helpers::camelToSnakeCase($item));
},
$names
);
Expand Down Expand Up @@ -113,10 +114,10 @@ private static function getHooksList(array $data, string $prefix = '', string $f

foreach ($data as $key => $value) {
if (\is_array($value)) {
$nestedKeys = self::getHooksList($value, $prefix . UtilsGeneralHelper::kebabToSnakeCase(UtilsGeneralHelper::camelToSnakeCase($key)) . '_', $filterPrefix);
$nestedKeys = self::getHooksList($value, $prefix . Helpers::kebabToSnakeCase(Helpers::camelToSnakeCase($key)) . '_', $filterPrefix);
$output = \array_merge($output, $nestedKeys);
} else {
$output[] = $filterPrefix . '_' . $prefix . UtilsGeneralHelper::kebabToSnakeCase(UtilsGeneralHelper::camelToSnakeCase($value));
$output[] = $filterPrefix . '_' . $prefix . Helpers::kebabToSnakeCase(Helpers::camelToSnakeCase($value));
}
}

Expand Down
Loading
Loading