| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* VikBooking Web App Manifest handling class. |
| 16 |
* |
| 17 |
* @since 1.16.5 (J) - 1.6.5 (WP) |
| 18 |
*/ |
| 19 |
final class VBOWebappManifest |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Loads the Web App Manifest JSON file and attaches it to the document. |
| 23 |
* |
| 24 |
* @return bool |
| 25 |
*/ |
| 26 |
public static function load() |
| 27 |
{ |
| 28 |
if (!is_file(self::getPath())) { |
| 29 |
// attempt to build the manifest |
| 30 |
try { |
| 31 |
self::build(); |
| 32 |
} catch (Exception $e) { |
| 33 |
// manifest shall not be built or loaded |
| 34 |
return false; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// access the document |
| 39 |
$document = JFactory::getDocument(); |
| 40 |
|
| 41 |
// prevent conflicts |
| 42 |
if (!method_exists($document, 'addHeadLink')) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
// attach the link tag to the document head |
| 47 |
$document->addHeadLink(self::getUri(), 'manifest'); |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Attempts to build the manifest file. This will run either when the manifest file is missing |
| 54 |
* (could be manually deleted), or when the configuration settings get saved. This is to allow |
| 55 |
* third-party plugins to manipulate the manifest file easily. |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
* |
| 59 |
* @throws Exception |
| 60 |
*/ |
| 61 |
public static function build() |
| 62 |
{ |
| 63 |
// default Web App name |
| 64 |
$web_app_name = JText::translate('COM_VIKBOOKING'); |
| 65 |
if ($web_app_name == 'COM_VIKBOOKING') { |
| 66 |
$web_app_name = 'VikBooking'; |
| 67 |
} |
| 68 |
|
| 69 |
// host name |
| 70 |
$host_name = JURI::getInstance()->toString(['host']); |
| 71 |
|
| 72 |
// manifest default payload |
| 73 |
$data = [ |
| 74 |
'name' => $web_app_name, |
| 75 |
'display' => 'standalone', |
| 76 |
'short_name' => $host_name, |
| 77 |
'description' => $web_app_name . ' - ' . $host_name, |
| 78 |
'start_url' => VBOFactory::getPlatform()->getUri()->admin('index.php?option=com_vikbooking&webapp=1', $xhtml = false), |
| 79 |
'icons' => [ |
| 80 |
[ |
| 81 |
'src' => VBO_ADMIN_URI . 'resources/channels/touch-icon-192.png', |
| 82 |
'sizes' => '192x192', |
| 83 |
'type' => 'image/png', |
| 84 |
], |
| 85 |
], |
| 86 |
]; |
| 87 |
|
| 88 |
/** |
| 89 |
* Trigger event to allow third party plugins to overwrite the Web App manifest payload. |
| 90 |
* It is possible to throw an Exception in order to prevent the Web App manifest from being generated. |
| 91 |
*/ |
| 92 |
VBOFactory::getPlatform()->getDispatcher()->trigger('onBeforeBuildWebAppManifestVikBooking', [&$data]); |
| 93 |
|
| 94 |
if (!$data) { |
| 95 |
throw new Exception('Invalid manifest payload', 500); |
| 96 |
} |
| 97 |
|
| 98 |
if (!JFile::write(self::getPath(), json_encode($data, JSON_PRETTY_PRINT))) { |
| 99 |
throw new Exception('Could not write the Web App manifest file', 500); |
| 100 |
} |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the path to the manifest file according to the platform. |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
private static function getPath() |
| 111 |
{ |
| 112 |
static $path = null; |
| 113 |
|
| 114 |
if ($path !== null) { |
| 115 |
return $path; |
| 116 |
} |
| 117 |
|
| 118 |
if (VBOPlatformDetection::isWordPress()) { |
| 119 |
$path = implode(DIRECTORY_SEPARATOR, [VBO_MEDIA_ASSETS_PATH, 'manifest.json']); |
| 120 |
} else { |
| 121 |
$path = implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'resources', 'manifest.json']); |
| 122 |
} |
| 123 |
|
| 124 |
return $path; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the URI to the manifest file according to the platform. |
| 129 |
* |
| 130 |
* @param bool $cached true to append the file modification timestamp to the URL for caching. |
| 131 |
* |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
private static function getUri($cached = true) |
| 135 |
{ |
| 136 |
$cache_id = ''; |
| 137 |
|
| 138 |
if ($cached) { |
| 139 |
$cache_id = '?' . @filemtime(self::getPath()); |
| 140 |
} |
| 141 |
|
| 142 |
if (VBOPlatformDetection::isWordPress()) { |
| 143 |
return VBO_MEDIA_ASSETS_URI . 'manifest.json' . $cache_id; |
| 144 |
} |
| 145 |
|
| 146 |
return VBO_ADMIN_URI . 'resources/manifest.json' . $cache_id; |
| 147 |
} |
| 148 |
} |
| 149 |
|