| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.6 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class SearchDropdownHelper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The total items to display on the dropdown |
| 23 |
* |
| 24 |
* @var integer |
| 25 |
*/ |
| 26 |
const SELECTION_ITEMS = 8; |
| 27 |
|
| 28 |
/** |
| 29 |
* Parses the given items via the class identified by the $type |
| 30 |
* |
| 31 |
* @param string $path |
| 32 |
* @param array $items |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public static function parseData($path, $items) |
| 37 |
{ |
| 38 |
if (!method_exists($path, 'parseData')) |
| 39 |
{ |
| 40 |
return SearchDropdownBaseHelper::parseData($items); |
| 41 |
} |
| 42 |
|
| 43 |
return $path::parseData($items); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns the selected items in a format that the Search Dropdown field can display. |
| 48 |
* |
| 49 |
* @param string $path |
| 50 |
* @param array $needle |
| 51 |
* @param array $haystack |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public static function getSelectedItems($path, $needle, $haystack = []) |
| 56 |
{ |
| 57 |
if (empty($needle) || empty($path)) |
| 58 |
{ |
| 59 |
return []; |
| 60 |
} |
| 61 |
|
| 62 |
if (!$helper_class = self::getHelperClass($path)) |
| 63 |
{ |
| 64 |
return []; |
| 65 |
} |
| 66 |
|
| 67 |
if (!method_exists($helper_class, 'getSelectedItems')) |
| 68 |
{ |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
// return the selected items from the helper class |
| 73 |
if ($haystack) |
| 74 |
{ |
| 75 |
return $helper_class->getSelectedItems($needle, $haystack); |
| 76 |
} |
| 77 |
|
| 78 |
return $helper_class->getSelectedItems($needle); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the helper class from the path's plugin. |
| 83 |
* i.e. if the path is in framework, we search the framework's helper middleware |
| 84 |
* to find the helper class. if it's in a plugin, we search the plugin's helper middleware. |
| 85 |
* |
| 86 |
* @param string $path |
| 87 |
* |
| 88 |
* @return mixed |
| 89 |
*/ |
| 90 |
/** |
| 91 |
* Bootstrap functions a searchdropdown path may resolve to. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public static function getAllowedPluginFunctions() |
| 96 |
{ |
| 97 |
return apply_filters('fpframework/searchdropdown/allowed_plugins', ['fpframework', 'firebox']); |
| 98 |
} |
| 99 |
|
| 100 |
public static function getHelperClass($path) |
| 101 |
{ |
| 102 |
if (empty($path)) |
| 103 |
{ |
| 104 |
return []; |
| 105 |
} |
| 106 |
|
| 107 |
// get helper class data |
| 108 |
if (!$helper_class_data = self::getHelperClassData($path)) |
| 109 |
{ |
| 110 |
return []; |
| 111 |
} |
| 112 |
|
| 113 |
// The plugin segment comes from the request; only the known bootstrap functions may be called. |
| 114 |
if (!in_array($helper_class_data['plugin'], self::getAllowedPluginFunctions(), true)) |
| 115 |
{ |
| 116 |
return []; |
| 117 |
} |
| 118 |
|
| 119 |
// ensure plugin(framework or plugin) function exists |
| 120 |
if (!function_exists($helper_class_data['plugin'])) |
| 121 |
{ |
| 122 |
return []; |
| 123 |
} |
| 124 |
|
| 125 |
$plugin = $helper_class_data['plugin'](); |
| 126 |
|
| 127 |
// ensure plugin contains helper property |
| 128 |
if (!is_object($plugin) || !property_exists($plugin, 'helper')) |
| 129 |
{ |
| 130 |
return []; |
| 131 |
} |
| 132 |
|
| 133 |
// get helper class name to call |
| 134 |
$helper_class_name = strtolower($helper_class_data['class_name']); |
| 135 |
|
| 136 |
// ensure the plugin's helper class contains the helper class name we need |
| 137 |
if (!property_exists($plugin->helper, $helper_class_name)) |
| 138 |
{ |
| 139 |
return []; |
| 140 |
} |
| 141 |
|
| 142 |
// return the selected items from the helper class |
| 143 |
return $plugin->helper->$helper_class_name; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the class data |
| 148 |
* |
| 149 |
* @param string $type |
| 150 |
* |
| 151 |
* @return array |
| 152 |
*/ |
| 153 |
public static function getHelperClassData($path) |
| 154 |
{ |
| 155 |
if (!is_string($path)) |
| 156 |
{ |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
if (empty($path)) |
| 161 |
{ |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
// helper class in the dependent plugin |
| 166 |
// retrieve the name of the plugin from the given namespace |
| 167 |
$path_data = explode('\\', $path); |
| 168 |
$path_data = array_filter($path_data); |
| 169 |
$path_data = array_values($path_data); |
| 170 |
|
| 171 |
// get plugin (fpframework or plugin name); |
| 172 |
$plugin = ltrim($path_data[0], '\\'); |
| 173 |
$plugin = strtolower($plugin); |
| 174 |
|
| 175 |
// get class name |
| 176 |
$class_name = strtolower(end($path_data)); |
| 177 |
$class_name = str_replace('helper', '', $class_name); |
| 178 |
|
| 179 |
return [ |
| 180 |
'plugin' => $plugin, |
| 181 |
'class_name' => $class_name |
| 182 |
]; |
| 183 |
} |
| 184 |
} |