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