Skip to content

Commit 1e8eaca

Browse files
committed
Move PWA icons to unique images dir
1 parent 024252f commit 1e8eaca

10 files changed

+131
-18
lines changed

controller/manifest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ public function handle(): JsonResponse
8282
{
8383
$manifest['icons'] = [
8484
[
85-
'src' => $board_url . '/' . $this->config['icons_path'] . '/' . $this->config['pwa_icon_small'],
85+
'src' => $board_url . '/' . ext::PWA_ICON_DIR . '/' . $this->config['pwa_icon_small'],
8686
'sizes' => '192x192',
8787
'type' => 'image/png'
8888
],
8989
[
90-
'src' => $board_url . '/' . $this->config['icons_path'] . '/' . $this->config['pwa_icon_large'],
90+
'src' => $board_url . '/' . ext::PWA_ICON_DIR . '/' . $this->config['pwa_icon_large'],
9191
'sizes' => '512x512',
9292
'type' => 'image/png'
9393
]

event/listener.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function pwa_manifest()
144144
{
145145
$this->template->assign_vars([
146146
'U_MANIFEST_URL' => $this->controller_helper->route('phpbb_webpushnotifications_manifest_controller'),
147-
'U_TOUCH_ICON' => $this->config['pwa_icon_small'],
147+
'U_TOUCH_ICON' => $this->config['pwa_icon_small'] ? ext::PWA_ICON_DIR . '/' . $this->config['pwa_icon_small'] : null,
148148
'SHORT_SITE_NAME' => $this->config['pwa_short_name'] ?: $this->trim_shortname($this->config['sitename']),
149149
]);
150150
}
@@ -201,7 +201,7 @@ public function acp_pwa_allow_emoji($event)
201201
*/
202202
public function pwa_icon_name($value, $key)
203203
{
204-
return $this->config['icons_path'] . '/<input id="' . $key . '" type="text" size="40" maxlength="255" name="config[' . $key . ']" value="' . $value . '">';
204+
return ext::PWA_ICON_DIR . '/<input id="' . $key . '" type="text" size="40" maxlength="255" name="config[' . $key . ']" value="' . $value . '">';
205205
}
206206

207207
/**
@@ -272,7 +272,7 @@ public function validate_pwa_options($event)
272272
}
273273

274274
// Check if image is valid
275-
$image = $this->root_path . $this->config['icons_path'] . '/' . $value;
275+
$image = $this->root_path . ext::PWA_ICON_DIR . '/' . $value;
276276
$image_info = $this->imagesize->getImageSize($image);
277277
if ($image_info !== false)
278278
{

ext.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
class ext extends \phpbb\extension\base
1717
{
18+
/**
19+
* Location for storing PWA touch/app icons.
20+
*/
21+
public const PWA_ICON_DIR = 'images/site_icons';
22+
1823
/**
1924
* Require phpBB 3.3.12 due to new template and core events.
2025
*/

language/en/info_acp_webpushnotifications.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
'ACP_WEBPUSH_REMOVE_WARNING' => 'Web Push Notifications are now built into phpBB',
4343
'ACP_WEBPUSH_REMOVE_NOTICE' => 'The extension “phpBB Browser Push Notifications” is no longer needed and should be uninstalled and removed.<br>All settings and user preferences associated with the extension will be migrated into phpBB’s built-in push notifications when you uninstall the extension.',
4444
'LOG_CONFIG_WEBPUSH' => '<strong>Altered Web Push settings</strong>',
45-
'LOG_WEBPUSH_MESSAGE_FAIL' => '<strong>Web Push message could not be sent:</strong> %s',
46-
'LOG_WEBPUSH_SUBSCRIPTION_REMOVED' => '<strong>Removed Web Push subscription:</strong>» %s',
45+
'LOG_WEBPUSH_MESSAGE_FAIL' => '<strong>Web Push message could not be sent:</strong><br>» %s',
46+
'LOG_WEBPUSH_SUBSCRIPTION_REMOVED' => '<strong>Removed Web Push subscription:</strong><br>» %s',
47+
'LOG_WEBPUSH_ICON_DIR_FAIL' => '<strong>Webpush Notifications could not migrate the following item in phpBB’s images directory:</strong><br>» %s',
48+
'LOG_WEBPUSH_ICON_DIR_SUCCESS' => '<strong>Webpush Notifications copied existing PWA touch icons to</strong> <samp>%s</samp>',
4749
]);

language/en/webpushnotifications_common_acp.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
'PWA_SHORT_NAME_EXPLAIN' => 'Your site name in 12 characters or fewer, which may be used as a label for an icon on a mobile device’s home screen. (If this field is left empty, the first 12 characters of the <samp>Site name</samp> will be used.)',
4444
'PWA_SHORT_NAME_INVALID' => '“Short site name” exceeds the 12 character limit.',
4545
'PWA_ICON_SMALL' => 'Small mobile device icon',
46-
'PWA_ICON_SMALL_EXPLAIN' => 'File name of a 192px x 192px PNG image. This file must be uploaded to your board’s <samp>icons</samp> directory.',
46+
'PWA_ICON_SMALL_EXPLAIN' => 'File name of a 192px x 192px PNG image. This file must be uploaded to your board’s <samp>' . \phpbb\webpushnotifications\ext::PWA_ICON_DIR . '</samp> directory.',
4747
'PWA_ICON_LARGE' => 'Large mobile device icon',
48-
'PWA_ICON_LARGE_EXPLAIN' => 'File name of a 512px x 512px PNG image. This file must be uploaded to your board’s <samp>icons</samp> directory.',
48+
'PWA_ICON_LARGE_EXPLAIN' => 'File name of a 512px x 512px PNG image. This file must be uploaded to your board’s <samp>' . \phpbb\webpushnotifications\ext::PWA_ICON_DIR . '</samp> directory.',
4949
'PWA_ICON_SIZE_INVALID' => '“%s” does not have the correct image dimensions.',
5050
'PWA_ICON_MIME_INVALID' => '“%s” must be a PNG image file.',
5151
'PWA_IMAGE_INVALID' => '“%s” does not appear to be a valid image file.',

language/ru/info_acp_webpushnotifications.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
'ACP_WEBPUSH_REMOVE_WARNING' => 'Браузерные push—уведомления теперь встроены в phpBB',
4343
'ACP_WEBPUSH_REMOVE_NOTICE' => 'Расширение «phpBB Browser Push Notifications» больше не требуется и должно быть удалено.<br>Все системные и пользовательские настройки, связанные с данным расширением, будут перенесены в соответствующие настройки браузерных push—уведомлений конференции автоматически при удалении данного расширения.',
4444
'LOG_CONFIG_WEBPUSH' => '<strong>Изменены настройки браузерных push—уведомлений</strong>',
45-
'LOG_WEBPUSH_MESSAGE_FAIL' => '<strong>Не удалось отправить браузерное push—уведомление:</strong> %s',
46-
'LOG_WEBPUSH_SUBSCRIPTION_REMOVED' => '<strong>Удалена подписка на браузерные push—уведомления:</strong>» %s',
45+
'LOG_WEBPUSH_MESSAGE_FAIL' => '<strong>Не удалось отправить браузерное push—уведомление:</strong><br>» %s',
46+
'LOG_WEBPUSH_SUBSCRIPTION_REMOVED' => '<strong>Удалена подписка на браузерные push—уведомления:</strong><br>» %s',
47+
'LOG_WEBPUSH_ICON_DIR_FAIL' => '<strong>Webpush Notifications could not migrate the following item in phpBB’s images directory:</strong><br>» %s',
48+
'LOG_WEBPUSH_ICON_DIR_SUCCESS' => '<strong>Webpush Notifications copied existing PWA touch icons to</strong> <samp>%s</samp>',
4749
]);

language/ru/webpushnotifications_common_acp.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
'PWA_SHORT_NAME_EXPLAIN' => 'Краткое имя сайта длиной не более 12 символов, которое будет использовано в качестве подписи к значку на домашнем экране мобильного устройства. Если оставить пустым, будут использованы первые 12 символов значения настройки, заданной в поле <samp>Название конференции</samp>.',
4444
'PWA_SHORT_NAME_INVALID' => 'Заданное значение в поле «Краткое имя сайта» превышает 12 символов.',
4545
'PWA_ICON_SMALL' => 'Маленький значок для мобильного устройства',
46-
'PWA_ICON_SMALL_EXPLAIN' => 'Имя файла изображения формата PNG размером 192 x 192 пикселя. Файл изображения должен быть загружен на сервер в папку <samp>images/icons</samp>.',
46+
'PWA_ICON_SMALL_EXPLAIN' => 'Имя файла изображения формата PNG размером 192 x 192 пикселя. Файл изображения должен быть загружен на сервер в папку <samp>' . \phpbb\webpushnotifications\ext::PWA_ICON_DIR . '</samp>.',
4747
'PWA_ICON_LARGE' => 'Большой значок для мобильного устройства',
48-
'PWA_ICON_LARGE_EXPLAIN' => 'Имя файла изображения формата PNG размером 512 x 512 пикселей. Файл изображения должен быть загружен на сервер в папку <samp>images/icons</samp>.',
48+
'PWA_ICON_LARGE_EXPLAIN' => 'Имя файла изображения формата PNG размером 512 x 512 пикселей. Файл изображения должен быть загружен на сервер в папку <samp>' . \phpbb\webpushnotifications\ext::PWA_ICON_DIR . '</samp>.',
4949
'PWA_ICON_SIZE_INVALID' => 'Изображение «%s» имеет некорректные размеры.',
5050
'PWA_ICON_MIME_INVALID' => 'Файл изображения «%s» должен иметь формат PNG.',
5151
'PWA_IMAGE_INVALID' => 'Файл «%s» не яввляется файлом изображения.',

migrations/move_site_icons.php

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
*
4+
* phpBB Browser Push Notifications. An extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2024, phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\webpushnotifications\migrations;
12+
13+
use phpbb\db\migration\container_aware_migration;
14+
use phpbb\filesystem\exception\filesystem_exception;
15+
use phpbb\filesystem\filesystem;
16+
17+
class move_site_icons extends container_aware_migration
18+
{
19+
private const NEW_ICON_DIR = 'images/site_icons';
20+
private const OLD_ICON_DIR = 'images/icons';
21+
22+
/* @var filesystem $filesystem */
23+
private $filesystem;
24+
25+
public function effectively_installed()
26+
{
27+
return $this->get_filesystem()->exists($this->container->getParameter('core.root_path') . self::NEW_ICON_DIR);
28+
}
29+
30+
public static function depends_on()
31+
{
32+
return ['\phpbb\webpushnotifications\migrations\add_acp_pwa_configs'];
33+
}
34+
35+
public function update_data(): array
36+
{
37+
return [
38+
['custom', [[$this, 'configure_site_icons']]],
39+
];
40+
}
41+
42+
/**
43+
* Create a site_icons directory in the images directory (and copy existing PWA the icons there)
44+
*
45+
* @return void
46+
*/
47+
public function configure_site_icons()
48+
{
49+
$filesystem = $this->get_filesystem();
50+
$root_path = $this->container->getParameter('core.root_path');
51+
52+
$user = $this->container->get('user');
53+
54+
$small_icon = $this->config->offsetGet('pwa_icon_small');
55+
$large_icon = $this->config->offsetGet('pwa_icon_large');
56+
57+
try
58+
{
59+
// Create the new site_icons directory
60+
if (!$filesystem->exists($root_path . self::NEW_ICON_DIR))
61+
{
62+
$filesystem->mkdir($root_path . self::NEW_ICON_DIR);
63+
}
64+
65+
// Copy existing PWA icons to the new site_icons directory
66+
foreach ([$small_icon, $large_icon] as $icon)
67+
{
68+
if (!$filesystem->exists($root_path . self::OLD_ICON_DIR . '/' . $icon))
69+
{
70+
continue;
71+
}
72+
73+
$filesystem->copy(
74+
$root_path . self::OLD_ICON_DIR . '/' . $icon,
75+
$root_path . self::NEW_ICON_DIR . '/' . $icon
76+
);
77+
}
78+
79+
$this->container->get('log')->add('admin', $user->data['user_id'], $user->ip, 'LOG_WEBPUSH_ICON_DIR_SUCCESS', false, [self::NEW_ICON_DIR]);
80+
}
81+
catch (filesystem_exception $e)
82+
{
83+
$this->container->get('log')->add('critical', $user->data['user_id'], $user->ip, 'LOG_WEBPUSH_ICON_DIR_FAIL', false, [$e->get_filename()]);
84+
}
85+
}
86+
87+
/**
88+
* Get the filesystem text object
89+
*
90+
* @return filesystem
91+
*/
92+
protected function get_filesystem()
93+
{
94+
if ($this->filesystem === null)
95+
{
96+
$this->filesystem = $this->container->get('filesystem');
97+
}
98+
99+
return $this->filesystem;
100+
}
101+
}

styles/all/template/event/overall_header_head_append.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
<!-- Link to app's configuration manifest -->
1717
<link rel="manifest" href="{{ U_MANIFEST_URL }}">
1818

19+
{% if U_TOUCH_ICON %}
1920
<!-- App icon for iOS, a fallback to icons defined in the manifest -->
20-
{% if U_TOUCH_ICON %}<link rel="apple-touch-icon" href="{{ T_ICONS_PATH ~ U_TOUCH_ICON }}">{% endif %}
21+
<link rel="apple-touch-icon" href="{{ ROOT_PATH ~ U_TOUCH_ICON }}">
22+
{% endif %}
2123

2224
{% if NOTIFICATIONS_WEBPUSH_ENABLE %}
2325
{% include '@phpbb_webpushnotifications/ucp_notifications_webpush.html' %}

tests/event/listener_test.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace phpbb\webpushnotifications\tests\event;
1212

13+
use phpbb\webpushnotifications\ext;
14+
1315
require_once __DIR__ . '/../../../../../includes/functions_acp.php';
1416

1517
class listener_test extends \phpbb_database_test_case
@@ -450,7 +452,6 @@ public function validate_pwa_options_data()
450452
*/
451453
public function test_validate_pwa_options($validate, $cfg_array, $expected_error)
452454
{
453-
$this->config['icons_path'] = 'images/icons';
454455
$config_name = key($cfg_array);
455456
$config_definition = ['validate' => $validate];
456457

@@ -467,9 +468,9 @@ public function test_validate_pwa_options($validate, $cfg_array, $expected_error
467468
$this->imagesize->expects($pwa_icon_small && $pwa_icon_large ? self::once() : self::never())
468469
->method('getImageSize')
469470
->willReturnMap([
470-
[$this->root_path . $this->config['icons_path'] . '/', '', false],
471-
[$this->root_path . $this->config['icons_path'] . '/' . $pwa_icon_small, '', ['width' => (int) $small_image_name, 'height' => (int) $small_image_name, 'type' => $small_image_ext === 'png' ? IMAGETYPE_PNG : IMAGETYPE_UNKNOWN]],
472-
[$this->root_path . $this->config['icons_path'] . '/' . $pwa_icon_large, '', ['width' => (int) $large_image_name, 'height' => (int) $large_image_name, 'type' => $large_image_ext === 'png' ? IMAGETYPE_PNG : IMAGETYPE_UNKNOWN]],
471+
[$this->root_path . ext::PWA_ICON_DIR . '/', '', false],
472+
[$this->root_path . ext::PWA_ICON_DIR . '/' . $pwa_icon_small, '', ['width' => (int) $small_image_name, 'height' => (int) $small_image_name, 'type' => $small_image_ext === 'png' ? IMAGETYPE_PNG : IMAGETYPE_UNKNOWN]],
473+
[$this->root_path . ext::PWA_ICON_DIR . '/' . $pwa_icon_large, '', ['width' => (int) $large_image_name, 'height' => (int) $large_image_name, 'type' => $large_image_ext === 'png' ? IMAGETYPE_PNG : IMAGETYPE_UNKNOWN]],
473474
]);
474475

475476
$dispatcher = new \phpbb\event\dispatcher();

0 commit comments

Comments
 (0)