PluginProbe
QuadMenu – Mega Menu / 1.8.4
QuadMenu – Mega Menu v1.8.4
3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 All 87 releases
quadmenu / includes / compiler.php

compiler.php in QuadMenu – Mega Menu 1.8.4, at includes/compiler.php

230 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 die('-1');
5 }
6
7 class QuadMenu_Compiler {
8
9 public $redux = '';
10 public $args = array();
11 public static $instance;
12
13 public function __construct() {
14
15 add_filter('quadmenu_global_js_data', array($this, 'js_data'));
16
17 add_action('init', array($this, 'activation'));
18
19 add_action('redux/page/' . QUADMENU_OPTIONS . '/enqueue', array($this, 'enqueue'));
20
21 add_filter('redux/options/' . QUADMENU_OPTIONS . '/ajax_save/response', array($this, 'developer_variables'));
22
23 add_filter('redux/options/' . QUADMENU_OPTIONS . '/ajax_save/response', array($this, 'compile_variables'));
24
25 add_filter('redux/options/' . QUADMENU_OPTIONS . '/compiler', array($this, 'compiler'), 5, 3);
26
27 add_action('wp_ajax_quadmenu_compiler_save', array($this, 'compiler_save'));
28
29 add_action('wp_ajax_nopriv_quadmenu_compiler_save', array($this, 'compiler_save'));
30 }
31
32 public static function instance() {
33 if (!isset(self::$instance)) {
34 self::$instance = new QuadMenu_Compiler();
35 }
36 return self::$instance;
37 }
38
39 function activation() {
40
41 if (!get_transient('_quadmenu_activation'))
42 return;
43
44 Quadmenu_Compiler::do_compiler(true);
45 }
46
47 function js_data($data) {
48
49 global $quadmenu;
50
51 $data['debug'] = QUADMENU_DEV;
52
53 if ($compiler = $this->run_compiler()) {
54 $data['variables'] = self::less_variables($quadmenu);
55 $data['compiler'] = $compiler;
56 }
57
58 $data['files'] = apply_filters('quadmenu_compiler_files', array());
59 $data['nonce'] = wp_create_nonce('quadmenu');
60
61 return $data;
62 }
63
64 public function enqueue() {
65
66 wp_register_script('quadmenu-less', QUADMENU_URL . 'assets/backend/js/less.min.js', array(), QUADMENU_VERSION, true);
67
68 wp_enqueue_script('quadmenu-compiler', QUADMENU_URL . 'assets/backend/js/quadmenu-compiler' . QuadMenu::isMin() . '.js', array('quadmenu-less'), QUADMENU_VERSION, true);
69
70 wp_localize_script('quadmenu-compiler', 'quadmenu', apply_filters('quadmenu_global_js_data', array()));
71 }
72
73 function developer_variables($return_array) {
74
75 if (is_array($return_array)) {
76 $return_array['options'] = apply_filters('quadmenu_developer_options', $return_array['options']);
77 }
78
79 return $return_array;
80 }
81
82 function compile_variables($return_array) {
83
84 if (is_array($return_array)) {
85 $return_array['variables'] = self::less_variables($return_array['options']);
86 }
87
88 return $return_array;
89 }
90
91 public function compiler_save() {
92
93 if (!check_ajax_referer('quadmenu', 'nonce', false)) {
94 QuadMenu::send_json_error(esc_html__('Please reload page.', 'quadmenu'));
95 }
96
97 $return_array = array('status' => 'error');
98
99 if (!isset($_REQUEST['output']['imports'][0])) {
100 QuadMenu_Redux::add_notification('red', esc_html__('Imports is undefined.', 'quadmenu'));
101 wp_die();
102 }
103
104 if (!isset($_REQUEST['output']['css'])) {
105 QuadMenu_Redux::add_notification('red', esc_html__('CSS is undefined.', 'quadmenu'));
106 wp_die();
107 }
108
109 $return_array['status'] = 'success';
110
111 try {
112 $this->save_file(str_replace('.less', '.css', basename($_REQUEST['output']['imports'][0])), QUADMENU_PATH_CSS, stripslashes($_REQUEST['output']['css']));
113 } catch (Exception $e) {
114 $return_array['status'] = $e->getMessage();
115 }
116
117 ob_start();
118
119 QuadMenu_Redux::notification_bar();
120
121 $notification_bar = ob_get_contents();
122
123 ob_end_clean();
124
125 $return_array['notification_bar'] = $notification_bar;
126
127 Quadmenu_Compiler::do_compiler(false);
128
129 echo json_encode($return_array);
130
131 wp_die();
132 }
133
134 public static function do_compiler($run = true) {
135
136 if ($run) {
137 update_option('_quadmenu_compiler', $run);
138 } else {
139 delete_option('_quadmenu_compiler');
140 }
141 }
142
143 public function run_compiler() {
144
145 return (int) get_option('_quadmenu_compiler', false);
146 }
147
148 public function compiler($options, $css, $changed) {
149
150 Quadmenu_Compiler::do_compiler(true);
151
152 QuadMenu_Redux::add_notification('yellow', sprintf(esc_html__('Some style options have been changed. Your stylesheet will be compiled to reflect changes. %s.', 'quadmenu'), esc_html__('Please wait', 'quadmenu')));
153 }
154
155 public function save_file($name = false, $dir = false, $content = false) {
156
157 if (!$name || !$dir || !$content) {
158 return;
159 }
160
161 if (!class_exists('ReduxFrameworkInstances')) {
162 QuadMenu_Redux::add_notification('error', esc_html__('ReduxFramework is not installed', 'quadmenu'));
163 return;
164 }
165
166 $this->redux = ReduxFrameworkInstances::get_instance(QUADMENU_OPTIONS);
167
168 // check if file exists ------------------------------------------------
169 $is_file = is_file(trailingslashit($dir) . $name);
170
171 // create the folder ---------------------------------------------------
172 if (!is_dir($dir)) {
173 $this->redux->filesystem->execute('mkdir', $dir);
174 QuadMenu_Redux::add_notification('yellow', sprintf(esc_html__('Folder created : %1$s', 'quadmenu'), $dir));
175 }
176
177 // write file ----------------------------------------------------------
178 if ($this->redux->filesystem->execute('put_contents', trailingslashit($dir) . $name, array('content' => $content))) {
179 QuadMenu_Redux::add_notification('green', sprintf(esc_html__('File has been %2$s : %1$s', 'quadmenu'), trailingslashit($dir) . $name, $is_file ? esc_html__('updated', 'quadmenu') : esc_html__('created', 'quadmenu')));
180 return;
181 }
182
183 QuadMenu_Redux::add_notification('error', sprintf(esc_html__('File cant\'t been created : %1$s', 'quadmenu'), trailingslashit($dir) . $name));
184 }
185
186 static public function less_variables(&$data, $header = '') {
187
188 $html = array(); //QuadMenu_Compiler::less_themes();
189
190 if (!is_array($data))
191 return $data;
192
193 if (isset($data['css'])) {
194 unset($data['css']);
195 }
196
197 foreach ($data as $key => &$val) {
198
199 $value = ($key != 'font-options') ? $val : '';
200
201 $value = (filter_var($value, FILTER_VALIDATE_URL)) ? "'{$value}'" : $value;
202
203 if (is_array($value)) {
204 $html = array_merge($html, self::less_variables($value, "{$key}_"));
205 } elseif ($value != '') {
206 $html["@{$header}{$key}"] = $value;
207 } else {
208 $html["@{$header}{$key}"] = 0;
209 }
210 }
211
212 return $html;
213 }
214
215 function redux_compiler($return_array = array()) {
216
217 global $quadmenu;
218
219 if (is_array($return_array)) {
220 $return_array['options'] = apply_filters('quadmenu_developer_options', $quadmenu);
221 $return_array['variables'] = self::less_variables($return_array['options']);
222 }
223
224 return $return_array;
225 }
226
227 }
228
229 new QuadMenu_Compiler();
230