PluginProbe
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.1.5
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.1.5
0.13.24 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.1.4 0.1.5 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.11.2 0.11.3 0.11.4 0.11.5 0.12.0 0.12.1 0.12.10 0.12.11 0.12.12 0.12.2 0.12.3 0.12.4 All 70 releases
menu-icons / includes / type.php

type.php in Menu Icons by Themeisle – Add Icons to Navigation Menus 0.1.5, at includes/type.php

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