PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.18
VikAppointments Services Booking Calendar v1.2.18
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / libraries / system / assets.php
vikappointments / libraries / system Last commit date
assets.php 6 months ago body.php 6 months ago builder.php 6 months ago cron.php 6 months ago feedback.php 6 months ago gutenberg.php 6 months ago install.php 6 months ago mce.php 6 months ago rssfeeds.php 6 months ago screen.php 6 months ago
assets.php
245 lines
1 <?php
2 /**
3 * @package VikAppointments - Libraries
4 * @subpackage system
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 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 VikAppointmentsAssets
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 $app = JFactory::getApplication();
42 $document = JFactory::getDocument();
43
44 $internalFilesOptions = array('version' => VIKAPPOINTMENTS_SOFTWARE_VERSION);
45
46 /**
47 * Do not load anymore the default assets if we are doing an AJAX call, because it is assumed
48 * that these resources have been already loaded.
49 * In case it is needed to reach a page of VikAppointments from a different plugin via AJAX,
50 * then this plugin will have to invoke this method first.
51 *
52 * @since 1.2
53 *
54 * In case the action started from a different plugin, always load the resources.
55 *
56 * @since 1.2.6
57 */
58 if (!wp_doing_ajax() || $app->input->get('action') !== 'vikappointments')
59 {
60 // include localised strings for script files
61 JText::script('CONNECTION_LOST');
62
63 // system.js must be loaded on both front-end and back-end for tmpl=component support
64 $document->addScript(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'js/system.js', $internalFilesOptions, array('id' => 'vap-sys-script'));
65
66 if ($app->isAdmin())
67 {
68 // add CSS and JS for all pages
69 AppointmentsHelper::load_css_js();
70 // always load font awesome
71 VikAppointments::load_font_awesome();
72
73 $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/system.css', $internalFilesOptions, array('id' => 'vap-sys-style'));
74 $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/bootstrap.lite.css', $internalFilesOptions, array('id' => 'bootstrap-lite-style'));
75
76 /**
77 * Added CSS compatibility for 5.3 version of WP.
78 *
79 * @since 1.1.1
80 */
81 $wp = new JVersion();
82
83 if (version_compare($wp->getShortVersion(), '5.3', '>='))
84 {
85 $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/bc/wp5.3.css', $internalFilesOptions, array('id' => 'vap-wp-bc53-style'));
86 }
87
88 /**
89 * Added CSS compatibility for 5.5 version of WP.
90 *
91 * @since 1.1.7
92 */
93 if (version_compare($wp->getShortVersion(), '5.5', '>='))
94 {
95 $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/bc/wp5.5.css', $internalFilesOptions, array('id' => 'vap-wp-bc55-style'));
96 }
97
98 $document->addScript(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'js/admin.js', $internalFilesOptions, array('id' => 'vap-admin-script'));
99 $document->addScript(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'js/bootstrap.min.js', $internalFilesOptions, array('id' => 'bootstrap-script'));
100 }
101 else
102 {
103 // add CSS and JS for all pages
104 VikAppointments::load_css_js();
105
106 $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/site.css', $internalFilesOptions, array('id' => 'vap-site-style'));
107 }
108 }
109 else
110 {
111 $document->addScriptDeclaration(
112 <<<JS
113 /**
114 * Always refresh the Joomla options after performing an AJAX request.
115 *
116 * @since 1.0
117 */
118 if (typeof Joomla !== 'undefined') {
119 // reload options
120 JoomlaCore.loadOptions();
121 }
122 JS
123 );
124 }
125
126 // Joomla instance is now available on both admin and site sections
127 $document->addScriptDeclaration(
128 <<<JS
129 if (typeof VikFormValidator !== 'undefined') {
130 // change default method to obtain the form label
131 VikFormValidator.prototype.getLabel = JFormValidator.prototype.getLabel;
132 }
133
134 // iterate all window vars and search for an instance of VikFormValidator
135 for (var k in window) {
136 if (window.hasOwnProperty(k)
137 && window[k]
138 && window[k].constructor
139 && window[k].constructor.name == 'VikFormValidator')
140 {
141 // instance found, overwrite getLabel method to access
142 // the <label> tag properly
143 window[k].getLabel = JFormValidator.prototype.getLabel;
144 }
145 }
146 JS
147 );
148 }
149
150 /**
151 * Fixes the usage of some scripts when loaded via AJAX.
152 *
153 * @return void
154 *
155 * @since 1.2
156 */
157 public static function fixAjax()
158 {
159 // loads only once
160 if (static::isLoaded(__METHOD__))
161 {
162 return;
163 }
164
165 add_filter('vik_before_include_script', function($return, $url, $id, $version, $footer)
166 {
167 if (!wp_doing_ajax())
168 {
169 // leave as it is if not AJAX
170 return $return;
171 }
172
173 // lookup intltel
174 $intltel = array(
175 VAPASSETS_URI . 'js/tel/jquery.intlTelInput.min.js',
176 VAPASSETS_URI . 'js/tel/utils.js',
177 );
178
179 // look for intltel plugin
180 if (in_array($url, $intltel))
181 {
182 $condition = 'jQuery.fn.intlTelInput === undefined';
183 $return = false;
184 }
185 // look for Google Maps
186 else if (strpos($url, 'https://maps.google.com/maps/api/js') === 0)
187 {
188 $condition = 'typeof google === \'undefined\'';
189 $return = false;
190 }
191 else
192 {
193 $condition = false;
194 }
195
196 if ($condition)
197 {
198 // register script to load the source file only
199 // in case the condition is satisfied
200 JFactory::getDocument()->addScriptDeclaration(
201 <<<JS
202 if ({$condition}) {
203 jQuery('body').append('<script type="text/javascript" src="{$url}"><\/script>');
204 }
205 JS
206 );
207 }
208
209 return $return;
210 }, 10, 5);
211 }
212
213 /**
214 * Checks if the method has been already loaded.
215 * This function assumes that after this check we are going
216 * to use the specified method.
217 *
218 * A method is considered loaded only if the arguments used are the same.
219 *
220 * @param string $method The method to check for.
221 * @param array $args The list of arguments.
222 *
223 * @return boolean True if already used, otherwise false.
224 */
225 protected static function isLoaded($method, array $args = array())
226 {
227 // generate a unique signature containing the method name
228 // and the list of arguments to use
229 $sign = serialize(array($method, $args));
230
231 // check if the method has been already loaded
232 if (isset(static::$loaded[$sign]))
233 {
234 // already loaded
235 return true;
236 }
237
238 // mark the method as loaded
239 static::$loaded[$sign] = 1;
240
241 // not loaded
242 return false;
243 }
244 }
245