assets.php
1 month ago
body.php
4 years ago
builder.php
1 month ago
cron.php
4 years ago
feedback.php
4 years ago
gutenberg.php
2 years ago
install.php
1 month ago
mce.php
4 years ago
rssfeeds.php
5 months ago
screen.php
4 years ago
assets.php
255 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 | /** |
| 99 | * Added CSS compatibility for 7.0 version of WP. |
| 100 | * |
| 101 | * @since 1.2.19 |
| 102 | */ |
| 103 | if (version_compare($wp->getShortVersion(), '7.0', '>=')) |
| 104 | { |
| 105 | $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/bc/wp7.0.css', $internalFilesOptions, array('id' => 'vap-wp-bc70-style')); |
| 106 | } |
| 107 | |
| 108 | $document->addScript(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'js/admin.js', $internalFilesOptions, array('id' => 'vap-admin-script')); |
| 109 | $document->addScript(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'js/bootstrap.min.js', $internalFilesOptions, array('id' => 'bootstrap-script')); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // add CSS and JS for all pages |
| 114 | VikAppointments::load_css_js(); |
| 115 | |
| 116 | $document->addStyleSheet(VIKAPPOINTMENTS_CORE_MEDIA_URI . 'css/site.css', $internalFilesOptions, array('id' => 'vap-site-style')); |
| 117 | } |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | $document->addScriptDeclaration( |
| 122 | <<<JS |
| 123 | /** |
| 124 | * Always refresh the Joomla options after performing an AJAX request. |
| 125 | * |
| 126 | * @since 1.0 |
| 127 | */ |
| 128 | if (typeof Joomla !== 'undefined') { |
| 129 | // reload options |
| 130 | JoomlaCore.loadOptions(); |
| 131 | } |
| 132 | JS |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | // Joomla instance is now available on both admin and site sections |
| 137 | $document->addScriptDeclaration( |
| 138 | <<<JS |
| 139 | if (typeof VikFormValidator !== 'undefined') { |
| 140 | // change default method to obtain the form label |
| 141 | VikFormValidator.prototype.getLabel = JFormValidator.prototype.getLabel; |
| 142 | } |
| 143 | |
| 144 | // iterate all window vars and search for an instance of VikFormValidator |
| 145 | for (var k in window) { |
| 146 | if (window.hasOwnProperty(k) |
| 147 | && window[k] |
| 148 | && window[k].constructor |
| 149 | && window[k].constructor.name == 'VikFormValidator') |
| 150 | { |
| 151 | // instance found, overwrite getLabel method to access |
| 152 | // the <label> tag properly |
| 153 | window[k].getLabel = JFormValidator.prototype.getLabel; |
| 154 | } |
| 155 | } |
| 156 | JS |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Fixes the usage of some scripts when loaded via AJAX. |
| 162 | * |
| 163 | * @return void |
| 164 | * |
| 165 | * @since 1.2 |
| 166 | */ |
| 167 | public static function fixAjax() |
| 168 | { |
| 169 | // loads only once |
| 170 | if (static::isLoaded(__METHOD__)) |
| 171 | { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | add_filter('vik_before_include_script', function($return, $url, $id, $version, $footer) |
| 176 | { |
| 177 | if (!wp_doing_ajax()) |
| 178 | { |
| 179 | // leave as it is if not AJAX |
| 180 | return $return; |
| 181 | } |
| 182 | |
| 183 | // lookup intltel |
| 184 | $intltel = array( |
| 185 | VAPASSETS_URI . 'js/tel/jquery.intlTelInput.min.js', |
| 186 | VAPASSETS_URI . 'js/tel/utils.js', |
| 187 | ); |
| 188 | |
| 189 | // look for intltel plugin |
| 190 | if (in_array($url, $intltel)) |
| 191 | { |
| 192 | $condition = 'jQuery.fn.intlTelInput === undefined'; |
| 193 | $return = false; |
| 194 | } |
| 195 | // look for Google Maps |
| 196 | else if (strpos($url, 'https://maps.google.com/maps/api/js') === 0) |
| 197 | { |
| 198 | $condition = 'typeof google === \'undefined\''; |
| 199 | $return = false; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | $condition = false; |
| 204 | } |
| 205 | |
| 206 | if ($condition) |
| 207 | { |
| 208 | // register script to load the source file only |
| 209 | // in case the condition is satisfied |
| 210 | JFactory::getDocument()->addScriptDeclaration( |
| 211 | <<<JS |
| 212 | if ({$condition}) { |
| 213 | jQuery('body').append('<script type="text/javascript" src="{$url}"><\/script>'); |
| 214 | } |
| 215 | JS |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | return $return; |
| 220 | }, 10, 5); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Checks if the method has been already loaded. |
| 225 | * This function assumes that after this check we are going |
| 226 | * to use the specified method. |
| 227 | * |
| 228 | * A method is considered loaded only if the arguments used are the same. |
| 229 | * |
| 230 | * @param string $method The method to check for. |
| 231 | * @param array $args The list of arguments. |
| 232 | * |
| 233 | * @return boolean True if already used, otherwise false. |
| 234 | */ |
| 235 | protected static function isLoaded($method, array $args = array()) |
| 236 | { |
| 237 | // generate a unique signature containing the method name |
| 238 | // and the list of arguments to use |
| 239 | $sign = serialize(array($method, $args)); |
| 240 | |
| 241 | // check if the method has been already loaded |
| 242 | if (isset(static::$loaded[$sign])) |
| 243 | { |
| 244 | // already loaded |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | // mark the method as loaded |
| 249 | static::$loaded[$sign] = 1; |
| 250 | |
| 251 | // not loaded |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 |