PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.12.11
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.12.11
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 4 years ago front.php 4 years ago media-template.php 4 years ago meta.php 4 years ago picker.php 4 years ago settings.php 4 years ago type-fonts.php 10 years ago type.php 10 years ago
meta.php
170 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_awesome5_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 if ( isset( $value['size'] ) && ! isset( $value['font_size'] ) ) {
107 $value['font_size'] = $value['size'];
108 unset( $value['size'] );
109 }
110
111 // The values below will NOT be saved
112 if ( ! empty( $value['icon'] ) &&
113 in_array( $value['type'], array( 'image', 'svg' ), true )
114 ) {
115 $value['url'] = wp_get_attachment_image_url( $value['icon'], 'thumbnail', false );
116 }
117
118 return $value;
119 }
120
121
122 /**
123 * Update menu item metadata
124 *
125 * @since 0.9.0
126 *
127 * @param int $id Menu item ID.
128 * @param mixed $value Metadata value.
129 *
130 * @return void
131 */
132 public static function update( $id, $value ) {
133 /**
134 * Allow plugins/themes to filter the values
135 *
136 * Deprecated.
137 *
138 * @since 0.1.0
139 * @param array $value Metadata value.
140 * @param int $id Menu item ID.
141 */
142 $_value = apply_filters( 'menu_icons_values', $value, $id );
143
144 if ( $_value !== $value && WP_DEBUG ) {
145 _deprecated_function( 'menu_icons_values', '0.8.0', 'menu_icons_item_meta_values' );
146 }
147
148 /**
149 * Allow plugins/themes to filter the values
150 *
151 * @since 0.8.0
152 * @param array $value Metadata value.
153 * @param int $id Menu item ID.
154 */
155 $value = apply_filters( 'menu_icons_item_meta_values', $_value, $id );
156
157 // Don't bother saving if `type` or `icon` is not set.
158 if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
159 $value = false;
160 }
161
162 // Update
163 if ( ! empty( $value ) ) {
164 update_post_meta( $id, self::KEY, $value );
165 } else {
166 delete_post_meta( $id, self::KEY );
167 }
168 }
169 }
170