| 1 |
<?php |
| 2 |
|
| 3 |
class VP_WP_Loader |
| 4 |
{ |
| 5 |
|
| 6 |
private static $_instance; |
| 7 |
|
| 8 |
private $_js_data = array(); |
| 9 |
|
| 10 |
private $_css_data = array(); |
| 11 |
|
| 12 |
private $_localize = array(); |
| 13 |
|
| 14 |
private $_scripts; |
| 15 |
|
| 16 |
private $_styles; |
| 17 |
|
| 18 |
private $_dependencies; |
| 19 |
|
| 20 |
private $_use_media_upload = false; |
| 21 |
|
| 22 |
private $_use_wp_35_media_upload = false; |
| 23 |
|
| 24 |
private $_types; |
| 25 |
|
| 26 |
public static function instance() |
| 27 |
{ |
| 28 |
if (is_null(self::$_instance)) |
| 29 |
{ |
| 30 |
self::$_instance = new self(); |
| 31 |
} |
| 32 |
return self::$_instance; |
| 33 |
} |
| 34 |
|
| 35 |
private function __construct() |
| 36 |
{ |
| 37 |
$this->_dependencies = apply_filters( 'vp_dependencies_array', VP_Util_Config::instance()->load('dependencies') ); |
| 38 |
$this->_types = array( |
| 39 |
'option' => array(), |
| 40 |
'metabox' => array(), |
| 41 |
'shortcodegenerator' => array(), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function build() |
| 46 |
{ |
| 47 |
|
| 48 |
// get scripts and styles dependencies configs |
| 49 |
$req_scripts = $this->_dependencies['scripts']['always']; |
| 50 |
$req_styles = $this->_dependencies['styles']['always']; |
| 51 |
$scripts = $this->_dependencies['scripts']['paths']; |
| 52 |
$styles = $this->_dependencies['styles']['paths']; |
| 53 |
$rules = $this->_dependencies['rules']; |
| 54 |
$types = $this->get_flat_types(); |
| 55 |
|
| 56 |
// for all types build required scripts and styles array |
| 57 |
foreach ($types as $type) |
| 58 |
{ |
| 59 |
if( array_key_exists($type, $rules) ) |
| 60 |
{ |
| 61 |
$req_scripts = array_merge($req_scripts, $rules[$type]['js']); |
| 62 |
$req_styles = array_merge($req_styles, $rules[$type]['css']); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// also determine whether to use media upload and the WP35 version or not |
| 67 |
if( in_array('upload', $types) ) |
| 68 |
{ |
| 69 |
global $wp_version; |
| 70 |
$this->_use_media_upload = true; |
| 71 |
if (!version_compare($wp_version, '3.5', '<')) |
| 72 |
{ |
| 73 |
$this->_use_wp_35_media_upload = true; |
| 74 |
wp_enqueue_media(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// build localize data |
| 79 |
$this->build_localize_data(); |
| 80 |
|
| 81 |
// register all depended js |
| 82 |
foreach ($req_scripts as $script) |
| 83 |
{ |
| 84 |
$this->js_unit_register($script); |
| 85 |
} |
| 86 |
|
| 87 |
// register and add shared-js at the end of dependencies |
| 88 |
$this->js_unit_register('shared', $req_scripts); |
| 89 |
|
| 90 |
// register all styles |
| 91 |
foreach ($styles as $name => $style) |
| 92 |
{ |
| 93 |
if(in_array($name, $req_styles) and ! wp_style_is($name, 'registered')) |
| 94 |
wp_register_style($name, $style['path'], $style['deps']); |
| 95 |
} |
| 96 |
|
| 97 |
// register all mains |
| 98 |
foreach ($this->_js_data as $name => $js) |
| 99 |
{ |
| 100 |
// build main js localize |
| 101 |
$localize = array(); |
| 102 |
foreach ($js['local_data'] as $datum) |
| 103 |
{ |
| 104 |
if(array_key_exists($datum, $this->_localize)) |
| 105 |
{ |
| 106 |
$localize[$datum] = $this->_localize[$datum]; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if( isset($js['custom_local']) ) |
| 111 |
{ |
| 112 |
$localize = array_merge( $localize, $js['custom_local'] ); |
| 113 |
} |
| 114 |
|
| 115 |
$deps = array(); |
| 116 |
if( isset($js['deps']) ) $deps = $js['deps']; |
| 117 |
$deps[] = 'shared'; |
| 118 |
|
| 119 |
foreach ($deps as $dep) |
| 120 |
{ |
| 121 |
$this->js_unit_register($dep); |
| 122 |
} |
| 123 |
|
| 124 |
// register, enqueue and localized scripts |
| 125 |
wp_register_script($name, $js['path'], $deps, '', true); |
| 126 |
wp_localize_script($name, $js['local_name'], $localize); |
| 127 |
wp_enqueue_script($name); |
| 128 |
} |
| 129 |
|
| 130 |
foreach ($this->_css_data as $name => $css) |
| 131 |
{ |
| 132 |
foreach ($css['deps'] as $dep) |
| 133 |
{ |
| 134 |
$this->css_unit_register($dep); |
| 135 |
} |
| 136 |
$req_styles = array_merge($req_styles, $css['deps']); |
| 137 |
wp_register_style($name, $css['path'], $req_styles); |
| 138 |
wp_enqueue_style($name); |
| 139 |
} |
| 140 |
|
| 141 |
do_action( 'vp_after_dependencies_loader_build' ); |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
public function add_localize_data($key, $value) |
| 146 |
{ |
| 147 |
$this->_localize[$key] = $value; |
| 148 |
} |
| 149 |
|
| 150 |
private function build_localize_data() |
| 151 |
{ |
| 152 |
$messages = VP_Util_Config::instance()->load('messages'); |
| 153 |
$localize = array( |
| 154 |
'use_upload' => $this->_use_media_upload, |
| 155 |
'use_new_media_upload' => $this->_use_wp_35_media_upload, |
| 156 |
'public_url' => VP_PUBLIC_URL, |
| 157 |
'wp_include_url' => includes_url(), |
| 158 |
'nonce' => wp_create_nonce( 'vafpress' ), |
| 159 |
'val_msg' => $messages['validation'], |
| 160 |
'util_msg' => $messages['util'], |
| 161 |
'ctrl_msg' => $messages['control'], |
| 162 |
// validatable data |
| 163 |
'alphabet_validatable' => apply_filters( 'vp_alphabet_validatable' , array( 'vp-textbox', 'vp-textarea' ) ), |
| 164 |
'alphanumeric_validatable' => apply_filters( 'vp_alphanumeric_validatable', array( 'vp-textbox', 'vp-textarea' ) ), |
| 165 |
'numeric_validatable' => apply_filters( 'vp_numeric_validatable' , array( 'vp-textbox', 'vp-textarea' ) ), |
| 166 |
'email_validatable' => apply_filters( 'vp_email_validatable' , array( 'vp-textbox', 'vp-textarea' ) ), |
| 167 |
'url_validatable' => apply_filters( 'vp_url_validatable' , array( 'vp-textbox', 'vp-textarea' ) ), |
| 168 |
'maxlength_validatable' => apply_filters( 'vp_maxlength_validatable' , array( 'vp-toggle', 'vp-radiobutton', 'vp-radioimage', 'vp-select' ) ), |
| 169 |
'minlength_validatable' => apply_filters( 'vp_minlength_validatable' , array( 'vp-toggle', 'vp-radiobutton', 'vp-radioimage', 'vp-select' ) ), |
| 170 |
); |
| 171 |
$this->_localize = array_merge($this->_localize, $localize); |
| 172 |
} |
| 173 |
|
| 174 |
private function js_unit_register($name, $extra_deps = null) |
| 175 |
{ |
| 176 |
global $wp_scripts; |
| 177 |
|
| 178 |
$scripts = $this->_dependencies['scripts']['paths']; |
| 179 |
|
| 180 |
if( isset($scripts[$name]) ) |
| 181 |
{ |
| 182 |
|
| 183 |
$registered = wp_script_is($name, 'registered'); |
| 184 |
$is_older = false; |
| 185 |
$script = $scripts[$name]; |
| 186 |
$override = isset($script['override']) ? $script['override'] : false; |
| 187 |
if( $registered ) |
| 188 |
{ |
| 189 |
$is_older = version_compare($script['ver'], $wp_scripts->registered[$name]->ver) == 1; |
| 190 |
} |
| 191 |
if( !$registered or ($is_older and $override) ) |
| 192 |
{ |
| 193 |
if( !is_null($extra_deps) ) |
| 194 |
{ |
| 195 |
$script['deps'] = array_unique( array_merge( $script['deps'], $extra_deps ) ); |
| 196 |
} |
| 197 |
if( !empty($script['deps']) ) |
| 198 |
{ |
| 199 |
foreach ($script['deps'] as $dep) |
| 200 |
{ |
| 201 |
$this->js_unit_register($dep); |
| 202 |
} |
| 203 |
} |
| 204 |
if( $is_older ) |
| 205 |
{ |
| 206 |
wp_deregister_script($name); |
| 207 |
} |
| 208 |
|
| 209 |
wp_register_script($name, $script['path'], $script['deps'], $script['ver'], true); |
| 210 |
|
| 211 |
if(isset($script['localize'])) |
| 212 |
{ |
| 213 |
$localize = array(); |
| 214 |
foreach ($script['localize']['keys'] as $key) |
| 215 |
{ |
| 216 |
if(array_key_exists($key, $this->_localize)) |
| 217 |
{ |
| 218 |
$localize[$key] = $this->_localize[$key]; |
| 219 |
} |
| 220 |
} |
| 221 |
wp_localize_script($name, $script['localize']['name'], $localize); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
private function css_unit_register($name, $extra_deps = null) |
| 228 |
{ |
| 229 |
$styles = $this->_dependencies['styles']['paths']; |
| 230 |
|
| 231 |
if( isset($styles[$name]) ) |
| 232 |
{ |
| 233 |
$style = $styles[$name]; |
| 234 |
|
| 235 |
if( !is_null($extra_deps) ) |
| 236 |
{ |
| 237 |
$style['deps'] = array_unique( array_merge( $style['deps'], $extra_deps ) ); |
| 238 |
} |
| 239 |
if( !empty($style['deps']) ) |
| 240 |
{ |
| 241 |
foreach ($style['deps'] as $dep) |
| 242 |
{ |
| 243 |
$this->css_unit_register($dep); |
| 244 |
} |
| 245 |
} |
| 246 |
wp_register_style($name, $style['path'], $style['deps'], isset($style['ver']) ? $style['ver'] : false); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// how to setup the localization data? |
| 251 |
public function add_js_data($js_name, $key, $data) |
| 252 |
{ |
| 253 |
$this->add_data($js_name, $key, $data, 'js'); |
| 254 |
} |
| 255 |
|
| 256 |
public function add_css_data($css_name, $key, $data) |
| 257 |
{ |
| 258 |
$this->add_data($css_name, $key, $data, 'css'); |
| 259 |
} |
| 260 |
|
| 261 |
public function add_data($name, $key, $data, $type) |
| 262 |
{ |
| 263 |
|
| 264 |
$array_data = array(); |
| 265 |
|
| 266 |
if( $type === 'js' ) |
| 267 |
$array_data = array('local_data'); |
| 268 |
|
| 269 |
$var_name = '_' . $type . '_data'; |
| 270 |
|
| 271 |
if( in_array($key, $array_data) ) |
| 272 |
{ |
| 273 |
if( !isset($this->{$var_name}[$name][$key]) || !is_array($this->{$var_name}[$name][$key]) ) |
| 274 |
$this->{$var_name}[$name][$key] = array(); |
| 275 |
|
| 276 |
$this->{$var_name}[$name][$key] = array_unique( |
| 277 |
array_merge( |
| 278 |
$this->{$var_name}[$name][$key], |
| 279 |
(array) $data |
| 280 |
) |
| 281 |
); |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
$keys = explode('.', $key); |
| 286 |
$arr = &$this->{$var_name}[$name]; |
| 287 |
foreach ($keys as $key) |
| 288 |
{ |
| 289 |
$arr = &$arr[$key]; |
| 290 |
} |
| 291 |
$arr = $data; |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
// how to setup the main js and css data? |
| 297 |
public function add_main_css($css) |
| 298 |
{ |
| 299 |
|
| 300 |
if( is_string($css) ) |
| 301 |
{ |
| 302 |
$css_name = $css; |
| 303 |
$deps = $this->_dependencies['styles']['paths']; |
| 304 |
$css = $deps[$css_name]; |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
$css_name = $css['name']; |
| 309 |
} |
| 310 |
|
| 311 |
if( isset($css['deps']) ) |
| 312 |
$this->add_css_data($css_name, 'deps', $css['deps']); |
| 313 |
|
| 314 |
if( isset($css['path']) ) |
| 315 |
$this->add_css_data($css_name, 'path', $css['path']); |
| 316 |
} |
| 317 |
|
| 318 |
public function add_main_js($js) |
| 319 |
{ |
| 320 |
|
| 321 |
if( is_string($js) ) |
| 322 |
{ |
| 323 |
$js_name = $js; |
| 324 |
$deps = $this->_dependencies['scripts']['paths']; |
| 325 |
$js = $deps[$js_name]; |
| 326 |
} |
| 327 |
else |
| 328 |
{ |
| 329 |
$js_name = $js['name']; |
| 330 |
} |
| 331 |
|
| 332 |
if( isset($js['localize']) and is_array($js['localize']) ) |
| 333 |
{ |
| 334 |
if( isset($js['localize']['name']) ) |
| 335 |
$this->add_js_data($js_name, 'local_name', $js['localize']['name']); |
| 336 |
|
| 337 |
if( isset($js['localize']['keys']) and is_array($js['localize']['keys']) ) |
| 338 |
$this->add_js_data($js_name, 'local_data', $js['localize']['keys']); |
| 339 |
} |
| 340 |
|
| 341 |
if( isset($js['path']) ) |
| 342 |
$this->add_js_data($js_name, 'path', $js['path']); |
| 343 |
|
| 344 |
if( isset($js['deps']) ) |
| 345 |
$this->add_js_data($js_name, 'deps', $js['deps']); |
| 346 |
} |
| 347 |
|
| 348 |
// option class added their types to this |
| 349 |
public function add_types($types, $key) |
| 350 |
{ |
| 351 |
$types = (array) $types; |
| 352 |
$this->_types[$key] = array_unique( array_merge( $this->_types[$key], $types ) ); |
| 353 |
} |
| 354 |
|
| 355 |
public function get_types($key = null) |
| 356 |
{ |
| 357 |
if( is_null($key) ) |
| 358 |
return $this->_types; |
| 359 |
else |
| 360 |
return $this->_types[$key]; |
| 361 |
} |
| 362 |
|
| 363 |
public function get_flat_types() |
| 364 |
{ |
| 365 |
$flat_types = array(); |
| 366 |
foreach ($this->_types as $types) |
| 367 |
{ |
| 368 |
$flat_types = array_unique( array_merge( $flat_types, $types ) ); |
| 369 |
} |
| 370 |
return $flat_types; |
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* EOF |
| 377 |
*/ |