library
11 years ago
admin.php
11 years ago
media-template.php
11 years ago
settings.php
11 years ago
type-dashicons.php
11 years ago
type-elusive.php
11 years ago
type-fontawesome.php
11 years ago
type-fontpack.php
11 years ago
type-fonts.php
11 years ago
type-foundation.php
11 years ago
type-genericons.php
11 years ago
type-image.php
11 years ago
type.php
11 years ago
walker-nav-menu-edit.php
11 years ago
type-image.php
298 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Image icon handler |
| 4 | * |
| 5 | * @package Menu_Icons |
| 6 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 7 | */ |
| 8 | |
| 9 | require_once dirname( __FILE__ ) . '/type.php'; |
| 10 | |
| 11 | /** |
| 12 | * Image icons |
| 13 | * |
| 14 | */ |
| 15 | class Menu_Icons_Type_Image extends Menu_Icons_Type { |
| 16 | |
| 17 | /** |
| 18 | * Holds icon type |
| 19 | * |
| 20 | * @since 0.4.0 |
| 21 | * @access protected |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $type = 'image'; |
| 25 | |
| 26 | /** |
| 27 | * Class constructor |
| 28 | * |
| 29 | * This simply sets $label |
| 30 | * |
| 31 | * @since 0.4.0 |
| 32 | */ |
| 33 | function __construct() { |
| 34 | $this->label = __( 'Image', 'menu-icons' ); |
| 35 | parent::__construct(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get image sizes |
| 40 | * |
| 41 | * @since 0.4.0 |
| 42 | * @access protected |
| 43 | * @return array |
| 44 | */ |
| 45 | protected function get_image_sizes() { |
| 46 | $_sizes = array( |
| 47 | 'thumbnail' => __( 'Thumbnail', 'menu-icons' ), |
| 48 | 'medium' => __( 'Medium', 'menu-icons' ), |
| 49 | 'large' => __( 'Large', 'menu-icons' ), |
| 50 | 'full' => __( 'Full Size', 'menu-icons' ), |
| 51 | ); |
| 52 | |
| 53 | $_sizes = apply_filters( 'image_size_names_choose', $_sizes ); |
| 54 | |
| 55 | $sizes = array(); |
| 56 | foreach ( $_sizes as $value => $label ) { |
| 57 | $sizes[] = array( |
| 58 | 'value' => $value, |
| 59 | 'label' => $label, |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | return $sizes; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Settings fields |
| 68 | * |
| 69 | * @since 0.4.0 |
| 70 | * @param array $fields |
| 71 | * @uses apply_filters() Calls 'menu_icons_{type}_settings_sections'. |
| 72 | * @return array |
| 73 | */ |
| 74 | public function _settings_fields( $fields ) { |
| 75 | $_fields = array( |
| 76 | 'image_size' => array( |
| 77 | 'id' => 'image_size', |
| 78 | 'type' => 'select', |
| 79 | 'label' => __( 'Image Size', 'menu-icons' ), |
| 80 | 'default' => 'full', |
| 81 | 'choices' => self::get_image_sizes(), |
| 82 | ), |
| 83 | 'vertical_align' => array( |
| 84 | 'id' => 'vertical_align', |
| 85 | 'type' => 'select', |
| 86 | 'label' => __( 'Vertical Align', 'menu-icons' ), |
| 87 | 'default' => 'middle', |
| 88 | 'choices' => array( |
| 89 | array( |
| 90 | 'value' => 'super', |
| 91 | 'label' => __( 'Super', 'menu-icons' ), |
| 92 | ), |
| 93 | array( |
| 94 | 'value' => 'top', |
| 95 | 'label' => __( 'Top', 'menu-icons' ), |
| 96 | ), |
| 97 | array( |
| 98 | 'value' => 'text-top', |
| 99 | 'label' => __( 'Text Top', 'menu-icons' ), |
| 100 | ), |
| 101 | array( |
| 102 | 'value' => 'middle', |
| 103 | 'label' => __( 'Middle', 'menu-icons' ), |
| 104 | ), |
| 105 | array( |
| 106 | 'value' => 'baseline', |
| 107 | 'label' => __( 'Baseline', 'menu-icons' ), |
| 108 | ), |
| 109 | array( |
| 110 | 'value' => 'text-bottom', |
| 111 | 'label' => __( 'Text Bottom', 'menu-icons' ), |
| 112 | ), |
| 113 | array( |
| 114 | 'value' => 'bottom', |
| 115 | 'label' => __( 'Bottom', 'menu-icons' ), |
| 116 | ), |
| 117 | array( |
| 118 | 'value' => 'sub', |
| 119 | 'label' => __( 'Sub', 'menu-icons' ), |
| 120 | ), |
| 121 | ), |
| 122 | ), |
| 123 | ); |
| 124 | |
| 125 | $_fields = apply_filters( sprintf( 'menu_icons_%s_settings_fields', $this->type ), $_fields ); |
| 126 | $fields = wp_parse_args( $_fields, $fields ); |
| 127 | |
| 128 | return $fields; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Print field for icons selection |
| 134 | * |
| 135 | * @since 0.4.0 |
| 136 | * @param int $id Menu item ID |
| 137 | * @param array $meta_value Current value of 'menu-icons' metadata |
| 138 | */ |
| 139 | public function the_field( $id, $meta_value ) { |
| 140 | $current = isset( $meta_value[ $this->key ] ) ? $meta_value[ $this->key ] : ''; |
| 141 | $input_id = sprintf( 'menu-icons-%d-%s', $id, $this->key ); |
| 142 | $input_name = sprintf( 'menu-icons[%d][%s]', $id, $this->key ); |
| 143 | ?> |
| 144 | <?php printf( |
| 145 | '<p class="field-icon-child description menu-icon-type-%1$s" data-dep-on="%1$s">', |
| 146 | esc_attr( $this->type ) |
| 147 | ) ?> |
| 148 | <label for="<?php echo esc_attr( $input_id ) ?>"><?php echo esc_html( $this->label ); ?></label> |
| 149 | <?php printf( |
| 150 | '<input type="text" id="%s" name="%s" data-key="%s" value="%s" />', |
| 151 | esc_attr( $input_id ), |
| 152 | esc_attr( $input_name ), |
| 153 | esc_attr( $this->key ), |
| 154 | esc_attr( $current ) |
| 155 | ) ?> |
| 156 | </p> |
| 157 | <?php |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Preview |
| 163 | * |
| 164 | * @since 0.4.0 |
| 165 | * @param string $id Menu item ID |
| 166 | * @param array $meta_value Menu item metadata value |
| 167 | * @return array |
| 168 | */ |
| 169 | public function preview_cb( $id, $meta_value ) { |
| 170 | if ( empty( $meta_value['image-icon'] ) ) { |
| 171 | return null; |
| 172 | } |
| 173 | |
| 174 | return wp_get_attachment_image( |
| 175 | $meta_value['image-icon'], |
| 176 | $meta_value['image_size'], |
| 177 | false, |
| 178 | array( 'class' => '_icon' ) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Media frame data |
| 185 | * |
| 186 | * @since 0.4.0 |
| 187 | * @param string $id Icon type ID |
| 188 | * @return array |
| 189 | */ |
| 190 | public function frame_cb( $id ) { |
| 191 | $data = array( |
| 192 | 'controller' => 'miImage', |
| 193 | ); |
| 194 | |
| 195 | return $data; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Media frame templates |
| 201 | * |
| 202 | * @since 0.4.0 |
| 203 | * @return array |
| 204 | */ |
| 205 | public function templates() { |
| 206 | $icon = '<img src="{{ data.url }}" alt="{{ data.alt }}" class="_icon {{ data.type }} _{{ data.position }}" style="vertical-align:{{ data.vertical_align }};" />'; |
| 207 | |
| 208 | $templates = array( |
| 209 | 'field' => '<img src="{{ data.sizes[data._settings.image_size].url }}" alt="{{ data.alt }}" class="_icon" />', |
| 210 | 'preview-before' => sprintf( '<a href="#">%s <span>{{ data.title }}</span></a>', $icon ), |
| 211 | 'preview-after' => sprintf( '<a href="#"><span>{{ data.title }}</span> %s</a>', $icon ), |
| 212 | 'preview-hide_label' => sprintf( '<a href="#">%s</a>', $icon ), |
| 213 | ); |
| 214 | |
| 215 | return $templates; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Add icon to menu title |
| 221 | * |
| 222 | * @since 0.4.0 |
| 223 | * @access protected |
| 224 | * @param string $title Menu item title |
| 225 | * @param array $values Menu item metadata value |
| 226 | * |
| 227 | * @return string |
| 228 | */ |
| 229 | protected function add_icon( $title, $values ) { |
| 230 | if ( empty( $values['image-icon'] ) ) { |
| 231 | return $title; |
| 232 | } |
| 233 | |
| 234 | $icon = get_post( $values['image-icon'] ); |
| 235 | if ( ! ( $icon instanceof WP_Post ) || 'attachment' !== $icon->post_type ) { |
| 236 | return $title; |
| 237 | } |
| 238 | |
| 239 | $t_class = ! empty( $values['hide_label'] ) ? 'visuallyhidden' : ''; |
| 240 | $title = sprintf( |
| 241 | '<span%s>%s</span>', |
| 242 | ( ! empty( $t_class ) ) ? sprintf( ' class="%s"', esc_attr( $t_class ) ) : '', |
| 243 | $title |
| 244 | ); |
| 245 | |
| 246 | $i_class = '_mi'; |
| 247 | $i_class .= empty( $values['hide_label'] ) ? " _{$values['position']}" : ''; |
| 248 | $i_style = $this->get_style( $values ); |
| 249 | $i_attrs = array( 'class' => $i_class ); |
| 250 | |
| 251 | if ( ! empty( $i_style ) ) { |
| 252 | $i_attrs['style'] = $i_style; |
| 253 | } |
| 254 | |
| 255 | $title = sprintf( |
| 256 | '%s%s%s', |
| 257 | 'before' === $values['position'] ? '' : $title, |
| 258 | wp_get_attachment_image( |
| 259 | $icon->ID, |
| 260 | $values['image_size'], |
| 261 | false, |
| 262 | $i_attrs |
| 263 | ), |
| 264 | 'after' === $values['position'] ? '' : $title |
| 265 | ); |
| 266 | |
| 267 | return $title; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /** |
| 272 | * Inline style for icon size, etc |
| 273 | * |
| 274 | * @since 0.4.0 |
| 275 | * @param array $values Menu item metadata value |
| 276 | * @return string |
| 277 | */ |
| 278 | protected function get_style( $values ) { |
| 279 | $style_d = Menu_Icons::get( 'default_style' ); |
| 280 | $style_a = array(); |
| 281 | $style_s = ''; |
| 282 | |
| 283 | if ( ! empty( $values['vertical_align'] ) ) { |
| 284 | $style_a['vertical-align'] = $values['vertical_align']; |
| 285 | } |
| 286 | |
| 287 | $style_a = array_diff_assoc( $style_a, $style_d ); |
| 288 | |
| 289 | if ( ! empty( $style_a ) ) { |
| 290 | foreach ( $style_a as $key => $value ) { |
| 291 | $style_s .= sprintf( '%s:%s;', esc_attr( $key ), esc_attr( $value ) ); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return $style_s; |
| 296 | } |
| 297 | } |
| 298 |