1: <?php
2:
3: 4: 5:
6: class Quform_ScriptLoader
7: {
8: 9: 10:
11: protected $options;
12:
13: 14: 15:
16: protected $themes;
17:
18: 19: 20:
21: protected $factory;
22:
23: 24: 25:
26: protected $repository;
27:
28: 29: 30:
31: protected $session;
32:
33: 34: 35: 36: 37: 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: 49:
50: public function enqueue()
51: {
52: if ($this->shouldLoadScripts()) {
53: $this->enqueueStyles();
54: $this->enqueueScripts();
55: }
56: }
57:
58: 59: 60: 61: 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: 92:
93: protected function enqueueStyles()
94: {
95:
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: 119: 120: 121: 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:
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: 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:
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: 255: 256: 257: 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.31.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:
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: 456: 457: 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: 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: 483: 484: 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: 505: 506: 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: 515: 516: 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: 529: 530: 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: 552: 553: 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: 575: 576: 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: 595: 596: 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: 621: 622: 623:
624: public function handleUntrashForms(array $ids)
625: {
626:
627: $this->handleDuplicateForms($ids);
628: }
629:
630: 631: 632:
633: public function handleSaveSettings()
634: {
635: $this->generateFiles();
636: }
637:
638: 639: 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: 675: 676: 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: chmod($path, 0666);
700:
701: if ($form->isActive()) {
702: $activeCustomCss[$id] = true;
703: unset($inactiveCustomCss[$id]);
704: } else {
705: unset($activeCustomCss[$id]);
706: $inactiveCustomCss[$id] = true;
707: }
708: } else {
709: if (is_file($path)) {
710: @unlink($path);
711: }
712:
713: unset($activeCustomCss[$id]);
714: unset($inactiveCustomCss[$id]);
715: }
716: }
717:
718: $this->options->set(compact('activeCustomCss', 'inactiveCustomCss'));
719: }
720:
721: 722: 723:
724: protected function generateCustomCssFile()
725: {
726: if ( ! $this->isCacheDirWritable()) {
727: Quform::debug('Could not generate custom CSS file: cache directory not writable');
728: return;
729: }
730:
731:
732: $combined = '';
733: foreach (array_keys($this->options->get('activeCustomCss')) as $formId) {
734: $cssFilePath = $this->getCachePath($this->getFormCssFilename($formId));
735: if (is_file($cssFilePath)) {
736: $contents = file_get_contents($cssFilePath);
737:
738: if ($contents) {
739: $combined .= $contents;
740: }
741: }
742: }
743:
744:
745: if (Quform::isNonEmptyString($this->options->get('customCss'))) {
746: $combined .= $this->minifyCss($this->options->get('customCss'));
747: }
748:
749: if (Quform::isNonEmptyString($this->options->get('customCssTablet'))) {
750: $combined .= sprintf('@media screen and (min-width: 569px) and (max-width: 1024px) { %s }', $this->minifyCss($this->options->get('customCssTablet')));
751: }
752:
753: if (Quform::isNonEmptyString($this->options->get('customCssPhone'))) {
754: $combined .= sprintf('@media screen and (max-width: 568px) { %s }', $this->minifyCss($this->options->get('customCssPhone')));
755: }
756:
757: $combinedPath = $this->getCachePath($this->getCustomCssFilename());
758:
759: if ($combined != '') {
760: $fp = fopen($combinedPath, 'w');
761: fwrite($fp, $combined);
762: fclose($fp);
763: chmod($combinedPath, 0666);
764: } else {
765: if (is_file($combinedPath)) {
766: @unlink($combinedPath);
767: }
768: }
769: }
770:
771: 772: 773:
774: protected function generateCustomJsFile()
775: {
776: $contents = $this->options->get('customJs');
777: $path = $this->getCachePath($this->getCustomJsFilename());
778:
779: if ($contents != '') {
780: $fp = fopen($path, 'w');
781: fwrite($fp, $contents);
782: fclose($fp);
783: chmod($path, 0666);
784: } else {
785: if (is_file($path)) {
786: @unlink($path);
787: }
788: }
789: }
790:
791: 792: 793:
794: protected function generateCombinedCssFile()
795: {
796: $path = $this->getCachePath($this->getCombinedCssFilename());
797: $styles = $this->getStyles();
798:
799: $fp = fopen($path, 'w');
800:
801: foreach($styles as $style) {
802: if ( ! is_file($style['path']) || ! ($contents = file_get_contents($style['path']))) {
803: continue;
804: }
805:
806: if ($this->getCacheUrl() != Quform::url('cache')) {
807: $contents = str_replace(
808: array('../fonts/', '../images/'),
809: array(Quform::url('/fonts/'), Quform::url('/images/')),
810: $contents
811: );
812: }
813:
814: fwrite($fp, $contents);
815: }
816:
817: fclose($fp);
818: chmod($path, 0666);
819: }
820:
821: 822: 823:
824: protected function generateCombinedJsFile()
825: {
826: $path = $this->getCachePath($this->getCombinedJsFilename());
827: $scripts = $this->getScripts();
828:
829: $fp = fopen($path, 'w');
830:
831: foreach($scripts as $script) {
832: if ( ! is_file($script['path']) || ! ($contents = file_get_contents($script['path']))) {
833: continue;
834: }
835:
836: fwrite($fp, $contents . PHP_EOL);
837: }
838:
839: fclose($fp);
840: chmod($path, 0666);
841: }
842:
843: 844: 845: 846: 847: 848:
849: public function getCachePath($extra = '')
850: {
851: $cachePath = apply_filters('quform_cache_path', QUFORM_PATH . '/cache', $this);
852:
853: return Quform::pathExtra($cachePath, $extra);
854: }
855:
856: 857: 858: 859: 860: 861:
862: protected function getCacheUrl($extra = '')
863: {
864: $cacheUrl = apply_filters('quform_cache_url', Quform::url('cache'), $this);
865:
866: return Quform::pathExtra($cacheUrl, $extra);
867: }
868:
869: 870: 871: 872: 873:
874: protected function isCacheDirWritable()
875: {
876: $cachePath = $this->getCachePath();
877: $isWritable = is_dir($cachePath) && wp_is_writable($cachePath);
878:
879: return apply_filters('quform_is_cache_dir_writable', $isWritable, $this);
880: }
881:
882: 883: 884: 885: 886: 887:
888: protected function getFormCssFilename($formId)
889: {
890: if (is_multisite()) {
891: return sprintf('form.%d.%d.css', get_current_blog_id(), $formId);
892: }
893:
894: return sprintf('form.%d.css', $formId);
895: }
896:
897: 898: 899: 900: 901:
902: protected function getCustomCssFilename()
903: {
904: if (is_multisite()) {
905: return sprintf('custom.%d.css', get_current_blog_id());
906: }
907:
908: return 'custom.css';
909: }
910:
911: 912: 913: 914: 915:
916: protected function getCombinedCssFilename()
917: {
918: if (is_multisite()) {
919: return sprintf('quform.%d.css', get_current_blog_id());
920: }
921:
922: return 'quform.css';
923: }
924:
925: 926: 927: 928: 929:
930: protected function getCustomJsFilename()
931: {
932: if (is_multisite()) {
933: return sprintf('custom.%d.js', get_current_blog_id());
934: }
935:
936: return 'custom.js';
937: }
938:
939: 940: 941: 942: 943:
944: protected function getCombinedJsFilename()
945: {
946: if (is_multisite()) {
947: return sprintf('quform.%d.js', get_current_blog_id());
948: }
949:
950: return 'quform.js';
951: }
952:
953: 954: 955: 956: 957:
958: protected function updateActiveFeatureCache(array $configs)
959: {
960: $activeThemes = $this->options->get('activeThemes');
961: $activeLocales = $this->options->get('activeLocales');
962: $activeDatepickers = $this->options->get('activeDatepickers');
963: $activeTimepickers = $this->options->get('activeTimepickers');
964: $activeEnhancedUploaders = $this->options->get('activeEnhancedUploaders');
965: $activeEnhancedSelects = $this->options->get('activeEnhancedSelects');
966:
967: foreach ($configs as $config) {
968: $id = $config['id'];
969: $active = $config['active'] && ! $config['trashed'];
970:
971: if ($active && Quform::isNonEmptyString($config['theme'])) {
972: $activeThemes[$id] = $config['theme'];
973: } else {
974: unset($activeThemes[$id]);
975: }
976:
977: if ($active && $config['hasDatepicker']) {
978: $activeDatepickers[$id] = true;
979: } else {
980: unset($activeDatepickers[$id]);
981: }
982:
983: if ($active && $config['hasTimepicker']) {
984: $activeTimepickers[$id] = true;
985: } else {
986: unset($activeTimepickers[$id]);
987: }
988:
989: if ($active && $config['hasEnhancedUploader']) {
990: $activeEnhancedUploaders[$id] = true;
991: } else {
992: unset($activeEnhancedUploaders[$id]);
993: }
994:
995: if ($active && $config['hasEnhancedSelect']) {
996: $activeEnhancedSelects[$id] = true;
997: } else {
998: unset($activeEnhancedSelects[$id]);
999: }
1000:
1001: if ($active && count($config['locales'])) {
1002: $activeLocales[$id] = $config['locales'];
1003: } else {
1004: unset($activeLocales[$id]);
1005: }
1006: }
1007:
1008: $this->options->set(compact(
1009: 'activeThemes',
1010: 'activeLocales',
1011: 'activeDatepickers',
1012: 'activeTimepickers',
1013: 'activeEnhancedUploaders',
1014: 'activeEnhancedSelects'
1015: ));
1016: }
1017:
1018: 1019: 1020:
1021: public function rebuildScriptCache()
1022: {
1023: $configs = $this->repository->allForms();
1024:
1025:
1026: $this->options->set(array(
1027: 'activeThemes' => array(),
1028: 'activeLocales' => array(),
1029: 'activeDatepickers' => array(),
1030: 'activeTimepickers' => array(),
1031: 'activeEnhancedUploaders' => array()
1032: ));
1033:
1034: $this->updateActiveFeatureCache($configs);
1035:
1036: $forms = array();
1037: foreach ($configs as $config) {
1038: $forms[] = $this->factory->create($config);
1039: }
1040:
1041: $this->generateFormCssFile($forms);
1042: $this->generateFiles();
1043: }
1044:
1045: 1046: 1047:
1048: public function activate()
1049: {
1050: $this->rebuildScriptCache();
1051: }
1052:
1053: 1054: 1055: 1056: 1057: 1058:
1059: protected function minifyCss($css)
1060: {
1061: static $instance;
1062:
1063: if ($instance === null) {
1064: if ( ! class_exists('CSSminNoConflict')) {
1065: require_once QUFORM_LIBRARY_PATH . '/cssmin.php';
1066: }
1067:
1068: $instance = new CSSminNoConflict();
1069: }
1070:
1071: return $instance->run($css);
1072: }
1073: }
1074: