functions.php
243 lines
| 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_FactoryBootstrap482_Manager { |
| 24 | |
| 25 | /** |
| 26 | * A plugin for which the manager was created. |
| 27 | * |
| 28 | * @since 3.2.0 |
| 29 | * @var Wbcr_Factory480_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_Factory480_Plugin $plugin) |
| 55 | { |
| 56 | $this->plugin = $plugin; |
| 57 | |
| 58 | add_action('admin_enqueue_scripts', [$this, 'loadAssets']); |
| 59 | add_filter('admin_body_class', [$this, 'adminBodyClass']); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Includes the Bootstrap scripts. |
| 64 | * |
| 65 | * @param array|string $scripts |
| 66 | * @since 3.2.0 |
| 67 | * |
| 68 | */ |
| 69 | public function enqueueScript($scripts) |
| 70 | { |
| 71 | if( is_array($scripts) ) { |
| 72 | foreach($scripts as $script) { |
| 73 | if( !in_array($script, $this->scripts) ) { |
| 74 | $this->scripts[] = $script; |
| 75 | } |
| 76 | } |
| 77 | } else { |
| 78 | if( !in_array($scripts, $this->scripts) ) { |
| 79 | $this->scripts[] = $scripts; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * * Includes the Bootstrap styles. |
| 86 | * |
| 87 | * @param array|string $styles |
| 88 | * @since 3.2.0 |
| 89 | * |
| 90 | */ |
| 91 | public function enqueueStyle($styles) |
| 92 | { |
| 93 | |
| 94 | if( is_array($styles) ) { |
| 95 | foreach($styles as $style) { |
| 96 | if( !in_array($style, $this->styles) ) { |
| 97 | $this->styles[] = $style; |
| 98 | } |
| 99 | } |
| 100 | } else { |
| 101 | if( !in_array($styles, $this->styles) ) { |
| 102 | $this->styles[] = $styles; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Loads Bootstrap assets. |
| 109 | * |
| 110 | * @return void |
| 111 | * @since 3.2.0 |
| 112 | * @see admin_enqueue_scripts |
| 113 | * |
| 114 | */ |
| 115 | public function loadAssets($hook) |
| 116 | { |
| 117 | |
| 118 | do_action('wbcr_factory_480_bootstrap_enqueue_scripts', $hook); |
| 119 | do_action('wbcr_factory_480_bootstrap_enqueue_scripts_' . $this->plugin->getPluginName(), $hook); |
| 120 | |
| 121 | $dependencies = []; |
| 122 | if( !empty($this->scripts) ) { |
| 123 | $dependencies[] = 'jquery'; |
| 124 | $dependencies[] = 'jquery-ui-core'; |
| 125 | $dependencies[] = 'jquery-ui-widget'; |
| 126 | } |
| 127 | |
| 128 | foreach($this->scripts as $script) { |
| 129 | switch( $script ) { |
| 130 | case 'plugin.iris': |
| 131 | $dependencies[] = 'jquery-ui-widget'; |
| 132 | $dependencies[] = 'jquery-ui-slider'; |
| 133 | $dependencies[] = 'jquery-ui-draggable'; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if( !empty($this->scripts) ) { |
| 139 | $this->enqueueScripts($this->scripts, 'js', $dependencies); |
| 140 | } |
| 141 | if( !empty($this->styles) ) { |
| 142 | $this->enqueueScripts($this->styles, 'css', $dependencies); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param array $scripts |
| 148 | * @param string $type |
| 149 | * @param array $dependencies |
| 150 | */ |
| 151 | protected function enqueueScripts(array $scripts, $type, array $dependencies) |
| 152 | { |
| 153 | |
| 154 | $is_first = true; |
| 155 | |
| 156 | /** |
| 157 | * Sets permission for file caching and combining into one file. |
| 158 | * |
| 159 | * @since 4.1.0 |
| 160 | */ |
| 161 | $cache_enable = apply_filters('wbcr/factory/bootstrap/cache_enable', true); |
| 162 | |
| 163 | $cache_id = md5(implode(',', $this->scripts) . $type . $this->plugin->getPluginVersion()); |
| 164 | $cache_dir_path = FACTORY_BOOTSTRAP_482_DIR . '/assets/cache/'; |
| 165 | $cache_dir_url = FACTORY_BOOTSTRAP_482_URL . '/assets/cache/'; |
| 166 | |
| 167 | $cache_filepath = $cache_dir_path . $cache_id . ".min." . $type; |
| 168 | $cache_fileurl = $cache_dir_url . $cache_id . ".min." . $type; |
| 169 | |
| 170 | if( $cache_enable && file_exists($cache_filepath) ) { |
| 171 | if( $type == 'js' ) { |
| 172 | wp_enqueue_script('wbcr-factory-bootstrap-cached', $cache_fileurl, $dependencies, $this->plugin->getPluginVersion()); |
| 173 | } else { |
| 174 | wp_enqueue_style('wbcr-factory-bootstrap-cached', $cache_fileurl, [], $this->plugin->getPluginVersion()); |
| 175 | } |
| 176 | } else { |
| 177 | $cache_dir_exists = false; |
| 178 | if( !file_exists($cache_dir_path) ) { |
| 179 | if( @mkdir($cache_dir_path, 0755) && wp_is_writable($cache_dir_path) ) { |
| 180 | $cache_dir_exists = true; |
| 181 | } |
| 182 | } else { |
| 183 | if( wp_is_writable($cache_dir_path) ) { |
| 184 | $cache_dir_exists = true; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | $concat_files = []; |
| 189 | foreach($scripts as $script_to_load) { |
| 190 | $script_to_load = sanitize_text_field($script_to_load); |
| 191 | if( $cache_enable && $cache_dir_exists ) { |
| 192 | $fname = FACTORY_BOOTSTRAP_482_DIR . "/assets/$type-min/$script_to_load.min." . $type; |
| 193 | if( file_exists($fname) ) { |
| 194 | $f = @fopen($fname, 'r'); |
| 195 | $concat_files[] = @fread($f, filesize($fname)); |
| 196 | @fclose($f); |
| 197 | } |
| 198 | } else { |
| 199 | if( $type == 'js' ) { |
| 200 | wp_enqueue_script(md5($script_to_load), FACTORY_BOOTSTRAP_482_URL . "/assets/$type-min/$script_to_load.min." . $type, $is_first ? $dependencies : false, $this->plugin->getPluginVersion()); |
| 201 | } else { |
| 202 | wp_enqueue_style(md5($script_to_load), FACTORY_BOOTSTRAP_482_URL . "/assets/$type-min/$script_to_load.min." . $type, [], $this->plugin->getPluginVersion()); |
| 203 | } |
| 204 | $is_first = false; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if( $cache_enable && $cache_dir_exists && !empty($concat_files) ) { |
| 209 | |
| 210 | $cf = @fopen($cache_filepath, 'w'); |
| 211 | $write_content = implode(PHP_EOL, $concat_files); |
| 212 | @fwrite($cf, $write_content); |
| 213 | @fclose($cf); |
| 214 | chmod($cache_filepath, 0755); |
| 215 | |
| 216 | if( file_exists($cache_filepath) ) { |
| 217 | if( $type == 'js' ) { |
| 218 | wp_enqueue_script('wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, $dependencies, $this->plugin->getPluginVersion()); |
| 219 | } else { |
| 220 | wp_enqueue_style('wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, [], $this->plugin->getPluginVersion()); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Adds the body classes: 'factory-flat or 'factory-volumetric'. |
| 229 | * |
| 230 | * @param string $classes |
| 231 | * |
| 232 | * @return string |
| 233 | * @since 3.2.0 |
| 234 | * |
| 235 | */ |
| 236 | public function adminBodyClass($classes) |
| 237 | { |
| 238 | $classes .= FACTORY_FLAT_ADMIN ? ' factory-flat ' : ' factory-volumetric '; |
| 239 | |
| 240 | return $classes; |
| 241 | } |
| 242 | } |
| 243 |