From 40f38fc87fe2993dfcc2c2953f0a073096e806e6 Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Fri, 15 Jan 2021 08:32:06 +0300
Subject: [PATCH] pluginhost: load plugin data automatically (also marks
 load_data method as private)

---
 classes/article.php        |  2 +-
 classes/diskcache.php      |  2 +-
 classes/handler/public.php |  4 ++--
 classes/pluginhost.php     | 11 +++++++++--
 classes/pref/prefs.php     |  2 +-
 classes/rssutils.php       |  4 ++--
 classes/userhelper.php     |  4 ++--
 7 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/classes/article.php b/classes/article.php
index 5527b7253..3a58f4576 100755
--- a/classes/article.php
+++ b/classes/article.php
@@ -29,7 +29,7 @@ class Article extends Handler_Protected {
 		if (!$content) {
 			$pluginhost = new PluginHost();
 			$pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid);
-			$pluginhost->load_data();
+			//$pluginhost->load_data();
 
 			foreach ($pluginhost->get_hooks(PluginHost::HOOK_GET_FULL_TEXT) as $p) {
 				$extracted_content = $p->hook_get_full_text($url);
diff --git a/classes/diskcache.php b/classes/diskcache.php
index c56dc6f14..daa171bf6 100644
--- a/classes/diskcache.php
+++ b/classes/diskcache.php
@@ -395,7 +395,7 @@ class DiskCache {
 			$tmppluginhost = new PluginHost();
 
 			$tmppluginhost->load(PLUGINS, PluginHost::KIND_SYSTEM);
-			$tmppluginhost->load_data();
+			//$tmppluginhost->load_data();
 
 			foreach ($tmppluginhost->get_hooks(PluginHost::HOOK_SEND_LOCAL_FILE) as $plugin) {
 				if ($plugin->hook_send_local_file($filename)) return true;
diff --git a/classes/handler/public.php b/classes/handler/public.php
index 6feb7df79..7fbad5530 100755
--- a/classes/handler/public.php
+++ b/classes/handler/public.php
@@ -45,7 +45,7 @@ class Handler_Public extends Handler {
 			$tmppluginhost = new PluginHost();
 			$tmppluginhost->load(PLUGINS, PluginHost::KIND_ALL);
 			$tmppluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid);
-			$tmppluginhost->load_data();
+			//$tmppluginhost->load_data();
 
 			$handler = $tmppluginhost->get_feed_handler(
 				PluginHost::feed_to_pfeed_id($feed));
@@ -1246,7 +1246,7 @@ class Handler_Public extends Handler {
 		$method = clean($_REQUEST["pmethod"]);
 
 		$host->load($plugin_name, PluginHost::KIND_USER, 0);
-		$host->load_data();
+		//$host->load_data();
 
 		$plugin = $host->get_plugin($plugin_name);
 
diff --git a/classes/pluginhost.php b/classes/pluginhost.php
index 3ff658918..2ce20be86 100755
--- a/classes/pluginhost.php
+++ b/classes/pluginhost.php
@@ -14,6 +14,7 @@ class PluginHost {
 	private $plugin_actions = array();
 	private $owner_uid;
 	private $last_registered;
+	private $data_loaded;
 	private static $instance;
 
 	const API_VERSION = 2;
@@ -268,6 +269,8 @@ class PluginHost {
 				}
 			}
 		}
+
+		$this->load_data();
 	}
 
 	function is_system($plugin) {
@@ -352,8 +355,8 @@ class PluginHost {
 		}
 	}
 
-	function load_data() {
-		if ($this->owner_uid)  {
+	private function load_data() {
+		if (get_schema_version() > 100 && $this->owner_uid && !$this->data_loaded)  {
 			$sth = $this->pdo->prepare("SELECT name, content FROM ttrss_plugin_storage
 				WHERE owner_uid = ?");
 			$sth->execute([$this->owner_uid]);
@@ -361,6 +364,8 @@ class PluginHost {
 			while ($line = $sth->fetch()) {
 				$this->storage[$line["name"]] = unserialize($line["content"]);
 			}
+
+			$this->data_loaded = true;
 		}
 	}
 
@@ -411,6 +416,8 @@ class PluginHost {
 	function get($sender, $name, $default_value = false) {
 		$idx = get_class($sender);
 
+		$this->load_data();
+
 		if (isset($this->storage[$idx][$name])) {
 			return $this->storage[$idx][$name];
 		} else {
diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php
index bce3c171b..55a15efb8 100644
--- a/classes/pref/prefs.php
+++ b/classes/pref/prefs.php
@@ -911,7 +911,7 @@ class Pref_Prefs extends Handler_Protected {
 
 		$tmppluginhost = new PluginHost();
 		$tmppluginhost->load_all($tmppluginhost::KIND_ALL, $_SESSION["uid"], true);
-		$tmppluginhost->load_data(true);
+		//$tmppluginhost->load_data(true);
 
 		foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
 			$about = $plugin->about();
diff --git a/classes/rssutils.php b/classes/rssutils.php
index 857bc2948..dc80dd965 100755
--- a/classes/rssutils.php
+++ b/classes/rssutils.php
@@ -276,7 +276,7 @@ class RSSUtils {
 
 			$pluginhost->load(PLUGINS, PluginHost::KIND_ALL);
 			$pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid);
-			$pluginhost->load_data();
+			//$pluginhost->load_data();
 
 			$basic_info = array();
 			foreach ($pluginhost->get_hooks(PluginHost::HOOK_FEED_BASIC_INFO) as $plugin) {
@@ -403,7 +403,7 @@ class RSSUtils {
 
 		$pluginhost->load(PLUGINS, PluginHost::KIND_ALL);
 		$pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid);
-		$pluginhost->load_data();
+		//$pluginhost->load_data();
 
 		$rss_hash = false;
 
diff --git a/classes/userhelper.php b/classes/userhelper.php
index 6a80aed2b..76bb338d4 100644
--- a/classes/userhelper.php
+++ b/classes/userhelper.php
@@ -80,9 +80,9 @@ class UserHelper {
 
 			$pluginhost->load($plugins, PluginHost::KIND_USER, $owner_uid);
 
-			if (get_schema_version() > 100) {
+			/*if (get_schema_version() > 100) {
 				$pluginhost->load_data();
-			}
+			}*/
 		}
 	}
 
-- 
GitLab