| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking - Libraries |
| 4 |
* @subpackage system |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2018 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 |
* Class used to provide support for the <head> of the page. |
| 16 |
* |
| 17 |
* @since 1.0 |
| 18 |
*/ |
| 19 |
class VikBookingAssets |
| 20 |
{ |
| 21 |
/** |
| 22 |
* A list containing all the methods already used. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected static $loaded = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Loads all the assets required for the plugin. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public static function load() |
| 34 |
{ |
| 35 |
// loads only once |
| 36 |
if (static::isLoaded(__METHOD__)) |
| 37 |
{ |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$document = JFactory::getDocument(); |
| 42 |
|
| 43 |
$internalFilesOptions = array('version' => VIKBOOKING_SOFTWARE_VERSION); |
| 44 |
|
| 45 |
// include localised strings for script files |
| 46 |
JText::script('CONNECTION_LOST'); |
| 47 |
|
| 48 |
// system.js must be loaded on both front-end and back-end for tmpl=component support |
| 49 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'js/system.js', $internalFilesOptions, array('id' => 'vbo-sys-script')); |
| 50 |
|
| 51 |
if (JFactory::getApplication()->isAdmin()) |
| 52 |
{ |
| 53 |
/* Load assets for CSS and JS */ |
| 54 |
VikBooking::loadFontAwesome(true); |
| 55 |
|
| 56 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'vikbooking.css', $internalFilesOptions, array('id' => 'vbo-style')); |
| 57 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'fonts/vboicomoon.css', $internalFilesOptions, array('id' => 'vbo-icomoon-style')); |
| 58 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'vikbooking_backendcustom.css', array('version' => filemtime(JPath::clean(VBO_ADMIN_PATH . '/resources/vikbooking_backendcustom.css'))), array('id' => 'vbo-custom-style')); |
| 59 |
|
| 60 |
VikBooking::getVboApplication()->normalizeBackendStyles(); |
| 61 |
|
| 62 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'css/system.css', $internalFilesOptions, array('id' => 'vbo-sys-style')); |
| 63 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'css/bootstrap.lite.css', $internalFilesOptions, array('id' => 'bootstrap-lite-style')); |
| 64 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'js/bootstrap.min.js', $internalFilesOptions, array('id' => 'bootstrap-script')); |
| 65 |
|
| 66 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'js/admin.js', $internalFilesOptions, array('id' => 'vbo-admin-script')); |
| 67 |
|
| 68 |
/** |
| 69 |
* Include the VBOCore JS class. |
| 70 |
* |
| 71 |
* @since 1.5.0 |
| 72 |
*/ |
| 73 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'vbocore.js', $internalFilesOptions, array('id' => 'vbo-core-script')); |
| 74 |
|
| 75 |
/** |
| 76 |
* Include the Toast JS class. |
| 77 |
* |
| 78 |
* @since 1.5.0 |
| 79 |
*/ |
| 80 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'toast.js', $internalFilesOptions, array('id' => 'vbo-toast-script')); |
| 81 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'toast.css', $internalFilesOptions, array('id' => 'vbo-toast-style')); |
| 82 |
|
| 83 |
$document->addScriptDeclaration( |
| 84 |
<<<JS |
| 85 |
(function($) { |
| 86 |
'use strict'; |
| 87 |
|
| 88 |
$(function() { |
| 89 |
VBOToast.create(VBOToast.POSITION_TOP_RIGHT); |
| 90 |
}); |
| 91 |
})(jQuery); |
| 92 |
JS |
| 93 |
); |
| 94 |
|
| 95 |
/** |
| 96 |
* Load necessary assets for WordPress >= 5.3 |
| 97 |
* |
| 98 |
* @since 1.2.10 |
| 99 |
*/ |
| 100 |
JLoader::import('adapter.application.version'); |
| 101 |
$wpv = new JVersion; |
| 102 |
if (version_compare($wpv->getShortVersion(), '5.3', '>=')) { |
| 103 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'css/bc/wp5.3.css', $internalFilesOptions, array('id' => 'vbo-wp-bc-style')); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Load the proper CSS file according to the appearance preferences. |
| 108 |
* |
| 109 |
* @since 1.5.0 |
| 110 |
*/ |
| 111 |
VikBooking::loadAppearancePreferenceAssets(); |
| 112 |
|
| 113 |
/** |
| 114 |
* Load the Web App Manifest JSON file. |
| 115 |
* |
| 116 |
* @since 1.6.5 |
| 117 |
*/ |
| 118 |
VBOWebappManifest::load(); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
if (VikBooking::loadBootstrap()) |
| 123 |
{ |
| 124 |
$document->addStyleSheet(VIKBOOKING_SITE_ASSETS_URI.'bootstrap.min.css', $internalFilesOptions, array('id' => 'vbo-bs-style')); |
| 125 |
$document->addStyleSheet(VIKBOOKING_SITE_ASSETS_URI.'bootstrap-theme.min.css', $internalFilesOptions, array('id' => 'vbo-bstheme-style')); |
| 126 |
} |
| 127 |
|
| 128 |
VikBooking::loadFontAwesome(); |
| 129 |
$document->addStyleSheet(VIKBOOKING_SITE_ASSETS_URI.'vikbooking_styles.css', $internalFilesOptions, array('id' => 'vbo-style')); |
| 130 |
|
| 131 |
/** |
| 132 |
* Load the proper CSS file according to the appearance preferences. |
| 133 |
* This is made after the main stylesheet and before the custom one. |
| 134 |
* |
| 135 |
* @since 1.5.0 |
| 136 |
*/ |
| 137 |
VikBooking::loadAppearancePreferenceAssets(); |
| 138 |
|
| 139 |
$document->addStyleSheet(VIKBOOKING_SITE_ASSETS_URI.'vikbooking_custom.css', $internalFilesOptions, array('id' => 'vbo-custom-style')); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Always prepare AJAX requests to pass a CSRF token. |
| 144 |
* |
| 145 |
* @since 1.6.0 |
| 146 |
*/ |
| 147 |
JHtml::fetch('vbohtml.scripts.ajaxcsrf'); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* This method should be called only within the wp-admin section when |
| 152 |
* the active page does not belong to the plugin Vik Booking. It loads |
| 153 |
* the necessary CSS and JS assets to support browser notifications. |
| 154 |
* |
| 155 |
* @return void |
| 156 |
* |
| 157 |
* @since 1.5.0 |
| 158 |
*/ |
| 159 |
public static function loadForExternal() |
| 160 |
{ |
| 161 |
// loads only once |
| 162 |
if (static::isLoaded(__METHOD__)) |
| 163 |
{ |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
// main library |
| 168 |
require_once VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikbooking.php'; |
| 169 |
|
| 170 |
// load the necessary JS and CSS assets |
| 171 |
$document = JFactory::getDocument(); |
| 172 |
$internalFilesOptions = array('version' => VIKBOOKING_SOFTWARE_VERSION); |
| 173 |
|
| 174 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'vbocore.js', $internalFilesOptions, array('id' => 'vbo-core-script')); |
| 175 |
$document->addScript(VIKBOOKING_ADMIN_ASSETS_URI . 'toast.js', $internalFilesOptions, array('id' => 'vbo-toast-script')); |
| 176 |
$document->addStyleSheet(VIKBOOKING_ADMIN_ASSETS_URI . 'toast.css', $internalFilesOptions, array('id' => 'vbo-toast-style')); |
| 177 |
|
| 178 |
/** |
| 179 |
* Always prepare AJAX requests to pass a CSRF token. |
| 180 |
* |
| 181 |
* @since 1.6.0 |
| 182 |
*/ |
| 183 |
JHtml::fetch('vbohtml.scripts.ajaxcsrf'); |
| 184 |
|
| 185 |
// build AJAX uri endpoints and CMS |
| 186 |
$widget_ajax_uri = VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=exec_admin_widget'); |
| 187 |
$assets_ajax_uri = VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=widgets_get_assets'); |
| 188 |
$multitask_ajax_uri = VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=exec_multitask_widgets'); |
| 189 |
$watchdata_ajax_uri = VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=widgets_watch_data'); |
| 190 |
$current_page_uri = htmlspecialchars((string) JUri::getInstance(), ENT_QUOTES); |
| 191 |
$root_uri = htmlspecialchars(JUri::root(), ENT_QUOTES); |
| 192 |
$current_cms = VBOPlatformDetection::isWordPress() ? 'wordpress' : 'joomla'; |
| 193 |
|
| 194 |
// check if the notification audio file exists within VCM |
| 195 |
$notif_audio_path = implode(DIRECTORY_SEPARATOR, [VCM_ADMIN_PATH, 'assets', 'css', 'audio', 'new_notification.mp3']); |
| 196 |
$notif_audio_url = is_file($notif_audio_path) ? (VCM_ADMIN_URI . implode('/', ['assets', 'css', 'audio', 'new_notification.mp3'])) : null; |
| 197 |
|
| 198 |
// service worker and push data |
| 199 |
$service_worker_path = VBOWebappServiceworker::getUri(); |
| 200 |
$service_worker_scope = VBOWebappServiceworker::getScope(); |
| 201 |
$push_config = json_encode(VBOWebappPush::getConfig()); |
| 202 |
|
| 203 |
// add the necessary script declaration |
| 204 |
$document->addScriptDeclaration( |
| 205 |
<<<JS |
| 206 |
(function($) { |
| 207 |
'use strict'; |
| 208 |
|
| 209 |
$(function() { |
| 210 |
|
| 211 |
VBOToast.create(VBOToast.POSITION_TOP_RIGHT); |
| 212 |
|
| 213 |
VBOCore.setOptions({ |
| 214 |
cms: "$current_cms", |
| 215 |
widget_ajax_uri: "$widget_ajax_uri", |
| 216 |
assets_ajax_uri: "$assets_ajax_uri", |
| 217 |
multitask_ajax_uri: "$multitask_ajax_uri", |
| 218 |
watchdata_ajax_uri: "$watchdata_ajax_uri", |
| 219 |
current_page: "wp-admin", |
| 220 |
current_page_uri: "$current_page_uri", |
| 221 |
root_uri: "$root_uri", |
| 222 |
notif_audio_url: "$notif_audio_url", |
| 223 |
service_worker_path: "{$service_worker_path}", |
| 224 |
service_worker_scope: "{$service_worker_scope}", |
| 225 |
push: $push_config, |
| 226 |
}); |
| 227 |
|
| 228 |
VBOCore.listenServiceWorkerMessages(); |
| 229 |
|
| 230 |
}); |
| 231 |
})(jQuery); |
| 232 |
JS |
| 233 |
); |
| 234 |
|
| 235 |
// finally, preload the admin widgets |
| 236 |
VikBooking::getAdminWidgetsInstance()->getWidgetNames($preload = true); |
| 237 |
|
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Checks if the method has been already loaded. |
| 243 |
* This function assumes that after this check we are going |
| 244 |
* to use the specified method. |
| 245 |
* |
| 246 |
* A method is considered loaded only if the arguments used are the same. |
| 247 |
* |
| 248 |
* @param string $method The method to check for. |
| 249 |
* @param array $args The list of arguments. |
| 250 |
* |
| 251 |
* @return boolean True if already used, otherwise false. |
| 252 |
*/ |
| 253 |
protected static function isLoaded($method, array $args = array()) |
| 254 |
{ |
| 255 |
// generate a unique signature containing the method name |
| 256 |
// and the list of arguments to use |
| 257 |
$sign = serialize(array($method, $args)); |
| 258 |
|
| 259 |
// check if the method has been already loaded |
| 260 |
if (isset(static::$loaded[$sign])) |
| 261 |
{ |
| 262 |
// already loaded |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
// mark the method as loaded |
| 267 |
static::$loaded[$sign] = 1; |
| 268 |
|
| 269 |
// not loaded |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
|