| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Tasks; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 5 |
use ResponsiveMenu\Formatters\Minifier; |
| 6 |
use ResponsiveMenu\View\View; |
| 7 |
|
| 8 |
class UpdateOptionsTask { |
| 9 |
|
| 10 |
private $translatables = [ |
| 11 |
'menu_to_use', |
| 12 |
'button_title', |
| 13 |
'menu_title', |
| 14 |
'menu_title_link', |
| 15 |
'menu_additional_content' |
| 16 |
]; |
| 17 |
|
| 18 |
public function run(OptionsCollection $options, View $view) { |
| 19 |
/* |
| 20 |
* Build CSS and Js files |
| 21 |
* |
| 22 |
*/ |
| 23 |
if($options['external_files'] == 'on'): |
| 24 |
|
| 25 |
$base_dir = wp_upload_dir()['basedir'] . '/responsive-menu'; |
| 26 |
|
| 27 |
if(!is_dir($base_dir)) |
| 28 |
if(!mkdir($base_dir)) |
| 29 |
throw new \Exception('You don\'t have permissions to create data folder - please check permissions.'); |
| 30 |
|
| 31 |
if(!is_dir($base_dir . '/css')) |
| 32 |
if(!mkdir($base_dir . '/css')) |
| 33 |
throw new \Exception('You don\'t have permissions to create CSS data folder - please check permissions.'); |
| 34 |
|
| 35 |
if(!is_dir($base_dir . '/js')) |
| 36 |
if(!mkdir($base_dir . '/js')) |
| 37 |
throw new \Exception('You don\'t have permissions to create JS data folder - please check permissions.'); |
| 38 |
|
| 39 |
$css_file = $base_dir . '/css/responsive-menu-' . get_current_blog_id() . '.css'; |
| 40 |
$css_data = $view->render('css/app.css.twig', ['options' => $options]); |
| 41 |
|
| 42 |
if($options['minify_scripts'] == 'on') |
| 43 |
$css_data = Minifier::minify($css_data); |
| 44 |
|
| 45 |
if(!file_put_contents($css_file, $css_data)) |
| 46 |
throw new \Exception('You don\'t have permissions to write external CSS file - please check permissions.'); |
| 47 |
|
| 48 |
$js_file = $base_dir . '/js/responsive-menu-' . get_current_blog_id() . '.js'; |
| 49 |
$js_data = $view->render('js/app.js.twig', ['options' => $options]); |
| 50 |
|
| 51 |
if($options['minify_scripts'] == 'on') |
| 52 |
$js_data = Minifier::minify($js_data); |
| 53 |
|
| 54 |
if(!file_put_contents($js_file, $js_data)): |
| 55 |
throw new \Exception('You don\'t have permissions to write external JS file - please check permissions.'); |
| 56 |
endif; |
| 57 |
|
| 58 |
else: |
| 59 |
/* |
| 60 |
* TODO: Do some tidy up like removing external files if this option is not set - be a good citizen! |
| 61 |
*/ |
| 62 |
endif; |
| 63 |
|
| 64 |
/* |
| 65 |
* Update translations for WPML |
| 66 |
*/ |
| 67 |
foreach($this->translatables as $option_name) |
| 68 |
if(isset($options[$option_name])) |
| 69 |
do_action('wpml_register_single_string', 'Responsive Menu', $option_name, $options[$option_name]); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
} |