From 5a87ccf0261733096f634cb03314604891e5f48a Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Sun, 25 Jul 2021 19:39:10 +0000
Subject: [PATCH] Getter/Setter for theme info

---
 doc/themes.md                         |  9 +++----
 mod/events.php                        |  4 ++--
 src/App.php                           | 34 ++++++++++++++++++++++++++-
 src/Content/OEmbed.php                |  8 +++----
 src/Content/Text/BBCode.php           |  4 ++--
 src/Core/Renderer.php                 |  2 +-
 src/Core/Theme.php                    |  4 ++--
 src/Model/Item.php                    |  4 ++--
 src/Module/BaseProfile.php            |  2 +-
 tests/src/Content/SmiliesTest.php     |  4 ++--
 tests/src/Content/Text/BBCodeTest.php |  4 ++--
 view/theme/frio/theme.php             |  4 ++--
 view/theme/vier/theme.php             |  2 +-
 13 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/doc/themes.md b/doc/themes.md
index b7bb2e226..786f3d5af 100644
--- a/doc/themes.md
+++ b/doc/themes.md
@@ -170,9 +170,8 @@ The content of this file should be something like
     use Friendica\App;
     
     function duepuntozero_lr_init(App $a) {
-        $a-> theme_info = array(
-            'extends' => 'duepuntozero'.
-        );
+        $a->setThemeInfoValue('extends', 'duepuntozero');
+
         $a->set_template_engine('smarty3');
         /* and more stuff e.g. the JavaScript function for the header */
     }
@@ -272,9 +271,7 @@ If you like to use another templating engine, please implement it.
 
 When you want to inherit stuff from another theme you have to *announce* this in the theme_info:
 
-    $a->theme_info = array(
-      'extends' => 'duepuntozero',
-    );
+    $a->setThemeInfoValue('extends', 'duepuntozero');
 
 which declares *duepuntozero* as parent of the theme.
 
diff --git a/mod/events.php b/mod/events.php
index 67666d670..f20d173dc 100644
--- a/mod/events.php
+++ b/mod/events.php
@@ -255,7 +255,7 @@ function events_content(App $a)
 		);
 	}
 
-	if ($a->theme_info['events_in_profile']) {
+	if ($a->getThemeInfoValue('events_in_profile')) {
 		Nav::setSelected('home');
 	} else {
 		Nav::setSelected('events');
@@ -279,7 +279,7 @@ function events_content(App $a)
 	$o = '';
 	$tabs = '';
 	// tabs
-	if ($a->theme_info['events_in_profile']) {
+	if ($a->getThemeInfoValue('events_in_profile')) {
 		$tabs = BaseProfile::getTabsHTML($a, 'events', true, $a->user);
 	}
 
diff --git a/src/App.php b/src/App.php
index a25c9a9ea..c9aafe14e 100644
--- a/src/App.php
+++ b/src/App.php
@@ -60,7 +60,7 @@ class App
 
 	// Allow themes to control internal parameters
 	// by changing App values in theme.php
-	public $theme_info = [
+	private $theme_info = [
 		'videowidth'        => 425,
 		'videoheight'       => 350,
 		'events_in_profile' => true
@@ -189,21 +189,53 @@ class App
 		return $this->timezone;
 	}
 
+	/**
+	 * Set workerqueue information
+	 *
+	 * @param array $queue 
+	 * @return void 
+	 */
 	public function setQueue(array $queue)
 	{
 		$this->queue = $queue;
 	}
 
+	/**
+	 * Fetch workerqueue information
+	 *
+	 * @return array 
+	 */
 	public function getQueue()
 	{
 		return $this->queue ?? [];
 	}
 
+	/**
+	 * Fetch a specific workerqueue field
+	 *
+	 * @param string $index 
+	 * @return mixed 
+	 */
 	public function getQueueValue(string $index)
 	{
 		return $this->queue[$index] ?? null;
 	}
 
+	public function setThemeInfoValue(string $index, $value)
+	{
+		$this->theme_info[$index] = $value;
+	}
+
+	public function getThemeInfo()
+	{
+		return $this->theme_info;
+	}
+
+	public function getThemeInfoValue(string $index, $default = null)
+	{
+		return $this->theme_info[$index] ?? $default;
+	}
+
 	/**
 	 * Returns the current config cache of this node
 	 *
diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php
index 723230873..5390a4bc5 100644
--- a/src/Content/OEmbed.php
+++ b/src/Content/OEmbed.php
@@ -73,9 +73,9 @@ class OEmbed
 
 		$a = DI::app();
 
-		$cache_key = 'oembed:' . $a->theme_info['videowidth'] . ':' . $embedurl;
+		$cache_key = 'oembed:' . $a->getThemeInfoValue('videowidth') . ':' . $embedurl;
 
-		$condition = ['url' => Strings::normaliseLink($embedurl), 'maxwidth' => $a->theme_info['videowidth']];
+		$condition = ['url' => Strings::normaliseLink($embedurl), 'maxwidth' => $a->getThemeInfoValue('videowidth')];
 		$oembed_record = DBA::selectFirst('oembed', ['content'], $condition);
 		if (DBA::isResult($oembed_record)) {
 			$json_string = $oembed_record['content'];
@@ -111,7 +111,7 @@ class OEmbed
 							// but their OEmbed endpoint is only accessible by HTTPS ¯\_(ツ)_/¯
 							$href = str_replace(['http://www.youtube.com/', 'http://player.vimeo.com/'],
 								['https://www.youtube.com/', 'https://player.vimeo.com/'], $href);
-							$result = DI::httpRequest()->fetchFull($href . '&maxwidth=' . $a->theme_info['videowidth']);
+							$result = DI::httpRequest()->fetchFull($href . '&maxwidth=' . $a->getThemeInfoValue('videowidth'));
 							if ($result->getReturnCode() === 200) {
 								$json_string = $result->getBody();
 								break;
@@ -132,7 +132,7 @@ class OEmbed
 			if (!empty($oembed->type) && $oembed->type != 'error') {
 				DBA::insert('oembed', [
 					'url' => Strings::normaliseLink($embedurl),
-					'maxwidth' => $a->theme_info['videowidth'],
+					'maxwidth' => $a->getThemeInfoValue('videowidth'),
 					'content' => $json_string,
 					'created' => DateTimeFormat::utcNow()
 				], Database::INSERT_UPDATE);
diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php
index d03f83ffd..a4502b646 100644
--- a/src/Content/Text/BBCode.php
+++ b/src/Content/Text/BBCode.php
@@ -1729,7 +1729,7 @@ class BBCode
 				$text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
 
 				if ($try_oembed) {
-					$text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->theme_info['videowidth'] . '" height="' . $a->theme_info['videoheight'] . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $text);
+					$text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '<iframe width="' . $a->getThemeInfoValue('videowidth') . '" height="' . $a->getThemeInfoValue('videoheight') . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $text);
 				} else {
 					$text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
 						'<a href="https://www.youtube.com/watch?v=$1" target="_blank" rel="noopener noreferrer">https://www.youtube.com/watch?v=$1</a>', $text);
@@ -1744,7 +1744,7 @@ class BBCode
 				$text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
 
 				if ($try_oembed) {
-					$text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->theme_info['videowidth'] . '" height="' . $a->theme_info['videoheight'] . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $text);
+					$text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '<iframe width="' . $a->getThemeInfoValue('videowidth') . '" height="' . $a->videoheight . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $text);
 				} else {
 					$text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
 						'<a href="https://vimeo.com/$1" target="_blank" rel="noopener noreferrer">https://vimeo.com/$1</a>', $text);
diff --git a/src/Core/Renderer.php b/src/Core/Renderer.php
index bb404965e..b6b443d50 100644
--- a/src/Core/Renderer.php
+++ b/src/Core/Renderer.php
@@ -166,7 +166,7 @@ class Renderer
 			} else {
 				$a = DI::app();
 				$class = self::$template_engines[$template_engine];
-				$obj = new $class($a->getCurrentTheme(), $a->theme_info);
+				$obj = new $class($a->getCurrentTheme(), $a->getThemeInfo());
 				self::$template_engine_instance[$template_engine] = $obj;
 				return $obj;
 			}
diff --git a/src/Core/Theme.php b/src/Core/Theme.php
index e3b7f4bde..6eb1587be 100644
--- a/src/Core/Theme.php
+++ b/src/Core/Theme.php
@@ -214,7 +214,7 @@ class Theme
 
 		$theme = $a->getCurrentTheme();
 
-		$parent = Strings::sanitizeFilePathItem($a->theme_info['extends'] ?? $theme);
+		$parent = Strings::sanitizeFilePathItem($a->getThemeInfoValue('extends', $theme));
 
 		$paths = [
 			"view/theme/$theme/$file",
@@ -271,7 +271,7 @@ class Theme
 		$theme = Strings::sanitizeFilePathItem($theme);
 
 		$a = DI::app();
-		$base_theme = $a->theme_info['extends'] ?? '';
+		$base_theme = $a->getThemeInfoValue('extends') ?? '';
 
 		if (file_exists("view/theme/$theme/config.php")) {
 			return "view/theme/$theme/config.php";
diff --git a/src/Model/Item.php b/src/Model/Item.php
index 327c60d5c..ad5ffdba6 100644
--- a/src/Model/Item.php
+++ b/src/Model/Item.php
@@ -2802,8 +2802,8 @@ class Item
 		}
 
 		// Replace friendica image url size with theme preference.
-		if (!empty($a->theme_info['item_image_size'])) {
-			$ps = $a->theme_info['item_image_size'];
+		if (!empty($a->getThemeInfoValue('item_image_size'))) {
+			$ps = $a->getThemeInfoValue('item_image_size');
 			$s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
 		}
 
diff --git a/src/Module/BaseProfile.php b/src/Module/BaseProfile.php
index eee903650..dca88f01c 100644
--- a/src/Module/BaseProfile.php
+++ b/src/Module/BaseProfile.php
@@ -81,7 +81,7 @@ class BaseProfile extends BaseModule
 		];
 
 		// the calendar link for the full featured events calendar
-		if ($is_owner && $a->theme_info['events_in_profile']) {
+		if ($is_owner && $a->getThemeInfoValue('events_in_profile')) {
 			$tabs[] = [
 				'label' => DI::l10n()->t('Events'),
 				'url'   => DI::baseUrl() . '/events',
diff --git a/tests/src/Content/SmiliesTest.php b/tests/src/Content/SmiliesTest.php
index 803d5d6f8..e2d35183f 100644
--- a/tests/src/Content/SmiliesTest.php
+++ b/tests/src/Content/SmiliesTest.php
@@ -24,8 +24,8 @@ class SmiliesTest extends MockedTest
 		parent::setUp();
 		$this->setUpVfsDir();
 		$this->mockApp($this->root);
-		$this->app->theme_info['videowidth']  = 425;
-		$this->app->theme_info['videoheight'] = 350;
+		$this->app->setThemeInfoValue('videowidth', 425);
+		$this->app->setThemeInfoValue('videoheight', 350);
 		$this->configMock->shouldReceive('get')
 			->with('system', 'no_smilies')
 			->andReturn(false);
diff --git a/tests/src/Content/Text/BBCodeTest.php b/tests/src/Content/Text/BBCodeTest.php
index b9c26ede0..460916b6f 100644
--- a/tests/src/Content/Text/BBCodeTest.php
+++ b/tests/src/Content/Text/BBCodeTest.php
@@ -40,8 +40,8 @@ class BBCodeTest extends MockedTest
 		parent::setUp();
 		$this->setUpVfsDir();
 		$this->mockApp($this->root);
-		$this->app->theme_info['videowidth']  = 425;
-		$this->app->theme_info['videoheight'] = 350;
+		$this->app->setThemeInfoValue('videowidth', 425);
+		$this->app->setThemeInfoValue('videoheight', 350);
 		$this->configMock->shouldReceive('get')
 			->with('system', 'remove_multiplicated_lines')
 			->andReturn(false);
diff --git a/view/theme/frio/theme.php b/view/theme/frio/theme.php
index d5441691e..2f6542d30 100644
--- a/view/theme/frio/theme.php
+++ b/view/theme/frio/theme.php
@@ -33,8 +33,8 @@ function frio_init(App $a)
 	$frio = 'view/theme/frio';
 
 	// disable the events module link in the profile tab
-	$a->theme_info['events_in_profile'] = false;
-	$a->theme_info['videowidth']        = 622;
+	$a->setThemeInfoValue('events_in_profile', false);
+	$a->setThemeInfoValue('videowidth', 622);
 
 	Renderer::setActiveTemplateEngine('smarty3');
 
diff --git a/view/theme/vier/theme.php b/view/theme/vier/theme.php
index 13caae98e..d28c8a325 100644
--- a/view/theme/vier/theme.php
+++ b/view/theme/vier/theme.php
@@ -21,7 +21,7 @@ use Friendica\Util\Strings;
 
 function vier_init(App $a)
 {
-	$a->theme_info['events_in_profile'] = false;
+	$a->setThemeInfoValue('events_in_profile', false);
 
 	Renderer::setActiveTemplateEngine('smarty3');