From ddf9227dc48faf7effbf3bf263aa271f35d74c43 Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Thu, 13 Aug 2020 12:23:27 +0300
Subject: [PATCH] pluginhost: allow overriding default sort modes via
 HOOK_HEADLINES_CUSTOM_SORT_MAP etc

---
 classes/feeds.php      | 7 +++++++
 classes/pluginhost.php | 2 ++
 index.php              | 8 ++++++++
 3 files changed, 17 insertions(+)

diff --git a/classes/feeds.php b/classes/feeds.php
index 86dd750ec..eb08fc452 100755
--- a/classes/feeds.php
+++ b/classes/feeds.php
@@ -2352,6 +2352,13 @@ class Feeds extends Handler_Protected {
 				break;
 		}
 
+		if (!$query) {
+			foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINES_CUSTOM_SORT_OVERRIDE) as $p) {
+				list ($query, $skip_first_id) = $p->hook_headlines_custom_sort_override($order);
+
+				if ($query)	break;
+			}
+		}
 		return [$query, $skip_first_id];
 	}
 }
diff --git a/classes/pluginhost.php b/classes/pluginhost.php
index 0ab979c4b..4fec13000 100755
--- a/classes/pluginhost.php
+++ b/classes/pluginhost.php
@@ -66,6 +66,8 @@ class PluginHost {
 	const HOOK_FEED_TREE = 43;
 	const HOOK_IFRAME_WHITELISTED = 44;
 	const HOOK_ENCLOSURE_IMPORTED = 45;
+	const HOOK_HEADLINES_CUSTOM_SORT_MAP = 46;
+	const HOOK_HEADLINES_CUSTOM_SORT_OVERRIDE = 47;
 
 	const KIND_ALL = 1;
 	const KIND_SYSTEM = 2;
diff --git a/index.php b/index.php
index f1a8d0f0c..543d2b008 100644
--- a/index.php
+++ b/index.php
@@ -198,6 +198,14 @@
                 <option value="feed_dates"><?php echo __('Newest first') ?></option>
                 <option value="date_reverse"><?php echo __('Oldest first') ?></option>
                 <option value="title"><?php echo __('Title') ?></option>
+
+				<?php foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINES_CUSTOM_SORT_MAP) as $p) {
+					$sort_map = $p->hook_headlines_custom_sort_map();
+
+					foreach ($sort_map as $sort_value => $sort_title) {
+						print "<option value=\"" . htmlspecialchars($sort_value) . "\">$sort_title</option>";
+					}
+				} ?>
             </select>
 
             <div dojoType="fox.form.ComboButton" onclick="Feeds.catchupCurrent()">
-- 
GitLab