-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmeta.php
More file actions
177 lines (151 loc) · 4.3 KB
/
meta.php
File metadata and controls
177 lines (151 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Menu item metadata
*
* @package Menu_Icons
* @author Dzikri Aziz <kvcrvt@gmail.com>
*/
final class Menu_Icons_Meta {
const KEY = 'menu-icons';
/**
* Default meta value
*
* @since 0.9.0
* @access protected
* @var array
*/
protected static $defaults = array(
'type' => '',
'icon' => '',
'url' => '',
);
/**
* Initialize metadata functionalities
*
* @since 0.9.0
*/
public static function init() {
add_filter( 'is_protected_meta', array( __CLASS__, '_protect_meta_key' ), 10, 3 );
}
/**
* Protect meta key
*
* This prevents our meta key from showing up on Custom Fields meta box.
*
* @since 0.3.0
* @wp_hook filter is_protected_meta
* @param bool $protected Protection status.
* @param string $meta_key Meta key.
* @param string $meta_type Meta type.
* @return bool Protection status.
*/
public static function _protect_meta_key( $protected, $meta_key, $meta_type ) {
if ( self::KEY === $meta_key ) {
$protected = true;
}
return $protected;
}
/**
* Get menu item meta value
*
* @since 0.3.0
* @since 0.9.0 Add $defaults parameter.
* @param int $id Menu item ID.
* @param array $defaults Optional. Default value.
* @return array
*/
public static function get( $id, $defaults = array() ) {
$defaults = wp_parse_args( $defaults, self::$defaults );
$value = get_post_meta( $id, self::KEY, true );
$value = wp_parse_args( (array) $value, $defaults );
if ( ! empty( $value['type'] ) && 'fa' === $value['type'] ) {
if ( ! empty( $value['icon'] ) && count( explode( ' ', $value['icon'] ) ) <= 1 ) {
$value['icon'] = sprintf( 'fa %s', $value['icon'] );
}
}
$font_awesome5 = font_awesome_backward_compatible();
$icon = ! empty( $value['icon'] ) ? $value['icon'] : '';
$icon = explode( ' ', $icon );
$icon = sprintf( '%s-%s', reset( $icon ), end( $icon ) );
if ( ! empty( $font_awesome5[ $icon ] ) ) {
$value['icon'] = $font_awesome5[ $icon ];
}
// Backward-compatibility.
if ( empty( $value['icon'] ) &&
! empty( $value['type'] ) &&
! empty( $value[ "{$value['type']}-icon" ] )
) {
$value['icon'] = $value[ "{$value['type']}-icon" ];
}
if ( ! empty( $value['width'] ) ) {
$value['svg_width'] = $value['width'];
}
unset( $value['width'] );
if ( isset( $value['position'] ) &&
! in_array( $value['position'], array( 'before', 'after' ), true )
) {
$value['position'] = $defaults['position'];
}
// Backward-compatibility: values removed in favour of align-self support.
$supported_vertical_align = array( 'top', 'middle', 'bottom', 'baseline' );
if ( isset( $value['vertical_align'] ) &&
! in_array( $value['vertical_align'], $supported_vertical_align, true )
) {
$value['vertical_align'] = 'middle';
}
if ( isset( $value['size'] ) && ! isset( $value['font_size'] ) ) {
$value['font_size'] = $value['size'];
unset( $value['size'] );
}
// The values below will NOT be saved
if ( ! empty( $value['icon'] ) &&
in_array( $value['type'], array( 'image', 'svg' ), true )
) {
$value['url'] = wp_get_attachment_image_url( $value['icon'], 'thumbnail', false );
}
return $value;
}
/**
* Update menu item metadata
*
* @since 0.9.0
*
* @param int $id Menu item ID.
* @param mixed $value Metadata value.
*
* @return void
*/
public static function update( $id, $value ) {
/**
* Allow plugins/themes to filter the values
*
* Deprecated.
*
* @since 0.1.0
* @param array $value Metadata value.
* @param int $id Menu item ID.
*/
$_value = apply_filters( 'menu_icons_values', $value, $id );
if ( $_value !== $value && WP_DEBUG ) {
_deprecated_function( 'menu_icons_values', '0.8.0', 'menu_icons_item_meta_values' );
}
/**
* Allow plugins/themes to filter the values
*
* @since 0.8.0
* @param array $value Metadata value.
* @param int $id Menu item ID.
*/
$value = apply_filters( 'menu_icons_item_meta_values', $_value, $id );
// Don't bother saving if `type` or `icon` is not set.
if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
$value = false;
}
// Update
if ( ! empty( $value ) ) {
update_post_meta( $id, self::KEY, $value );
} else {
delete_post_meta( $id, self::KEY );
}
}
}