assets
6 years ago
views
6 years ago
api.php
6 years ago
init.php
6 years ago
options.php
6 years ago
walker-nav-menu.php
6 years ago
options.php
112 lines
| 1 | <?php |
| 2 | namespace ElementsKit\Modules\Megamenu; |
| 3 | use ElementsKit\Libs\Framework\Attr; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | class Options{ |
| 8 | private $dir; |
| 9 | private $url; |
| 10 | |
| 11 | protected $current_menu_id = null; |
| 12 | |
| 13 | public function __construct() { |
| 14 | // get current directory path |
| 15 | $this->dir = dirname(__FILE__) . '/'; |
| 16 | |
| 17 | // get current module's url |
| 18 | $this->url = \ElementsKit::plugin_url() . 'modules/megamenu/'; |
| 19 | |
| 20 | add_action( 'admin_footer', [ $this, 'options_menu_item'] ); |
| 21 | add_action( 'admin_footer', [ $this, 'options_megamenu'] ); |
| 22 | add_action( 'admin_head', [ $this, 'save_megamenu_options'] ); |
| 23 | } |
| 24 | |
| 25 | public function current_menu_id() { |
| 26 | |
| 27 | if ( null !== $this->current_menu_id ) { |
| 28 | return $this->current_menu_id; |
| 29 | } |
| 30 | |
| 31 | $nav_menus = wp_get_nav_menus( array('orderby' => 'name') ); |
| 32 | $menu_count = count( $nav_menus ); |
| 33 | $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) sanitize_key($_REQUEST['menu']) : 0; |
| 34 | $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false; |
| 35 | |
| 36 | $this->current_menu_id = $nav_menu_selected_id; |
| 37 | |
| 38 | // If we have one theme location, and zero menus, we take them right into editing their first menu |
| 39 | $page_count = wp_count_posts( 'page' ); |
| 40 | $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false; |
| 41 | |
| 42 | // Get recently edited nav menu |
| 43 | $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) ); |
| 44 | if ( empty( $recently_edited ) && is_nav_menu( $this->current_menu_id ) ) { |
| 45 | $recently_edited = $this->current_menu_id; |
| 46 | } |
| 47 | |
| 48 | // Use $recently_edited if none are selected |
| 49 | if ( empty( $this->current_menu_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) ) { |
| 50 | $this->current_menu_id = $recently_edited; |
| 51 | } |
| 52 | |
| 53 | // On deletion of menu, if another menu exists, show it |
| 54 | if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] ) { |
| 55 | $this->current_menu_id = $nav_menus[0]->term_id; |
| 56 | } |
| 57 | |
| 58 | // Set $this->current_menu_id to 0 if no menus |
| 59 | if ( $one_theme_location_no_menus ) { |
| 60 | $this->current_menu_id = 0; |
| 61 | } elseif ( empty( $this->current_menu_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) { |
| 62 | // if we have no selection yet, and we have menus, set to the first one in the list |
| 63 | $this->current_menu_id = $nav_menus[0]->term_id; |
| 64 | } |
| 65 | |
| 66 | return $this->current_menu_id; |
| 67 | |
| 68 | } |
| 69 | |
| 70 | public static function get_icons() { |
| 71 | return include \ElementsKit::module_dir() . 'controls/icon-list.php'; |
| 72 | } |
| 73 | |
| 74 | function options_menu_item() { |
| 75 | $screen = get_current_screen(); |
| 76 | if($screen->base != 'nav-menus'){ |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | include 'views/options-menu-item.php'; |
| 81 | } |
| 82 | |
| 83 | function options_megamenu() { |
| 84 | $screen = get_current_screen(); |
| 85 | if($screen->base != 'nav-menus'){ |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | $menu_id = $this->current_menu_id(); |
| 90 | $data = Attr::instance()->utils->get_option(Init::$megamenu_settings_key, []); |
| 91 | $data = (isset($data['menu_location_' . $menu_id])) ? $data['menu_location_' . $menu_id] : []; |
| 92 | |
| 93 | include 'views/options-megamenu.php'; |
| 94 | } |
| 95 | |
| 96 | public function save_megamenu_options(){ |
| 97 | $screen = get_current_screen(); |
| 98 | if($screen->base != 'nav-menus' || !isset($_POST['update-nav-menu-nonce']) ){ |
| 99 | return; |
| 100 | } |
| 101 | $menu_id = isset($_POST['menu']) ? sanitize_key($_POST['menu']) : 0; |
| 102 | $is_enabled = isset($_POST['is_enabled']) ? sanitize_key($_POST['is_enabled']) : 0; |
| 103 | |
| 104 | $data = Attr::instance()->utils->get_option(Init::$megamenu_settings_key, []); |
| 105 | $data['menu_location_' . $menu_id] = [ |
| 106 | 'is_enabled' => $is_enabled, |
| 107 | ]; |
| 108 | |
| 109 | Attr::instance()->utils->save_sanitized(Init::$megamenu_settings_key, $data); |
| 110 | |
| 111 | } |
| 112 | } |