Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Nov 8, 2024
1 parent b326e4c commit d0703b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
38 changes: 21 additions & 17 deletions controller/acp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,38 +189,42 @@ protected function get_google_fonts()
protected function save_google_fonts()
{
$fonts = $this->request->variable('abbc3_google_fonts', '');

if (!empty($fonts))
{
$fonts = explode("\n", $fonts);
$fonts = array_filter($fonts, 'strlen'); // Remove empty strings
$fonts = array_filter(
array_map('trim', explode("\n", $fonts)),
[$this, 'validate_google_fonts']
);

if (!empty($fonts))
{
$this->validate_google_fonts($fonts);
}
$fonts = $fonts ? json_encode(array_values($fonts)) : '';
}

$fonts = (is_array($fonts) && !empty($fonts)) ? json_encode($fonts) : '';
$this->config_text->set('abbc3_google_fonts', $fonts);
}

/**
* Validate Google Font names provided link to a CSS file
* Validate Google Font names link to an existing CSS file
*
* @param array $fonts
* @param string $font
* @return bool
*/
protected function validate_google_fonts(&$fonts)
protected function validate_google_fonts($font)
{
foreach ($fonts as $key => $font)
if ($font === '')
{
if (empty($font) || $this->valid_url('https://fonts.googleapis.com/css?family=' . urlencode($font)))
{
continue;
}
return false;
}

$url = 'https://fonts.googleapis.com/css?family=' . urlencode($font);

$this->errors[] = $this->language->lang('ABBC3_INVALID_FONT', $font);
unset($fonts[$key]);
if ($this->valid_url($url))
{
return true;
}

$this->errors[] = $this->language->lang('ABBC3_INVALID_FONT', $font);
return false;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/acp/acp_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public function save_google_fonts_data()
['', '', E_USER_NOTICE, 'CONFIG_UPDATED'],
['Droid Sans', '["Droid Sans"]', E_USER_NOTICE, 'CONFIG_UPDATED'],
["Droid Sans\nRoboto", '["Droid Sans","Roboto"]', E_USER_NOTICE, 'CONFIG_UPDATED'],
["\n\nDroid Sans\n\nRoboto\n\n", '["Droid Sans","Roboto"]', E_USER_NOTICE, 'CONFIG_UPDATED'],
["Droid Sans\nRoboto\nMac Donald", '["Droid Sans","Roboto"]', E_USER_WARNING, 'ABBC3_INVALID_FONT'],
['Mac Donald', '', E_USER_WARNING, 'ABBC3_INVALID_FONT'],
];
Expand Down

0 comments on commit d0703b6

Please sign in to comment.