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