| @@ -1,0 +1,355 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.html | |
| 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 | + * Utility class for script behaviors. | |
| 16 | + * | |
| 17 | + * @since 10.0 | |
| 18 | + */ | |
| 19 | +abstract class JHtmlBehavior | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Includes TinyMCE editor assets. | |
| 23 | + * | |
| 24 | + * @return void | |
| 25 | + */ | |
| 26 | + public static function tinyMCE() | |
| 27 | + { | |
| 28 | + wp_enqueue_editor(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Includes CodeMirror editor assets. | |
| 33 | + * | |
| 34 | + * @param mixed $type An array of argument of the language type string. | |
| 35 | + * | |
| 36 | + * @return void | |
| 37 | + */ | |
| 38 | + public static function codeMirror($type = 'php') | |
| 39 | + { | |
| 40 | + if (is_string($type)) | |
| 41 | + { | |
| 42 | + $type = array('type' => $type); | |
| 43 | + } | |
| 44 | + | |
| 45 | + wp_enqueue_code_editor($type); | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Loads the datepicker JS plugin. | |
| 50 | + * | |
| 51 | + * @return void | |
| 52 | + */ | |
| 53 | + public static function calendar() | |
| 54 | + { | |
| 55 | + static $loaded = 0; | |
| 56 | + | |
| 57 | + if ($loaded) | |
| 58 | + { | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + | |
| 62 | + $loaded = 1; | |
| 63 | + | |
| 64 | + // Labels | |
| 65 | + $done = __('Done'); | |
| 66 | + $prev = __('Previous'); | |
| 67 | + $next = __('Next'); | |
| 68 | + $today = __('Today'); | |
| 69 | + $wk = 'Wk'; | |
| 70 | + | |
| 71 | + // Months | |
| 72 | + $months = array( | |
| 73 | + __('January'), | |
| 74 | + __('February'), | |
| 75 | + __('March'), | |
| 76 | + __('April'), | |
| 77 | + __('May'), | |
| 78 | + __('June'), | |
| 79 | + __('July'), | |
| 80 | + __('August'), | |
| 81 | + __('September'), | |
| 82 | + __('October'), | |
| 83 | + __('November'), | |
| 84 | + __('December'), | |
| 85 | + ); | |
| 86 | + | |
| 87 | + $months_short = array( | |
| 88 | + _x('Jan', 'January abbreviation'), | |
| 89 | + _x('Feb', 'February abbreviation'), | |
| 90 | + _x('Mar', 'March abbreviation'), | |
| 91 | + _x('Apr', 'April abbreviation'), | |
| 92 | + _x('May', 'May abbreviation'), | |
| 93 | + _x('Jun', 'June abbreviation'), | |
| 94 | + _x('Jul', 'July abbreviation'), | |
| 95 | + _x('Aug', 'August abbreviation'), | |
| 96 | + _x('Sep', 'September abbreviation'), | |
| 97 | + _x('Oct', 'October abbreviation'), | |
| 98 | + _x('Nov', 'November abbreviation'), | |
| 99 | + _x('Dec', 'December abbreviation'), | |
| 100 | + ); | |
| 101 | + | |
| 102 | + $months = json_encode($months); | |
| 103 | + $months_short = json_encode($months_short); | |
| 104 | + | |
| 105 | + // Days | |
| 106 | + $days = array( | |
| 107 | + __('Sunday'), | |
| 108 | + __('Monday'), | |
| 109 | + __('Tuesday'), | |
| 110 | + __('Wednesday'), | |
| 111 | + __('Thursday'), | |
| 112 | + __('Friday'), | |
| 113 | + __('Saturday'), | |
| 114 | + ); | |
| 115 | + | |
| 116 | + $days_short_3 = array( | |
| 117 | + __('Sun'), | |
| 118 | + __('Mon'), | |
| 119 | + __('Tue'), | |
| 120 | + __('Wed'), | |
| 121 | + __('Thu'), | |
| 122 | + __('Fri'), | |
| 123 | + __('Sat'), | |
| 124 | + ); | |
| 125 | + | |
| 126 | + $days_short_2 = array(); | |
| 127 | + foreach ($days_short_3 as $d) | |
| 128 | + { | |
| 129 | + $days_short_2[] = mb_substr($d, 0, 2, 'UTF-8'); | |
| 130 | + } | |
| 131 | + | |
| 132 | + // snippet used to make sure the substring of | |
| 133 | + // the week days doesn't return the same value (see Hebrew) | |
| 134 | + // for all the elements | |
| 135 | + $days_short_2 = array_unique($days_short_2); | |
| 136 | + | |
| 137 | + if (count($days_short_2) != count($days_short_3)) | |
| 138 | + { | |
| 139 | + // the count doesn't match, use the 3 chars days | |
| 140 | + $days_short_2 = $days_short_3; | |
| 141 | + } | |
| 142 | + | |
| 143 | + $days = json_encode($days); | |
| 144 | + $days_short_3 = json_encode($days_short_3); | |
| 145 | + $days_short_2 = json_encode($days_short_2); | |
| 146 | + | |
| 147 | + // should return a value between 0-6 (1: Monday, 0: Sunday) | |
| 148 | + $start_of_week = (int) get_option('start_of_week', 0); | |
| 149 | + | |
| 150 | + JFactory::getDocument()->addScriptDeclaration( | |
| 151 | +<<<JS | |
| 152 | +jQuery(function($){ | |
| 153 | + $.datepicker.regional["wp-datepicker"] = { | |
| 154 | + closeText: "$done", | |
| 155 | + prevText: "$prev", | |
| 156 | + nextText: "$next", | |
| 157 | + currentText: "$today", | |
| 158 | + monthNames: $months, | |
| 159 | + monthNamesShort: $months_short, | |
| 160 | + dayNames: $days, | |
| 161 | + dayNamesShort: $days_short_3, | |
| 162 | + dayNamesMin: $days_short_2, | |
| 163 | + weekHeader: "$wk", | |
| 164 | + firstDay: $start_of_week, | |
| 165 | + isRTL: false, | |
| 166 | + showMonthAfterYear: false, | |
| 167 | + yearSuffix: "" | |
| 168 | + }; | |
| 169 | + | |
| 170 | + $.datepicker.setDefaults($.datepicker.regional["wp-datepicker"]); | |
| 171 | +}); | |
| 172 | +JS | |
| 173 | + ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * Loads a tooltip. | |
| 178 | + * | |
| 179 | + * @return void | |
| 180 | + */ | |
| 181 | + public static function tooltip() | |
| 182 | + { | |
| 183 | + // do nothing for the moment | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Keep session alive, for example, while editing or creating an article. | |
| 188 | + * | |
| 189 | + * @return void | |
| 190 | + */ | |
| 191 | + public static function keepalive() | |
| 192 | + { | |
| 193 | + // do nothing for the moment | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Loads a modal. | |
| 198 | + * | |
| 199 | + * @return void | |
| 200 | + */ | |
| 201 | + public static function modal() | |
| 202 | + { | |
| 203 | + static $loaded = 0; | |
| 204 | + | |
| 205 | + if ($loaded) | |
| 206 | + { | |
| 207 | + return; | |
| 208 | + } | |
| 209 | + | |
| 210 | + $loaded = 1; | |
| 211 | + | |
| 212 | + JFactory::getDocument()->addScriptDeclaration( | |
| 213 | +<<<JAVASCRIPT | |
| 214 | +jQuery(function() { | |
| 215 | + jQuery('.wrap a.modal[target="_blank"]').on('click', function(e) { | |
| 216 | + // get link HREF | |
| 217 | + var href = jQuery(this).attr('href'); | |
| 218 | + | |
| 219 | + // make sure we have an image | |
| 220 | + if (href.match(/\.(png|jpe?g|gif|bmp)$/i)) { | |
| 221 | + // prevent default link action | |
| 222 | + e.preventDefault(); | |
| 223 | + | |
| 224 | + // open modal containing image preview | |
| 225 | + wpOpenJModal('wpmodal', jQuery(this).attr('href')); | |
| 226 | + | |
| 227 | + return false; | |
| 228 | + } | |
| 229 | + | |
| 230 | + // otherwise fallback to default browser opening | |
| 231 | + }); | |
| 232 | +}); | |
| 233 | +JAVASCRIPT | |
| 234 | + ); | |
| 235 | + | |
| 236 | + // display modal to preview the images | |
| 237 | + echo JHtml::fetch( | |
| 238 | + 'bootstrap.renderModal', | |
| 239 | + 'jmodal-wpmodal', | |
| 240 | + array( | |
| 241 | + 'title' => JText::translate('JMEDIA_PREVIEW_TITLE'), | |
| 242 | + 'closeButton' => true, | |
| 243 | + 'keyboard' => true, | |
| 244 | + 'bodyHeight' => 80, | |
| 245 | + ) | |
| 246 | + ); | |
| 247 | + } | |
| 248 | + | |
| 249 | + /** | |
| 250 | + * Enhance the compatibility with Wordpress via javascript. | |
| 251 | + * | |
| 252 | + * When "tmpl" var is equals to "component", tries to remove the contents | |
| 253 | + * displayed by the theme. | |
| 254 | + * | |
| 255 | + * Provides a script to replace all the "index.php" occurrences into "admin.php". | |
| 256 | + * | |
| 257 | + * @return void | |
| 258 | + */ | |
| 259 | + public static function component() | |
| 260 | + { | |
| 261 | + static $loaded = 0; | |
| 262 | + | |
| 263 | + if ($loaded) | |
| 264 | + { | |
| 265 | + return; | |
| 266 | + } | |
| 267 | + | |
| 268 | + $loaded = 1; | |
| 269 | + | |
| 270 | + $app = JFactory::getApplication(); | |
| 271 | + $input = $app->input; | |
| 272 | + $document = JFactory::getDocument(); | |
| 273 | + | |
| 274 | + // check if tmpl component | |
| 275 | + if ($input->get('tmpl') === 'component') | |
| 276 | + { | |
| 277 | + $document->addScriptDeclaration( | |
| 278 | +<<<JS | |
| 279 | +(function($) { | |
| 280 | + 'use strict'; | |
| 281 | + | |
| 282 | + $(function() { | |
| 283 | + // clone DOM wrapper | |
| 284 | + var clone = $('.wrap.plugin-container').detach(); | |
| 285 | + | |
| 286 | + // remove all body elements and attach the wrapper | |
| 287 | + $('body').children().not('script,style,link,.colorpicker').remove(); | |
| 288 | + $('body').append(clone); | |
| 289 | + | |
| 290 | + // adjust wrapper margin | |
| 291 | + $('.wrap.plugin-container').css('margin', '10px'); | |
| 292 | + | |
| 293 | + // remove padding from WP toolbar | |
| 294 | + $('html.wp-toolbar').css('padding', 0); | |
| 295 | + }); | |
| 296 | +})(jQuery); | |
| 297 | +JS | |
| 298 | + ); | |
| 299 | + } | |
| 300 | + | |
| 301 | + // script to change all <form> and <a> tags from "index.php" to "admin.php" | |
| 302 | + if ($app->isAdmin()) | |
| 303 | + { | |
| 304 | + $document->addScriptDeclaration( | |
| 305 | +<<<JS | |
| 306 | +(function($) { | |
| 307 | + 'use strict'; | |
| 308 | + | |
| 309 | + $(function() { | |
| 310 | + routePageTargets('.wrap.plugin-container'); | |
| 311 | + }); | |
| 312 | +})(jQuery); | |
| 313 | +JS | |
| 314 | + ); | |
| 315 | + } | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** | |
| 319 | + * Loads the core functionalities that need to be used | |
| 320 | + * every time a page is requested. | |
| 321 | + * | |
| 322 | + * @return void | |
| 323 | + * | |
| 324 | + * @since 10.1.14 | |
| 325 | + */ | |
| 326 | + public static function core() | |
| 327 | + { | |
| 328 | + /** | |
| 329 | + * Core usage shouldn't be cached because | |
| 330 | + * widgets might declare their own options. | |
| 331 | + * | |
| 332 | + * @since 10.1.21 | |
| 333 | + */ | |
| 334 | + | |
| 335 | + $document = JFactory::getDocument(); | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * Generate scripts options. | |
| 339 | + * | |
| 340 | + * @since 10.1.14 | |
| 341 | + */ | |
| 342 | + $scriptOptions = $document->getScriptOptions(); | |
| 343 | + | |
| 344 | + if (!empty($scriptOptions)) | |
| 345 | + { | |
| 346 | + // encode options in JSON format (evaluate PRETTY_PRINT usage) | |
| 347 | + $prettyPrint = (WP_DEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false); | |
| 348 | + $jsonOptions = json_encode($scriptOptions, $prettyPrint); | |
| 349 | + $jsonOptions = $jsonOptions ? $jsonOptions : '{}'; | |
| 350 | + | |
| 351 | + // add application/json declaration to document | |
| 352 | + $document->addScriptDeclaration($jsonOptions, 'application/json'); | |
| 353 | + } | |
| 354 | + } | |
| 355 | +} | |