summaryrefslogtreecommitdiff
path: root/classes/PluginHost.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/PluginHost.php')
-rw-r--r--classes/PluginHost.php82
1 files changed, 42 insertions, 40 deletions
diff --git a/classes/PluginHost.php b/classes/PluginHost.php
index cc81bc9c0..5f1d97f53 100644
--- a/classes/PluginHost.php
+++ b/classes/PluginHost.php
@@ -23,8 +23,8 @@ class PluginHost {
/** @var array<string, array<string, mixed>> plugin name -> (potential profile array) -> key -> value */
private array $storage = [];
- /** @var array<int, array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}>> */
- private array $feeds = [];
+ /** @var array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}> */
+ private array $special_feeds = [];
/** @var array<string, Plugin> API method name, Plugin sender */
private array $api_methods = [];
@@ -759,45 +759,51 @@ class PluginHost {
}
}
- // Plugin feed functions are *EXPERIMENTAL*!
-
- // cat_id: only -1 (Feeds::CATEGORY_SPECIAL) is supported (Special)
- function add_feed(int $cat_id, string $title, string $icon, Plugin $sender): int {
-
- if (empty($this->feeds[$cat_id]))
- $this->feeds[$cat_id] = [];
+ /**
+ * Add a special (plugin-provided) feed
+ *
+ * @param int $cat_id only -1 (Feeds::CATEGORY_SPECIAL) is supported
+ * @return false|int false if the feed wasn't added (e.g. $cat_id wasn't Feeds::CATEGORY_SPECIAL),
+ * otherwise an integer "feed ID" that might change between executions
+ */
+ function add_feed(int $cat_id, string $title, string $icon, Plugin $sender): false|int {
+ if ($cat_id !== Feeds::CATEGORY_SPECIAL)
+ return false;
- $id = count($this->feeds[$cat_id]);
+ $id = count($this->special_feeds);
- array_push($this->feeds[$cat_id],
- ['id' => $id, 'title' => $title, 'sender' => $sender, 'icon' => $icon]);
+ $this->special_feeds[] = [
+ 'id' => $id,
+ 'title' => $title,
+ 'sender' => $sender,
+ 'icon' => $icon,
+ ];
return $id;
}
/**
+ * Get special (plugin-provided) feeds
+ *
+ * @param int $cat_id only -1 (Feeds::CATEGORY_SPECIAL) is supported
* @return array<int, array{'id': int, 'title': string, 'sender': Plugin, 'icon': string}>
*/
function get_feeds(int $cat_id) {
- return $this->feeds[$cat_id] ?? [];
+ return $cat_id === Feeds::CATEGORY_SPECIAL ? $this->special_feeds : [];
}
/**
- * convert feed_id (e.g. -129) to pfeed_id first
- *
- * @return (Plugin&IVirtualFeed)|null
+ * Get the Plugin handling a specific virtual feed.
+ *
+ * Convert feed_id (e.g. -129) to pfeed_id first.
+ *
+ * @return (Plugin&IVirtualFeed)|null a Plugin that implements IVirtualFeed, otherwise null
*/
function get_feed_handler(int $pfeed_id): ?Plugin {
- foreach ($this->feeds as $cat) {
- foreach ($cat as $feed) {
- if ($feed['id'] == $pfeed_id) {
- if (implements_interface($feed['sender'], 'IVirtualFeed')) {
- /** @var Plugin&IVirtualFeed $feed['sender'] */
- return $feed['sender'];
- } else {
- return null;
- }
- }
+ foreach ($this->special_feeds as $feed) {
+ if ($feed['id'] == $pfeed_id) {
+ /** @var Plugin&IVirtualFeed $feed['sender'] */
+ return implements_interface($feed['sender'], 'IVirtualFeed') ? $feed['sender'] : null;
}
}
return null;
@@ -851,14 +857,12 @@ class PluginHost {
*/
function get_method_url(Plugin $sender, string $method, array $params = []): string {
return Config::get_self_url() . "/backend.php?" .
- http_build_query(
- array_merge(
- [
- "op" => "pluginhandler",
- "plugin" => strtolower(get_class($sender)),
- "method" => $method
- ],
- $params));
+ http_build_query([
+ 'op' => 'pluginhandler',
+ 'plugin' => strtolower(get_class($sender)),
+ 'method' => $method,
+ ...$params,
+ ]);
}
// shortcut syntax (disabled for now)
@@ -880,12 +884,10 @@ class PluginHost {
function get_public_method_url(Plugin $sender, string $method, array $params = []): ?string {
if ($sender->is_public_method($method)) {
return Config::get_self_url() . "/public.php?" .
- http_build_query(
- array_merge(
- [
- "op" => strtolower(get_class($sender) . self::PUBLIC_METHOD_DELIMITER . $method),
- ],
- $params));
+ http_build_query([
+ 'op' => strtolower(get_class($sender) . self::PUBLIC_METHOD_DELIMITER . $method),
+ ...$params,
+ ]);
}
user_error("get_public_method_url: requested method '$method' of '" . get_class($sender) . "' is private.");
return null;