Skip to content

Commit

Permalink
updating uploads helper
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Jan 4, 2024
1 parent f372b1a commit cc4a11a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 53 deletions.
23 changes: 22 additions & 1 deletion src/Config/UtilsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class UtilsConfig
public const SETTINGS_NAME_PREFIX = 'es-forms';

public const FILTER_SETTINGS_DATA = self::FILTER_PREFIX . '_settings_data';
public const FILTER_PUBLIC_FILTERS_DATA = self::FILTER_PREFIX . '_public_filters_data';
public const FILTER_SETTINGS_NONE_TRANSLATABLE_NAMES = self::FILTER_PREFIX . '_settings_none_translatable_names';

/**
Expand All @@ -94,10 +95,30 @@ public static function getTempUploadDir(): string
return "esforms-tmp";
}

/**
* Method that returns projects setting name prefix.
*
* @return string
*/
public static function getSettingNamePrefix(): string
{
return "es-forms";
}

/**
* Filter settings is debug active key.
*/
public const FILTER_SETTINGS_IS_DEBUG_ACTIVE = 'es_forms_settings_is_debug_active';

public const SETTINGS_DEBUG_DEVELOPER_MODE_KEY = 'developer-mode';
public const SETTINGS_DEBUG_QM_LOG = 'skip-qm-log';
public const SETTINGS_DEBUG_FORCE_DISABLED_FIELDS = 'skip-force-disabled-fields';


public const SLUG_POST_TYPE = 'eightshift-forms';
public const SLUG_ADMIN = 'es-forms';
public const SLUG_ADMIN_SETTINGS = 'es-settings';
public const SLUG_ADMIN_SETTINGS_GLOBAL = 'es-settings-global';
public const SLUG_ADMIN_DASHBOARD = 'dashboard';

public const FILTER_SETTINGS_WPML_IS_VALID_NAME = 'es_forms_settings_is_valid_wpml';
}
6 changes: 3 additions & 3 deletions src/Helpers/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static function getApiErrorOutput(string $msg, array $additional = [], ar
$output['data'] = $additional;
}

if (Helper::isDeveloperMode() && $debug) {
if (Helper::isDeveloperModeActive() && $debug) {
$output['debug'] = $debug;
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public static function getApiSuccessOutput(string $msg, array $additional = [],
$output['data'] = $additional;
}

if (Helper::isDeveloperMode() && $debug) {
if (Helper::isDeveloperModeActive() && $debug) {
$output['debug'] = $debug;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ public static function getApiWarningOutput(string $msg, array $additional = [],
$output['data'] = $additional;
}

if (Helper::isDeveloperMode() && $debug) {
if (Helper::isDeveloperModeActive() && $debug) {
$output['debug'] = $debug;
}

Expand Down
71 changes: 42 additions & 29 deletions src/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@

namespace EightshiftForms\Helpers;

use EightshiftForms\AdminMenus\FormAdminMenu;
use EightshiftForms\AdminMenus\FormGlobalSettingsAdminSubMenu;
use EightshiftForms\AdminMenus\FormSettingsAdminSubMenu;
use EightshiftForms\AdminMenus\FormListingAdminSubMenu;
use EightshiftForms\CustomPostType\Forms;
use EightshiftForms\Hooks\Filters;
use EightshiftForms\Integrations\ActiveCampaign\SettingsActiveCampaign;
use EightshiftForms\Integrations\Jira\SettingsJira;
use EightshiftForms\Integrations\Mailer\SettingsMailer;
use EightshiftForms\Dashboard\SettingsDashboard;
use EightshiftForms\General\SettingsGeneral;
use EightshiftForms\Integrations\Pipedrive\SettingsPipedrive;
use EightshiftForms\Troubleshooting\SettingsDebug;
use EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftFormsVendor\EightshiftLibs\Helpers\Components;
use EightshiftFormsUtilsVendor\EightshiftLibs\Helpers\Components;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;

Expand All @@ -40,7 +33,7 @@ final class Helper
*/
public static function getListingPageUrl(): string
{
$page = FormListingAdminSubMenu::ADMIN_MENU_SLUG;
$page = UtilsConfig::SLUG_ADMIN;

return \get_admin_url(null, "admin.php?page={$page}");
}
Expand All @@ -53,9 +46,9 @@ public static function getListingPageUrl(): string
*
* @return string
*/
public static function getSettingsPageUrl(string $formId, string $type = SettingsGeneral::SETTINGS_TYPE_KEY): string
public static function getSettingsPageUrl(string $formId, string $type): string
{
$page = FormSettingsAdminSubMenu::ADMIN_MENU_SLUG;
$page = UtilsConfig::SLUG_ADMIN_SETTINGS;
$typeKey = '';

if (!empty($type)) {
Expand All @@ -72,9 +65,9 @@ public static function getSettingsPageUrl(string $formId, string $type = Setting
*
* @return string
*/
public static function getSettingsGlobalPageUrl(string $type = SettingsDashboard::SETTINGS_TYPE_KEY): string
public static function getSettingsGlobalPageUrl(string $type): string
{
$page = FormGlobalSettingsAdminSubMenu::ADMIN_MENU_SLUG;
$page = UtilsConfig::SLUG_ADMIN_SETTINGS_GLOBAL;
$typeKey = '';

if (!empty($type)) {
Expand All @@ -91,7 +84,7 @@ public static function getSettingsGlobalPageUrl(string $type = SettingsDashboard
*/
public static function getNewFormPageUrl(): string
{
$postType = Forms::POST_TYPE_SLUG;
$postType = UtilsConfig::SLUG_POST_TYPE;

return \get_admin_url(null, "post-new.php?post_type={$postType}");
}
Expand Down Expand Up @@ -140,9 +133,9 @@ public static function isEightshiftFormsAdminPages(): bool
$page = isset($_GET['page']) ? \sanitize_text_field(\wp_unslash($_GET['page'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$pages = \array_flip([
FormAdminMenu::ADMIN_MENU_SLUG,
FormSettingsAdminSubMenu::ADMIN_MENU_SLUG,
FormGlobalSettingsAdminSubMenu::ADMIN_MENU_SLUG,
UtilsConfig::SLUG_ADMIN,
UtilsConfig::SLUG_ADMIN_SETTINGS,
UtilsConfig::SLUG_ADMIN_SETTINGS_GLOBAL,
]);

return isset($pages[$page]) && \is_admin();
Expand Down Expand Up @@ -398,7 +391,7 @@ public static function getFormDetailsById(string $formId): array

$fieldsOnly = $blocks['innerBlocks'][0]['innerBlocks'] ?? [];

$settings = \apply_filters(Filters::FILTER_SETTINGS_DATA, [])[$type] ?? [];
$settings = \apply_filters(UtilsConfig::FILTER_SETTINGS_DATA, [])[$type] ?? [];

$output['type'] = $type;
$output['typeFilter'] = $blockName['name'];
Expand Down Expand Up @@ -895,7 +888,7 @@ static function ($item) use ($customFields, $additional) {
*/
public static function canIntegrationUseSync(string $integrationName): bool
{
return isset(\apply_filters(Filters::FILTER_SETTINGS_DATA, [])[$integrationName]['fields']);
return isset(\apply_filters(UtilsConfig::FILTER_SETTINGS_DATA, [])[$integrationName]['fields']);
}

/**
Expand All @@ -919,7 +912,7 @@ public static function cleanPageUrl(string $url): string
*/
public static function setQmLogsOutput($data = ''): void
{
if (\is_plugin_active('query-monitor/query-monitor.php') && \apply_filters(SettingsDebug::FILTER_SETTINGS_IS_DEBUG_ACTIVE, SettingsDebug::SETTINGS_DEBUG_QM_LOG) && $data) {
if (\is_plugin_active('query-monitor/query-monitor.php') && self::isDeveloperQMLogActive() && $data) {
\do_action('qm/debug', $data); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
}
Expand Down Expand Up @@ -951,13 +944,13 @@ public static function getBlockLocations(string $formId): array
return [];
}

$isDeveloperMode = \apply_filters(SettingsDebug::FILTER_SETTINGS_IS_DEBUG_ACTIVE, SettingsDebug::SETTINGS_DEBUG_DEVELOPER_MODE_KEY);
$isDeveloperModeActive = Helper::isDeveloperModeActive();

return \array_map(
function ($item) use ($isDeveloperMode) {
function ($item) use ($isDeveloperModeActive) {
$id = $item->ID;
$title = $item->post_title; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$title = $isDeveloperMode ? "{$id} - {$title}" : $title;
$title = $isDeveloperModeActive ? "{$id} - {$title}" : $title;

return [
'id' => $id,
Expand Down Expand Up @@ -1075,13 +1068,13 @@ public static function getSpecialConstants(string $type): array
*/
public static function getFilterName(array $names): string
{
$filters = \wp_cache_get(Filters::FILTER_PREFIX . '_filters_public_list', Filters::FILTER_PREFIX);
$filters = \wp_cache_get(UtilsConfig::FILTER_PREFIX . '_filters_public_list', UtilsConfig::FILTER_PREFIX);

// Cache filter names for faster access.
if (!$filters) {
$filters = self::getAllPublicFiltersNames(\apply_filters(Filters::FILTER_PUBLIC_FILTERS_DATA, []));
$filters = self::getAllPublicFiltersNames(\apply_filters(UtilsConfig::FILTER_PUBLIC_FILTERS_DATA, []));

\wp_cache_add(Filters::FILTER_PREFIX . '_filters_public_list', $filters, Filters::FILTER_PREFIX, \HOUR_IN_SECONDS);
\wp_cache_add(UtilsConfig::FILTER_PREFIX . '_filters_public_list', $filters, UtilsConfig::FILTER_PREFIX, \HOUR_IN_SECONDS);
}

// List of all keys provided for the filter name.
Expand All @@ -1096,7 +1089,7 @@ function ($item) {
$names = \implode('_', $names);

// Create a full filter name.
$filterName = Filters::FILTER_PREFIX . "_{$names}";
$filterName = UtilsConfig::FILTER_PREFIX . "_{$names}";

if (!\in_array($filterName, $filters, true)) {
// translators: %s is the filter name.
Expand All @@ -1112,11 +1105,31 @@ function ($item) {
*
* @return boolean
*/
public static function isDeveloperMode(): bool
public static function isDeveloperModeActive(): bool
{
return \apply_filters(UtilsConfig::FILTER_SETTINGS_IS_DEBUG_ACTIVE, UtilsConfig::SETTINGS_DEBUG_DEVELOPER_MODE_KEY) ?? false;
}

/**
* Check if QA Monitor Log mode is active.
*
* @return boolean
*/
public static function isDeveloperQMLogActive(): bool
{
return \apply_filters(UtilsConfig::FILTER_SETTINGS_IS_DEBUG_ACTIVE, UtilsConfig::SETTINGS_DEBUG_QM_LOG) ?? false;
}

/**
* Check if Force Disabled Fields mode is active.
*
* @return boolean
*/
public static function isDeveloperForceDisabledFieldsActive(): bool
{
return \apply_filters(UtilsConfig::FILTER_SETTINGS_IS_DEBUG_ACTIVE, UtilsConfig::SETTINGS_DEBUG_FORCE_DISABLED_FIELDS) ?? false;
}

/**
* Get list of all full filter names build from array.
*
Expand All @@ -1134,7 +1147,7 @@ private static function getAllPublicFiltersNames(array $data, string $prefix = '
$nestedKeys = self::getAllPublicFiltersNames($value, $prefix . Helper::kebabToSnakeCase(Helper::camelToSnakeCase($key)) . '_');
$output = \array_merge($output, $nestedKeys);
} else {
$output[] = Filters::FILTER_PREFIX . '_' . $prefix . Helper::kebabToSnakeCase(Helper::camelToSnakeCase($value));
$output[] = UtilsConfig::FILTER_PREFIX . '_' . $prefix . Helper::kebabToSnakeCase(Helper::camelToSnakeCase($value));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Helpers/I18nHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EightshiftForms\Helpers;

use EightshiftForms\Misc\SettingsWpml;
use EightshiftFormsUtils\Config\UtilsConfig;

/**
* Class I18nHelper
Expand All @@ -32,7 +33,7 @@ public static function getLocale(): string
$locale = \apply_filters($filterName, $localeInit);
}

$useWpml = \apply_filters(SettingsWpml::FILTER_SETTINGS_IS_VALID_NAME, []);
$useWpml = \apply_filters(UtilsConfig::FILTER_SETTINGS_WPML_IS_VALID_NAME, []);
if ($useWpml) {
$defaultLanguage = \apply_filters('wpml_default_language', null);
$currentLanguage = \apply_filters('wpml_current_language', null);
Expand Down
8 changes: 4 additions & 4 deletions src/Helpers/IntegrationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace EightshiftForms\Helpers;

use EightshiftForms\Hooks\Filters;
use EightshiftForms\Helpers\SettingsHelper;
use EightshiftFormsUtils\Config\UtilsConfig;

/**
* Class IntegrationsHelper
Expand All @@ -34,7 +34,7 @@ public static function getIntegrationDetailsById(string $id): array
}

$type = $integrationDetails['typeFilter'];
$useFilter = \apply_filters(Filters::FILTER_SETTINGS_DATA, [])[$type]['use'] ?? '';
$useFilter = \apply_filters(UtilsConfig::FILTER_SETTINGS_DATA, [])[$type]['use'] ?? '';

return [
'label' => $integrationDetails['label'],
Expand All @@ -55,7 +55,7 @@ public static function getActiveIntegrations(): array
{
$output = [];

foreach (\apply_filters(Filters::FILTER_SETTINGS_DATA, []) as $key => $value) {
foreach (\apply_filters(UtilsConfig::FILTER_SETTINGS_DATA, []) as $key => $value) {
$useFilter = $value['use'] ?? '';

if (!$useFilter) {
Expand All @@ -64,7 +64,7 @@ public static function getActiveIntegrations(): array

$type = $value['type'] ?? '';

if ($type !== Filters::SETTINGS_INTERNAL_TYPE_INTEGRATION) {
if ($type !== UtilsConfig::SETTINGS_INTERNAL_TYPE_INTEGRATION) {
continue;
}

Expand Down
Loading

0 comments on commit cc4a11a

Please sign in to comment.