admin.php
12 years ago
type-dashicons.php
12 years ago
type-fontawesome.php
12 years ago
type-fonts.php
12 years ago
type-genericons.php
12 years ago
type.php
12 years ago
walker-nav-menu-edit.php
12 years ago
type.php
237 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Icon type handler |
| 4 | * |
| 5 | * @package Menu_Icons |
| 6 | * @version 0.1.0 |
| 7 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | /** |
| 12 | * Generic handler for icon type |
| 13 | * |
| 14 | * @since 0.1.0 |
| 15 | */ |
| 16 | abstract class Menu_Icons_Type { |
| 17 | |
| 18 | /** |
| 19 | * Holds icon type |
| 20 | * |
| 21 | * @since 0.1.0 |
| 22 | * @access protected |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $type; |
| 26 | |
| 27 | /** |
| 28 | * Holds icon label |
| 29 | * |
| 30 | * @since 0.1.0 |
| 31 | * @access protected |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $label; |
| 35 | |
| 36 | /** |
| 37 | * Holds icon stylesheet URL |
| 38 | * |
| 39 | * @since 0.1.0 |
| 40 | * @access protected |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $stylesheet; |
| 44 | |
| 45 | /** |
| 46 | * Holds icon version |
| 47 | * |
| 48 | * @since 0.1.0 |
| 49 | * @access protected |
| 50 | * @var string |
| 51 | */ |
| 52 | protected $version; |
| 53 | |
| 54 | /** |
| 55 | * Holds array key for icon value |
| 56 | * |
| 57 | * @since 0.1.0 |
| 58 | * @access protected |
| 59 | * @var string |
| 60 | */ |
| 61 | protected $key; |
| 62 | |
| 63 | /** |
| 64 | * Holds icon positions |
| 65 | * |
| 66 | * @since 0.2.0 |
| 67 | * @access protected |
| 68 | * @var array |
| 69 | */ |
| 70 | protected $positions = array( |
| 71 | 'before', |
| 72 | 'after', |
| 73 | ); |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Class constructor |
| 78 | * |
| 79 | * This simply sets $key |
| 80 | * |
| 81 | * @since 0.1.0 |
| 82 | */ |
| 83 | function __construct() { |
| 84 | $this->key = $this->type . '-icon'; |
| 85 | |
| 86 | if ( is_null( $this->version ) ) { |
| 87 | $this->version = get_bloginfo( 'version' ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Register our type |
| 94 | * |
| 95 | * @since 0.1.0 |
| 96 | * @param array $types Icon Types |
| 97 | * @return array |
| 98 | */ |
| 99 | public function register( $types ) { |
| 100 | $props = array( |
| 101 | 'label' => $this->label, |
| 102 | 'field_cb' => array( $this, 'the_field' ), |
| 103 | 'front_cb' => array( $this, 'front' ), |
| 104 | 'stylesheet' => $this->stylesheet, |
| 105 | 'version' => $this->version, |
| 106 | ); |
| 107 | |
| 108 | if ( method_exists( $this, 'frame_cb' ) ) { |
| 109 | $props['frame_cb'] = array( $this, 'frame_cb' ); |
| 110 | if ( method_exists( $this, 'templates' ) ) { |
| 111 | $props['templates'] = $this->templates(); |
| 112 | } |
| 113 | if ( method_exists( $this, 'preview_cb' ) ) { |
| 114 | $props['preview_cb'] = array( $this, 'preview_cb' ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | $types[ $this->type ] = $props; |
| 119 | |
| 120 | return $types; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Print field for icons selection |
| 126 | * |
| 127 | * @since 0.1.0 |
| 128 | * @param int $id Menu item ID |
| 129 | * @param array $meta_value Current value of 'menu-icons' metadata |
| 130 | */ |
| 131 | abstract public function the_field( $id, $meta_value ); |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Front-end tasks |
| 136 | * |
| 137 | * @since 0.1.0 |
| 138 | * @param string $type Icon type |
| 139 | */ |
| 140 | public function front() { |
| 141 | add_filter( 'wp_nav_menu_args', array( $this, '_add_menu_item_title_filter' ) ); |
| 142 | add_filter( 'wp_nav_menu', array( $this, '_remove_menu_item_title_filter' ) ); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Add filter to 'the_title' hook |
| 148 | * |
| 149 | * We need to filter the menu item title but **not** regular post titles. |
| 150 | * Thus, we're adding the filter when `wp_nav_menu()` is called. |
| 151 | * |
| 152 | * @since 0.1.0 |
| 153 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_nav_menu_args Filter: wp_nav_menu_args/999/2 |
| 154 | * @param array $args Not used |
| 155 | * |
| 156 | * @return array |
| 157 | */ |
| 158 | public function _add_menu_item_title_filter( $args ) { |
| 159 | add_filter( 'the_title', array( $this, '_filter_menu_item_title' ), 999, 2 ); |
| 160 | |
| 161 | return $args; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Remove filter from 'the_title' hook |
| 167 | * |
| 168 | * Because we don't want to filter post titles, we need to remove our |
| 169 | * filter when `wp_nav_menu()` exits. |
| 170 | * |
| 171 | * @since 0.1.0 |
| 172 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_nav_menu Filter: wp_nav_menu/999/2 |
| 173 | * @param array $nav_menu Not used |
| 174 | * @return array |
| 175 | */ |
| 176 | public function _remove_menu_item_title_filter( $nav_menu ) { |
| 177 | remove_filter( 'the_title', array( $this, '_filter_menu_item_title' ), 999, 2 ); |
| 178 | |
| 179 | return $nav_menu; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Filter menu item titles |
| 185 | * |
| 186 | * @since 0.1.0 |
| 187 | * @link http://codex.wordpress.org/Plugin_API/Action_Reference/the_title Filter: the_title/999/2 |
| 188 | * |
| 189 | * @param string $title Menu item title |
| 190 | * @param int $id Menu item ID |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | public function _filter_menu_item_title( $title, $id ) { |
| 195 | $values = array_filter( (array) get_post_meta( $id, 'menu-icons', true ) ); |
| 196 | |
| 197 | if ( empty( $values['type'] ) ) { |
| 198 | return $title; |
| 199 | } |
| 200 | |
| 201 | if ( $values['type'] !== $this->type ) { |
| 202 | return $title; |
| 203 | } |
| 204 | |
| 205 | if ( empty( $values[ $this->key ] ) ) { |
| 206 | return $title; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Set icon position, defaults to 'before' |
| 211 | * |
| 212 | * @since 0.2.0 |
| 213 | */ |
| 214 | if ( ! isset( $values['position'] ) || ! in_array( $values['position'], $this->positions ) ) { |
| 215 | $values['position'] = $this->positions[0]; |
| 216 | } |
| 217 | |
| 218 | $title = $this->add_icon( $title, $values ); |
| 219 | |
| 220 | return $title; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /** |
| 225 | * Add icon to menu title |
| 226 | * |
| 227 | * Icon types should override this method if they want to provide different markup. |
| 228 | * |
| 229 | * @since 0.1.0 |
| 230 | * @param string $title Menu item title |
| 231 | * @param array $values Menu item metadata value |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | abstract protected function add_icon( $title, $values ); |
| 236 | } |
| 237 |