Skip to content

Commit

Permalink
Upd. RemoveSettings. Implementation of the remove function for the JR…
Browse files Browse the repository at this point in the history
…egistry class in case it is missing there
  • Loading branch information
AntonV1211 committed Dec 19, 2024
1 parent b97f9a5 commit bc76053
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
90 changes: 90 additions & 0 deletions lib/Cleantalk/Custom/StorageHandler/RemoveSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Cleantalk\Custom\StorageHandler;

/**
* Implementation of the remove function for the JRegistry class in case it is missing there
*/
class RemoveSetting extends \JRegistry
{
/**
* Delete a registry value
*
* @param string $path Registry Path (e.g. joomla.content.showauthor)
*
* @return mixed The value of the removed node or null if not set
*/
public function remove($path)
{
// Cheap optimisation to direct remove the node if there is no separator
if ($this->separator === null || $this->separator === '' || !\strpos($path, $this->separator)) {
$result = (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '')
? $this->data->$path
: null;


unset($this->data->$path);

return $result;
}

/*
* Explode the registry path into an array and remove empty
* nodes that occur as a result of a double separator. ex: joomla..test
* Finally, re-key the array so they are sequential.
*/
$nodes = \array_values(\array_filter(\explode($this->separator, $path), 'strlen'));

if (!$nodes) {
return null;
}

// Initialize the current node to be the registry root.
$node = $this->data;
$parent = null;

// Traverse the registry to find the correct node for the result.
for ($i = 0, $n = \count($nodes) - 1; $i < $n; $i++) {
if (\is_object($node)) {
if (!isset($node->{$nodes[$i]})) {
continue;
}

$parent = &$node;
$node = $node->{$nodes[$i]};

continue;
}

if (\is_array($node)) {
if (!isset($node[$nodes[$i]])) {
continue;
}

$parent = &$node;
$node = $node[$nodes[$i]];

continue;
}
}

// Get the old value if exists so we can return it
switch (true) {
case \is_object($node):
$result = $node->{$nodes[$i]} ? $node->{$nodes[$i]} : null;
unset($parent->{$nodes[$i]});
break;

case \is_array($node):
$result = $node[$nodes[$i]] ? $node[$nodes[$i]] : null;
unset($parent[$nodes[$i]]);
break;

default:
$result = null;
break;
}

return $result;
}
}
13 changes: 10 additions & 3 deletions lib/Cleantalk/Custom/StorageHandler/StorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ public function deleteSetting($setting_name)
$data = new \JRegistry($table->custom_data);
$data_array = $data->toArray();
if ( isset($data_array[$setting_name]) ) {
$data->remove($setting_name);
if (method_exists($data, 'remove')) {
$data->remove($setting_name);
$table->custom_data = $data->toString();
} else {
$removeSettingInstance = new RemoveSetting($table->custom_data);
$removeSettingInstance->remove($setting_name);
$table->custom_data = $removeSettingInstance->toString();
}
return $table->store();
}
$table->custom_data = $data->toString();
$table->store();
return false;
}

public function saveSetting($setting_name, $setting_value)
Expand Down

0 comments on commit bc76053

Please sign in to comment.