| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
die('-1'); |
| 5 |
} |
| 6 |
|
| 7 |
class QuadMenu_Compiler |
| 8 |
{ |
| 9 |
|
| 10 |
public $redux = ''; |
| 11 |
public $args = array(); |
| 12 |
public static $instance; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
|
| 17 |
add_filter('quadmenu_global_js_data', array($this, 'js_data')); |
| 18 |
|
| 19 |
add_action('init', array($this, 'activation')); |
| 20 |
|
| 21 |
add_action('redux/page/' . QUADMENU_DB_OPTIONS . '/enqueue', array($this, 'enqueue')); |
| 22 |
|
| 23 |
add_filter('redux/options/' . QUADMENU_DB_OPTIONS . '/ajax_save/response', array($this, 'developer_variables')); |
| 24 |
|
| 25 |
add_filter('redux/options/' . QUADMENU_DB_OPTIONS . '/ajax_save/response', array($this, 'compile_variables')); |
| 26 |
|
| 27 |
add_filter('redux/options/' . QUADMENU_DB_OPTIONS . '/compiler', array($this, 'compiler'), 5, 3); |
| 28 |
|
| 29 |
add_action('wp_ajax_quadmenu_compiler_save', array($this, 'compiler_save')); |
| 30 |
|
| 31 |
add_action('wp_ajax_nopriv_quadmenu_compiler_save', array($this, 'compiler_save')); |
| 32 |
} |
| 33 |
|
| 34 |
public static function instance() |
| 35 |
{ |
| 36 |
if (!isset(self::$instance)) { |
| 37 |
self::$instance = new QuadMenu_Compiler(); |
| 38 |
} |
| 39 |
return self::$instance; |
| 40 |
} |
| 41 |
|
| 42 |
function activation() |
| 43 |
{ |
| 44 |
|
| 45 |
if (!get_transient('_quadmenu_activation')) |
| 46 |
return; |
| 47 |
|
| 48 |
Quadmenu_Compiler::do_compiler(true); |
| 49 |
} |
| 50 |
|
| 51 |
function js_data($data) |
| 52 |
{ |
| 53 |
|
| 54 |
global $quadmenu; |
| 55 |
|
| 56 |
$data['debug'] = QUADMENU_DEV; |
| 57 |
|
| 58 |
if ($compiler = $this->run_compiler()) { |
| 59 |
$data['variables'] = self::less_variables($quadmenu); |
| 60 |
$data['compiler'] = $compiler; |
| 61 |
} |
| 62 |
|
| 63 |
$data['files'] = apply_filters('quadmenu_compiler_files', array()); |
| 64 |
$data['nonce'] = wp_create_nonce('quadmenu'); |
| 65 |
|
| 66 |
return $data; |
| 67 |
} |
| 68 |
|
| 69 |
public function enqueue() |
| 70 |
{ |
| 71 |
|
| 72 |
wp_register_script('quadmenu-less', QUADMENU_PLUGIN_URL . 'assets/backend/js/less.min.js', array(), QUADMENU_PLUGIN_VERSION, true); |
| 73 |
|
| 74 |
wp_enqueue_script('quadmenu-compiler', QUADMENU_PLUGIN_URL . 'assets/backend/js/quadmenu-compiler' . QuadMenu::isMin() . '.js', array('quadmenu-less'), QUADMENU_PLUGIN_VERSION, true); |
| 75 |
|
| 76 |
wp_localize_script('quadmenu-compiler', 'quadmenu', apply_filters('quadmenu_global_js_data', array())); |
| 77 |
} |
| 78 |
|
| 79 |
function developer_variables($return_array) |
| 80 |
{ |
| 81 |
|
| 82 |
if (is_array($return_array)) { |
| 83 |
$return_array['options'] = apply_filters('quadmenu_developer_options', $return_array['options']); |
| 84 |
} |
| 85 |
|
| 86 |
return $return_array; |
| 87 |
} |
| 88 |
|
| 89 |
function compile_variables($return_array) |
| 90 |
{ |
| 91 |
|
| 92 |
if (is_array($return_array)) { |
| 93 |
$return_array['variables'] = self::less_variables($return_array['options']); |
| 94 |
} |
| 95 |
|
| 96 |
return $return_array; |
| 97 |
} |
| 98 |
|
| 99 |
public function compiler_save() |
| 100 |
{ |
| 101 |
|
| 102 |
if (!check_ajax_referer('quadmenu', 'nonce', false)) { |
| 103 |
QuadMenu::send_json_error(esc_html__('Please reload page.', 'quadmenu')); |
| 104 |
} |
| 105 |
|
| 106 |
$return_array = array('status' => 'error'); |
| 107 |
|
| 108 |
if (!isset($_REQUEST['output']['imports'][0])) { |
| 109 |
QuadMenu_Redux::add_notification('red', esc_html__('Imports is undefined.', 'quadmenu')); |
| 110 |
wp_die(); |
| 111 |
} |
| 112 |
|
| 113 |
if (!isset($_REQUEST['output']['css'])) { |
| 114 |
QuadMenu_Redux::add_notification('red', esc_html__('CSS is undefined.', 'quadmenu')); |
| 115 |
wp_die(); |
| 116 |
} |
| 117 |
|
| 118 |
$file_ext = pathinfo($_REQUEST['output']['imports'][0], PATHINFO_EXTENSION); |
| 119 |
|
| 120 |
if (!in_array($file_ext, array('less', 'css'))) { |
| 121 |
wp_die('Cheating?'); |
| 122 |
} |
| 123 |
|
| 124 |
$return_array['status'] = 'success'; |
| 125 |
|
| 126 |
$file_name = str_replace(".{$file_ext}", '.css', basename($_REQUEST['output']['imports'][0])); |
| 127 |
|
| 128 |
try { |
| 129 |
$this->save_file($file_name, QUADMENU_UPLOAD_DIR, stripslashes($_REQUEST['output']['css'])); |
| 130 |
} catch (Exception $e) { |
| 131 |
$return_array['status'] = $e->getMessage(); |
| 132 |
} |
| 133 |
|
| 134 |
ob_start(); |
| 135 |
|
| 136 |
QuadMenu_Redux::notification_bar(); |
| 137 |
|
| 138 |
$notification_bar = ob_get_contents(); |
| 139 |
|
| 140 |
ob_end_clean(); |
| 141 |
|
| 142 |
$return_array['notification_bar'] = $notification_bar; |
| 143 |
|
| 144 |
Quadmenu_Compiler::do_compiler(false); |
| 145 |
|
| 146 |
echo json_encode($return_array); |
| 147 |
|
| 148 |
wp_die(); |
| 149 |
} |
| 150 |
|
| 151 |
public static function do_compiler($run = true) |
| 152 |
{ |
| 153 |
|
| 154 |
if ($run) { |
| 155 |
update_option('_quadmenu_compiler', $run); |
| 156 |
} else { |
| 157 |
delete_option('_quadmenu_compiler'); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public function run_compiler() |
| 162 |
{ |
| 163 |
|
| 164 |
return (int) get_option('_quadmenu_compiler', false); |
| 165 |
} |
| 166 |
|
| 167 |
public function compiler($options, $css, $changed) |
| 168 |
{ |
| 169 |
|
| 170 |
Quadmenu_Compiler::do_compiler(true); |
| 171 |
|
| 172 |
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'))); |
| 173 |
} |
| 174 |
|
| 175 |
public function save_file($name = false, $dir = false, $content = false) |
| 176 |
{ |
| 177 |
|
| 178 |
if (!$name || !$dir || !$content) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
if (!class_exists('ReduxFrameworkInstances')) { |
| 183 |
QuadMenu_Redux::add_notification('error', esc_html__('ReduxFramework is not installed', 'quadmenu')); |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
$this->redux = ReduxFrameworkInstances::get_instance(QUADMENU_DB_OPTIONS); |
| 188 |
|
| 189 |
// check if file exists ------------------------------------------------ |
| 190 |
$is_file = is_file(trailingslashit($dir) . $name); |
| 191 |
|
| 192 |
// create the folder --------------------------------------------------- |
| 193 |
if (!is_dir($dir)) { |
| 194 |
$this->redux->filesystem->execute('mkdir', $dir); |
| 195 |
QuadMenu_Redux::add_notification('yellow', sprintf(esc_html__('Folder created : %1$s', 'quadmenu'), $dir)); |
| 196 |
} |
| 197 |
|
| 198 |
// write file ---------------------------------------------------------- |
| 199 |
if ($this->redux->filesystem->execute('put_contents', trailingslashit($dir) . $name, array('content' => $content))) { |
| 200 |
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'))); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
QuadMenu_Redux::add_notification('error', sprintf(esc_html__('File cant\'t been created : %1$s', 'quadmenu'), trailingslashit($dir) . $name)); |
| 205 |
} |
| 206 |
|
| 207 |
static public function less_variables(&$data, $header = '') |
| 208 |
{ |
| 209 |
|
| 210 |
$html = array(); //QuadMenu_Compiler::less_themes(); |
| 211 |
|
| 212 |
if (!is_array($data)) |
| 213 |
return $data; |
| 214 |
|
| 215 |
if (isset($data['styles'])) { |
| 216 |
unset($data['styles']); |
| 217 |
} |
| 218 |
|
| 219 |
if (isset($data['styles_normalize'])) { |
| 220 |
unset($data['styles_normalize']); |
| 221 |
} |
| 222 |
|
| 223 |
if (isset($data['styles_widgets'])) { |
| 224 |
unset($data['styles_widgets']); |
| 225 |
} |
| 226 |
|
| 227 |
if (isset($data['styles_pscrollbar'])) { |
| 228 |
unset($data['styles_pscrollbar']); |
| 229 |
} |
| 230 |
|
| 231 |
if (isset($data['styles_owlcarousel'])) { |
| 232 |
unset($data['styles_owlcarousel']); |
| 233 |
} |
| 234 |
|
| 235 |
//if (isset($data['styles_icons'])) { |
| 236 |
// unset($data['styles_icons']); |
| 237 |
//} |
| 238 |
|
| 239 |
if (isset($data['viewport'])) { |
| 240 |
unset($data['viewport']); |
| 241 |
} |
| 242 |
|
| 243 |
if (isset($data['css'])) { |
| 244 |
unset($data['css']); |
| 245 |
} |
| 246 |
|
| 247 |
if (isset($data['social'])) { |
| 248 |
unset($data['social']); |
| 249 |
} |
| 250 |
|
| 251 |
if (isset($data['errors'])) { |
| 252 |
unset($data['errors']); |
| 253 |
} |
| 254 |
|
| 255 |
if (isset($data['warnings'])) { |
| 256 |
unset($data['warnings']); |
| 257 |
} |
| 258 |
|
| 259 |
if (isset($data['notification_bar'])) { |
| 260 |
unset($data['notification_bar']); |
| 261 |
} |
| 262 |
|
| 263 |
foreach ($data as $key => &$val) { |
| 264 |
|
| 265 |
$value = ($key != 'font-options') ? $val : ''; |
| 266 |
|
| 267 |
$value = (filter_var($value, FILTER_VALIDATE_URL)) ? "'{$value}'" : $value; |
| 268 |
|
| 269 |
if (is_array($value)) { |
| 270 |
$html = array_merge($html, self::less_variables($value, "{$key}_")); |
| 271 |
} elseif ($value != '') { |
| 272 |
$html["@{$header}{$key}"] = $value; |
| 273 |
} else { |
| 274 |
$html["@{$header}{$key}"] = 0; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return $html; |
| 279 |
} |
| 280 |
|
| 281 |
function redux_compiler($return_array = array()) |
| 282 |
{ |
| 283 |
|
| 284 |
global $quadmenu; |
| 285 |
|
| 286 |
if (is_array($return_array)) { |
| 287 |
$return_array['options'] = apply_filters('quadmenu_developer_options', $quadmenu); |
| 288 |
$return_array['variables'] = self::less_variables($return_array['options']); |
| 289 |
} |
| 290 |
|
| 291 |
return $return_array; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
new QuadMenu_Compiler(); |
| 296 |
|