Overview

Namespaces

  • None
  • Quform
    • Traduttore_Registry

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface

Constants

  • Quform\Traduttore_Registry\TRANSIENT_KEY_PLUGIN
  • Quform\Traduttore_Registry\TRANSIENT_KEY_THEME

Functions

  • Quform\Traduttore_Registry\add_project
  • Quform\Traduttore_Registry\clean_translations_cache
  • Quform\Traduttore_Registry\get_available_locales
  • Quform\Traduttore_Registry\get_installed_translations
  • Quform\Traduttore_Registry\get_translations
  • Quform\Traduttore_Registry\register_clean_translations_cache
  • Quform\Traduttore_Registry\sanitize_date
  • Overview
  • Namespace
  • Class
   1: <?php
   2: 
   3: /**
   4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
   5:  */
   6: class Quform_ScriptLoader
   7: {
   8:     /**
   9:      * @var Quform_Options
  10:      */
  11:     protected $options;
  12: 
  13:     /**
  14:      * @var Quform_Themes
  15:      */
  16:     protected $themes;
  17: 
  18:     /**
  19:      * @var Quform_Form_Factory
  20:      */
  21:     protected $factory;
  22: 
  23:     /**
  24:      * @var Quform_Repository
  25:      */
  26:     protected $repository;
  27: 
  28:     /**
  29:      * @var Quform_Session
  30:      */
  31:     protected $session;
  32: 
  33:     /**
  34:      * @param  Quform_Options       $options
  35:      * @param  Quform_Themes        $themes
  36:      * @param  Quform_Repository    $repository
  37:      * @param  Quform_Form_Factory  $factory
  38:      */
  39:     public function __construct(Quform_Options $options, Quform_Themes $themes, Quform_Repository $repository, Quform_Form_Factory $factory, Quform_Session $session) {
  40:         $this->options = $options;
  41:         $this->themes = $themes;
  42:         $this->factory = $factory;
  43:         $this->repository = $repository;
  44:         $this->session = $session;
  45:     }
  46: 
  47:     /**
  48:      * Enqueue the plugin scripts/styles
  49:      */
  50:     public function enqueue()
  51:     {
  52:         if ($this->shouldLoadScripts()) {
  53:             $this->enqueueStyles();
  54:             $this->enqueueScripts();
  55:         }
  56:     }
  57: 
  58:     /**
  59:      * Should the load the scripts/styles be loaded?
  60:      *
  61:      * @return bool
  62:      */
  63:     protected function shouldLoadScripts()
  64:     {
  65:         static $loadScripts = null;
  66: 
  67:         if ($loadScripts === null) {
  68:             $loadScripts = true;
  69: 
  70:             if ($this->options->get('loadScripts') == 'autodetect') {
  71:                 if ( ! $this->detectFormInContent() && ! $this->detectFormInWidget()) {
  72:                     $loadScripts = false;
  73:                 }
  74:             } else if ($this->options->get('loadScripts') == 'custom') {
  75:                 $post = Quform::getCurrentPost();
  76:                 $postIds = $this->options->get('loadScriptsCustom');
  77:                 $loadScripts = false;
  78: 
  79:                 if ($post instanceof WP_Post && count($postIds) && in_array($post->ID, $postIds)) {
  80:                     $loadScripts = true;
  81:                 }
  82:             }
  83: 
  84:             $loadScripts = apply_filters('quform_enqueue_scripts', $loadScripts);
  85:         }
  86: 
  87:         return $loadScripts;
  88:     }
  89: 
  90:     /**
  91:      * Enqueue the plugin styles
  92:      */
  93:     protected function enqueueStyles()
  94:     {
  95:         // Load non-core theme CSS files
  96:         foreach (array_unique($this->options->get('activeThemes')) as $key) {
  97:             if ( ! $this->themes->isCoreTheme($key)) {
  98:                 $data = $this->themes->getTheme($key);
  99: 
 100:                 if (is_array($data)) {
 101:                     wp_enqueue_style($key, $data['cssUrl'], array(), isset($data['version']) ? $data['version'] : QUFORM_VERSION);
 102:                 }
 103:             }
 104:         }
 105: 
 106:         if ($this->options->get('combineCss')) {
 107:             wp_enqueue_style('quform', $this->getCacheUrl($this->getCombinedCssFilename()), array(), $this->options->get('cacheBuster'));
 108:         } else {
 109:             foreach ($this->getStyles() as $key => $style) {
 110:                 wp_enqueue_style($key, $style['url'], array(), $style['version']);
 111:             }
 112:         }
 113: 
 114:         do_action('quform_styles_enqueued');
 115:     }
 116: 
 117:     /**
 118:      * Get the styles to be enqueued
 119:      *
 120:      * @param   bool   $isPreview  True to get the styles for the form preview
 121:      * @return  array
 122:      */
 123:     public function getStyles($isPreview = false)
 124:     {
 125:         $styles = array();
 126: 
 127:         if ( ! $this->options->get('disabledStyles.fontAwesome')) {
 128:             $styles['font-awesome'] = array(
 129:                 'url' => Quform::url('css/font-awesome.min.css'),
 130:                 'path' => QUFORM_PATH . '/css/font-awesome.min.css',
 131:                 'version' => '4.7.0'
 132:             );
 133:         }
 134: 
 135:         if ( ! $this->options->get('disabledStyles.select2') && (count($this->options->get('activeEnhancedSelects')) || $isPreview)) {
 136:             $styles['quform-select2'] = array(
 137:                 'url' => Quform::url('css/select2.min.css'),
 138:                 'path' => QUFORM_PATH . '/css/select2.min.css',
 139:                 'version' => '4.0.13'
 140:             );
 141:         }
 142: 
 143:         if ( ! $this->options->get('disabledStyles.qtip')) {
 144:             $styles['qtip'] = array(
 145:                 'url' => Quform::url('css/jquery.qtip.min.css'),
 146:                 'path' => QUFORM_PATH . '/css/jquery.qtip.min.css',
 147:                 'version' => '3.0.4'
 148:             );
 149:         }
 150: 
 151:         if ($this->options->get('popupEnabled')) {
 152:             if ($this->options->get('popupScript') == 'fancybox-1' && ! $this->options->get('disabledStyles.fancybox')) {
 153:                 $styles['fancybox'] = array(
 154:                     'url' => Quform::url('css/jquery.fancybox1.min.css'),
 155:                     'path' => QUFORM_PATH . '/css/jquery.fancybox1.min.css',
 156:                     'version' => '1.3.9'
 157:                 );
 158:             } elseif ($this->options->get('popupScript') == 'fancybox-2' && ! $this->options->get('disabledStyles.fancybox2')) {
 159:                 $styles['fancybox2'] = array(
 160:                     'url' => Quform::url('css/jquery.fancybox.min.css'),
 161:                     'path' => QUFORM_PATH . '/css/jquery.fancybox.min.css',
 162:                     'version' => '2.1.9'
 163:                 );
 164:             } elseif ($this->options->get('popupScript') == 'fancybox-3' && ! $this->options->get('disabledStyles.fancybox3')) {
 165:                 $styles['fancybox3'] = array(
 166:                     'url' => Quform::url('css/jquery.fancybox3.min.css'),
 167:                     'path' => QUFORM_PATH . '/css/jquery.fancybox3.min.css',
 168:                     'version' => '3.5.7'
 169:                 );
 170:             } elseif ($this->options->get('popupScript') == 'magnific-popup' && ! $this->options->get('disabledStyles.magnificPopup')) {
 171:                 $styles['magnific-popup'] = array(
 172:                     'url' => Quform::url('css/magnific-popup.min.css'),
 173:                     'path' => QUFORM_PATH . '/css/magnific-popup.min.css',
 174:                     'version' => '1.1.0'
 175:                 );
 176:             }
 177:         }
 178: 
 179:         $styles['quform'] = array(
 180:             'url' => Quform::url('css/styles.min.css'),
 181:             'path' => QUFORM_PATH . '/css/styles.min.css',
 182:             'version' => QUFORM_VERSION
 183:         );
 184: 
 185:         // Load core theme CSS files
 186:         foreach (array_unique($this->options->get('activeThemes')) as $key) {
 187:             if ($this->themes->isCoreTheme($key)) {
 188:                 $data = $this->themes->getTheme($key);
 189: 
 190:                 if (is_array($data)) {
 191:                     $styles['quform-theme-' . $key] = array(
 192:                         'url' => $data['cssUrl'],
 193:                         'path' => $data['cssPath'],
 194:                         'version' => isset($data['version']) ? $data['version'] : QUFORM_VERSION
 195:                     );
 196:                 }
 197:             }
 198:         }
 199: 
 200:         if (apply_filters('quform_enfold_compatibility', defined('AV_FRAMEWORK_VERSION'))) {
 201:             $styles['quform-enfold'] = array(
 202:                 'url' => Quform::url('css/enfold.min.css'),
 203:                 'path' => QUFORM_PATH . '/css/enfold.min.css',
 204:                 'version' => QUFORM_VERSION
 205:             );
 206:         }
 207: 
 208:         if (is_file($this->getCachePath($this->getCustomCssFilename()))) {
 209:             $styles['quform-custom'] = array(
 210:                 'url' => $this->getCacheUrl($this->getCustomCssFilename()),
 211:                 'path' => $this->getCachePath($this->getCustomCssFilename()),
 212:                 'version' => $this->options->get('cacheBuster')
 213:             );
 214:         }
 215: 
 216:         return $styles;
 217:     }
 218: 
 219:     /**
 220:      * Enqueue the plugin styles
 221:      */
 222:     protected function enqueueScripts()
 223:     {
 224:         wp_deregister_script('jquery-form');
 225:         wp_register_script('jquery-form', Quform::url('js/jquery.form.min.js'), array('jquery'), '4.3.0', true);
 226: 
 227:         // Load non-core theme JS files
 228:         foreach (array_unique($this->options->get('activeThemes')) as $key) {
 229:             if ( ! $this->themes->isCoreTheme($key)) {
 230:                 $data = $this->themes->getTheme($key);
 231: 
 232:                 if (is_array($data) && isset($data['jsUrl'])) {
 233:                     wp_enqueue_script($key, $data['jsUrl'], array('jquery'), isset($data['version']) ? $data['version'] : QUFORM_VERSION, true);
 234:                 }
 235:             }
 236:         }
 237: 
 238:         if ($this->options->get('combineJs')) {
 239:             wp_enqueue_script('quform', $this->getCacheUrl($this->getCombinedJsFilename()), array('jquery'), $this->options->get('cacheBuster'), true);
 240:         } else {
 241:             foreach ($this->getScripts() as $key => $script) {
 242:                 wp_enqueue_script($key, $script['url'], array('jquery'), $script['version'], true);
 243:             }
 244:         }
 245: 
 246:         wp_localize_script('quform', 'quformL10n', array(
 247:             'l10n_print_after' => 'quformL10n = ' . wp_json_encode($this->jsL10n())
 248:         ));
 249: 
 250:         do_action('quform_scripts_enqueued');
 251:     }
 252: 
 253:     /**
 254:      * Get the scripts to be enqueued
 255:      *
 256:      * @param   bool   $isPreview  True to get the scripts for the form preview
 257:      * @return  array
 258:      */
 259:     public function getScripts($isPreview = false)
 260:     {
 261:         global $wp_version;
 262:         $scripts = array();
 263: 
 264:         $scripts['jquery-form'] = array(
 265:             'url' => Quform::url('js/jquery.form.min.js'),
 266:             'path' => QUFORM_PATH . '/js/jquery.form.min.js',
 267:             'version' => '4.3.0'
 268:         );
 269: 
 270:         if ( ! $this->options->get('disabledScripts.fileUpload') && (count($this->options->get('activeEnhancedUploaders')) || $isPreview)) {
 271:             if (version_compare($wp_version, '5.6', '>=')) {
 272:                 $scripts['jquery-ui-core'] = array(
 273:                     'url' => site_url('wp-includes/js/jquery/ui/core.min.js'),
 274:                     'path' => ABSPATH . WPINC . '/js/jquery/ui/core.min.js',
 275:                     'version' => '1.12.1'
 276:                 );
 277:             } else {
 278:                 $scripts['jquery-ui-widget'] = array(
 279:                     'url' => site_url('wp-includes/js/jquery/ui/widget.min.js'),
 280:                     'path' => ABSPATH . WPINC . '/js/jquery/ui/widget.min.js',
 281:                     'version' => '1.11.4'
 282:                 );
 283:             }
 284: 
 285:             $scripts['jquery-fileupload'] = array(
 286:                 'url' => Quform::url('js/jquery.fileupload.min.js'),
 287:                 'path' => QUFORM_PATH . '/js/jquery.fileupload.min.js',
 288:                 'version' => '10.32.0'
 289:             );
 290:         }
 291: 
 292:         if ( ! $this->options->get('disabledScripts.scrollTo')) {
 293:             $scripts['jquery-scroll-to'] = array(
 294:                 'url' => Quform::url('js/jquery.scrollTo.min.js'),
 295:                 'path' => QUFORM_PATH . '/js/jquery.scrollTo.min.js',
 296:                 'version' => '2.1.2'
 297:             );
 298:         }
 299: 
 300:         if ( ! $this->options->get('disabledScripts.select2') && (count($this->options->get('activeEnhancedSelects')) || $isPreview)) {
 301:             $scripts['quform-select2'] = array(
 302:                 'url' => Quform::url('js/select2.min.js'),
 303:                 'path' => QUFORM_PATH . '/js/select2.min.js',
 304:                 'version' => '4.0.13'
 305:             );
 306:         }
 307: 
 308:         if ( ! $this->options->get('disabledScripts.qtip')) {
 309:             $scripts['qtip'] = array(
 310:                 'url' => Quform::url('js/jquery.qtip.min.js'),
 311:                 'path' => QUFORM_PATH . '/js/jquery.qtip.min.js',
 312:                 'version' => '3.0.4'
 313:             );
 314:         }
 315: 
 316:         if ($this->options->get('popupEnabled')) {
 317:             if ($this->options->get('popupScript') == 'fancybox-1' && ! $this->options->get('disabledScripts.fancybox')) {
 318:                 $scripts['fancybox'] = array(
 319:                     'url' => Quform::url('js/jquery.fancybox1.min.js'),
 320:                     'path' => QUFORM_PATH . '/js/jquery.fancybox1.min.js',
 321:                     'version' => '1.3.9'
 322:                 );
 323:             } elseif ($this->options->get('popupScript') == 'fancybox-2' && ! $this->options->get('disabledScripts.fancybox2')) {
 324:                 $scripts['fancybox2'] = array(
 325:                     'url' => Quform::url('js/jquery.fancybox.pack.js'),
 326:                     'path' => QUFORM_PATH . '/js/jquery.fancybox.pack.js',
 327:                     'version' => '2.1.9'
 328:                 );
 329:             } elseif ($this->options->get('popupScript') == 'fancybox-3' && ! $this->options->get('disabledScripts.fancybox3')) {
 330:                 $scripts['fancybox3'] = array(
 331:                     'url' => Quform::url('js/jquery.fancybox3.min.js'),
 332:                     'path' => QUFORM_PATH . '/js/jquery.fancybox3.min.js',
 333:                     'version' => '3.5.7'
 334:                 );
 335:             } elseif ($this->options->get('popupScript') == 'magnific-popup' && ! $this->options->get('disabledScripts.magnificPopup')) {
 336:                 $scripts['magnific-popup'] = array(
 337:                     'url' => Quform::url('js/jquery.magnific-popup.min.js'),
 338:                     'path' => QUFORM_PATH . '/js/jquery.magnific-popup.min.js',
 339:                     'version' => '1.1.0'
 340:                 );
 341:             }
 342:         }
 343: 
 344:         if ( ! $this->options->get('disabledScripts.infieldLabels')) {
 345:             $scripts['infield-label'] = array(
 346:                 'url' => Quform::url('js/jquery.infieldlabel.min.js'),
 347:                 'path' => QUFORM_PATH . '/js/jquery.infieldlabel.min.js',
 348:                 'version' => '0.1.5'
 349:             );
 350:         }
 351: 
 352:         $loadDatepicker = ! $this->options->get('disabledScripts.datepicker') && (count($this->options->get('activeDatepickers')) || $isPreview);
 353:         $loadTimepicker = ! $this->options->get('disabledScripts.timepicker') && (count($this->options->get('activeTimepickers')) || $isPreview);
 354: 
 355:         if ($loadDatepicker || $loadTimepicker) {
 356:             $scripts['kendo-core'] = array(
 357:                 'url' => Quform::url('js/kendo.core.min.js'),
 358:                 'path' => QUFORM_PATH . '/js/kendo.core.min.js',
 359:                 'version' => '2020.2.617'
 360:             );
 361: 
 362:             $scripts['kendo-calendar'] = array(
 363:                 'url' => Quform::url('js/kendo.calendar.min.js'),
 364:                 'path' => QUFORM_PATH . '/js/kendo.calendar.min.js',
 365:                 'version' => '2020.2.617'
 366:             );
 367: 
 368:             $scripts['kendo-popup'] = array(
 369:                 'url' => Quform::url('js/kendo.popup.min.js'),
 370:                 'path' => QUFORM_PATH . '/js/kendo.popup.min.js',
 371:                 'version' => '2020.2.617'
 372:             );
 373: 
 374:             if ($loadDatepicker) {
 375:                 $scripts['kendo-datepicker'] = array(
 376:                     'url' => Quform::url('js/kendo.datepicker.min.js'),
 377:                     'path' => QUFORM_PATH . '/js/kendo.datepicker.min.js',
 378:                     'version' => '2020.2.617'
 379:                 );
 380:             }
 381: 
 382:             if ($loadTimepicker) {
 383:                 $scripts['kendo-timepicker'] = array(
 384:                     'url' => Quform::url('js/kendo.timepicker.min.js'),
 385:                     'path' => QUFORM_PATH . '/js/kendo.timepicker.min.js',
 386:                     'version' => '2020.2.617'
 387:                 );
 388:             }
 389: 
 390:             $loadDefaultLocale = false;
 391:             $activeLocales = array();
 392:             foreach ($this->options->get('activeLocales') as $locales) {
 393:                 $activeLocales = array_merge($activeLocales, $locales);
 394:             }
 395: 
 396:             $activeLocales = array_unique(apply_filters('quform_active_locales', $activeLocales));
 397: 
 398:             foreach ($activeLocales as $locale) {
 399:                 if ($locale == '') {
 400:                     $loadDefaultLocale = true;
 401:                     continue;
 402:                 } elseif ($locale == 'en-US') {
 403:                     continue;
 404:                 }
 405: 
 406:                 $scripts['kendo-culture-' . $locale] = array(
 407:                     'url' => Quform::url('js/cultures/kendo.culture.' . $locale . '.min.js'),
 408:                     'path' => QUFORM_PATH . '/js/cultures/kendo.culture.'  . $locale . '.min.js',
 409:                     'version' => '2020.2.617'
 410:                 );
 411:             }
 412: 
 413:             if ($loadDefaultLocale && $this->options->get('locale') && $this->options->get('locale') != 'en-US') {
 414:                 $scripts['kendo-culture-' . $this->options->get('locale')] = array(
 415:                     'url' => Quform::url('js/cultures/kendo.culture.'  . $this->options->get('locale') . '.min.js'),
 416:                     'path' => QUFORM_PATH . '/js/cultures/kendo.culture.'  . $this->options->get('locale') . '.min.js',
 417:                     'version' => '2020.2.617'
 418:                 );
 419:             }
 420:         }
 421: 
 422:         // Load core theme JS files
 423:         foreach (array_unique($this->options->get('activeThemes')) as $key) {
 424:             if ($this->themes->isCoreTheme($key)) {
 425:                 $data = $this->themes->getTheme($key);
 426: 
 427:                 if (is_array($data) && isset($data['jsUrl'])) {
 428:                     $scripts['quform-theme-' . $key] = array(
 429:                         'url' => $data['jsUrl'],
 430:                         'path' => $data['jsPath'],
 431:                         'version' => isset($data['version']) ? $data['version'] : QUFORM_VERSION
 432:                     );
 433:                 }
 434:             }
 435:         }
 436: 
 437:         $scripts['quform'] = array(
 438:             'url' => Quform::url('js/quform.min.js'),
 439:             'path' => QUFORM_PATH . '/js/quform.min.js',
 440:             'version' => QUFORM_VERSION
 441:         );
 442: 
 443:         if ( ! $isPreview && is_file($this->getCachePath($this->getCustomJsFilename()))) {
 444:             $scripts['quform-custom'] = array(
 445:                 'url' => $this->getCacheUrl($this->getCustomJsFilename()),
 446:                 'path' => $this->getCachePath($this->getCustomJsFilename()),
 447:                 'version' => $this->options->get('cacheBuster')
 448:             );
 449:         }
 450: 
 451:         return $scripts;
 452:     }
 453: 
 454:     /**
 455:      * JavaScript localisation
 456:      *
 457:      * @return array
 458:      */
 459:     public function jsL10n()
 460:     {
 461:         return apply_filters('quform_script_loader_js_l10n', array(
 462:             'pluginUrl' => Quform::url(),
 463:             'ajaxUrl' => admin_url('admin-ajax.php', null),
 464:             'ajaxError' => __('Ajax error', 'quform'),
 465:             'errorMessageTitle' => __('There was a problem', 'quform'),
 466:             'removeFile' => __('Remove', 'quform'),
 467:             'supportPageCaching' => $this->options->get('supportPageCaching') && ! Quform::isPostRequest()
 468:         ));
 469:     }
 470: 
 471:     /**
 472:      * Print head scripts
 473:      */
 474:     public function printHeadScripts()
 475:     {
 476:         if ($this->shouldLoadScripts()) {
 477:             echo '<script>!function(e,c){e[c]=e[c]+(e[c]&&" ")+"quform-js"}(document.documentElement,"className");</script>';
 478:         }
 479:     }
 480: 
 481:     /**
 482:      * Check if the page content has one of the shortcodes
 483:      *
 484:      * @return bool
 485:      */
 486:     protected function detectFormInContent()
 487:     {
 488:         $post = Quform::getCurrentPost();
 489: 
 490:         if ($post instanceof WP_Post) {
 491:             if (has_shortcode($post->post_content, 'quform') || has_shortcode($post->post_content, 'quform_popup')) {
 492:                 return true;
 493:             }
 494: 
 495:             if (function_exists('has_block') && has_block('quform/form', $post->post_content)) {
 496:                 return true;
 497:             }
 498:         }
 499: 
 500:         return false;
 501:     }
 502: 
 503:     /**
 504:      * Check if there is one of the widgets on the current page
 505:      *
 506:      * @return bool
 507:      */
 508:     protected function detectFormInWidget()
 509:     {
 510:         return is_active_widget(false, false, 'quform-widget') || is_active_widget(false, false, 'quform-popup-widget');
 511:     }
 512: 
 513:     /**
 514:      * Update the feature cache and generate the CSS/JS files
 515:      *
 516:      * @param array $config
 517:      */
 518:     public function handleSaveForm(array $config)
 519:     {
 520:         $form = $this->factory->create($config);
 521: 
 522:         $this->updateActiveFeatureCache(array($config));
 523:         $this->generateFormCssFile(array($form));
 524:         $this->generateFiles();
 525:     }
 526: 
 527:     /**
 528:      * Update the feature cache and move the custom CSS flag to active
 529:      *
 530:      * @param array $ids
 531:      */
 532:     public function handleActivateForms(array $ids)
 533:     {
 534:         $this->updateActiveFeatureCache($this->repository->getFormsById($ids));
 535:         $activeCustomCss = $this->options->get('activeCustomCss');
 536:         $inactiveCustomCss = $this->options->get('inactiveCustomCss');
 537: 
 538:         foreach ($ids as $id) {
 539:             if (isset($inactiveCustomCss[$id])) {
 540:                 unset($inactiveCustomCss[$id]);
 541:                 $activeCustomCss[$id] = true;
 542:             }
 543:         }
 544: 
 545:         $this->options->set(compact('activeCustomCss', 'inactiveCustomCss'));
 546: 
 547:         $this->generateFiles();
 548:     }
 549: 
 550:     /**
 551:      * Update the feature cache and move the custom CSS flag to inactive
 552:      *
 553:      * @param array $ids
 554:      */
 555:     public function handleDeactivateForms(array $ids)
 556:     {
 557:         $this->updateActiveFeatureCache($this->repository->getFormsById($ids));
 558:         $activeCustomCss = $this->options->get('activeCustomCss');
 559:         $inactiveCustomCss = $this->options->get('inactiveCustomCss');
 560: 
 561:         foreach ($ids as $id) {
 562:             if (isset($activeCustomCss[$id])) {
 563:                 unset($activeCustomCss[$id]);
 564:                 $inactiveCustomCss[$id] = true;
 565:             }
 566:         }
 567: 
 568:         $this->options->set(compact('activeCustomCss', 'inactiveCustomCss'));
 569: 
 570:         $this->generateFiles();
 571:     }
 572: 
 573:     /**
 574:      * Update the feature cache and generate the custom CSS file
 575:      *
 576:      * @param array $ids
 577:      */
 578:     public function handleDuplicateForms(array $ids)
 579:     {
 580:         $configs = $this->repository->getFormsById($ids);
 581: 
 582:         $this->updateActiveFeatureCache($configs);
 583: 
 584:         $forms = array();
 585:         foreach ($configs as $config) {
 586:             $forms[] = $this->factory->create($config);
 587:         }
 588: 
 589:         $this->generateFormCssFile($forms);
 590:         $this->generateFiles();
 591:     }
 592: 
 593:     /**
 594:      * Remove anything related to the form from the feature cache, and delete the form custom CSS file
 595:      *
 596:      * @param array $ids
 597:      */
 598:     public function handleTrashForms(array $ids)
 599:     {
 600:         $this->updateActiveFeatureCache($this->repository->getFormsById($ids));
 601:         $activeCustomCss = $this->options->get('activeCustomCss');
 602:         $inactiveCustomCss = $this->options->get('inactiveCustomCss');
 603: 
 604:         foreach ($ids as $id) {
 605:             unset($activeCustomCss[$id]);
 606:             unset($inactiveCustomCss[$id]);
 607: 
 608:             $customCssFile = $this->getCachePath($this->getFormCssFilename($id));
 609:             if (is_file($customCssFile)) {
 610:                 @unlink($customCssFile);
 611:             }
 612:         }
 613: 
 614:         $this->options->set(compact('activeCustomCss', 'inactiveCustomCss'));
 615: 
 616:         $this->generateFiles();
 617:     }
 618: 
 619:     /**
 620:      * Handle untrashing forms
 621:      *
 622:      * @param array $ids
 623:      */
 624:     public function handleUntrashForms(array $ids)
 625:     {
 626:         // The process is the same for duplicate
 627:         $this->handleDuplicateForms($ids);
 628:     }
 629: 
 630:     /**
 631:      * Generate the custom CSS/JS files when the settings are saved
 632:      */
 633:     public function handleSaveSettings()
 634:     {
 635:         $this->generateFiles();
 636:     }
 637: 
 638:     /**
 639:      * Generate the custom/combined CSS and JS files
 640:      */
 641:     public function generateFiles()
 642:     {
 643:         if ( ! $this->isCacheDirWritable()) {
 644:             Quform::debug('Could not generate custom CSS/JS files: cache directory not writable');
 645:             return;
 646:         }
 647: 
 648:         $this->generateCustomCssFile();
 649: 
 650:         if ($this->options->get('combineCss')) {
 651:             $this->generateCombinedCssFile();
 652:         } else {
 653:             $combinedCssPath = $this->getCachePath($this->getCombinedCssFilename());
 654:             if (is_file($combinedCssPath)) {
 655:                 @unlink($combinedCssPath);
 656:             }
 657:         }
 658: 
 659:         $this->generateCustomJsFile();
 660: 
 661:         if ($this->options->get('combineJs')) {
 662:             $this->generateCombinedJsFile();
 663:         } else {
 664:             $combinedJsPath = $this->getCachePath($this->getCombinedJsFilename());
 665:             if (is_file($combinedJsPath)) {
 666:                 @unlink($combinedJsPath);
 667:             }
 668:         }
 669: 
 670:         $this->options->set('cacheBuster', time());
 671:     }
 672: 
 673:     /**
 674:      * Generate the custom CSS file for the given forms
 675:      *
 676:      * @param Quform_Form[] $forms The array of forms
 677:      */
 678:     protected function generateFormCssFile($forms)
 679:     {
 680:         if ( ! $this->isCacheDirWritable()) {
 681:             Quform::debug('Could not generate form custom CSS file: cache directory not writable');
 682:             return;
 683:         }
 684: 
 685:         $activeCustomCss = $this->options->get('activeCustomCss');
 686:         $inactiveCustomCss = $this->options->get('inactiveCustomCss');
 687: 
 688:         foreach ($forms as $form) {
 689:             $id = $form->getId();
 690:             $css = $form->getCss();
 691:             $path = $this->getCachePath($this->getFormCssFilename($id));
 692: 
 693:             if ($css) {
 694:                 $css = $this->minifyCss($css);
 695: 
 696:                 $fp = fopen($path, 'w');
 697:                 fwrite($fp, $css);
 698:                 fclose($fp);
 699: 
 700:                 $this->setPermissions($path);
 701: 
 702:                 if ($form->isActive()) {
 703:                     $activeCustomCss[$id] = true;
 704:                     unset($inactiveCustomCss[$id]);
 705:                 } else {
 706:                     unset($activeCustomCss[$id]);
 707:                     $inactiveCustomCss[$id] = true;
 708:                 }
 709:             } else {
 710:                 if (is_file($path)) {
 711:                     @unlink($path);
 712:                 }
 713: 
 714:                 unset($activeCustomCss[$id]);
 715:                 unset($inactiveCustomCss[$id]);
 716:             }
 717:         }
 718: 
 719:         $this->options->set(compact('activeCustomCss', 'inactiveCustomCss'));
 720:     }
 721: 
 722:     /**
 723:      * Generate the custom CSS file which is all form custom CSS files combined into one
 724:      */
 725:     protected function generateCustomCssFile()
 726:     {
 727:         if ( ! $this->isCacheDirWritable()) {
 728:             Quform::debug('Could not generate custom CSS file: cache directory not writable');
 729:             return;
 730:         }
 731: 
 732:         // Combine all of the active form individual CSS files into a single file
 733:         $combined = '';
 734:         foreach (array_keys($this->options->get('activeCustomCss')) as $formId) {
 735:             $cssFilePath = $this->getCachePath($this->getFormCssFilename($formId));
 736:             if (is_file($cssFilePath)) {
 737:                 $contents = file_get_contents($cssFilePath);
 738: 
 739:                 if ($contents) {
 740:                     $combined .= $contents;
 741:                 }
 742:             }
 743:         }
 744: 
 745:         // Add in custom CSS from the Settings page
 746:         if (Quform::isNonEmptyString($this->options->get('customCss'))) {
 747:             $combined .= $this->minifyCss($this->options->get('customCss'));
 748:         }
 749: 
 750:         if (Quform::isNonEmptyString($this->options->get('customCssTablet'))) {
 751:             $combined .= sprintf('@media screen and (min-width: 569px) and (max-width: 1024px) { %s }', $this->minifyCss($this->options->get('customCssTablet')));
 752:         }
 753: 
 754:         if (Quform::isNonEmptyString($this->options->get('customCssPhone'))) {
 755:             $combined .= sprintf('@media screen and (max-width: 568px) { %s }', $this->minifyCss($this->options->get('customCssPhone')));
 756:         }
 757: 
 758:         $combinedPath = $this->getCachePath($this->getCustomCssFilename());
 759: 
 760:         if ($combined != '') {
 761:             $fp = fopen($combinedPath, 'w');
 762:             fwrite($fp, $combined);
 763:             fclose($fp);
 764: 
 765:             $this->setPermissions($combinedPath);
 766:         } else {
 767:             if (is_file($combinedPath)) {
 768:                 @unlink($combinedPath);
 769:             }
 770:         }
 771:     }
 772: 
 773:     /**
 774:      * Generate the custom JS file with the code from the Settings page
 775:      */
 776:     protected function generateCustomJsFile()
 777:     {
 778:         $contents = $this->options->get('customJs');
 779:         $path = $this->getCachePath($this->getCustomJsFilename());
 780: 
 781:         if ($contents != '') {
 782:             $fp = fopen($path, 'w');
 783:             fwrite($fp, $contents);
 784:             fclose($fp);
 785: 
 786:             $this->setPermissions($path);
 787:         } else {
 788:             if (is_file($path)) {
 789:                 @unlink($path);
 790:             }
 791:         }
 792:     }
 793: 
 794:     /**
 795:      * Generate the CSS file containing all plugin CSS files combined
 796:      */
 797:     protected function generateCombinedCssFile()
 798:     {
 799:         $path = $this->getCachePath($this->getCombinedCssFilename());
 800:         $styles = $this->getStyles();
 801: 
 802:         $fp = fopen($path, 'w');
 803: 
 804:         foreach ($styles as $style) {
 805:             if ( ! is_file($style['path']) || ! ($contents = file_get_contents($style['path']))) {
 806:                 continue;
 807:             }
 808: 
 809:             if ($this->getCacheUrl() != Quform::url('cache')) {
 810:                 $contents = str_replace(
 811:                     array('../fonts/', '../images/'),
 812:                     array(Quform::url('/fonts/'), Quform::url('/images/')),
 813:                     $contents
 814:                 );
 815:             }
 816: 
 817:             fwrite($fp, $contents);
 818:         }
 819: 
 820:         fclose($fp);
 821: 
 822:         $this->setPermissions($path);
 823:     }
 824: 
 825:     /**
 826:      * Generate the JS file containing all plugin JS files combined
 827:      */
 828:     protected function generateCombinedJsFile()
 829:     {
 830:         $path = $this->getCachePath($this->getCombinedJsFilename());
 831:         $scripts = $this->getScripts();
 832: 
 833:         $fp = fopen($path, 'w');
 834: 
 835:         foreach ($scripts as $script) {
 836:             if ( ! is_file($script['path']) || ! ($contents = file_get_contents($script['path']))) {
 837:                 continue;
 838:             }
 839: 
 840:             fwrite($fp, $contents . PHP_EOL);
 841:         }
 842: 
 843:         fclose($fp);
 844: 
 845:         $this->setPermissions($path);
 846:     }
 847: 
 848:     /**
 849:      * Get the path to the cache directory
 850:      *
 851:      * @param   string  $extra  Extra path to append to the path
 852:      * @return  string          Path without trailing slash
 853:      */
 854:     public function getCachePath($extra = '')
 855:     {
 856:         $cachePath = apply_filters('quform_cache_path', QUFORM_PATH . '/cache', $this);
 857: 
 858:         return Quform::pathExtra($cachePath, $extra);
 859:     }
 860: 
 861:     /**
 862:      * Get the URL to the cache directory
 863:      *
 864:      * @param   string  $extra  Extra path to append to the path
 865:      * @return  string          Path without trailing slash
 866:      */
 867:     protected function getCacheUrl($extra = '')
 868:     {
 869:         $cacheUrl = apply_filters('quform_cache_url', Quform::url('cache'), $this);
 870: 
 871:         return Quform::pathExtra($cacheUrl, $extra);
 872:     }
 873: 
 874:     /**
 875:      * Is the cache directory writable?
 876:      *
 877:      * @return bool
 878:      */
 879:     protected function isCacheDirWritable()
 880:     {
 881:         $cachePath = $this->getCachePath();
 882:         $isWritable = is_dir($cachePath) && wp_is_writable($cachePath);
 883: 
 884:         return apply_filters('quform_is_cache_dir_writable', $isWritable, $this);
 885:     }
 886: 
 887:     /**
 888:      * Get the filename of the individual custom CSS file for the given form ID
 889:      *
 890:      * @param   int     $formId
 891:      * @return  string
 892:      */
 893:     protected function getFormCssFilename($formId)
 894:     {
 895:         if (is_multisite()) {
 896:             return sprintf('form.%d.%d.css', get_current_blog_id(), $formId);
 897:         }
 898: 
 899:         return sprintf('form.%d.css', $formId);
 900:     }
 901: 
 902:     /**
 903:      * Get the filename of the combined custom CSS file
 904:      *
 905:      * @return string
 906:      */
 907:     protected function getCustomCssFilename()
 908:     {
 909:         if (is_multisite()) {
 910:             return sprintf('custom.%d.css', get_current_blog_id());
 911:         }
 912: 
 913:         return 'custom.css';
 914:     }
 915: 
 916:     /**
 917:      * Get the filename of the combined CSS file
 918:      *
 919:      * @return string
 920:      */
 921:     protected function getCombinedCssFilename()
 922:     {
 923:         if (is_multisite()) {
 924:             return sprintf('quform.%d.css', get_current_blog_id());
 925:         }
 926: 
 927:         return 'quform.css';
 928:     }
 929: 
 930:     /**
 931:      * Get the filename of the custom JS file
 932:      *
 933:      * @return string
 934:      */
 935:     protected function getCustomJsFilename()
 936:     {
 937:         if (is_multisite()) {
 938:             return sprintf('custom.%d.js', get_current_blog_id());
 939:         }
 940: 
 941:         return 'custom.js';
 942:     }
 943: 
 944:     /**
 945:      * Get the filename of the combined JS file
 946:      *
 947:      * @return string
 948:      */
 949:     protected function getCombinedJsFilename()
 950:     {
 951:         if (is_multisite()) {
 952:             return sprintf('quform.%d.js', get_current_blog_id());
 953:         }
 954: 
 955:         return 'quform.js';
 956:     }
 957: 
 958:     /**
 959:      * Update the active feature cache for the given form configs
 960:      *
 961:      * @param array $configs
 962:      */
 963:     protected function updateActiveFeatureCache(array $configs)
 964:     {
 965:         $activeThemes = $this->options->get('activeThemes');
 966:         $activeLocales = $this->options->get('activeLocales');
 967:         $activeDatepickers = $this->options->get('activeDatepickers');
 968:         $activeTimepickers = $this->options->get('activeTimepickers');
 969:         $activeEnhancedUploaders = $this->options->get('activeEnhancedUploaders');
 970:         $activeEnhancedSelects = $this->options->get('activeEnhancedSelects');
 971: 
 972:         foreach ($configs as $config) {
 973:             $id = $config['id'];
 974:             $active = $config['active'] && ! $config['trashed'];
 975: 
 976:             if ($active && Quform::isNonEmptyString($config['theme'])) {
 977:                 $activeThemes[$id] = $config['theme'];
 978:             } else {
 979:                 unset($activeThemes[$id]);
 980:             }
 981: 
 982:             if ($active && $config['hasDatepicker']) {
 983:                 $activeDatepickers[$id] = true;
 984:             } else {
 985:                 unset($activeDatepickers[$id]);
 986:             }
 987: 
 988:             if ($active && $config['hasTimepicker']) {
 989:                 $activeTimepickers[$id] = true;
 990:             } else {
 991:                 unset($activeTimepickers[$id]);
 992:             }
 993: 
 994:             if ($active && $config['hasEnhancedUploader']) {
 995:                 $activeEnhancedUploaders[$id] = true;
 996:             } else {
 997:                 unset($activeEnhancedUploaders[$id]);
 998:             }
 999: 
1000:             if ($active && $config['hasEnhancedSelect']) {
1001:                 $activeEnhancedSelects[$id] = true;
1002:             } else {
1003:                 unset($activeEnhancedSelects[$id]);
1004:             }
1005: 
1006:             if ($active && count($config['locales'])) {
1007:                 $activeLocales[$id] = $config['locales'];
1008:             } else {
1009:                 unset($activeLocales[$id]);
1010:             }
1011:         }
1012: 
1013:         $this->options->set(compact(
1014:             'activeThemes',
1015:             'activeLocales',
1016:             'activeDatepickers',
1017:             'activeTimepickers',
1018:             'activeEnhancedUploaders',
1019:             'activeEnhancedSelects'
1020:         ));
1021:     }
1022: 
1023:     /**
1024:      * Rebuild the feature cache and custom CSS files
1025:      */
1026:     public function rebuildScriptCache()
1027:     {
1028:         $configs = $this->repository->allForms();
1029: 
1030:         // Reset the feature cache
1031:         $this->options->set(array(
1032:             'activeThemes' => array(),
1033:             'activeLocales' => array(),
1034:             'activeDatepickers' => array(),
1035:             'activeTimepickers' => array(),
1036:             'activeEnhancedUploaders' => array()
1037:         ));
1038: 
1039:         $this->updateActiveFeatureCache($configs);
1040: 
1041:         $forms = array();
1042:         foreach ($configs as $config) {
1043:             $forms[] = $this->factory->create($config);
1044:         }
1045: 
1046:         $this->generateFormCssFile($forms);
1047:         $this->generateFiles();
1048:     }
1049: 
1050:     /**
1051:      * On plugin activation generate the combined script files
1052:      */
1053:     public function activate()
1054:     {
1055:         $this->rebuildScriptCache();
1056:     }
1057: 
1058:     /**
1059:      * Minify the given CSS
1060:      *
1061:      * @param   string  $css
1062:      * @return  string
1063:      */
1064:     protected function minifyCss($css)
1065:     {
1066:         static $instance;
1067: 
1068:         if ($instance === null) {
1069:             if ( ! class_exists('CSSminNoConflict')) {
1070:                 require_once QUFORM_LIBRARY_PATH . '/cssmin.php';
1071:             }
1072: 
1073:             $instance = new CSSminNoConflict();
1074:         }
1075: 
1076:         return $instance->run($css);
1077:     }
1078: 
1079:     /**
1080:      * Set the permissions of the generated file
1081:      *
1082:      * @param string $file
1083:      */
1084:     protected function setPermissions($file) {
1085:         $stat = stat(dirname($file));
1086:         $perms = $stat['mode'] & 0000666;
1087:         chmod($file, $perms);
1088:     }
1089: }
1090: 
API documentation generated by ApiGen