PluginProbe ʕ •ᴥ•ʔ
Menu Icons by Themeisle – Add Icons to Navigation Menus / 0.12.3
Menu Icons by Themeisle – Add Icons to Navigation Menus v0.12.3
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 5 years ago front.php 5 years ago media-template.php 5 years ago meta.php 5 years ago picker.php 5 years ago settings.php 5 years ago type-fonts.php 5 years ago type.php 5 years ago
meta.php
155 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 // Backward-compatibility.
73 if ( empty( $value['icon'] ) &&
74 ! empty( $value['type'] ) &&
75 ! empty( $value[ "{$value['type']}-icon" ] )
76 ) {
77 $value['icon'] = $value[ "{$value['type']}-icon" ];
78 }
79
80 if ( ! empty( $value['width'] ) ) {
81 $value['svg_width'] = $value['width'];
82 }
83 unset( $value['width'] );
84
85 if ( isset( $value['position'] ) &&
86 ! in_array( $value['position'], array( 'before', 'after' ), true )
87 ) {
88 $value['position'] = $defaults['position'];
89 }
90
91 if ( isset( $value['size'] ) && ! isset( $value['font_size'] ) ) {
92 $value['font_size'] = $value['size'];
93 unset( $value['size'] );
94 }
95
96 // The values below will NOT be saved
97 if ( ! empty( $value['icon'] ) &&
98 in_array( $value['type'], array( 'image', 'svg' ), true )
99 ) {
100 $value['url'] = wp_get_attachment_image_url( $value['icon'], 'thumbnail', false );
101 }
102
103 return $value;
104 }
105
106
107 /**
108 * Update menu item metadata
109 *
110 * @since 0.9.0
111 *
112 * @param int $id Menu item ID.
113 * @param mixed $value Metadata value.
114 *
115 * @return void
116 */
117 public static function update( $id, $value ) {
118 /**
119 * Allow plugins/themes to filter the values
120 *
121 * Deprecated.
122 *
123 * @since 0.1.0
124 * @param array $value Metadata value.
125 * @param int $id Menu item ID.
126 */
127 $_value = apply_filters( 'menu_icons_values', $value, $id );
128
129 if ( $_value !== $value && WP_DEBUG ) {
130 _deprecated_function( 'menu_icons_values', '0.8.0', 'menu_icons_item_meta_values' );
131 }
132
133 /**
134 * Allow plugins/themes to filter the values
135 *
136 * @since 0.8.0
137 * @param array $value Metadata value.
138 * @param int $id Menu item ID.
139 */
140 $value = apply_filters( 'menu_icons_item_meta_values', $_value, $id );
141
142 // Don't bother saving if `type` or `icon` is not set.
143 if ( empty( $value['type'] ) || empty( $value['icon'] ) ) {
144 $value = false;
145 }
146
147 // Update
148 if ( ! empty( $value ) ) {
149 update_post_meta( $id, self::KEY, $value );
150 } else {
151 delete_post_meta( $id, self::KEY );
152 }
153 }
154 }
155