PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / trunk
Menu Icons by Themeisle – Add Icons to Navigation Menus vtrunk
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 0.12.5 0.12.6 0.12.7 0.12.8 0.12.9 0.13.0 0.13.1 0.13.10 0.13.11 0.13.12 0.13.13 0.13.14 0.13.15 0.13.16 0.13.17 0.13.18 0.13.19 0.13.2 0.13.20 0.13.21 0.13.22 0.13.23 0.13.3 0.13.4 0.13.5 0.13.6 0.13.7 0.13.8 0.13.9 0.2.0 0.2.1 0.2.2 0.2.3 0.3.0 0.3.1 0.3.2 0.4.0 0.5.0 0.5.1 0.6.0 0.7.0 0.8.0 0.8.1 0.9.0 0.9.2
menu-icons / includes / meta.php
menu-icons / includes Last commit date
library 6 months ago front.php 2 months ago media-template.php 4 years ago meta.php 2 months ago picker.php 3 years ago settings.php 2 months ago type-fonts.php 10 years ago type.php 10 years ago
meta.php
178 lines
1 <?php
2
3 /**
4 * Menu item metadata
5 *
6 * @package Menu_Icons
7 * @author Dzikri Aziz <kvcrvt@gmail.com>
8 */
9 final class Menu_Icons_Meta {
10
11 const KEY = 'menu-icons';
12
13 /**
14 * Default meta value
15 *
16 * @since 0.9.0
17 * @access protected
18 * @var array
19 */
20 protected static $defaults = array(
21 'type' => '',
22 'icon' => '',
23 'url' => '',
24 );
25
26
27 /**
28 * Initialize metadata functionalities
29 *
30 * @since 0.9.0
31 */
32 public static function init() {
33 add_filter( 'is_protected_meta', array( __CLASS__, '_protect_meta_key' ), 10, 3 );
34 }
35
36
37 /**
38 * Protect meta key
39 *
40 * This prevents our meta key from showing up on Custom Fields meta box.
41 *
42 * @since 0.3.0
43 * @wp_hook filter is_protected_meta
44 * @param bool $protected Protection status.
45 * @param string $meta_key Meta key.
46 * @param string $meta_type Meta type.
47 * @return bool Protection status.
48 */
49 public static function _protect_meta_key( $protected, $meta_key, $meta_type ) {
50 if ( self::KEY === $meta_key ) {
51 $protected = true;
52 }
53
54 return $protected;
55 }
56
57
58 /**
59 * Get menu item meta value
60 *
61 * @since 0.3.0
62 * @since 0.9.0 Add $defaults parameter.
63 * @param int $id Menu item ID.
64 * @param array $defaults Optional. Default value.
65 * @return array
66 */
67 public static function get( $id, $defaults = array() ) {
68 $defaults = wp_parse_args( $defaults, self::$defaults );
69 $value = get_post_meta( $id, self::KEY, true );
70 $value = wp_parse_args( (array) $value, $defaults );
71
72 if ( ! empty( $value['type'] ) && 'fa' === $value['type'] ) {
73 if ( ! empty( $value['icon'] ) && count( explode( ' ', $value['icon'] ) ) <= 1 ) {
74 $value['icon'] = sprintf( 'fa %s', $value['icon'] );
75 }
76 }
77
78 $font_awesome5 = font_awesome_backward_compatible();
79 $icon = ! empty( $value['icon'] ) ? $value['icon'] : '';
80 $icon = explode( ' ', $icon );
81 $icon = sprintf( '%s-%s', reset( $icon ), end( $icon ) );
82
83 if ( ! empty( $font_awesome5[ $icon ] ) ) {
84 $value['icon'] = $font_awesome5[ $icon ];
85 }
86
87 // Backward-compatibility.
88 if ( empty( $value['icon'] ) &&
89 ! empty( $value['type'] ) &&
90 ! empty( $value[ "{$value['type']}-icon" ] )
91 ) {
92 $value['icon'] = $value[ "{$value['type']}-icon" ];
93 }
94
95 if ( ! empty( $value['width'] ) ) {
96 $value['svg_width'] = $value['width'];
97 }
98 unset( $value['width'] );
99
100 if ( isset( $value['position'] ) &&
101 ! in_array( $value['position'], array( 'before', 'after' ), true )
102 ) {
103 $value['position'] = $defaults['position'];
104 }
105
106 // Backward-compatibility: values removed in favour of align-self support.
107 $supported_vertical_align = array( 'top', 'middle', 'bottom', 'baseline' );
108 if ( isset( $value['vertical_align'] ) &&
109 ! in_array( $value['vertical_align'], $supported_vertical_align, true )
110 ) {
111 $value['vertical_align'] = 'middle';
112 }
113
114 if ( isset( $value['size'] ) && ! isset( $value['font_size'] ) ) {
115 $value['font_size'] = $value['size'];
116 unset( $value['size'] );
117 }
118
119 // The values below will NOT be saved
120 if ( ! empty( $value['icon'] ) &&
121 in_array( $value['type'], array( 'image', 'svg' ), true )
122 ) {
123 $value['url'] = wp_get_attachment_image_url( $value['icon'], 'thumbnail', false );
124 }
125
126 return $value;
127 }
128
129
130 /**
131 * Update menu item metadata
132 *
133 * @since 0.9.0
134 *
135 * @param int $id Menu item ID.
136 * @param mixed $value Metadata value.
137 *
138 * @return void
139 */
140 public static function update( $id, $value ) {
141 /**
142 * Allow plugins/themes to filter the values
143 *
144 * Deprecated.
145 *
146 * @since 0.1.0
147 * @param array $value Metadata value.
148 * @param int $id Menu item ID.
149 */
150 $_value = apply_filters( 'menu_icons_values', $value, $id );
151
152 if ( $_value !== $value && WP_DEBUG ) {
153 _deprecated_function( 'menu_icons_values', '0.8.0', 'menu_icons_item_meta_values' );
154 }
155
156 /**
157 * Allow plugins/themes to filter the values
158 *
159 * @since 0.8.0
160 * @param array $value Metadata value.
161 * @param int $id Menu item ID.
162 */
163 $value = apply_filters( 'menu_icons_item_meta_values', $_value, $id );
164
165 // Don't bother saving if `type` or `icon` is not set.
166 if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
167 $value = false;
168 }
169
170 // Update
171 if ( ! empty( $value ) ) {
172 update_post_meta( $id, self::KEY, $value );
173 } else {
174 delete_post_meta( $id, self::KEY );
175 }
176 }
177 }
178