/** * Custom walker * * @since 0.3.0 * @access protected * @wp_hook filter wp_edit_nav_menu_walker */ public static function _filter_wp_edit_nav_menu_walker($walker) { // Load menu item custom fields plugin if (!class_exists('Menu_Item_Custom_Fields_Walker')) { require_once Menu_Icons::get('dir') . 'includes/library/walker-nav-menu-edit.php'; } $walker = 'Menu_Item_Custom_Fields_Walker'; return $walker; }
/** * Inline style for icon size, etc * * @since 0.4.0 * @param array $values Menu item metadata value * @return string */ public static function get_style($values) { if (class_exists('Menu_Icons', false)) { return; } $style_d = Menu_Icons::get('default_style'); $style_a = array(); $style_s = ''; if (!empty($values['vertical_align'])) { $style_a['vertical-align'] = $values['vertical_align']; } $style_a = array_diff_assoc($style_a, $style_d); if (!empty($style_a)) { foreach ($style_a as $key => $value) { $style_s .= sprintf('%s:%s;', esc_attr($key), esc_attr($value)); } } return $style_s; }
/** * Load plugin * * 1. Load translation * 2. Set plugin data (directory and URL paths) * 3. Attach plugin initialization at icon_picker_init hook * * @since 0.1.0 * @wp_hook action plugins_loaded * @link http://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded */ public static function _load() { load_plugin_textdomain('menu-icons', false, dirname(plugin_basename(__FILE__)) . '/languages/'); self::$data = array('dir' => plugin_dir_path(__FILE__), 'url' => plugin_dir_url(__FILE__), 'types' => array()); // Load Icon Picker. if (!class_exists('Icon_Picker')) { $ip_file = self::$data['dir'] . 'includes/library/icon-picker/icon-picker.php'; if (file_exists($ip_file)) { require_once $ip_file; } else { add_action('admin_notices', array(__CLASS__, '_notice_missing_icon_picker')); return; } } Icon_Picker::instance(); require_once self::$data['dir'] . 'includes/library/compat.php'; require_once self::$data['dir'] . 'includes/library/functions.php'; require_once self::$data['dir'] . 'includes/meta.php'; Menu_Icons_Meta::init(); add_action('icon_picker_init', array(__CLASS__, '_init'), 9); }
/** * 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 }
/** * parse li, copy from system navmenu by wordpress at /wp-includes/nav-menu-template.php (since wordpress v) * @param string $output * @param object $item * @param int $depth * @param array $args * @param int $id */ protected function _start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { $args = (object) $args; //cast to object //valid if (!isset($args->before)) { $args->before = ''; } if (!isset($args->submenu_before)) { $args->submenu_before = ''; } if (!isset($args->after)) { $args->after = ''; } if (!isset($args->submenu_after)) { $args->submenu_after = ''; } if (!isset($args->link_before)) { $args->link_before = ''; } if (!isset($args->submenu_link_before)) { $args->submenu_link_before = ''; } if (!isset($args->link_after)) { $args->link_after = ''; } if (!isset($args->submenu_link_after)) { $args->submenu_link_after = ''; } //whether current menu item has sub menu $item_has_sub = $this->item_has_sub($item); //menu item classes $classes = empty($item->classes) ? array() : (array) $item->classes; if (!empty($args->ex_separator) && isset($args->show_items_separator)) { $output .= $args->ex_separator; } $data['args'] = clone $args; $data['indent'] = $indent = $depth ? str_repeat("\t", $depth) : ''; $data['item_has_sub'] = $item_has_sub; //menu item classes $classes[] = 'nav' . ++$this->counter; $classes[] = 'menu-item-' . $item->ID; if ($item_has_sub && !empty($args->menu_item_class_has_submenu)) { //menu item class has submenu $classes[] = $args->menu_item_class_has_submenu; } if ($depth > 0 && !empty($args->menu_item_class_submenu)) { //menu item class in submenu $classes[] = $args->menu_item_class_submenu; } $data['classes'] = $class_names = join(' ', apply_filters('nav_menu_css_class', $this->valid_classes_attr(array_filter($classes)), $item, $args, $depth)); //$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth); $data['id'] = $id = $id ? ' id="' . esc_attr($id) . '"' : ''; //custom fields $custom_item = get_post_custom($item->ID); $data['field-01'] = isset($custom_item['menu-item-field-01'][0]) ? $custom_item['menu-item-field-01'][0] : ''; $data['field-02'] = isset($custom_item['menu-item-field-02'][0]) ? $custom_item['menu-item-field-02'][0] : ''; /** * menu item image */ if (isset($args->show_icon) && $args->show_icon) { $image_url = get_post_meta($item->ID, 'menu-item-hw_icon', true); if ($image_url) { $image_img = '<img src="' . $image_url . '" class="hw-menu-item-icon"/>'; } else { $image_img = ''; } } #_print(get_post_custom($item->ID)); /** * menu font icon */ #$fonticon_meta = get_post_meta($item->ID, 'menu-icons', true); //Menu_Icons::get_meta( $item->ID ); $fonticon_meta = Menu_Icons::get_meta($item->ID); $data['fonticon_setting'] = $fonticon_meta; //font icon setting $font_icon = HW_Menu_Icons::render_icon($fonticon_meta); $data['image_url'] = isset($image_url) ? $image_url : ''; $data['image_img'] = isset($image_img) ? $image_img : ''; $data['font_icon'] = isset($font_icon) ? $font_icon : ''; /** * render nav item */ //clear submenu before/after for non-ancestor item if (!$item_has_sub) { $data['args']->submenu_before = ''; $data['args']->submenu_after = ''; $data['args']->submenu_link_before = ''; $data['args']->submenu_link_after = ''; } //anchor attributes $atts = array(); $atts['title'] = !empty($item->attr_title) ? $item->attr_title : ''; $atts['target'] = !empty($item->target) ? $item->target : ''; $atts['rel'] = !empty($item->xfn) ? $item->xfn : ''; $atts['href'] = !empty($item->url) ? $item->url : ''; $atts['class'] = array(); //anchor classes if ($item_has_sub && !empty($args->anchor_class_has_submenu)) { //anchor class has submenu $atts['class'][] = $args->anchor_class_has_submenu; } if ($depth > 0 && !empty($args->anchor_class_submenu)) { //anchor class in submenu $atts['class'][] = $args->anchor_class_submenu; } $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth, $this); //validation $atts['class'] = $this->valid_classes_attr(array_filter($atts['class'])); $atts['class'] = join(' ', $atts['class']); $attributes = ''; foreach ($atts as $attr => $value) { if (!empty($value)) { $value = 'href' === $attr ? esc_url($value) : esc_attr($value); $attributes .= ' ' . $attr . '="' . $value . '"'; } } //addition attribute build for item link if ($depth == 0 && !empty($args->anchor_attrs)) { $attributes .= " {$args->anchor_attrs}"; } elseif ($depth != 0 && !empty($args->anchor_attrs_submenu)) { $attributes .= " {$args->anchor_attrs_submenu}"; } //attributes item link for submenu if ($item_has_sub && !empty($args->anchor_attrs_has_submenu)) { $attributes .= " {$args->anchor_attrs_has_submenu}"; } $data['attributes'] = $attributes; $data['title'] = apply_filters('the_title', $item->title, $item->ID); $item_output = $args->before; $item_output .= '<a' . $attributes . '>'; //This filter is documented in wp-includes/post-template.php $item_output .= $args->link_before . $data['title'] . $args->link_after; $item_output .= '</a>'; $item_output .= $args->after; if (isset($args->twig) && HW_NAVMENU::twig_asset_exists('start_el.twig', $args->twig)) { $tpl = $args->twig->loadTemplate('start_el.twig'); $output .= $tpl->render($data); } else { $classes = $class_names ? 'class="' . $class_names . '"' : ""; //class attr $output .= $indent . '<li' . $id . $classes . '>'; $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, (array) $args); } }
/** * Enqueue stylesheets * * @since 0.1.0 * @wp_hook action wp_enqueue_scripts * @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts */ public static function _enqueue_styles() { foreach (self::$icon_types as $type) { if (wp_style_is($type->stylesheet_id, 'registered')) { wp_enqueue_style($type->stylesheet_id); } } /** * Allow plugins/themes to override the extra stylesheet location * * @since 0.9.0 * @param string $extra_stylesheet_uri Extra stylesheet URI. */ $extra_stylesheet_uri = apply_filters('menu_icons_extra_stylesheet_uri', sprintf('%scss/extra%s.css', Menu_Icons::get('url'), kucrut_get_script_suffix())); wp_enqueue_style('menu-icons-extra', $extra_stylesheet_uri, false, Menu_Icons::VERSION); }
/** * Class constructor * * We need to override the parent's to set our stylesheet URL * * @since 0.1.0 * @param array $types Icon Types * @return array */ public function __construct() { $this->stylesheet = sprintf('%scss/%s%s.css', Menu_Icons::get('url'), $this->type, Menu_Icons::get_script_suffix()); parent::__construct(); }
/** * Inline style for icon size, etc * * @since 0.2.0 * @param array $values Menu item metadata value * @return string */ protected function get_style($values) { $style_d = Menu_Icons::get('default_style'); $style_a = array(); $style_s = ''; if (!empty($values['font_size'])) { $style_a['font-size'] = sprintf('%sem', $values['font_size']); } if (!empty($values['vertical_align'])) { $style_a['vertical-align'] = $values['vertical_align']; } $style_a = array_diff_assoc($style_a, $style_d); if (!empty($style_a)) { foreach ($style_a as $key => $value) { $style_s .= sprintf('%s:%s;', esc_attr($key), esc_attr($value)); } $style_s = sprintf(' style="%s"', $style_s); } return $style_s; }
/** * Class constructor * * We need to override the parent's to set our stylesheet URL * * @since 0.1.0 * @param array $types Icon Types * @return array */ public function __construct($pack) { $this->messages = array('no_config' => __('Menu Icons: %1$s was not found in %2$s.', 'menu-icons'), 'invalid' => __('Menu Icons: %1$s is not set or invalid in %2$s.', 'menu-icons'), 'duplicate' => __('Menu Icons: %1$s is already registered. Please check your font pack config file: %2$s.', 'menu-icons')); $this->dir = sprintf('%s/%s', Menu_Icons::get('fontpacks_dir_path'), $pack); $this->url = sprintf('%s/%s', Menu_Icons::get('fontpacks_dir_url'), $pack); if (!is_readable($this->dir . '/config.json')) { trigger_error(sprintf(esc_html($this->messages['no_config']), '<code><em>config.json</em></code>', sprintf('<code>%s</code>', esc_html($this->dir)))); return; } $this->read_config(); $this->validate(); if (false === $this->is_config_valid) { return; } $this->set_properties(); parent::__construct(); }
/** * do while create the class instance * @param WP_Widget $widget * @return mixed|void */ public function init(WP_Widget $widget) { if (!class_exists('Kucrut_Form_Field') && class_exists('Menu_Icons', false)) { require_once Menu_Icons::get('dir') . 'includes/library/form-fields.php'; } add_action('wp_loaded', array(__CLASS__, '_init'), 9); }
/** * Filter menu item titles * * @since 0.1.0 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/the_title Filter: the_title/999/2 * * @param string $title Menu item title * @param int $id Menu item ID * * @return string */ public function _filter_menu_item_title($title, $id) { $values = wp_parse_args(Menu_Icons::get_meta($id), $this->menu_settings); if (empty($values['type'])) { return $title; } if ($values['type'] !== $this->type) { return $title; } if (empty($values[$this->key])) { return $title; } $title = $this->add_icon($title, $values); return $title; }
/** * Filter menu item titles * * @since 0.1.0 * @link http://codex.wordpress.org/Plugin_API/Action_Reference/the_title Filter: the_title/999/2 * * @param string $title Menu item title * @param int $id Menu item ID * * @return string */ public function _filter_menu_item_title($title, $id) { $values = wp_parse_args(Menu_Icons::get_meta($id), $this->menu_settings); if (empty($values['type'])) { return $title; } if ($values['type'] !== $this->type) { return $title; } if (empty($values[$this->key])) { return $title; } $title_with_icon = $this->add_icon($title, $values); /** * Allow plugins/themes to override menu item markup * * @since 0.8.0 * * @param string $title_with_icon Menu item markup after the icon is added. * @param integer $id Menu item ID. * @param array $values Menu item metadata values. * @param string $title Original menu item title. * * @return string */ $title_with_icon = apply_filters('menu_icons_item_title', $title_with_icon, $id, $values, $title); return $title_with_icon; }
/** * 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); }
/** * Enqueue scripts & styles for admin page * * @since 0.3.0 * @wp_hook action admin_enqueue_scripts */ public static function _enqueue_assets() { $suffix = Menu_Icons::get_script_suffix(); wp_enqueue_style('menu-icons', Menu_Icons::get('url') . 'css/admin' . $suffix . '.css', false, Menu_Icons::VERSION); wp_register_script('kucrut-jquery-input-dependencies', Menu_Icons::get('url') . 'js/input-dependencies' . $suffix . '.js', array('jquery'), '0.1.0', true); if (!empty(self::$settings['global']['icon_types'])) { wp_enqueue_media(); } wp_enqueue_script('menu-icons', Menu_Icons::get('url') . 'js/admin' . $suffix . '.js', array('kucrut-jquery-input-dependencies'), Menu_Icons::VERSION, true); }
/** * Enqueue scripts & styles for Appearance > Menus page * * @since 0.3.0 * @wp_hook action admin_enqueue_scripts */ public static function _enqueue_assets() { $url = Menu_Icons::get('url'); $suffix = kucrut_get_script_suffix(); wp_enqueue_style('menu-icons', "{$url}css/admin{$suffix}.css", false, Menu_Icons::VERSION); wp_enqueue_script('menu-icons', "{$url}js/admin{$suffix}.js", self::$script_deps, Menu_Icons::VERSION, true); /** * Allow plugins/themes to filter the settings' JS data * * @since 0.9.0 * @param array $js_data JS Data. */ $js_data = apply_filters('menu_icons_settings_js_data', array('text' => array('title' => __('Select Icon', 'menu-icons'), 'select' => __('Select', 'menu-icons'), 'remove' => __('Remove', 'menu-icons'), 'change' => __('Change', 'menu-icons'), 'all' => __('All', 'menu-icons'), 'preview' => __('Preview', 'menu-icons'), 'settingsInfo' => sprintf(esc_html__("Please note that the actual look of the icons on the front-end will also be affected by your active theme's style. You can use %s if you need to override it.", 'menu-icons'), '<a target="_blank" href="http://wordpress.org/plugins/simple-custom-css/">Simple Custom CSS</a>')), 'settingsFields' => self::get_settings_fields(), 'activeTypes' => self::get('global', 'icon_types'), 'ajaxUrls' => array('update' => add_query_arg('action', 'menu_icons_update_settings', admin_url('/admin-ajax.php'))), 'menuSettings' => self::get_menu_settings(self::get_current_menu_id()))); wp_localize_script('menu-icons', 'menuIcons', $js_data); }