/**
* Method to return a list of actions from a string or from an xml for which permissions can be set.
*
* @param string|\SimpleXMLElement $data The XML string or an XML element.
* @param string $xpath An optional xpath to search for the fields.
*
* @return boolean|array False if case of error or the list of actions available.
*
* @since 3.0.0
*/
public static function getActionsFromData($data, $xpath = "/access/section[@name='component']/")
{
// If the data to load isn't already an XML element or string return false.
if (!$data instanceof \SimpleXMLElement && !\is_string($data)) {
return false;
}
// Attempt to load the XML if a string.
if (\is_string($data)) {
try {
$data = new \SimpleXMLElement($data);
} catch (\Exception $e) {
return false;
}
// Make sure the XML loaded correctly.
if (!$data) {
return false;
}
}
// Initialise the actions array
$actions = array();
// Get the elements from the xpath
$elements = $data->xpath($xpath . 'action[@name][@title]');
// If there some elements, analyse them
if (!empty($elements)) {
foreach ($elements as $element) {
// Add the action to the actions array
$action = array('name' => (string) $element['name'], 'title' => (string) $element['title']);
if (isset($element['description'])) {
$action['description'] = (string) $element['description'];
}
$actions[] = (object) $action;
}
}
// Finally return the actions array
return $actions;
}