library
11 years ago
admin.php
11 years ago
media-template.php
11 years ago
settings.php
11 years ago
type-dashicons.php
11 years ago
type-elusive.php
11 years ago
type-fontawesome.php
11 years ago
type-fontpack.php
11 years ago
type-fonts.php
11 years ago
type-foundation.php
11 years ago
type-genericons.php
11 years ago
type-image.php
11 years ago
type.php
11 years ago
walker-nav-menu-edit.php
11 years ago
type-fontpack.php
273 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Font Packs |
| 4 | * |
| 5 | * @package Menu_Icons |
| 6 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 7 | * @author Joshua F. Rountree <joshua@swodev.com> |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | require_once dirname( __FILE__ ) . '/type-fonts.php'; |
| 12 | |
| 13 | /** |
| 14 | * Icon type: Font Packs |
| 15 | * |
| 16 | * @version 0.1.0 |
| 17 | */ |
| 18 | class Menu_Icons_Type_Fontpack extends Menu_Icons_Type_Fonts { |
| 19 | |
| 20 | /** |
| 21 | * Holds icon type |
| 22 | * |
| 23 | * @since 0.1.0 |
| 24 | * @access protected |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $type = ''; |
| 28 | |
| 29 | /** |
| 30 | * Holds icon label |
| 31 | * |
| 32 | * @since 0.1.0 |
| 33 | * @access protected |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $label = ''; |
| 37 | |
| 38 | /** |
| 39 | * Holds icon version |
| 40 | * |
| 41 | * @since 0.1.0 |
| 42 | * @access protected |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $version = ''; |
| 46 | |
| 47 | /** |
| 48 | * Holds fontpack dir |
| 49 | * |
| 50 | * @since 0.1.0 |
| 51 | * @access protected |
| 52 | * @var string |
| 53 | */ |
| 54 | protected $dir = ''; |
| 55 | |
| 56 | /** |
| 57 | * Holds fontpack url path |
| 58 | * |
| 59 | * @since 0.1.0 |
| 60 | * @access protected |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $url = ''; |
| 64 | |
| 65 | /** |
| 66 | * Holds error messages |
| 67 | * |
| 68 | * @since 0.1.0 |
| 69 | * @access protected |
| 70 | * @var array |
| 71 | */ |
| 72 | protected $messages = array(); |
| 73 | |
| 74 | /** |
| 75 | * Holds config array |
| 76 | * |
| 77 | * @since 0.1.0 |
| 78 | * @access protected |
| 79 | * @var array |
| 80 | */ |
| 81 | protected $config = array(); |
| 82 | |
| 83 | /** |
| 84 | * Holds config validation status |
| 85 | * |
| 86 | * @since 0.1.0 |
| 87 | * @access protected |
| 88 | * @var bool |
| 89 | */ |
| 90 | protected $is_config_valid = false; |
| 91 | |
| 92 | /** |
| 93 | * Holds icon names |
| 94 | * |
| 95 | * @since 0.1.0 |
| 96 | * @access protected |
| 97 | * @var array |
| 98 | */ |
| 99 | protected $icons = array(); |
| 100 | |
| 101 | /** |
| 102 | * Class constructor |
| 103 | * |
| 104 | * We need to override the parent's to set our stylesheet URL |
| 105 | * |
| 106 | * @since 0.1.0 |
| 107 | * @param array $types Icon Types |
| 108 | * @return array |
| 109 | */ |
| 110 | public function __construct( $pack ) { |
| 111 | $this->messages = array( |
| 112 | 'no_config' => __( 'Menu Icons: %1$s was not found in %2$s.', 'menu-icons' ), |
| 113 | 'invalid' => __( 'Menu Icons: %1$s is not set or invalid in %2$s.', 'menu-icons' ), |
| 114 | 'duplicate' => __( 'Menu Icons: %1$s is already registered. Please check your font pack config file: %2$s.', 'menu-icons' ), |
| 115 | ); |
| 116 | |
| 117 | $this->dir = sprintf( '%s/%s', Menu_Icons::get( 'fontpacks_dir_path' ), $pack ); |
| 118 | $this->url = sprintf( '%s/%s', Menu_Icons::get( 'fontpacks_dir_url' ), $pack ); |
| 119 | |
| 120 | if ( ! is_readable( $this->dir . '/config.json' ) ) { |
| 121 | trigger_error( |
| 122 | sprintf( |
| 123 | $this->messages['no_config'], |
| 124 | '<code><em>config.json</em></code>', |
| 125 | sprintf( '<code>%s</code>', $this->dir ) |
| 126 | ) |
| 127 | ); |
| 128 | |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $this->read_config(); |
| 133 | $this->validate(); |
| 134 | |
| 135 | if ( false === $this->is_config_valid ) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | $this->set_properties(); |
| 140 | |
| 141 | parent::__construct(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Read in config and store for later. |
| 147 | * |
| 148 | * @since 0.1.0 |
| 149 | * @access protected |
| 150 | */ |
| 151 | protected function read_config() { |
| 152 | $config_path = $this->dir . '/config.json'; |
| 153 | $config_json = file_get_contents( $config_path ); |
| 154 | $this->config = json_decode( $config_json, true ); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Validate config file |
| 160 | * |
| 161 | * @since 0.1.0 |
| 162 | * @access protected |
| 163 | */ |
| 164 | protected function validate() { |
| 165 | $keys = array( 'name', 'glyphs', 'css_prefix_text' ); |
| 166 | |
| 167 | foreach ( $keys as $key ) { |
| 168 | if ( empty( $this->config[ $key ] ) ) { |
| 169 | trigger_error( |
| 170 | sprintf( |
| 171 | $this->messages['invalid'], |
| 172 | sprintf( '<code><em>%s</em></code>', $key ), |
| 173 | sprintf( '<code>%s/config.json</code>', $this->dir ) |
| 174 | ) |
| 175 | ); |
| 176 | |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Validate & get all glyphs |
| 182 | if ( ! is_array( $this->config['glyphs'] ) ) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | $icons = array(); |
| 187 | foreach ( $this->config['glyphs'] as $glyph ) { |
| 188 | if ( ! empty( $glyph['css'] ) ) { |
| 189 | $class = $this->config['css_prefix_text'] . $glyph['css']; |
| 190 | $label = $glyph['css']; |
| 191 | |
| 192 | $icons[ $class ] = $label; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if ( empty( $icons ) ) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | $this->icons = $icons; |
| 201 | $this->is_config_valid = true; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * Set class properties |
| 207 | * |
| 208 | * @since 0.1.0 |
| 209 | * @access protected |
| 210 | */ |
| 211 | protected function set_properties() { |
| 212 | $this->type = sprintf( 'pack-%s', $this->config['name'] ); |
| 213 | $this->label = sprintf( __( 'Pack: %s', 'menu-icons' ), $this->config['name'] ); |
| 214 | $this->stylesheet = sprintf( '%s/css/%s.css', $this->url, $this->config['name'] ); |
| 215 | |
| 216 | if ( ! empty( $this->config['version'] ) ) { |
| 217 | $this->version = $this->config['version']; |
| 218 | } |
| 219 | else { |
| 220 | $this->version = filemtime( sprintf( '%s/css/%s.css', $this->dir, $this->config['name'] ) ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Register our type |
| 227 | * |
| 228 | * @since 0.1.0 |
| 229 | * @param array $types Icon Types |
| 230 | * @return array |
| 231 | */ |
| 232 | public function register( $icon_types ) { |
| 233 | if ( true !== $this->is_config_valid ) { |
| 234 | return $icon_types; |
| 235 | } |
| 236 | |
| 237 | // Check for duplicate packs |
| 238 | if ( isset( $icon_types[ $this->type ] ) ) { |
| 239 | trigger_error( |
| 240 | sprintf( |
| 241 | $this->messages['duplicate'], |
| 242 | sprintf( '<strong>%s</strong>', $this->config['name'] ), |
| 243 | sprintf( '<code><em>%s/config.json</em></code>', $this->dir ) |
| 244 | ) |
| 245 | ); |
| 246 | |
| 247 | return $icon_types; |
| 248 | } |
| 249 | |
| 250 | $icon_types = parent::register( $icon_types ); |
| 251 | |
| 252 | return $icon_types; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /** |
| 257 | * Read fontpacks directory for config.json's and find icons names |
| 258 | * |
| 259 | * @since 0.1.0 |
| 260 | * @return array |
| 261 | */ |
| 262 | public function get_names() { |
| 263 | $glyphs = $this->config['glyphs']; |
| 264 | $names = array( |
| 265 | 'key' => 'all', |
| 266 | 'label' => __( 'All', 'menu-icons' ), |
| 267 | 'items' => $this->icons, |
| 268 | ); |
| 269 | |
| 270 | return array( $names ); |
| 271 | } |
| 272 | } |
| 273 |