/** * Add hooks for front-end functionalities * * @since 0.9.0 */ public static function init() { $active_types = Menu_Icons_Settings::get('global', 'icon_types'); if (empty($active_types)) { return; } foreach (Menu_Icons::get('types') as $type) { if (in_array($type->id, $active_types)) { self::$icon_types[$type->id] = $type; } } /** * Allow themes/plugins to override the hidden label class * * @since 0.8.0 * @param string $hidden_label_class Hidden label class. * @return string */ self::$hidden_label_class = apply_filters('menu_icons_hidden_label_class', self::$hidden_label_class); /** * Allow themes/plugins to override default inline style * * @since 0.9.0 * @param array $default_style Default inline style. * @return array */ self::$default_style = apply_filters('menu_icons_default_style', self::$default_style); add_action('wp_enqueue_scripts', array(__CLASS__, '_enqueue_styles'), 7); add_filter('wp_nav_menu_args', array(__CLASS__, '_add_menu_item_title_filter')); add_filter('wp_nav_menu', array(__CLASS__, '_remove_menu_item_title_filter')); }
/** * Perform actions after Menu Icons is fully loaded * * @since 0.8.0 * @wp_hook action menu_icons_loaded * @return void */ public function _menu_icons_loaded() { $active_types = Menu_Icons_Settings::get('global', 'icon_types'); if (in_array($this->type, $active_types)) { add_filter('upload_mimes', array($this, '_add_mime_type')); } }
/** * Get setting values and apply sanitation * * @since 0.3.0 * @acess private */ private static function _get() { $settings = get_option('menu-icons', null); if (is_null($settings)) { $settings['global'] = self::$defaults['global']; } /** * Check icon types * * A type could be enabled in the settings but disabled by a filter, * so we need to 'fix' it here. */ if (!empty($settings['global']['icon_types'])) { $active_types = array(); $icon_types = Menu_Icons::get('icon_types'); foreach ((array) $settings['global']['icon_types'] as $index => $id) { if (isset($icon_types[$id])) { $active_types[] = $id; } } if ($settings['global']['icon_types'] !== $active_types) { $settings['global']['icon_types'] = $active_types; update_option('menu-icons', $settings); } } self::$settings = $settings; }
/** * Print fields * * @since 0.1.0 * @access protected * @uses add_action() Calls 'menu_icons_before_fields' hook * @uses add_action() Calls 'menu_icons_after_fields' hook * @wp_hook action menu_item_custom_fields/10/3 * * @param object $item Menu item data object. * @param int $depth Nav menu depth. * @param array $args Menu item args. * @param int $id Nav menu ID. * * @return string Form fields */ public static function _fields($id, $item, $depth, $args) { if (!class_exists('Kucrut_Form_Field')) { require_once Menu_Icons::get('dir') . 'includes/library/form-fields.php'; } $type_ids = array_values(array_filter(array_keys(self::_get_types()))); $input_id = sprintf('menu-icons-%d', $item->ID); $input_name = sprintf('menu-icons[%d]', $item->ID); $current = wp_parse_args(Menu_Icons::get_meta($item->ID), Menu_Icons_Settings::get_menu_settings(Menu_Icons_Settings::get_current_menu_id())); ?> <div class="field-icon description-wide menu-icons-wrap"> <?php /** * Allow plugins/themes to inject HTML before menu icons' fields * * @param object $item Menu item data object. * @param int $depth Nav menu depth. * @param array $args Menu item args. * @param int $id Nav menu ID. * */ do_action('menu_icons_before_fields', $item, $depth, $args, $id); ?> <div class="easy"> <p class="description submitbox"> <label><?php esc_html_e('Icon:', 'menu-icons'); ?> </label> <?php printf('<a id="menu-icons-%1$d-select" class="_select" title="%2$s" data-id="%1$d" data-text="%2$s">%3$s</a>', esc_attr__($item->ID), esc_attr__('Select', 'menu-icons'), self::_get_preview($item->ID, $current)); ?> <?php printf('<a id="menu-icons-%1$s-remove" class="_remove hidden submitdelete" data-id="%1$s">%2$s</a>', esc_attr($item->ID), esc_html__('Remove', 'menu-icons')); ?> </p> </div> <div class="original hidden"> <p class="description"> <label for="<?php echo esc_attr($input_id); ?> -type"><?php esc_html_e('Icon type', 'menu-icons'); ?> </label> <?php printf('<select id="%s-type" name="%s[type]" class="_type hasdep" data-dep-scope="div.menu-icons-wrap" data-dep-children=".field-icon-child" data-key="type">', esc_attr($input_id), esc_attr($input_name)); ?> <?php foreach (self::_get_types() as $id => $props) { ?> <?php printf('<option value="%s"%s>%s</option>', esc_attr($id), selected(isset($current['type']) && $id === $current['type'], true, false), esc_html($props['label'])); ?> <?php } ?> </select> </p> <?php foreach (self::_get_types() as $props) { ?> <?php if (!empty($props['field_cb']) && is_callable($props['field_cb'])) { ?> <?php call_user_func_array($props['field_cb'], array($item->ID, $current)); ?> <?php } ?> <?php } ?> <?php foreach (self::_get_fields($current) as $field) { $field = Kucrut_Form_Field::create($field, array('keys' => array('menu-icons', $item->ID), 'inline_description' => true)); ?> <p class="description field-icon-child" data-dep-on='<?php echo wp_json_encode($type_ids); ?> '> <?php printf('<label for="%s">%s</label>', esc_attr($field->id), esc_html($field->label)); ?> <?php $field->render(); ?> </p> <?php } ?> </div> <?php /** * Allow plugins/themes to inject HTML after menu icons' fields * * @param object $item Menu item data object. * @param int $depth Nav menu depth. * @param array $args Menu item args. * @param int $id Nav menu ID. * */ do_action('menu_icons_after_fields', $item, $depth, $args, $id); ?> </div> <?php }
/** * Initialize * * 1. Get registered types from Icon Picker * 2. Load settings * 3. Load front-end functionalities * * @since 0.1.0 * @since 0.9.0 Hook into `icon_picker_init`. * @wp_hook action icon_picker_init * @link http://codex.wordpress.org/Plugin_API/Action_Reference */ public static function _init() { /** * Allow themes/plugins to add/remove icon types * * @since 0.1.0 * @param array $types Icon types */ self::$data['types'] = apply_filters('menu_icons_types', Icon_Picker_Types_Registry::instance()->types); // Nothing to do if there are no icon types registered. if (empty(self::$data['types'])) { if (WP_DEBUG) { trigger_error(esc_html__('Menu Icons: No registered icon types found.', 'menu-icons')); } return; } // Load settings. require_once self::$data['dir'] . 'includes/settings.php'; Menu_Icons_Settings::init(); // Load front-end functionalities. if (!is_admin()) { require_once self::$data['dir'] . '/includes/front.php'; Menu_Icons_Front_End::init(); } do_action('menu_icons_loaded'); }
/** * Add filter to 'the_title' hook * * We need to filter the menu item title but **not** regular post titles. * Thus, we're adding the filter when `wp_nav_menu()` is called. * * @since 0.1.0 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_nav_menu_args Filter: wp_nav_menu_args/999/2 * @param array $args Not used * * @return array */ public function _add_menu_item_title_filter($args) { $menu_id = Menu_Icons::get_nav_menu_id($args); if (false !== $menu_id) { $this->menu_settings = Menu_Icons_Settings::get_menu_settings($menu_id); add_filter('the_title', array($this, '_filter_menu_item_title'), 999, 2); } return $args; }
/** * Enqueue stylesheets * * @since 0.1.0 * @access protected * @wp_hook action wp_enqueue_scripts/10 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts Action: wp_enqueue_scripts/10 */ public static function _enqueue_styles() { // Enqueue icon types' stylesheets foreach (Menu_Icons_Settings::get('global', 'icon_types') as $id) { if (isset(self::$data['icon_types'][$id])) { self::enqueue_type_stylesheet($id, self::$data['icon_types'][$id]); } } wp_enqueue_style('menu-icons-extra', Menu_Icons::get('url') . 'css/extra' . self::get_script_suffix() . '.css', false, Menu_Icons::VERSION); }
/** * Settings init * * @since 0.3.0 */ public static function init() { /** * Allow themes/plugins to override the default settings * * @since 0.9.0 * @param array $default_settings Default settings. */ self::$defaults = apply_filters('menu_icons_settings_defaults', self::$defaults); self::$settings = get_option('menu-icons', self::$defaults); foreach (self::$settings as $key => &$value) { if ('global' === $key) { // Remove unregistered icon types. $value['icon_types'] = array_values(array_intersect(array_keys(Menu_Icons::get('types')), array_filter((array) $value['icon_types']))); } else { // Backward-compatibility. if (isset($value['width']) && !isset($value['svg_width'])) { $value['svg_width'] = $value['width']; } unset($value['width']); } } unset($value); /** * Allow themes/plugins to override the settings * * @since 0.9.0 * @param array $settings Menu Icons settings. */ self::$settings = apply_filters('menu_icons_settings', self::$settings); if (self::is_menu_icons_disabled_for_menu()) { return; } if (!empty(self::$settings['global']['icon_types'])) { require_once Menu_Icons::get('dir') . 'includes/picker.php'; Menu_Icons_Picker::init(); self::$script_deps[] = 'icon-picker'; } add_action('load-nav-menus.php', array(__CLASS__, '_load_nav_menus'), 1); add_action('wp_ajax_menu_icons_update_settings', array(__CLASS__, '_ajax_menu_icons_update_settings')); }
/** * Print fields * * @since 0.1.0 * @access protected * @uses add_action() Calls 'menu_icons_before_fields' hook * @uses add_action() Calls 'menu_icons_after_fields' hook * @wp_hook action menu_item_custom_fields * * @param object $item Menu item data object. * @param int $depth Nav menu depth. * @param array $args Menu item args. * @param int $id Nav menu ID. * * @return string Form fields */ public static function _fields($id, $item, $depth, $args) { $input_id = sprintf('menu-icons-%d', $item->ID); $input_name = sprintf('menu-icons[%d]', $item->ID); $menu_settings = Menu_Icons_Settings::get_menu_settings(Menu_Icons_Settings::get_current_menu_id()); $meta = Menu_Icons_Meta::get($item->ID, $menu_settings); $fields = self::_get_menu_item_fields($meta); ?> <div class="field-icon description-wide menu-icons-wrap" data-id="<?php echo json_encode($item->ID); ?> "> <?php /** * Allow plugins/themes to inject HTML before menu icons' fields * * @param object $item Menu item data object. * @param int $depth Nav menu depth. * @param array $args Menu item args. * @param int $id Nav menu ID. * */ do_action('menu_icons_before_fields', $item, $depth, $args, $id); ?> <p class="description submitbox"> <label><?php esc_html_e('Icon:', 'menu-icons'); ?> </label> <?php printf('<a class="_select">%s</a>', esc_html__('Select', 'menu-icons')); ?> <?php printf('<a class="_remove submitdelete hidden">%s</a>', esc_html__('Remove', 'menu-icons')); ?> </p> <div class="_settings hidden"> <?php foreach ($fields as $field) { printf('<label>%1$s: <input type="text" name="%2$s" class="_mi-%3$s" value="%4$s" /></label><br />', esc_html($field['label']), esc_attr("{$input_name}[{$field['id']}]"), esc_attr($field['id']), esc_attr($field['value'])); } // The fields below will not be saved. They're only used for the preview. printf('<input type="hidden" class="_mi-url" value="%s" />', esc_attr($meta['url'])); ?> </div> <?php /** * Allow plugins/themes to inject HTML after menu icons' fields * * @param object $item Menu item data object. * @param int $depth Nav menu depth. * @param array $args Menu item args. * @param int $id Nav menu ID. * */ do_action('menu_icons_after_fields', $item, $depth, $args, $id); ?> </div> <?php }