| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Controllers; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 5 |
use ResponsiveMenu\View\View; |
| 6 |
use ResponsiveMenu\Management\OptionManager; |
| 7 |
use ResponsiveMenu\Formatters\Minifier; |
| 8 |
|
| 9 |
class FrontController { |
| 10 |
|
| 11 |
public function __construct(OptionManager $manager, View $view) { |
| 12 |
$this->manager = $manager; |
| 13 |
$this->view = $view; |
| 14 |
} |
| 15 |
|
| 16 |
public function index() { |
| 17 |
$options = $this->manager->all(); |
| 18 |
$this->buildFrontEnd($options); |
| 19 |
} |
| 20 |
|
| 21 |
public function preview() { |
| 22 |
return $this->view->render('preview.html.twig'); |
| 23 |
} |
| 24 |
|
| 25 |
private function buildFrontEnd(OptionsCollection $options) { |
| 26 |
add_filter('body_class', function($classes) use($options) { |
| 27 |
$classes[] = 'responsive-menu-' . $options['animation_type'] . '-' . $options['menu_appear_from']; |
| 28 |
return $classes; |
| 29 |
}); |
| 30 |
|
| 31 |
if($options['external_files'] == 'on'): |
| 32 |
add_action('wp_enqueue_scripts', function() use($options) { |
| 33 |
$css_file = wp_upload_dir()['baseurl'] . '/responsive-menu/css/responsive-menu-' . get_current_blog_id() . '.css'; |
| 34 |
$js_file = wp_upload_dir()['baseurl'] . '/responsive-menu/js/responsive-menu-' . get_current_blog_id() . '.js'; |
| 35 |
wp_enqueue_style('responsive-menu', $css_file, null, false); |
| 36 |
wp_enqueue_script('responsive-menu', $js_file, ['jquery'], false, $options['scripts_in_footer'] == 'on' ? true : false); |
| 37 |
}); |
| 38 |
else: |
| 39 |
add_action('wp_head', function() use($options) { |
| 40 |
$css_data = $this->view->render('css/app.css.twig', ['options' => $options]); |
| 41 |
if($options['minify_scripts'] == 'on') |
| 42 |
$css_data = Minifier::minify($css_data); |
| 43 |
|
| 44 |
echo '<style>' . $css_data . '</style>'; |
| 45 |
}, 100); |
| 46 |
|
| 47 |
add_action($options['scripts_in_footer'] == 'on' ? 'wp_footer' : 'wp_head', function() use($options) { |
| 48 |
$js_data = $this->view->render('js/app.js.twig', ['options' => $options]); |
| 49 |
if($options['minify_scripts'] == 'on') |
| 50 |
$js_data = Minifier::minify($js_data); |
| 51 |
|
| 52 |
echo '<script>' . $js_data . '</script>'; |
| 53 |
}, 100); |
| 54 |
endif; |
| 55 |
|
| 56 |
if($options['shortcode'] == 'on'): |
| 57 |
add_shortcode('responsive_menu', function($atts) use($options) { |
| 58 |
|
| 59 |
if(is_array($atts)) |
| 60 |
$merged_options = array_merge($options->toArray(), $atts); |
| 61 |
else |
| 62 |
$merged_options = $options->toArray(); |
| 63 |
|
| 64 |
$new_collection = new OptionsCollection($merged_options); |
| 65 |
return $this->view->render('app.html.twig', ['options' => $new_collection]); |
| 66 |
}); |
| 67 |
else: |
| 68 |
add_action('wp_footer', function() use($options) { |
| 69 |
echo $this->view->render('app.html.twig', ['options' => $options]); |
| 70 |
}); |
| 71 |
endif; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|