| 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 menu settings |
| 65 |
* |
| 66 |
* @since 0.3.0 |
| 67 |
* @access protected |
| 68 |
* @var array |
| 69 |
*/ |
| 70 |
protected $menu_setttings = array(); |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Class constructor |
| 75 |
* |
| 76 |
* This simply sets $key |
| 77 |
* |
| 78 |
* @since 0.1.0 |
| 79 |
*/ |
| 80 |
function __construct() { |
| 81 |
$this->key = $this->type . '-icon'; |
| 82 |
|
| 83 |
if ( is_null( $this->version ) ) { |
| 84 |
$this->version = get_bloginfo( 'version' ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* Register our type |
| 91 |
* |
| 92 |
* @since 0.1.0 |
| 93 |
* @param array $types Icon Types |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function register( $types ) { |
| 97 |
$props = array( |
| 98 |
'label' => $this->label, |
| 99 |
'field_cb' => array( $this, 'the_field' ), |
| 100 |
'front_cb' => array( $this, 'front' ), |
| 101 |
'stylesheet' => $this->stylesheet, |
| 102 |
'version' => $this->version, |
| 103 |
); |
| 104 |
|
| 105 |
if ( method_exists( $this, 'frame_cb' ) ) { |
| 106 |
$props['frame_cb'] = array( $this, 'frame_cb' ); |
| 107 |
if ( method_exists( $this, 'templates' ) ) { |
| 108 |
$props['templates'] = $this->templates(); |
| 109 |
} |
| 110 |
if ( method_exists( $this, 'preview_cb' ) ) { |
| 111 |
$props['preview_cb'] = array( $this, 'preview_cb' ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$types[ $this->type ] = $props; |
| 116 |
|
| 117 |
return $types; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Print field for icons selection |
| 123 |
* |
| 124 |
* @since 0.1.0 |
| 125 |
* @param int $id Menu item ID |
| 126 |
* @param array $meta_value Current value of 'menu-icons' metadata |
| 127 |
*/ |
| 128 |
abstract public function the_field( $id, $meta_value ); |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Front-end tasks |
| 133 |
* |
| 134 |
* @since 0.1.0 |
| 135 |
* @param string $type Icon type |
| 136 |
*/ |
| 137 |
public function front() { |
| 138 |
add_filter( 'wp_nav_menu_args', array( $this, '_add_menu_item_title_filter' ) ); |
| 139 |
add_filter( 'wp_nav_menu', array( $this, '_remove_menu_item_title_filter' ) ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Add filter to 'the_title' hook |
| 145 |
* |
| 146 |
* We need to filter the menu item title but **not** regular post titles. |
| 147 |
* Thus, we're adding the filter when `wp_nav_menu()` is called. |
| 148 |
* |
| 149 |
* @since 0.1.0 |
| 150 |
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_nav_menu_args Filter: wp_nav_menu_args/999/2 |
| 151 |
* @param array $args Not used |
| 152 |
* |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
public function _add_menu_item_title_filter( $args ) { |
| 156 |
$menu_id = Menu_Icons::get_nav_menu_id( $args ); |
| 157 |
|
| 158 |
if ( false !== $menu_id ) { |
| 159 |
$this->menu_settings = Menu_Icons_Settings::get_menu_settings( $menu_id ); |
| 160 |
add_filter( 'the_title', array( $this, '_filter_menu_item_title' ), 999, 2 ); |
| 161 |
} |
| 162 |
|
| 163 |
return $args; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Remove filter from 'the_title' hook |
| 169 |
* |
| 170 |
* Because we don't want to filter post titles, we need to remove our |
| 171 |
* filter when `wp_nav_menu()` exits. |
| 172 |
* |
| 173 |
* @since 0.1.0 |
| 174 |
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_nav_menu Filter: wp_nav_menu/999/2 |
| 175 |
* @param array $nav_menu Not used |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
public function _remove_menu_item_title_filter( $nav_menu ) { |
| 179 |
$this->menu_settings = array(); |
| 180 |
remove_filter( 'the_title', array( $this, '_filter_menu_item_title' ), 999, 2 ); |
| 181 |
|
| 182 |
return $nav_menu; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Filter menu item titles |
| 188 |
* |
| 189 |
* @since 0.1.0 |
| 190 |
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/the_title Filter: the_title/999/2 |
| 191 |
* |
| 192 |
* @param string $title Menu item title |
| 193 |
* @param int $id Menu item ID |
| 194 |
* |
| 195 |
* @return string |
| 196 |
*/ |
| 197 |
public function _filter_menu_item_title( $title, $id ) { |
| 198 |
$values = wp_parse_args( Menu_Icons::get_meta( $id ), $this->menu_settings ); |
| 199 |
|
| 200 |
if ( empty( $values['type'] ) ) { |
| 201 |
return $title; |
| 202 |
} |
| 203 |
|
| 204 |
if ( $values['type'] !== $this->type ) { |
| 205 |
return $title; |
| 206 |
} |
| 207 |
|
| 208 |
if ( empty( $values[ $this->key ] ) ) { |
| 209 |
return $title; |
| 210 |
} |
| 211 |
|
| 212 |
$title = $this->add_icon( $title, $values ); |
| 213 |
|
| 214 |
return $title; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Add icon to menu title |
| 220 |
* |
| 221 |
* Icon types should override this method if they want to provide different markup. |
| 222 |
* |
| 223 |
* @since 0.1.0 |
| 224 |
* @param string $title Menu item title |
| 225 |
* @param array $values Menu item metadata value |
| 226 |
* |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
abstract protected function add_icon( $title, $values ); |
| 230 |
} |
| 231 |
|