Skip to content

Commit 6e810cb

Browse files
authored
Merge pull request #9010 from Sesquipedalian/3.0/load_theme_options
[3.0] Fixes issues loading theme options for user profiles
2 parents 97cc74d + 7be4822 commit 6e810cb

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

Sources/Profile.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,28 +1140,37 @@ public function loadCustomFields(string $area = 'summary'): void
11401140
}
11411141

11421142
/**
1143-
* Loads the theme options for the member.
1143+
* Populates Utils::$context['member']['options'] with this member's theme
1144+
* options.
11441145
*
1145-
* @param bool $defaultSettings If true, we are loading default options.
1146+
* @param ?bool $default_only If true, only load options for the default
1147+
* theme, where "default theme" means whichever theme is used for guests.
1148+
* Default: false.
11461149
*/
1147-
public function loadThemeOptions(bool $defaultSettings = false)
1150+
public function loadThemeOptions(bool $default_only = false)
11481151
{
1149-
if (isset($_POST['default_options'])) {
1150-
$_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options'];
1152+
// Get this member's current theme options.
1153+
if ($default_only && $this->theme != (Config::$modSettings['theme_guests'] ?? 1)) {
1154+
$temp = $this->data['options'] ?? [];
1155+
parent::loadOptions($this->id, $default_only);
1156+
Utils::$context['member']['options'] = $this->data['options'];
1157+
$this->data['options'] = $temp;
1158+
} else {
1159+
Utils::$context['member']['options'] = $this->data['options'] ?? [];
11511160
}
11521161

1153-
Utils::$context['member']['options'] = $this->data['options'] ?? [];
1162+
// Overwrite their current options with anything that is being set to a
1163+
// new value.
1164+
$_POST['options'] = ($_POST['options'] ?? []) + ($_POST['default_options'] ?? []);
11541165

1155-
if (isset($_POST['options']) && \is_array($_POST['options'])) {
1156-
foreach ($_POST['options'] as $k => $v) {
1157-
Utils::$context['member']['options'][$k] = $v;
1158-
}
1166+
foreach ($_POST['options'] as $k => $v) {
1167+
Utils::$context['member']['options'][$k] = $v;
11591168
}
11601169

1161-
// Load up the default theme options for any missing.
1162-
parent::loadOptions(-1);
1170+
// If any theme options are still missing, set them to default values.
1171+
parent::loadOptions(-1, $default_only);
11631172

1164-
foreach (parent::$profiles[-1]['options'] as $k => $v) {
1173+
foreach (parent::$profiles[-1]['options'] ?? [] as $k => $v) {
11651174
if (!isset(Utils::$context['member']['options'][$k])) {
11661175
Utils::$context['member']['options'][$k] = $v;
11671176
}

Sources/User.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4774,23 +4774,52 @@ protected static function loadUserData(array $users, int $type = self::LOAD_BY_I
47744774
* Loads theme options for the given users.
47754775
*
47764776
* @param array|int $ids One or more user ID numbers.
4777+
* @param ?bool $default_only If true, only load options for the default
4778+
* theme, where "default theme" means whichever theme is used for guests.
4779+
* Default: false.
47774780
*/
4778-
protected static function loadOptions(array|int $ids): void
4781+
protected static function loadOptions(array|int $ids, bool $default_only = false): void
47794782
{
47804783
$ids = (array) $ids;
47814784

47824785
$request = Db::$db->query(
47834786
'SELECT id_member, id_theme, variable, value
47844787
FROM {db_prefix}themes
4785-
WHERE id_member IN ({array_int:ids})',
4788+
WHERE id_member IN ({array_int:ids})
4789+
ORDER BY id_theme',
47864790
[
47874791
'ids' => $ids,
47884792
],
47894793
);
47904794

47914795
while ($row = Db::$db->fetch_assoc($request)) {
4792-
self::$profiles[$row['id_member']]['options'][$row['variable']] = $row['value'];
4796+
// Which theme is used for guests?
4797+
$guest_theme = (int) (Config::$modSettings['theme_guests'] ?? 1);
4798+
4799+
// Which theme is this member using?
4800+
$user_theme = $default_only ? $guest_theme : (int) (self::$profiles[$row['id_member']]['id_theme'] ?? $guest_theme);
4801+
4802+
if (
4803+
// Rows are returned in ascending order by id_theme, so we start
4804+
// with theme 1's value and then overwrite as needed.
4805+
$row['id_theme'] == 1
4806+
// If the guest theme isn't theme 1, then overwrite the value
4807+
// from theme 1 with the value from the guest theme.
4808+
|| (
4809+
$row['id_theme'] == $guest_theme
4810+
// Special check needed here to ensure the guest theme does
4811+
// not overwrite the user's preferred theme. For example, if
4812+
// the guest theme is 5 but the user's theme is 2, then we
4813+
// want to keep the value from theme 2.
4814+
&& $guest_theme < $user_theme
4815+
)
4816+
// The value from the user's preferred theme takes precedence.
4817+
|| $row['id_theme'] == $user_theme
4818+
) {
4819+
self::$profiles[$row['id_member']]['options'][$row['variable']] = $row['value'];
4820+
}
47934821
}
4822+
47944823
Db::$db->free_result($request);
47954824
}
47964825

0 commit comments

Comments
 (0)