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