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:
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: 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:
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:
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: 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: 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: 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: 850: 851: 852: 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: 863: 864: 865: 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: 876: 877: 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: 889: 890: 891: 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: 904: 905: 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: 918: 919: 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: 932: 933: 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: 946: 947: 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: 960: 961: 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: 1025:
1026: public function rebuildScriptCache()
1027: {
1028: $configs = $this->repository->allForms();
1029:
1030:
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: 1052:
1053: public function activate()
1054: {
1055: $this->rebuildScriptCache();
1056: }
1057:
1058: 1059: 1060: 1061: 1062: 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: 1081: 1082: 1083:
1084: protected function setPermissions($file) {
1085: $stat = stat(dirname($file));
1086: $perms = $stat['mode'] & 0000666;
1087: chmod($file, $perms);
1088: }
1089: }
1090: