icons
4 years ago
config.php
4 years ago
wf-flyout.css
4 years ago
wf-flyout.js
4 years ago
wf-flyout.php
4 years ago
wf-flyout.php
167 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Universal fly-out menu for WebFactory plugins |
| 4 | * (c) WebFactory Ltd, 2022 |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | if (false == class_exists('wf_flyout')) { |
| 9 | class wf_flyout |
| 10 | { |
| 11 | var $ver = 1.0; |
| 12 | var $plugin_file = ''; |
| 13 | var $plugin_slug = ''; |
| 14 | var $config = array(); |
| 15 | |
| 16 | |
| 17 | function __construct($plugin_file) |
| 18 | { |
| 19 | $this->plugin_file = $plugin_file; |
| 20 | $this->plugin_slug = basename(dirname($plugin_file)); |
| 21 | $this->load_config(); |
| 22 | |
| 23 | if (!is_admin()) { |
| 24 | return; |
| 25 | } else { |
| 26 | add_action('admin_init', array($this, 'init')); |
| 27 | } |
| 28 | } // __construct |
| 29 | |
| 30 | |
| 31 | function load_config() |
| 32 | { |
| 33 | $config = array(); |
| 34 | require_once plugin_dir_path($this->plugin_file) . 'wf-flyout/config.php'; |
| 35 | |
| 36 | $defaults = array( |
| 37 | 'plugin_screen' => '', |
| 38 | 'icon_border' => '#0000ff', |
| 39 | 'icon_right' => '40px', |
| 40 | 'icon_bottom' => '40px', |
| 41 | 'icon_image' => '', |
| 42 | 'icon_padding' => '2px', |
| 43 | 'icon_size' => '55px', |
| 44 | 'menu_accent_color' => '#ca4a1f', |
| 45 | 'custom_css' => '', |
| 46 | 'menu_items' => array(), |
| 47 | ); |
| 48 | |
| 49 | $config = array_merge($defaults, $config); |
| 50 | if (!is_array($config['plugin_screen'])) { |
| 51 | $config['plugin_screen'] = array($config['plugin_screen']); |
| 52 | } |
| 53 | |
| 54 | $this->config = $config; |
| 55 | } // load_config |
| 56 | |
| 57 | |
| 58 | function is_plugin_screen() |
| 59 | { |
| 60 | $screen = get_current_screen(); |
| 61 | |
| 62 | if (in_array($screen->id, $this->config['plugin_screen'])) { |
| 63 | return true; |
| 64 | } else { |
| 65 | return false; |
| 66 | } |
| 67 | } // is_plugin_screen |
| 68 | |
| 69 | |
| 70 | function init() |
| 71 | { |
| 72 | add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 73 | add_action('admin_head', array($this, 'admin_head')); |
| 74 | add_action('admin_footer', array($this, 'admin_footer')); |
| 75 | } // init |
| 76 | |
| 77 | |
| 78 | function admin_enqueue_scripts() |
| 79 | { |
| 80 | if (false === $this->is_plugin_screen()) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | wp_enqueue_style('wf_flyout', plugin_dir_url($this->plugin_file) . 'wf-flyout/wf-flyout.css', array(), $this->ver); |
| 85 | wp_enqueue_script('wf_flyout', plugin_dir_url($this->plugin_file) . 'wf-flyout/wf-flyout.js', array(), $this->ver, true);; |
| 86 | } // admin_enqueue_scripts |
| 87 | |
| 88 | |
| 89 | function admin_head() |
| 90 | { |
| 91 | if (false === $this->is_plugin_screen()) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $out = '<style type="text/css">'; |
| 96 | $out .= '#wf-flyout { |
| 97 | right: ' . sanitize_text_field($this->config['icon_right']) . '; |
| 98 | bottom: ' . sanitize_text_field($this->config['icon_bottom']) . '; |
| 99 | }'; |
| 100 | $out .= '#wf-flyout #wff-image-wrapper { |
| 101 | border: ' . sanitize_text_field($this->config['icon_border']) . '; |
| 102 | }'; |
| 103 | $out .= '#wf-flyout #wff-button img { |
| 104 | padding: ' . sanitize_text_field($this->config['icon_padding']) . '; |
| 105 | width: ' . sanitize_text_field($this->config['icon_size']) . '; |
| 106 | height: ' . sanitize_text_field($this->config['icon_size']) . '; |
| 107 | }'; |
| 108 | $out .= '#wf-flyout .wff-menu-item.accent { |
| 109 | background: ' . sanitize_text_field($this->config['menu_accent_color']) . '; |
| 110 | }'; |
| 111 | $out .= sanitize_text_field($this->config['custom_css']); |
| 112 | $out .= '</style>'; |
| 113 | |
| 114 | mtnc_wp_kses($out); |
| 115 | } // admin_head |
| 116 | |
| 117 | |
| 118 | function admin_footer() |
| 119 | { |
| 120 | if (false === $this->is_plugin_screen()) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $out = ''; |
| 125 | $icons_url = plugin_dir_url($this->plugin_file) . 'wf-flyout/icons/'; |
| 126 | $default_link_item = array('class' => '', 'href' => '#', 'target' => '_blank', 'label' => '', 'icon' => '', 'data' => ''); |
| 127 | |
| 128 | $out .= '<div id="wff-overlay"></div>'; |
| 129 | |
| 130 | $out .= '<div id="wf-flyout">'; |
| 131 | |
| 132 | $out .= '<a href="#" id="wff-button">'; |
| 133 | $out .= '<span class="wff-label">Open Quick Links</span>'; |
| 134 | $out .= '<span id="wff-image-wrapper">'; |
| 135 | $out .= '<img src="' . esc_url($icons_url . $this->config['icon_image']) . '" alt="Open Quick Links" title="Open Quick Links">'; |
| 136 | $out .= '</span>'; |
| 137 | $out .= '</a>'; |
| 138 | |
| 139 | $out .= '<div id="wff-menu">'; |
| 140 | $i = 0; |
| 141 | foreach (array_reverse($this->config['menu_items']) as $item) { |
| 142 | $i++; |
| 143 | $item = array_merge($default_link_item, $item); |
| 144 | |
| 145 | if (!empty($item['icon']) && substr($item['icon'], 0, 9) != 'dashicons') { |
| 146 | $item['class'] .= ' wff-custom-icon'; |
| 147 | $item['class'] = trim($item['class']); |
| 148 | } |
| 149 | |
| 150 | $out .= '<a ' . $item['data'] . ' href="' . esc_url($item['href']) . '" class="wff-menu-item wff-menu-item-' . $i . ' ' . esc_attr($item['class']) . '" target="_blank">'; |
| 151 | $out .= '<span class="wff-label visible">' . esc_html($item['label']) . '</span>'; |
| 152 | if (substr($item['icon'], 0, 9) == 'dashicons') { |
| 153 | $out .= '<span class="dashicons ' . sanitize_text_field($item['icon']) . '"></span>'; |
| 154 | } elseif (!empty($item['icon'])) { |
| 155 | $out .= '<span class="wff-icon"><img src="' . esc_url($icons_url . $item['icon']) . '"></span>'; |
| 156 | } |
| 157 | $out .= '</a>'; |
| 158 | } // foreach |
| 159 | $out .= '</div>'; // #wff-menu |
| 160 | |
| 161 | $out .= '</div>'; // #wf-flyout |
| 162 | |
| 163 | mtnc_wp_kses($out); |
| 164 | } // admin_footer |
| 165 | } // wf_flyout |
| 166 | } // if class exists |
| 167 |