| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file manages assets of the Factory Bootstap. |
| 4 |
* |
| 5 |
* @author Alex Kovalev <alex@byonepress.com> |
| 6 |
* @author Paul Kashtanoff <paul@byonepress.com> |
| 7 |
* @copyright (c) 2018, OnePress Ltd |
| 8 |
* |
| 9 |
* @package factory-bootstrap |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
add_action('wbcr_factory_bootstrap_400_plugin_created', 'wbcr_factory_bootstrap_400_plugin_created'); |
| 14 |
|
| 15 |
/** |
| 16 |
* @param Wbcr_Factory400_Plugin $plugin |
| 17 |
*/ |
| 18 |
function wbcr_factory_bootstrap_400_plugin_created($plugin) |
| 19 |
{ |
| 20 |
$manager = new Wbcr_FactoryBootstrap400_Manager($plugin); |
| 21 |
$plugin->setBootstap($manager); |
| 22 |
} |
| 23 |
|
| 24 |
if( !class_exists('Wbcr_FactoryBootstrap400_Manager') ) { |
| 25 |
|
| 26 |
/** |
| 27 |
* The Bootstrap Manager class. |
| 28 |
* |
| 29 |
* @since 3.2.0 |
| 30 |
*/ |
| 31 |
class Wbcr_FactoryBootstrap400_Manager { |
| 32 |
|
| 33 |
/** |
| 34 |
* A plugin for which the manager was created. |
| 35 |
* |
| 36 |
* @since 3.2.0 |
| 37 |
* @var Wbcr_Factory400_Plugin |
| 38 |
*/ |
| 39 |
public $plugin; |
| 40 |
|
| 41 |
/** |
| 42 |
* Contains scripts to include. |
| 43 |
* |
| 44 |
* @since 3.2.0 |
| 45 |
* @var string[] |
| 46 |
*/ |
| 47 |
public $scripts = array(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Contains styles to include. |
| 51 |
* |
| 52 |
* @since 3.2.0 |
| 53 |
* @var string[] |
| 54 |
*/ |
| 55 |
public $styles = array(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Createas a new instance of the license api for a given plugin. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
public function __construct(Wbcr_Factory400_Plugin $plugin) |
| 63 |
{ |
| 64 |
$this->plugin = $plugin; |
| 65 |
|
| 66 |
add_action('admin_enqueue_scripts', array($this, 'loadAssets')); |
| 67 |
add_filter('admin_body_class', array($this, 'adminBodyClass')); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Includes the Bootstrap scripts. |
| 72 |
* @since 3.2.0 |
| 73 |
* @param array|string $scripts |
| 74 |
*/ |
| 75 |
public function enqueueScript($scripts) |
| 76 |
{ |
| 77 |
if( is_array($scripts) ) { |
| 78 |
foreach($scripts as $script) { |
| 79 |
if( !in_array($script, $this->scripts) ) { |
| 80 |
$this->scripts[] = $script; |
| 81 |
} |
| 82 |
} |
| 83 |
} else { |
| 84 |
if( !in_array($scripts, $this->scripts) ) { |
| 85 |
$this->scripts[] = $scripts; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* * Includes the Bootstrap styles. |
| 92 |
* |
| 93 |
* @since 3.2.0 |
| 94 |
* @param array|string $styles |
| 95 |
*/ |
| 96 |
public function enqueueStyle($styles) |
| 97 |
{ |
| 98 |
|
| 99 |
if( is_array($styles) ) { |
| 100 |
foreach($styles as $style) { |
| 101 |
if( !in_array($style, $this->styles) ) { |
| 102 |
$this->styles[] = $style; |
| 103 |
} |
| 104 |
} |
| 105 |
} else { |
| 106 |
if( !in_array($styles, $this->styles) ) { |
| 107 |
$this->styles[] = $styles; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Loads Bootstrap assets. |
| 114 |
* |
| 115 |
* @see admin_enqueue_scripts |
| 116 |
* |
| 117 |
* @since 3.2.0 |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function loadAssets($hook) |
| 121 |
{ |
| 122 |
|
| 123 |
do_action('wbcr_factory_400_bootstrap_enqueue_scripts', $hook); |
| 124 |
do_action('wbcr_factory_400_bootstrap_enqueue_scripts_' . $this->plugin->getPluginName(), $hook); |
| 125 |
|
| 126 |
$dependencies = array(); |
| 127 |
if( !empty($this->scripts) ) { |
| 128 |
$dependencies[] = 'jquery'; |
| 129 |
$dependencies[] = 'jquery-ui-core'; |
| 130 |
$dependencies[] = 'jquery-ui-widget'; |
| 131 |
} |
| 132 |
|
| 133 |
foreach($this->scripts as $script) { |
| 134 |
switch( $script ) { |
| 135 |
case 'plugin.iris': |
| 136 |
$dependencies[] = 'jquery-ui-widget'; |
| 137 |
$dependencies[] = 'jquery-ui-slider'; |
| 138 |
$dependencies[] = 'jquery-ui-draggable'; |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if( !empty($this->scripts) ) { |
| 144 |
$this->enqueueScripts($this->scripts, 'js', $dependencies); |
| 145 |
} |
| 146 |
if( !empty($this->styles) ) { |
| 147 |
$this->enqueueScripts($this->styles, 'css', $dependencies); |
| 148 |
} |
| 149 |
|
| 150 |
$user_id = get_current_user_id(); |
| 151 |
$color_name = get_user_meta($user_id, 'admin_color', true); |
| 152 |
|
| 153 |
if( $color_name !== 'fresh' ) { |
| 154 |
if( file_exists(FACTORY_BOOTSTRAP_400_DIR . '/assets/flat/css/bootstrap.' . $color_name . '.css') ) { |
| 155 |
wp_enqueue_style('wbcr-factory-bootstrap-400-colors', FACTORY_BOOTSTRAP_400_URL . '/assets/flat/css/bootstrap.' . $color_name . '.css'); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if( $color_name == 'light' ) { |
| 160 |
$primary_dark = '#037c9a'; |
| 161 |
$primary_light = '#04a4cc'; |
| 162 |
} elseif( $color_name == 'blue' ) { |
| 163 |
$primary_dark = '#d39323'; |
| 164 |
$primary_light = '#e1a948'; |
| 165 |
} elseif( $color_name == 'coffee' ) { |
| 166 |
$primary_dark = '#b78a66'; |
| 167 |
$primary_light = '#c7a589'; |
| 168 |
} elseif( $color_name == 'ectoplasm' ) { |
| 169 |
$primary_dark = '#839237'; |
| 170 |
$primary_light = '#a3b745'; |
| 171 |
} elseif( $color_name == 'ocean' ) { |
| 172 |
$primary_dark = '#80a583'; |
| 173 |
$primary_light = '#9ebaa0'; |
| 174 |
} elseif( $color_name == 'midnight' ) { |
| 175 |
$primary_dark = '#d02a21'; |
| 176 |
$primary_light = '#e14d43'; |
| 177 |
} elseif( $color_name == 'sunrise' ) { |
| 178 |
$primary_dark = '#c36822'; |
| 179 |
$primary_light = '#dd823b'; |
| 180 |
} else { |
| 181 |
$primary_dark = '#0074a2'; |
| 182 |
$primary_light = '#2ea2cc'; |
| 183 |
} |
| 184 |
|
| 185 |
?> |
| 186 |
|
| 187 |
<script> |
| 188 |
if( !window.factory ) { |
| 189 |
window.factory = {}; |
| 190 |
} |
| 191 |
if( !window.factory.factoryBootstrap400 ) { |
| 192 |
window.factory.factoryBootstrap400 = {}; |
| 193 |
} |
| 194 |
window.factory.factoryBootstrap400.colors = { |
| 195 |
primaryDark: '<?php echo $primary_dark ?>', |
| 196 |
primaryLight: '<?php echo $primary_light ?>' |
| 197 |
}; |
| 198 |
</script> |
| 199 |
<?php |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* @param array $sripts |
| 204 |
* @param string $type |
| 205 |
* @param array $dependencies |
| 206 |
*/ |
| 207 |
protected function enqueueScripts(array $sripts, $type = 'js', array $dependencies) |
| 208 |
{ |
| 209 |
|
| 210 |
$is_first = true; |
| 211 |
$cache_id = md5(implode(',', $this->scripts) . $type . $this->plugin->getPluginVersion()); |
| 212 |
$cache_dir_path = FACTORY_BOOTSTRAP_400_DIR . '/assets/cache/'; |
| 213 |
$cache_dir_url = FACTORY_BOOTSTRAP_400_URL . '/assets/cache/'; |
| 214 |
|
| 215 |
$cache_filepath = $cache_dir_path . $cache_id . ".min." . $type; |
| 216 |
$cache_fileurl = $cache_dir_url . $cache_id . ".min." . $type; |
| 217 |
|
| 218 |
if( file_exists($cache_filepath) ) { |
| 219 |
if( $type == 'js' ) { |
| 220 |
wp_enqueue_script('wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, $dependencies, $this->plugin->getPluginVersion()); |
| 221 |
} else { |
| 222 |
wp_enqueue_style('wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, array(), $this->plugin->getPluginVersion()); |
| 223 |
} |
| 224 |
} else { |
| 225 |
$cache_dir_exists = false; |
| 226 |
if( !file_exists($cache_dir_path) ) { |
| 227 |
if( @mkdir($cache_dir_path, 0777) && is_writable($cache_dir_path) ) { |
| 228 |
$cache_dir_exists = true; |
| 229 |
} |
| 230 |
} else { |
| 231 |
if( is_writable($cache_dir_path) ) { |
| 232 |
$cache_dir_exists = true; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
$concat_files = array(); |
| 237 |
foreach($sripts as $script_to_load) { |
| 238 |
$script_to_load = sanitize_text_field($script_to_load); |
| 239 |
if( $cache_dir_exists ) { |
| 240 |
$fname = FACTORY_BOOTSTRAP_400_DIR . "/assets/$type-min/$script_to_load.min." . $type; |
| 241 |
if( file_exists($fname) ) { |
| 242 |
$f = @fopen($fname, 'r'); |
| 243 |
$concat_files[] = @fread($f, filesize($fname)); |
| 244 |
@fclose($f); |
| 245 |
} |
| 246 |
} else { |
| 247 |
if( $type == 'js' ) { |
| 248 |
wp_enqueue_script(md5($script_to_load), FACTORY_BOOTSTRAP_400_URL . "/assets/$type-min/$script_to_load.min." . $type, $is_first |
| 249 |
? $dependencies |
| 250 |
: false, $this->plugin->getPluginVersion()); |
| 251 |
} else { |
| 252 |
wp_enqueue_style(md5($script_to_load), FACTORY_BOOTSTRAP_400_URL . "/assets/$type-min/$script_to_load.min." . $type, array(), $this->plugin->getPluginVersion()); |
| 253 |
} |
| 254 |
$is_first = false; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if( $cache_dir_exists && !empty($concat_files) ) { |
| 259 |
|
| 260 |
$cf = @fopen($cache_filepath, 'w'); |
| 261 |
$write_content = implode(PHP_EOL, $concat_files); |
| 262 |
@fwrite($cf, $write_content); |
| 263 |
@fclose($cf); |
| 264 |
|
| 265 |
if( file_exists($cache_filepath) ) { |
| 266 |
if( $type == 'js' ) { |
| 267 |
wp_enqueue_script('wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, $dependencies, $this->plugin->getPluginVersion()); |
| 268 |
} else { |
| 269 |
wp_enqueue_style('wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, array(), $this->plugin->getPluginVersion()); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Adds the body classes: 'factory-flat or 'factory-volumetric'. |
| 278 |
* |
| 279 |
* @since 3.2.0 |
| 280 |
* @param string $classes |
| 281 |
* @return string |
| 282 |
*/ |
| 283 |
public function adminBodyClass($classes) |
| 284 |
{ |
| 285 |
$classes .= FACTORY_FLAT_ADMIN |
| 286 |
? ' factory-flat ' |
| 287 |
: ' factory-volumetric '; |
| 288 |
|
| 289 |
return $classes; |
| 290 |
} |
| 291 |
} |
| 292 |
} |