| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Front end functionalities |
| 5 |
* |
| 6 |
* @package Menu_Icons |
| 7 |
* @author Dzikri Aziz <kvcrvt@gmail.com> |
| 8 |
*/ |
| 9 |
final class Menu_Icons_Front_End { |
| 10 |
|
| 11 |
/** |
| 12 |
* Icon types |
| 13 |
* |
| 14 |
* @since 0.9.0 |
| 15 |
* @access protected |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected static $icon_types = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* Default icon style |
| 22 |
* |
| 23 |
* @since 0.9.0 |
| 24 |
* @access protected |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected static $default_style = array( |
| 28 |
'font_size' => array( |
| 29 |
'property' => 'font-size', |
| 30 |
'value' => '1.2', |
| 31 |
'unit' => 'em', |
| 32 |
), |
| 33 |
'vertical_align' => array( |
| 34 |
'property' => 'vertical-align', |
| 35 |
'value' => 'middle', |
| 36 |
'unit' => null, |
| 37 |
), |
| 38 |
'svg_width' => array( |
| 39 |
'property' => 'width', |
| 40 |
'value' => '1', |
| 41 |
'unit' => 'em', |
| 42 |
), |
| 43 |
); |
| 44 |
|
| 45 |
/** |
| 46 |
* Hidden label class |
| 47 |
* |
| 48 |
* @since 0.9.0 |
| 49 |
* @access protected |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
protected static $hidden_label_class = 'visuallyhidden'; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Add hooks for front-end functionalities |
| 57 |
* |
| 58 |
* @since 0.9.0 |
| 59 |
*/ |
| 60 |
public static function init() { |
| 61 |
$active_types = Menu_Icons_Settings::get( 'global', 'icon_types' ); |
| 62 |
|
| 63 |
if ( empty( $active_types ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( Menu_Icons::get( 'types' ) as $type ) { |
| 68 |
if ( in_array( $type->id, $active_types ) ) { |
| 69 |
self::$icon_types[ $type->id ] = $type; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Allow themes/plugins to override the hidden label class |
| 75 |
* |
| 76 |
* @since 0.8.0 |
| 77 |
* @param string $hidden_label_class Hidden label class. |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
self::$hidden_label_class = apply_filters( 'menu_icons_hidden_label_class', self::$hidden_label_class ); |
| 81 |
|
| 82 |
/** |
| 83 |
* Allow themes/plugins to override default inline style |
| 84 |
* |
| 85 |
* @since 0.9.0 |
| 86 |
* @param array $default_style Default inline style. |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
self::$default_style = apply_filters( 'menu_icons_default_style', self::$default_style ); |
| 90 |
|
| 91 |
add_action( 'wp_enqueue_scripts', array( __CLASS__, '_enqueue_styles' ), 7 ); |
| 92 |
add_filter( 'wp_nav_menu_args', array( __CLASS__, '_add_menu_item_title_filter' ) ); |
| 93 |
add_filter( 'wp_nav_menu', array( __CLASS__, '_remove_menu_item_title_filter' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Get nav menu ID based on arguments passed to wp_nav_menu() |
| 99 |
* |
| 100 |
* @since 0.3.0 |
| 101 |
* @param array $args wp_nav_menu() Arguments |
| 102 |
* @return mixed Nav menu ID or FALSE on failure |
| 103 |
*/ |
| 104 |
public static function get_nav_menu_id( $args ) { |
| 105 |
$args = (object) $args; |
| 106 |
$menu = wp_get_nav_menu_object( $args->menu ); |
| 107 |
|
| 108 |
// Get the nav menu based on the theme_location |
| 109 |
if ( ! $menu |
| 110 |
&& $args->theme_location |
| 111 |
&& ( $locations = get_nav_menu_locations() ) |
| 112 |
&& isset( $locations[ $args->theme_location ] ) |
| 113 |
) { |
| 114 |
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); |
| 115 |
} |
| 116 |
|
| 117 |
// get the first menu that has items if we still can't find a menu |
| 118 |
if ( ! $menu && ! $args->theme_location ) { |
| 119 |
$menus = wp_get_nav_menus(); |
| 120 |
foreach ( $menus as $menu_maybe ) { |
| 121 |
if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) { |
| 122 |
$menu = $menu_maybe; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if ( is_object( $menu ) && ! is_wp_error( $menu ) ) { |
| 129 |
return $menu->term_id; |
| 130 |
} else { |
| 131 |
return false; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/** |
| 137 |
* Enqueue stylesheets |
| 138 |
* |
| 139 |
* @since 0.1.0 |
| 140 |
* @wp_hook action wp_enqueue_scripts |
| 141 |
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts |
| 142 |
*/ |
| 143 |
public static function _enqueue_styles() { |
| 144 |
foreach ( self::$icon_types as $type ) { |
| 145 |
if ( wp_style_is( $type->stylesheet_id, 'registered' ) ) { |
| 146 |
wp_enqueue_style( $type->stylesheet_id ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Allow plugins/themes to override the extra stylesheet location |
| 152 |
* |
| 153 |
* @since 0.9.0 |
| 154 |
* @param string $extra_stylesheet_uri Extra stylesheet URI. |
| 155 |
*/ |
| 156 |
$extra_stylesheet_uri = apply_filters( |
| 157 |
'menu_icons_extra_stylesheet_uri', |
| 158 |
sprintf( '%scss/extra%s.css', Menu_Icons::get( 'url' ), kucrut_get_script_suffix() ) |
| 159 |
); |
| 160 |
|
| 161 |
wp_enqueue_style( |
| 162 |
'menu-icons-extra', |
| 163 |
$extra_stylesheet_uri, |
| 164 |
false, |
| 165 |
Menu_Icons::VERSION |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Add filter to 'the_title' hook |
| 172 |
* |
| 173 |
* We need to filter the menu item title but **not** regular post titles. |
| 174 |
* Thus, we're adding the filter when `wp_nav_menu()` is called. |
| 175 |
* |
| 176 |
* @since 0.1.0 |
| 177 |
* @wp_hook filter wp_nav_menu_args |
| 178 |
* @param array $args Not used. |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public static function _add_menu_item_title_filter( $args ) { |
| 183 |
add_filter( 'the_title', array( __CLASS__, '_add_icon' ), 999, 2 ); |
| 184 |
|
| 185 |
return $args; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Remove filter from 'the_title' hook |
| 191 |
* |
| 192 |
* Because we don't want to filter post titles, we need to remove our |
| 193 |
* filter when `wp_nav_menu()` exits. |
| 194 |
* |
| 195 |
* @since 0.1.0 |
| 196 |
* @wp_hook filter wp_nav_menu |
| 197 |
* @param array $nav_menu Not used. |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
public static function _remove_menu_item_title_filter( $nav_menu ) { |
| 201 |
remove_filter( 'the_title', array( __CLASS__, '_add_icon' ), 999, 2 ); |
| 202 |
|
| 203 |
return $nav_menu; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Add icon to menu item title |
| 209 |
* |
| 210 |
* @since 0.1.0 |
| 211 |
* @since 0.9.0 Renamed the method to `add_icon()`. |
| 212 |
* @wp_hook filter the_title |
| 213 |
* @param string $title Menu item title. |
| 214 |
* @param int $id Menu item ID. |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public static function _add_icon( $title, $id ) { |
| 219 |
$meta = Menu_Icons_Meta::get( $id ); |
| 220 |
$icon = self::get_icon( $meta ); |
| 221 |
|
| 222 |
if ( empty( $icon ) ) { |
| 223 |
return $title; |
| 224 |
} |
| 225 |
|
| 226 |
$title_class = ! empty( $meta['hide_label'] ) ? self::$hidden_label_class : ''; |
| 227 |
$title_wrapped = sprintf( |
| 228 |
'<span%s>%s</span>', |
| 229 |
( ! empty( $title_class ) ) ? sprintf( ' class="%s"', esc_attr( $title_class ) ) : '', |
| 230 |
$title |
| 231 |
); |
| 232 |
|
| 233 |
if ( 'after' === $meta['position'] ) { |
| 234 |
$title_with_icon = "{$title_wrapped}{$icon}"; |
| 235 |
} else { |
| 236 |
$title_with_icon = "{$icon}{$title_wrapped}"; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Allow plugins/themes to override menu item markup |
| 241 |
* |
| 242 |
* @since 0.8.0 |
| 243 |
* |
| 244 |
* @param string $title_with_icon Menu item markup after the icon is added. |
| 245 |
* @param integer $id Menu item ID. |
| 246 |
* @param array $meta Menu item metadata values. |
| 247 |
* @param string $title Original menu item title. |
| 248 |
* |
| 249 |
* @return string |
| 250 |
*/ |
| 251 |
$title_with_icon = apply_filters( 'menu_icons_item_title', $title_with_icon, $id, $meta, $title ); |
| 252 |
|
| 253 |
return $title_with_icon; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* Get icon |
| 259 |
* |
| 260 |
* @since 0.9.0 |
| 261 |
* @param array $meta Menu item meta value. |
| 262 |
* @return string |
| 263 |
*/ |
| 264 |
public static function get_icon( $meta ) { |
| 265 |
$icon = ''; |
| 266 |
|
| 267 |
// Icon type is not set. |
| 268 |
if ( empty( $meta['type'] ) ) { |
| 269 |
return $icon; |
| 270 |
} |
| 271 |
|
| 272 |
// Icon is not set. |
| 273 |
if ( empty( $meta['icon'] ) ) { |
| 274 |
return $icon; |
| 275 |
} |
| 276 |
|
| 277 |
// Icon type is not registered/enabled. |
| 278 |
if ( ! isset( self::$icon_types[ $meta['type'] ] ) ) { |
| 279 |
return $icon; |
| 280 |
} |
| 281 |
|
| 282 |
$type = self::$icon_types[ $meta['type'] ]; |
| 283 |
|
| 284 |
$callbacks = array( |
| 285 |
array( $type, 'get_icon' ), |
| 286 |
array( __CLASS__, "get_{$type->id}_icon" ), |
| 287 |
array( __CLASS__, "get_{$type->template_id}_icon" ), |
| 288 |
); |
| 289 |
|
| 290 |
foreach ( $callbacks as $callback ) { |
| 291 |
if ( is_callable( $callback ) ) { |
| 292 |
$icon = call_user_func( $callback, $meta ); |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
return $icon; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* Get icon style |
| 303 |
* |
| 304 |
* @since 0.9.0 |
| 305 |
* @param array $meta Menu item meta value. |
| 306 |
* @param array $keys Style properties. |
| 307 |
* @param bool $as_attribute Optional. Whether to output the style as HTML attribute or value only. |
| 308 |
* Defaults to TRUE. |
| 309 |
* @return string |
| 310 |
*/ |
| 311 |
public static function get_icon_style( $meta, $keys, $as_attribute = true ) { |
| 312 |
$style_a = array(); |
| 313 |
$style_s = ''; |
| 314 |
|
| 315 |
foreach ( $keys as $key ) { |
| 316 |
if ( ! isset( self::$default_style[ $key ] ) ) { |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
$rule = self::$default_style[ $key ]; |
| 321 |
|
| 322 |
if ( ! isset( $meta[ $key ] ) || $meta[ $key ] === $rule['value'] ) { |
| 323 |
continue; |
| 324 |
} |
| 325 |
|
| 326 |
$value = $meta[ $key ]; |
| 327 |
if ( ! empty( $rule['unit'] ) ) { |
| 328 |
$value .= $rule['unit']; |
| 329 |
} |
| 330 |
|
| 331 |
$style_a[ $rule['property'] ] = $value; |
| 332 |
} |
| 333 |
|
| 334 |
if ( empty( $style_a ) ) { |
| 335 |
return $style_s; |
| 336 |
} |
| 337 |
|
| 338 |
foreach ( $style_a as $key => $value ) { |
| 339 |
$style_s .= "{$key}:{$value};"; |
| 340 |
} |
| 341 |
|
| 342 |
$style_s = esc_attr( $style_s ); |
| 343 |
|
| 344 |
if ( $as_attribute ) { |
| 345 |
$style_s = sprintf( ' style="%s"', $style_s ); |
| 346 |
} |
| 347 |
|
| 348 |
return $style_s; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
/** |
| 353 |
* Get icon classes |
| 354 |
* |
| 355 |
* @since 0.9.0 |
| 356 |
* @param array $meta Menu item meta value. |
| 357 |
* @param string $output Whether to output the classes as string or array. Defaults to string. |
| 358 |
* @return string|array |
| 359 |
*/ |
| 360 |
public static function get_icon_classes( $meta, $output = 'string' ) { |
| 361 |
$classes = array( '_mi' ); |
| 362 |
|
| 363 |
if ( empty( $meta['hide_label'] ) ) { |
| 364 |
$classes[] = "_{$meta['position']}"; |
| 365 |
} |
| 366 |
|
| 367 |
if ( 'string' === $output ) { |
| 368 |
$classes = implode( ' ', $classes ); |
| 369 |
} |
| 370 |
|
| 371 |
return $classes; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* Get font icon |
| 377 |
* |
| 378 |
* @since 0.9.0 |
| 379 |
* @param array $meta Menu item meta value. |
| 380 |
* @return string |
| 381 |
*/ |
| 382 |
public static function get_font_icon( $meta ) { |
| 383 |
$classes = sprintf( '%s %s %s', self::get_icon_classes( $meta ), $meta['type'], $meta['icon'] ); |
| 384 |
$style = self::get_icon_style( $meta, array( 'font_size', 'vertical_align' ) ); |
| 385 |
|
| 386 |
return sprintf( '<i class="%s"%s></i>', esc_attr( $classes ), $style ); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* Get image icon |
| 392 |
* |
| 393 |
* @since 0.9.0 |
| 394 |
* @param array $meta Menu item meta value. |
| 395 |
* @return string |
| 396 |
*/ |
| 397 |
public static function get_image_icon( $meta ) { |
| 398 |
$args = array( |
| 399 |
'class' => sprintf( '%s _image', self::get_icon_classes( $meta ) ), |
| 400 |
); |
| 401 |
|
| 402 |
$style = self::get_icon_style( $meta, array( 'vertical_align' ), false ); |
| 403 |
if ( ! empty( $style ) ) { |
| 404 |
$args['style'] = $style; |
| 405 |
} |
| 406 |
|
| 407 |
return wp_get_attachment_image( $meta['icon'], $meta['image_size'], false, $args ); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
/** |
| 412 |
* Get SVG icon |
| 413 |
* |
| 414 |
* @since 0.9.0 |
| 415 |
* @param array $meta Menu item meta value. |
| 416 |
* @return string |
| 417 |
*/ |
| 418 |
public static function get_svg_icon( $meta ) { |
| 419 |
$classes = sprintf( '%s _svg', self::get_icon_classes( $meta ) ); |
| 420 |
$style = self::get_icon_style( $meta, array( 'svg_width', 'vertical_align' ) ); |
| 421 |
|
| 422 |
return sprintf( |
| 423 |
'<img src="%s" class="%s"%s />', |
| 424 |
esc_url( wp_get_attachment_url( $meta['icon'] ) ), |
| 425 |
esc_attr( $classes ), |
| 426 |
$style |
| 427 |
); |
| 428 |
} |
| 429 |
} |
| 430 |
|