• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dragomano / Light-Portal / 19949548504

05 Dec 2025 01:26AM UTC coverage: 51.562% (+0.5%) from 51.086%
19949548504

push

github

web-flow
Merge pull request #313 from dragomano/fixes

Update to 3.0 beta 2

15 of 56 new or added lines in 15 files covered. (26.79%)

4 existing lines in 2 files now uncovered.

5660 of 10977 relevant lines covered (51.56%)

4.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.56
/src/Sources/LightPortal/Hooks/MenuButtons.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * @package Light Portal
5
 * @link https://dragomano.ru/mods/light-portal
6
 * @author Bugo <bugo@dragomano.ru>
7
 * @copyright 2019-2025 Bugo
8
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
9
 *
10
 * @version 3.0
11
 */
12

13
namespace LightPortal\Hooks;
14

15
use Bugo\Compat\Config;
16
use Bugo\Compat\Lang;
17
use Bugo\Compat\User;
18
use Bugo\Compat\Utils;
19
use LightPortal\Actions\Block;
20
use LightPortal\Enums\Action;
21
use LightPortal\Enums\Permission;
22
use LightPortal\Repositories\PageRepositoryInterface;
23
use LightPortal\Utils\Setting;
24
use LightPortal\Utils\Str;
25
use LightPortal\Utils\Traits\HasBreadcrumbs;
26
use LightPortal\Utils\Traits\HasPortalSql;
27

28
use function LightPortal\app;
29

30
use const LP_ACTION;
31
use const LP_PAGE_URL;
32

33
if (! defined('SMF'))
1✔
34
        die('No direct access...');
×
35

36
class MenuButtons
37
{
38
        use HasCommonChecks;
39
        use HasBreadcrumbs;
40
        use HasPortalSql;
41

42
        public function __invoke(array &$buttons): void
43
        {
44
                if ($this->isPortalCanBeLoaded() === false)
×
45
                        return;
×
46

47
                app(Block::class)->show();
×
48

49
                /* @uses template_lp_custom_above, template_lp_custom_below */
50
                Utils::$context['template_layers'][] = 'lp_custom';
×
51

52
                $this->prepareAdminButtons($buttons);
×
53
                $this->prepareModerationButtons($buttons);
×
54
                $this->preparePageButtons($buttons);
×
55
                $this->showDebugInfo();
×
56

57
                if (empty(Config::$modSettings['lp_frontpage_mode']))
×
58
                        return;
×
59

60
                $this->preparePortalButtons($buttons);
×
61
                $this->fixCanonicalUrl();
×
62
                $this->fixLinktree();
×
63
        }
64

65
        /**
66
         * Display "Portal settings" in Main Menu => Admin
67
         *
68
         * Отображаем "Настройки портала" в Главном меню => Админка
69
         */
70
        protected function prepareAdminButtons(array &$buttons): void
71
        {
72
                if (Utils::$context['user']['is_admin'] === false)
×
73
                        return;
×
74

75
                $counter = 0;
×
76
                foreach (array_keys($buttons['admin']['sub_buttons']) as $area) {
×
77
                        $counter++;
×
78

79
                        if ($area === 'featuresettings')
×
80
                                break;
×
81
                }
82

83
                $buttons['admin']['sub_buttons'] = array_merge(
×
84
                        array_slice($buttons['admin']['sub_buttons'], 0, $counter, true),
×
85
                        [
×
86
                                'portal_settings' => [
×
87
                                        'title'       => Lang::$txt['lp_settings'],
×
88
                                        'href'        => Config::$scripturl . '?action=admin;area=lp_settings',
×
89
                                        'show'        => true,
×
90
                                        'sub_buttons' => [
×
91
                                                'blocks'  => [
×
92
                                                        'title' => Lang::$txt['lp_blocks'],
×
93
                                                        'href'  => Config::$scripturl . '?action=admin;area=lp_blocks',
×
94
                                                        'amt'   => Utils::$context['lp_quantities']['active_blocks'],
×
95
                                                        'show'  => true,
×
96
                                                ],
×
97
                                                'pages'   => [
×
98
                                                        'title' => Lang::$txt['lp_pages'],
×
99
                                                        'href'  => Config::$scripturl . '?action=admin;area=lp_pages',
×
100
                                                        'amt'   => Utils::$context['lp_quantities']['active_pages'],
×
101
                                                        'show'  => true,
×
102
                                                ],
×
103
                                                'categories'   => [
×
104
                                                        'title' => Lang::$txt['lp_categories'],
×
105
                                                        'href'  => Config::$scripturl . '?action=admin;area=lp_categories',
×
106
                                                        'amt'   => Utils::$context['lp_quantities']['active_categories'],
×
107
                                                        'show'  => true,
×
108
                                                ],
×
109
                                                'tags'   => [
×
110
                                                        'title' => Lang::$txt['lp_tags'],
×
111
                                                        'href'  => Config::$scripturl . '?action=admin;area=lp_tags',
×
112
                                                        'amt'   => Utils::$context['lp_quantities']['active_tags'],
×
113
                                                        'show'  => true,
×
114
                                                ],
×
115
                                                'plugins' => [
×
116
                                                        'title'   => Lang::$txt['lp_plugins'],
×
117
                                                        'href'    => Config::$scripturl . '?action=admin;area=lp_plugins',
×
118
                                                        'amt'     => count(Setting::getEnabledPlugins()),
×
119
                                                        'show'    => true,
×
120
                                                        'is_last' => true,
×
121
                                                ],
×
122
                                        ],
×
123
                                ],
×
124
                        ],
×
125
                        array_slice($buttons['admin']['sub_buttons'], $counter, null, true)
×
126
                );
×
127
        }
128

129
        protected function prepareModerationButtons(array &$buttons): void
130
        {
131
                if (! User::$me->allowedTo('light_portal_manage_pages_any'))
×
132
                        return;
×
133

134
                $buttons['moderate']['show'] = true;
×
135

136
                $buttons['moderate']['sub_buttons'] = array_merge(
×
137
                        [
×
138
                                'lp_pages' => [
×
139
                                        'title' => Lang::$txt['lp_pages_unapproved'],
×
140
                                        'href'  => Config::$scripturl . '?action=admin;area=lp_pages;sa=main;moderate',
×
141
                                        'amt'   => Utils::$context['lp_quantities']['unapproved_pages'],
×
142
                                        'show'  => true,
×
143
                                ],
×
144
                        ],
×
145
                        (array) $buttons['moderate']['sub_buttons']
×
146
                );
×
147
        }
148

149
        protected function preparePageButtons(array &$buttons): void
150
        {
151
                if (empty(Utils::$context['lp_menu_pages'] = app(PageRepositoryInterface::class)->getMenuItems()))
×
152
                        return;
×
153

154
                $pageButtons = [];
×
155
                foreach (Utils::$context['lp_menu_pages'] as $item) {
×
156
                        $pageButtons['portal_page_' . $item['slug']] = [
×
157
                                'title' => (
×
158
                                        $item['icon']
×
159
                                                ? Str::html('span', ['class' => 'portal_menu_icons ' . $item['icon']]) . ' '
×
160
                                                : ''
×
161
                                        ) . $item['title'],
×
162
                                'href'  => LP_PAGE_URL . $item['slug'],
×
163
                                'icon'  => '" style="display: none"></span><span',
×
164
                                'show'  => Permission::canViewItem($item['permissions']),
×
165
                        ];
×
166
                }
167

168
                $counter = -1;
×
169
                foreach (array_keys($buttons) as $area) {
×
170
                        $counter++;
×
171

172
                        if ($area === 'admin')
×
173
                                break;
×
174
                }
175

176
                $buttons = array_merge(
×
177
                        array_slice($buttons, 0, $counter, true),
×
178
                        empty(Config::$modSettings['lp_menu_separate_subsection']) ? $pageButtons : [
×
179
                                'lp_pages' => [
×
180
                                        'title'       => $this->getPageSubsectionTitle(),
×
181
                                        'href'        => Config::$modSettings['lp_menu_separate_subsection_href'] ?? Config::$scripturl,
×
182
                                        'icon'        => 'topics_replies',
×
183
                                        'show'        => User::$me->allowedTo('light_portal_view'),
×
184
                                        'sub_buttons' => $pageButtons,
×
185
                                ]
×
186
                        ],
×
187
                        array_slice($buttons, $counter, null, true)
×
188
                );
×
189
        }
190

191
        protected function preparePortalButtons(array &$buttons): void
192
        {
193
                // Display "Portal" item in Main Menu
194
                $buttons = array_merge([
×
195
                        LP_ACTION => [
×
NEW
196
                                'title'   => Lang::$txt['lp_portal'],
×
NEW
197
                                'href'    => Config::$scripturl,
×
NEW
198
                                'icon'    => Action::HOME->value,
×
NEW
199
                                'show'    => true,
×
NEW
200
                                'is_last' => Utils::$context['right_to_left'],
×
201
                        ],
×
202
                ], $buttons);
×
203

204
                // "Forum"
205
                $buttons[Action::HOME->value]['title'] = Lang::$txt['lp_forum'];
×
206
                $buttons[Action::HOME->value]['href']  = Config::$scripturl . '?action=forum';
×
207
                $buttons[Action::HOME->value]['icon']  = 'im_on';
×
208

209
                // Standalone mode
210
                if (empty(Config::$modSettings['lp_standalone_mode']))
×
211
                        return;
×
212

213
                $buttons[LP_ACTION]['title']   = Lang::$txt['lp_portal'];
×
214
                $buttons[LP_ACTION]['href']    = Config::$modSettings['lp_standalone_url'] ?: Config::$scripturl;
×
215
                $buttons[LP_ACTION]['icon']    = Action::HOME->value;
×
216
                $buttons[LP_ACTION]['is_last'] = Utils::$context['right_to_left'];
×
217

NEW
218
                $href = Config::$modSettings['lp_standalone_url'] ? Config::$scripturl : Config::$scripturl . '?action=forum';
×
219

220
                $buttons = array_merge(
×
221
                        array_slice($buttons, 0, 2, true),
×
222
                        [
×
223
                                Action::FORUM->value => [
×
NEW
224
                                        'title' => Lang::$txt['lp_forum'],
×
NEW
225
                                        'href'  => $href,
×
NEW
226
                                        'icon'  => 'im_on',
×
NEW
227
                                        'show'  => true,
×
228
                                ],
×
229
                        ],
×
230
                        array_slice($buttons, 2, null, true)
×
231
                );
×
232

233
                $this->unsetDisabledActions($buttons);
×
234
        }
235

236
        protected function showDebugInfo(): void
237
        {
238
                if (
239
                        empty(Utils::$context['user']['is_admin'])
×
240
                        || empty(Utils::$context['template_layers'])
×
241
                        || $this->request()->is('devtools')
×
242
                        || $this->request()->is('xmlhttp')
×
243
                        || $this->request()->has('backtrace')
×
244
                        || $this->request()->has('file')
×
245
                ) {
246
                        return;
×
247
                }
248

249
                if (! empty(Config::$modSettings['lp_show_debug_info'])) {
×
250
                        Utils::$context['lp_load_page_stats'] = Lang::getTxt('lp_load_page_stats', [
×
251
                                Lang::getTxt('lp_seconds_set', [
×
252
                                        'seconds' => microtime(true) - Utils::$context['lp_load_time'],
×
253
                                ]),
×
254
                        ]);
×
255
                }
256

257
                if (empty(Config::$modSettings['lp_show_portal_queries']))
×
258
                        return;
×
259

260
                $sql = $this->getPortalSql();
×
261
                $profiler = $sql->getAdapter()->getProfiler();
×
262

263
                Utils::$context['lp_portal_queries'] = $profiler->getProfiles();
×
264
        }
265

266
        protected function fixCanonicalUrl(): void
267
        {
268
                if ($this->request()->is(Action::FORUM->value)) {
×
269
                        Utils::$context['canonical_url'] = Config::$scripturl . '?action=forum';
×
270
                }
271
        }
272

273
        protected function fixLinktree(): void
274
        {
275
                $linkTree = $this->breadcrumbs()->getByIndex(1);
×
276

277
                if (
278
                        $this->request()->hasNot('c')
×
279
                        && empty(Utils::$context['current_board'])
×
280
                        || empty($linkTree)
×
281
                        || empty($linkTree['url'])
×
282
                ) {
283
                        return;
×
284
                }
285

286
                $oldUrl = explode('#', $linkTree['url']);
×
287

288
                if (empty($oldUrl[1]))
×
289
                        return;
×
290

291
                $this->breadcrumbs()->update(1, 'url', Config::$scripturl . '?action=forum#' . $oldUrl[1]);
×
292
        }
293

294
        private function getPageSubsectionTitle(): string
295
        {
296
                if (empty($title = Config::$modSettings['lp_menu_separate_subsection_title'] ?? '')) {
×
297
                        return Lang::tokenTxtReplace('{lp_pages}');
×
298
                }
299

300
                return Lang::tokenTxtReplace($title);
×
301
        }
302
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc