functions.php
223 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file manages assets of the Factory Bootstap. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package factory-bootstrap |
| 7 | */ |
| 8 | |
| 9 | // Exit if accessed directly |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * The Bootstrap Manager class. |
| 16 | * |
| 17 | * @since 3.2.0 |
| 18 | */ |
| 19 | class Wbcr_FactoryBootstrap500_Manager { |
| 20 | |
| 21 | /** |
| 22 | * A plugin for which the manager was created. |
| 23 | * |
| 24 | * @since 3.2.0 |
| 25 | * @var Wbcr_Factory600_Plugin |
| 26 | */ |
| 27 | public $plugin; |
| 28 | |
| 29 | /** |
| 30 | * Contains scripts to include. |
| 31 | * |
| 32 | * @since 3.2.0 |
| 33 | * @var string[] |
| 34 | */ |
| 35 | public $scripts = []; |
| 36 | |
| 37 | /** |
| 38 | * Contains styles to include. |
| 39 | * |
| 40 | * @since 3.2.0 |
| 41 | * @var string[] |
| 42 | */ |
| 43 | public $styles = []; |
| 44 | |
| 45 | /** |
| 46 | * Createas a new instance of the license api for a given plugin. |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function __construct( Wbcr_Factory600_Plugin $plugin ) { |
| 51 | $this->plugin = $plugin; |
| 52 | |
| 53 | add_action( 'admin_enqueue_scripts', [ $this, 'loadAssets' ] ); |
| 54 | add_filter( 'admin_body_class', [ $this, 'adminBodyClass' ] ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Includes the Bootstrap scripts. |
| 59 | * |
| 60 | * @param array|string $scripts |
| 61 | * @since 3.2.0 |
| 62 | */ |
| 63 | public function enqueueScript( $scripts ) { |
| 64 | if ( is_array( $scripts ) ) { |
| 65 | foreach ( $scripts as $script ) { |
| 66 | if ( ! in_array( $script, $this->scripts ) ) { |
| 67 | $this->scripts[] = $script; |
| 68 | } |
| 69 | } |
| 70 | } elseif ( ! in_array( $scripts, $this->scripts ) ) { |
| 71 | $this->scripts[] = $scripts; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * * Includes the Bootstrap styles. |
| 77 | * |
| 78 | * @param array|string $styles |
| 79 | * @since 3.2.0 |
| 80 | */ |
| 81 | public function enqueueStyle( $styles ) { |
| 82 | |
| 83 | if ( is_array( $styles ) ) { |
| 84 | foreach ( $styles as $style ) { |
| 85 | if ( ! in_array( $style, $this->styles ) ) { |
| 86 | $this->styles[] = $style; |
| 87 | } |
| 88 | } |
| 89 | } elseif ( ! in_array( $styles, $this->styles ) ) { |
| 90 | $this->styles[] = $styles; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Loads Bootstrap assets. |
| 96 | * |
| 97 | * @return void |
| 98 | * @since 3.2.0 |
| 99 | * @see admin_enqueue_scripts |
| 100 | */ |
| 101 | public function loadAssets( $hook ) { |
| 102 | |
| 103 | do_action( 'wbcr_factory_600_bootstrap_enqueue_scripts', $hook ); |
| 104 | do_action( 'wbcr_factory_600_bootstrap_enqueue_scripts_' . $this->plugin->getPluginName(), $hook ); |
| 105 | |
| 106 | $dependencies = []; |
| 107 | if ( ! empty( $this->scripts ) ) { |
| 108 | $dependencies[] = 'jquery'; |
| 109 | $dependencies[] = 'jquery-ui-core'; |
| 110 | $dependencies[] = 'jquery-ui-widget'; |
| 111 | } |
| 112 | |
| 113 | foreach ( $this->scripts as $script ) { |
| 114 | switch ( $script ) { |
| 115 | case 'plugin.iris': |
| 116 | $dependencies[] = 'jquery-ui-widget'; |
| 117 | $dependencies[] = 'jquery-ui-slider'; |
| 118 | $dependencies[] = 'jquery-ui-draggable'; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if ( ! empty( $this->scripts ) ) { |
| 124 | $this->enqueueScripts( $this->scripts, 'js', $dependencies ); |
| 125 | } |
| 126 | if ( ! empty( $this->styles ) ) { |
| 127 | $this->enqueueScripts( $this->styles, 'css', $dependencies ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param array $scripts |
| 133 | * @param string $type |
| 134 | * @param array $dependencies |
| 135 | */ |
| 136 | protected function enqueueScripts( array $scripts, $type, array $dependencies ) { |
| 137 | |
| 138 | $is_first = true; |
| 139 | |
| 140 | /** |
| 141 | * Sets permission for file caching and combining into one file. |
| 142 | * |
| 143 | * @since 4.1.0 |
| 144 | */ |
| 145 | $cache_enable = apply_filters( 'wbcr/factory/bootstrap/cache_enable', true ); |
| 146 | |
| 147 | $cache_id = md5( implode( ',', $scripts ) . $type . $this->plugin->getPluginVersion() ); |
| 148 | $cache_dir_path = FACTORY_BOOTSTRAP_500_DIR . '/assets/cache/'; |
| 149 | $cache_dir_url = FACTORY_BOOTSTRAP_500_URL . '/assets/cache/'; |
| 150 | |
| 151 | $cache_filepath = $cache_dir_path . $cache_id . '.min.' . $type; |
| 152 | $cache_fileurl = $cache_dir_url . $cache_id . '.min.' . $type; |
| 153 | |
| 154 | if ( $cache_enable && file_exists( $cache_filepath ) ) { |
| 155 | if ( $type == 'js' ) { |
| 156 | wp_enqueue_script( 'wbcr-factory-bootstrap-cached', $cache_fileurl, $dependencies, $this->plugin->getPluginVersion() ); |
| 157 | } else { |
| 158 | wp_enqueue_style( 'wbcr-factory-bootstrap-cached', $cache_fileurl, [], $this->plugin->getPluginVersion() ); |
| 159 | } |
| 160 | } else { |
| 161 | $cache_dir_exists = false; |
| 162 | if ( ! file_exists( $cache_dir_path ) ) { |
| 163 | if ( @mkdir( $cache_dir_path, 0755 ) && wp_is_writable( $cache_dir_path ) ) { |
| 164 | $cache_dir_exists = true; |
| 165 | } |
| 166 | } elseif ( wp_is_writable( $cache_dir_path ) ) { |
| 167 | $cache_dir_exists = true; |
| 168 | } |
| 169 | |
| 170 | $concat_files = []; |
| 171 | foreach ( $scripts as $script_to_load ) { |
| 172 | $script_to_load = sanitize_text_field( $script_to_load ); |
| 173 | if ( $cache_enable && $cache_dir_exists ) { |
| 174 | $fname = FACTORY_BOOTSTRAP_500_DIR . "/assets/$type-min/$script_to_load.min." . $type; |
| 175 | if ( file_exists( $fname ) ) { |
| 176 | $f = @fopen( $fname, 'r' ); |
| 177 | $concat_files[] = @fread( $f, filesize( $fname ) ); |
| 178 | @fclose( $f ); |
| 179 | } |
| 180 | } else { |
| 181 | if ( $type == 'js' ) { |
| 182 | wp_enqueue_script( md5( $script_to_load ), FACTORY_BOOTSTRAP_500_URL . "/assets/$type-min/$script_to_load.min." . $type, $is_first ? $dependencies : false, $this->plugin->getPluginVersion() ); |
| 183 | } else { |
| 184 | wp_enqueue_style( md5( $script_to_load ), FACTORY_BOOTSTRAP_500_URL . "/assets/$type-min/$script_to_load.min." . $type, [], $this->plugin->getPluginVersion() ); |
| 185 | } |
| 186 | $is_first = false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if ( $cache_enable && $cache_dir_exists && ! empty( $concat_files ) ) { |
| 191 | |
| 192 | $cf = @fopen( $cache_filepath, 'w' ); |
| 193 | $write_content = implode( PHP_EOL, $concat_files ); |
| 194 | @fwrite( $cf, $write_content ); |
| 195 | @fclose( $cf ); |
| 196 | chmod( $cache_filepath, 0755 ); |
| 197 | |
| 198 | if ( file_exists( $cache_filepath ) ) { |
| 199 | if ( $type == 'js' ) { |
| 200 | wp_enqueue_script( 'wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, $dependencies, $this->plugin->getPluginVersion() ); |
| 201 | } else { |
| 202 | wp_enqueue_style( 'wbcr-factory-bootstrap-' . $cache_id, $cache_fileurl, [], $this->plugin->getPluginVersion() ); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Adds the body classes: 'factory-flat or 'factory-volumetric'. |
| 211 | * |
| 212 | * @param string $classes |
| 213 | * |
| 214 | * @return string |
| 215 | * @since 3.2.0 |
| 216 | */ |
| 217 | public function adminBodyClass( $classes ) { |
| 218 | $classes .= FACTORY_FLAT_ADMIN ? ' factory-flat ' : ' factory-volumetric '; |
| 219 | |
| 220 | return $classes; |
| 221 | } |
| 222 | } |
| 223 |