| 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 |
/** |
| 10 |
* Entry point for all front end functionality. |
| 11 |
* |
| 12 |
* All routing for the front end comes through the functions below. When a |
| 13 |
* front end page is loaded in the browser it will come through here. |
| 14 |
* |
| 15 |
* @author Peter Featherstone <peter@featherstone.me> |
| 16 |
* |
| 17 |
* @since 3.0 |
| 18 |
*/ |
| 19 |
class FrontController { |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor for setting up the FrontController. |
| 23 |
* |
| 24 |
* The constructor allows us to switch implementations for managing options |
| 25 |
* and for rendering views. Useful for switching out mocked or stubbed |
| 26 |
* classes during testing. |
| 27 |
* |
| 28 |
* @author Peter Featherstone <peter@featherstone.me> |
| 29 |
* |
| 30 |
* @since 3.0 |
| 31 |
* |
| 32 |
* @param OptionManager $manager Instance of a Management options class. |
| 33 |
* @param View $view Instance of a View class for rendering. |
| 34 |
*/ |
| 35 |
public function __construct(OptionManager $manager, View $view) { |
| 36 |
$this->manager = $manager; |
| 37 |
$this->view = $view; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Main route for the front end. |
| 42 |
* |
| 43 |
* This is the default view for the plugin on an initial GET request to the |
| 44 |
* front end page. |
| 45 |
* |
| 46 |
* @author Peter Featherstone <peter@featherstone.me> |
| 47 |
* |
| 48 |
* @since 3.0 |
| 49 |
* |
| 50 |
* @return string Output HTML from rendered view. |
| 51 |
*/ |
| 52 |
public function index() { |
| 53 |
$options = $this->manager->all(); |
| 54 |
$this->buildFrontEnd($options); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Preview route for the front end. |
| 59 |
* |
| 60 |
* This is the preview view for the plugin when the preview admin option is |
| 61 |
* pressed. |
| 62 |
* |
| 63 |
* @author Peter Featherstone <peter@featherstone.me> |
| 64 |
* |
| 65 |
* @since 3.0 |
| 66 |
* |
| 67 |
* @return string Output HTML from rendered view. |
| 68 |
*/ |
| 69 |
public function preview() { |
| 70 |
return $this->view->render('preview.html.twig'); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Helper private method to setup and build the front end. |
| 75 |
* |
| 76 |
* This is the preview view for the plugin when the preview admin option is |
| 77 |
* pressed. We turn external files off to enable the preview options to |
| 78 |
* take effect. |
| 79 |
* |
| 80 |
* TODO: This is a horrible method that really needs to be broken up in some |
| 81 |
* way. There is a lot of setup and WordPress specific functionality going |
| 82 |
* on here that would ideally be abstracted away. |
| 83 |
* |
| 84 |
* @author Peter Featherstone <peter@featherstone.me> |
| 85 |
* |
| 86 |
* @since 3.0 |
| 87 |
* |
| 88 |
* @param OptionsCollection $options A OptionsCollection object. |
| 89 |
*/ |
| 90 |
private function buildFrontEnd(OptionsCollection $options) { |
| 91 |
add_filter('body_class', function($classes) use($options) { |
| 92 |
$classes[] = 'responsive-menu-' . $options['animation_type'] . '-' . $options['menu_appear_from']; |
| 93 |
return $classes; |
| 94 |
}); |
| 95 |
|
| 96 |
if($options['external_files'] == 'on'): |
| 97 |
add_action('wp_enqueue_scripts', function() use($options) { |
| 98 |
$css_file = wp_upload_dir()['baseurl'] . '/responsive-menu/css/responsive-menu-' . get_current_blog_id() . '.css'; |
| 99 |
$js_file = wp_upload_dir()['baseurl'] . '/responsive-menu/js/responsive-menu-' . get_current_blog_id() . '.js'; |
| 100 |
wp_enqueue_style('responsive-menu', $css_file, null, false); |
| 101 |
wp_enqueue_script('responsive-menu', $js_file, ['jquery'], false, $options['scripts_in_footer'] == 'on' ? true : false); |
| 102 |
}); |
| 103 |
else: |
| 104 |
add_action('wp_head', function() use($options) { |
| 105 |
$css_data = $this->view->render('css/app.css.twig', ['options' => $options]); |
| 106 |
if($options['minify_scripts'] == 'on') |
| 107 |
$css_data = Minifier::minify($css_data); |
| 108 |
|
| 109 |
echo '<style>' . $css_data . '</style>'; |
| 110 |
}, 100); |
| 111 |
|
| 112 |
add_action($options['scripts_in_footer'] == 'on' ? 'wp_footer' : 'wp_head', function() use($options) { |
| 113 |
$js_data = $this->view->render('js/app.js.twig', ['options' => $options]); |
| 114 |
if($options['minify_scripts'] == 'on') |
| 115 |
$js_data = Minifier::minify($js_data); |
| 116 |
|
| 117 |
echo '<script>' . $js_data . '</script>'; |
| 118 |
}, 100); |
| 119 |
endif; |
| 120 |
|
| 121 |
if($options['shortcode'] == 'on'): |
| 122 |
add_shortcode('responsive_menu', function($atts) use($options) { |
| 123 |
|
| 124 |
if(is_array($atts)) |
| 125 |
$merged_options = array_merge($options->toArray(), $atts); |
| 126 |
else |
| 127 |
$merged_options = $options->toArray(); |
| 128 |
|
| 129 |
$new_collection = new OptionsCollection($merged_options); |
| 130 |
return $this->view->render('app.html.twig', ['options' => $new_collection]); |
| 131 |
}); |
| 132 |
else: |
| 133 |
add_action('wp_footer', function() use($options) { |
| 134 |
echo $this->view->render('app.html.twig', ['options' => $options]); |
| 135 |
}); |
| 136 |
endif; |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|