Skip to content

Commit 27c362f

Browse files
committed
Migrate the function to change the style to a command
1 parent 84269cd commit 27c362f

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace wcf\command\style;
4+
5+
use wcf\data\style\Style;
6+
use wcf\data\user\UserAction;
7+
use wcf\event\style\StyleChanged;
8+
use wcf\system\event\EventHandler;
9+
use wcf\system\style\StyleHandler;
10+
use wcf\system\WCF;
11+
12+
/**
13+
* Change the style for the current user.
14+
*
15+
* @author Olaf Braun
16+
* @copyright 2001-2025 WoltLab GmbH
17+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18+
* @since 6.3
19+
*/
20+
final class ChangeStyle
21+
{
22+
public function __construct(
23+
private readonly Style $style
24+
) {}
25+
26+
public function __invoke(): void
27+
{
28+
StyleHandler::getInstance()->changeStyle($this->style->styleID);
29+
if (StyleHandler::getInstance()->getStyle()->styleID !== $this->style->styleID) {
30+
// style could not be changed
31+
return;
32+
}
33+
34+
if (WCF::getUser()->userID) {
35+
$this->saveUserStyle($this->style->styleID, (bool)$this->style->isDefault);
36+
} else {
37+
$this->saveGuestStyle($this->style->styleID, (bool)$this->style->isDefault);
38+
}
39+
40+
$event = new StyleChanged($this->style);
41+
EventHandler::getInstance()->fire($event);
42+
}
43+
44+
private function saveUserStyle(int $styleID, bool $defaultStyle): void
45+
{
46+
(new UserAction([WCF::getUser()], 'update', [
47+
'data' => [
48+
'styleID' => $defaultStyle ? 0 : $styleID,
49+
],
50+
]))->executeAction();
51+
}
52+
53+
private function saveGuestStyle(int $styleID, bool $defaultStyle): void
54+
{
55+
if ($defaultStyle) {
56+
WCF::getSession()->unregister('styleID');
57+
} else {
58+
WCF::getSession()->register('styleID', $styleID);
59+
}
60+
}
61+
}

wcfsetup/install/files/lib/data/style/StyleAction.class.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace wcf\data\style;
44

55
use ParagonIE\ConstantTime\Hex;
6+
use wcf\command\style\ChangeStyle;
67
use wcf\data\AbstractDatabaseObjectAction;
78
use wcf\data\IToggleAction;
89
use wcf\data\TDatabaseObjectToggle;
9-
use wcf\data\user\UserAction;
1010
use wcf\system\exception\PermissionDeniedException;
1111
use wcf\system\image\ImageHandler;
1212
use wcf\system\request\LinkHandler;
@@ -520,6 +520,8 @@ public function copy()
520520
* Validates parameters to change user style.
521521
*
522522
* @return void
523+
*
524+
* @deprecated 6.3
523525
*/
524526
public function validateChangeStyle()
525527
{
@@ -533,27 +535,12 @@ public function validateChangeStyle()
533535
* Changes user style.
534536
*
535537
* @return void
538+
*
539+
* @deprecated 6.3 Use the `ChangeStyle` command instead.
536540
*/
537541
public function changeStyle()
538542
{
539-
StyleHandler::getInstance()->changeStyle($this->style->styleID);
540-
if (StyleHandler::getInstance()->getStyle()->styleID == $this->style->styleID) {
541-
if (WCF::getUser()->userID) {
542-
// set this as the permanent style
543-
$userAction = new UserAction([WCF::getUser()], 'update', [
544-
'data' => [
545-
'styleID' => $this->style->isDefault ? 0 : $this->style->styleID,
546-
],
547-
]);
548-
$userAction->executeAction();
549-
} else {
550-
if ($this->style->isDefault) {
551-
WCF::getSession()->unregister('styleID');
552-
} else {
553-
WCF::getSession()->register('styleID', $this->style->styleID);
554-
}
555-
}
556-
}
543+
(new ChangeStyle($this->style))();
557544
}
558545

559546
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace wcf\event\style;
4+
5+
use wcf\data\style\Style;
6+
use wcf\event\IPsr14Event;
7+
8+
/**
9+
* Indicates that a style has been changed for the current user.
10+
*
11+
* @author Olaf Braun
12+
* @copyright 2001-2025 WoltLab GmbH
13+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14+
* @since 6.3
15+
*/
16+
final class StyleChanged implements IPsr14Event
17+
{
18+
public function __construct(public readonly Style $style) {}
19+
}

0 commit comments

Comments
 (0)