Updated URL Replace addon #1483
|
@ -134,6 +134,7 @@ function url_replace_addon_admin(string &$o)
|
|||
*/
|
||||
toddy marked this conversation as resolved
Outdated
|
||||
function url_replace_render(array &$b)
|
||||
{
|
||||
$replaced = false;
|
||||
$replacements = [];
|
||||
|
||||
$nitter_server = DI::config()->get('url_replace', 'nitter_server') ?? URL_REPLACE_NITTER_DEFAULT;
|
||||
|
@ -171,6 +172,7 @@ function url_replace_render(array &$b)
|
|||
foreach ($replacements as $server => $replacement) {
|
||||
if (strpos($b['html'], $server) !== false) {
|
||||
$b['html'] = str_replace($server, $replacement, $b['html']);
|
||||
$replaced = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -178,10 +180,11 @@ function url_replace_render(array &$b)
|
|||
foreach ($twelvefeet_sites as $twelvefeet_site) {
|
||||
if (strpos($b['html'], $twelvefeet_site) !== false) {
|
||||
$b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/' . $twelvefeet_site, $b['html']);
|
||||
$replaced = true;
|
||||
}
|
||||
toddy marked this conversation as resolved
Outdated
heluecht
commented
I suggest to compare the original value with the new value to decide, if content had been replaced. I suggest to compare the original value with the new value to decide, if content had been replaced.
toddy
commented
You're right, that makes more sense. I'll change that probably tomorrow, I'm short on time right now. You're right, that makes more sense. I'll change that probably tomorrow, I'm short on time right now.
|
||||
}
|
||||
|
||||
if (count($replacements) > 0) {
|
||||
if ($replaced) {
|
||||
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(URL replace addon enabled for X, YouTube, Instagram and some news sites.)') . '</small></p>';
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue
Could you use the third parameter of the
get
function for the default value? Also it would be nice to use constants for the default URL. See for example the inviduous addon for that.Thanks for your review. Unfotunately, the third parameter of get() does not work in this case, I've tried that. When the addon is freshly installed, there is no configuration in the database. In that case,
DI::config()->get()
gets a NULL value from the database, so the third parameter will be returned as the default value.However, if the site admin sets another server, then the URL of that server will be stored in the database. If afterwards the site admin deletes that custom server again and leaves the input field blank, then
DI::config()->get()
will get a result from the database -- an empty string. In that case, the empty string will be returned. This is the reason why I'm checking the value with theempty()
function.An alternative approach would be to specifically check in
url_replace_addon_admin_post()
if the posted string is empty. If it is, I could then useDI::config->delete()
instead ofDI::config->set()
, that should solve the problem.The alternative approach could look like this:
Should I rewrite the addon in that style?
Looks better.
As a general rule, the default value parameter of both
Config->get
andIConfig->get
is now deprecated in favor of using the short ternary operators?:
and??
. This allows for a better control for default values.I've just pushed a commit which uses this pattern. Please take a look.