menu-icons.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Menu Icons |
| 5 | * |
| 6 | * @package Menu_Icons |
| 7 | * @version 0.1.5 |
| 8 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 9 | * |
| 10 | * |
| 11 | * Plugin name: Menu Icons |
| 12 | * Plugin URI: http://kucrut.org/ |
| 13 | * Description: Easily add icons to your navigation menu items |
| 14 | * Version: 0.1.5 |
| 15 | * Author: Dzikri Aziz |
| 16 | * Author URI: http://kucrut.org/ |
| 17 | * License: GPLv2 |
| 18 | * Text Domain: menu-icons |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Main plugin class |
| 24 | * |
| 25 | * @version 0.1.5 |
| 26 | */ |
| 27 | final class Menu_Icons { |
| 28 | |
| 29 | const VERSION = '0.1.5'; |
| 30 | |
| 31 | /** |
| 32 | * Holds plugin data |
| 33 | * |
| 34 | * @access protected |
| 35 | * @since 0.1.0 |
| 36 | * @var array |
| 37 | */ |
| 38 | protected static $data; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Get plugin data |
| 43 | * |
| 44 | * @since 0.1.0 |
| 45 | * @param string $name |
| 46 | * |
| 47 | * @return mixed |
| 48 | */ |
| 49 | public static function get( $name = null ) { |
| 50 | if ( is_null( $name ) ) { |
| 51 | return self::$data; |
| 52 | } |
| 53 | |
| 54 | if ( isset( self::$data[ $name ] ) ) { |
| 55 | return self::$data[ $name ]; |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Load plugin |
| 64 | * |
| 65 | * 1. Load translation |
| 66 | * 2. Set plugin data (directory and URL paths) |
| 67 | * 3. Register built-in icon types |
| 68 | * 4. Attach plugin initialization at wp_loaded hook |
| 69 | * |
| 70 | * @since 0.1.0 |
| 71 | * @wp_hook action plugins_loaded/10 |
| 72 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded Action: plugins_loaded/10 |
| 73 | */ |
| 74 | public static function load() { |
| 75 | load_plugin_textdomain( 'menu-icons', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 76 | |
| 77 | self::$data = array( |
| 78 | 'dir' => plugin_dir_path( __FILE__ ), |
| 79 | 'url' => plugin_dir_url( __FILE__ ), |
| 80 | 'icon_types' => array(), |
| 81 | ); |
| 82 | |
| 83 | add_filter( 'menu_icons_types', array( __CLASS__, '_register_icon_types' ), 7 ); |
| 84 | add_action( 'wp_loaded', array( __CLASS__, 'init' ), 9 ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Initialize plugin |
| 90 | * |
| 91 | * 1. Collect registered types |
| 92 | * 2. Add hook callbacks |
| 93 | * |
| 94 | * @since 0.1.0 |
| 95 | * @wp_hook action wp_loaded/9 |
| 96 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded Action: wp_loaded/9 |
| 97 | */ |
| 98 | public static function init() { |
| 99 | self::_collect_icon_types(); |
| 100 | |
| 101 | // Nothing to do if there are no icon types registered |
| 102 | if ( empty( self::$data['icon_types'] ) ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | // Back |
| 107 | add_filter( 'load-nav-menus.php', array( __CLASS__, '_load_nav_menus' ) ); |
| 108 | add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_wp_edit_nav_menu_walker' ), 99 ); |
| 109 | |
| 110 | // Front |
| 111 | add_action( 'get_header', array( __CLASS__, '_load_front_end' ) ); |
| 112 | add_action( 'wp_enqueue_scripts', array( __CLASS__, '_enqueue_styles' ), 7 ); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Register built-in icon types |
| 118 | * |
| 119 | * @since 0.1.5 |
| 120 | * @access protected |
| 121 | * @wp_hook filter menu_icons_types |
| 122 | * |
| 123 | * @param array $icon_types Current icon types |
| 124 | * @return array |
| 125 | */ |
| 126 | public static function _register_icon_types( $icon_types ) { |
| 127 | $builtin_types = array( |
| 128 | 'dashicons', |
| 129 | 'genericons', |
| 130 | ); |
| 131 | |
| 132 | foreach ( $builtin_types as $type ) { |
| 133 | require_once sprintf( '%s/includes/type-%s.php', self::$data['dir'], $type ); |
| 134 | |
| 135 | $class_name = sprintf( 'Menu_Icons_Type_%s', ucfirst( $type ) ); |
| 136 | $type_instance = new $class_name; |
| 137 | $icon_types = $type_instance->register( $icon_types ); |
| 138 | } |
| 139 | |
| 140 | return $icon_types; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Collect icon types |
| 146 | * |
| 147 | * @since 0.1.0 |
| 148 | * @access private |
| 149 | * @uses apply_filters() Calls 'menu_icons_types' to get registered types. |
| 150 | */ |
| 151 | private static function _collect_icon_types() { |
| 152 | $types = (array) apply_filters( 'menu_icons_types', array() ); |
| 153 | $defaults = array( |
| 154 | 'label' => '', |
| 155 | 'field_cb' => '', |
| 156 | 'front_cb' => '', |
| 157 | 'stylesheet' => '', |
| 158 | 'version' => get_bloginfo( 'version' ), |
| 159 | ); |
| 160 | |
| 161 | foreach ( $types as $type => $props ) { |
| 162 | $type_props = wp_parse_args( $props, $defaults ); |
| 163 | foreach ( $type_props as $key => $value ) { |
| 164 | // Stylesheet can be empty |
| 165 | if ( 'stylesheet' === $key ) { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // Everything else must be specified |
| 170 | if ( empty( $value ) ) { |
| 171 | continue 2; |
| 172 | } |
| 173 | |
| 174 | if ( 'field_cb' === $key && ! is_callable( $value ) ) { |
| 175 | continue 2; |
| 176 | } |
| 177 | |
| 178 | if ( 'front_cb' === $key && ! is_callable( $value ) ) { |
| 179 | continue 2; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | self::$data['icon_types'][ $type ] = $type_props; |
| 184 | } |
| 185 | |
| 186 | ksort( self::$data['icon_types'] ); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * Prepare custom walker and custom field |
| 192 | * |
| 193 | * @since 0.1.3 |
| 194 | * @access protected |
| 195 | * @wp_hook filter wp_edit_nav_menu_walker/10/1 |
| 196 | */ |
| 197 | public static function _filter_wp_edit_nav_menu_walker( $walker ) { |
| 198 | // Load custom fields |
| 199 | require_once self::$data['dir'] . 'includes/admin.php'; |
| 200 | add_filter( 'menu_item_custom_fields', array( 'Menu_Icons_Admin_Nav_Menus', '_fields' ), 10, 3 ); |
| 201 | |
| 202 | // Load menu item custom fields plugin |
| 203 | if ( ! class_exists( 'Menu_Item_Custom_Fields_Walker' ) ) { |
| 204 | require_once self::$data['dir'] . '/includes/walker-nav-menu-edit.php'; |
| 205 | } |
| 206 | $walker = 'Menu_Item_Custom_Fields_Walker'; |
| 207 | |
| 208 | return $walker; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /** |
| 213 | * Prepare wp-admin/nav-menus.php |
| 214 | * |
| 215 | * @since 0.1.5 |
| 216 | * @access protected |
| 217 | * @wp_hook action load-nav-menus.php/10 |
| 218 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/load-%28page%29 Action: load-nav-menus.php/10 |
| 219 | */ |
| 220 | public static function _load_nav_menus() { |
| 221 | // Load custom fields |
| 222 | require_once self::$data['dir'] . 'includes/admin.php'; |
| 223 | Menu_Icons_Admin_Nav_Menus::init(); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * Load front-end tasks |
| 229 | * |
| 230 | * @since 0.1.0 |
| 231 | * @access protected |
| 232 | * @wp_hook action load-nav-menus.php/10 |
| 233 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/get_header Action: get_header/10 |
| 234 | */ |
| 235 | public static function _load_front_end() { |
| 236 | foreach ( self::$data['icon_types'] as $props ) { |
| 237 | call_user_func( $props['front_cb'] ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Enqueue extra stylesheet |
| 244 | * |
| 245 | * This stylesheet will override some styles of the icons |
| 246 | * |
| 247 | * @since 0.1.0 |
| 248 | * @access protected |
| 249 | * @wp_hook action wp_enqueue_scripts/10 |
| 250 | * @uses apply_filters() Calls 'menu_icons_load_extra_style' allow plugins/themes to |
| 251 | * enable/disable the loading of the extra stylesheet |
| 252 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts Action: wp_enqueue_scripts/10 |
| 253 | */ |
| 254 | public static function _enqueue_styles() { |
| 255 | // Enqueue icon types' stylesheets |
| 256 | foreach ( self::$data['icon_types'] as $id => $props ) { |
| 257 | if ( empty( $props['stylesheet'] ) ) { |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | if ( wp_style_is( $props['stylesheet'], 'registered' ) ) { |
| 262 | wp_enqueue_style( $id ); |
| 263 | } |
| 264 | else { |
| 265 | wp_enqueue_style( $id, $props['stylesheet'], false, $props['version'] ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Enable/disable loading of extra stylesheet |
| 271 | * |
| 272 | * @since 0.1.0 |
| 273 | * @param bool $load_extra_style |
| 274 | */ |
| 275 | $load_extra_style = (bool) apply_filters( 'menu_icons_load_extra_style', true ); |
| 276 | |
| 277 | if ( true === $load_extra_style ) { |
| 278 | wp_enqueue_style( |
| 279 | 'menu-icons-extra', |
| 280 | Menu_Icons::get( 'url' ) . 'css/extra.css', |
| 281 | false, |
| 282 | Menu_Icons::VERSION |
| 283 | ); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | add_action( 'plugins_loaded', array( 'Menu_Icons', 'load' ) ); |
| 288 |