| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Modules\Display\MegaMenu; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class Megamenu_Options |
| 8 |
{ |
| 9 |
private $dir; |
| 10 |
private $url; |
| 11 |
|
| 12 |
protected $current_menu_id = null; |
| 13 |
|
| 14 |
private static $_instance = null; |
| 15 |
|
| 16 |
public static $key = 'masteraddons_options'; |
| 17 |
public static $jltma_menuitem_settings_key = 'jltma_menu_settings'; |
| 18 |
public static $jltma_menu_settings_key = 'jltma_megamenu_settings'; |
| 19 |
|
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
|
| 24 |
$this->dir = dirname(__FILE__) . '/'; |
| 25 |
$this->url = \MasterAddons\Modules\Display\MegaMenu::plugin_url(); |
| 26 |
|
| 27 |
add_action('admin_footer', [$this, 'options_menu_item']); |
| 28 |
|
| 29 |
add_action('admin_head-nav-menus.php', array($this, 'jltma_nav_meta_box_register'), 9); |
| 30 |
|
| 31 |
add_action('wp_ajax_jltma_save_megamenu_options', [$this, 'jltma_save_megamenu_options']); |
| 32 |
add_action('wp_ajax_jltma_get_megamenu_options', [$this, 'get_jltma_get_megamenu_options']); |
| 33 |
|
| 34 |
// Exclude Mega Menu from Search Engine indexing |
| 35 |
add_action('wp_head', [$this, 'jltma_meta_add_exclude_search']); |
| 36 |
} |
| 37 |
|
| 38 |
function get_jltma_get_megamenu_options() |
| 39 |
{ |
| 40 |
$menu_id = $this->current_menu_id(); |
| 41 |
$data = $this->get_option(self::$jltma_menu_settings_key, []); |
| 42 |
$data = (isset($data['menu_location_' . $menu_id])) ? $data['menu_location_' . $menu_id] : []; |
| 43 |
|
| 44 |
$response = array( |
| 45 |
"menu_id" => $menu_id, |
| 46 |
"is_enabled" => isset($data['is_enabled']) ? $data['is_enabled'] : '0' |
| 47 |
); |
| 48 |
echo json_encode($response); |
| 49 |
die(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
public function jltma_meta_add_exclude_search() |
| 54 |
{ |
| 55 |
if (get_post_type() == 'master_addons_template') { |
| 56 |
echo '<meta name="robots" content="noindex,nofollow" />', "\n"; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public static function get_icons() |
| 61 |
{ |
| 62 |
return include 'icon-list.php'; |
| 63 |
|
| 64 |
// return include \Master_Menu::plugin_url() .'/inc/icon_settings.php'; |
| 65 |
} |
| 66 |
|
| 67 |
function options_menu_item() |
| 68 |
{ |
| 69 |
$screen = get_current_screen(); |
| 70 |
if ($screen->base != 'nav-menus') { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
include 'view/modal-options.php'; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/* |
| 79 |
* Register Page Nav Meta Box settings |
| 80 |
*/ |
| 81 |
public function jltma_nav_meta_box_register() |
| 82 |
{ |
| 83 |
global $pagenow; |
| 84 |
|
| 85 |
if ('nav-menus.php' !== $pagenow) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
add_meta_box( |
| 90 |
'jltma-mega-menu-settings', |
| 91 |
esc_html__('Master Addons Mega Menu', 'master-addons' ), |
| 92 |
[$this, 'jltma_metabox_render'], |
| 93 |
'nav-menus', |
| 94 |
'side', |
| 95 |
'high' |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public function get_settings($menu_id) |
| 101 |
{ |
| 102 |
return get_term_meta($menu_id, $this->meta_key, true); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public function update_settings($menu_id = 0, $settings = array()) |
| 107 |
{ |
| 108 |
update_term_meta($menu_id, $this->meta_key, $settings); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function jltma_metabox_render() |
| 115 |
{ |
| 116 |
$menu_id = $this->current_menu_id(); |
| 117 |
$data = $this->get_option(self::$jltma_menu_settings_key, []); |
| 118 |
$data = (isset($data['menu_location_' . $menu_id])) ? $data['menu_location_' . $menu_id] : []; |
| 119 |
?> |
| 120 |
<div class="master-mega-menu-accordion" style="display: flex; align-items: center; justify-content: space-between;" id="jltma-megamenu-options"> |
| 121 |
<div style="font-weight: 600;"> |
| 122 |
<?php esc_html_e("Enable Mega Menu", "master-addons" ) ?> |
| 123 |
</div> |
| 124 |
<div class="jltma-checkbox-container" style="display: flex; align-items: center;"> |
| 125 |
<input type='checkbox' id="jltma-menu-metabox-input-is-enabled" class='jltma-menu-metabox-input-is-enabled' name='is_enabled' value='1' <?php checked((isset($data['is_enabled']) ? $data['is_enabled'] : ''), '1'); ?> /> |
| 126 |
<label for="jltma-menu-metabox-input-is-enabled"> |
| 127 |
<?php esc_html_e("Enable Mega Menu?", "master-addons" ) ?> |
| 128 |
</label> |
| 129 |
<span class="spinner"></span> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
<div class="jltma-notice p-2 mt-2 mb-1 bg-success text-white" style="display: none;"></div> |
| 133 |
|
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function current_menu_id() |
| 140 |
{ |
| 141 |
|
| 142 |
if (null !== $this->current_menu_id) { |
| 143 |
return $this->current_menu_id; |
| 144 |
} |
| 145 |
|
| 146 |
$nav_menus = wp_get_nav_menus(array('orderby' => 'name')); |
| 147 |
$menu_count = count($nav_menus); |
| 148 |
$nav_menu_selected_id = isset($_REQUEST['menu']) ? (int) $_REQUEST['menu'] : 0; |
| 149 |
$add_new_screen = (isset($_GET['menu']) && 0 == (int) $_GET['menu']) ? true : false; |
| 150 |
|
| 151 |
$this->current_menu_id = $nav_menu_selected_id; |
| 152 |
|
| 153 |
// If we have one theme location, and zero menus, we take them right into editing their first menu |
| 154 |
$page_count = wp_count_posts('page'); |
| 155 |
$one_theme_location_no_menus = (1 == count(get_registered_nav_menus()) && !$add_new_screen && empty($nav_menus) && !empty($page_count->publish)) ? true : false; |
| 156 |
|
| 157 |
// Get recently edited nav menu |
| 158 |
$recently_edited = absint(get_user_option('nav_menu_recently_edited')); |
| 159 |
if (empty($recently_edited) && is_nav_menu($this->current_menu_id)) { |
| 160 |
$recently_edited = $this->current_menu_id; |
| 161 |
} |
| 162 |
|
| 163 |
// Use $recently_edited if none are selected |
| 164 |
if (empty($this->current_menu_id) && !isset($_GET['menu']) && is_nav_menu($recently_edited)) { |
| 165 |
$this->current_menu_id = $recently_edited; |
| 166 |
} |
| 167 |
|
| 168 |
// On deletion of menu, if another menu exists, show it |
| 169 |
if (!$add_new_screen && 0 < $menu_count && isset($_GET['action']) && 'delete' == $_GET['action']) { |
| 170 |
$this->current_menu_id = $nav_menus[0]->term_id; |
| 171 |
} |
| 172 |
|
| 173 |
// Set $this->current_menu_id to 0 if no menus |
| 174 |
if ($one_theme_location_no_menus) { |
| 175 |
$this->current_menu_id = 0; |
| 176 |
} elseif (empty($this->current_menu_id) && !empty($nav_menus) && !$add_new_screen) { |
| 177 |
// if we have no selection yet, and we have menus, set to the first one in the list |
| 178 |
$this->current_menu_id = $nav_menus[0]->term_id; |
| 179 |
} |
| 180 |
|
| 181 |
return $this->current_menu_id; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
public function jltma_save_megamenu_options() |
| 186 |
{ |
| 187 |
if (!current_user_can('manage_options')) { |
| 188 |
wp_send_json_error(array('message' => esc_html__('Invalid access', 'master-addons'))); |
| 189 |
} |
| 190 |
|
| 191 |
if (!check_ajax_referer('jltma_megamenu_nonce', 'nonce', false)) { |
| 192 |
wp_send_json_error(array('message' => esc_html__('Invalid nonce', 'master-addons'))); |
| 193 |
} |
| 194 |
|
| 195 |
$menu_id = $this->current_menu_id(); |
| 196 |
$is_enabled = isset($_REQUEST['is_enabled']) ? sanitize_key($_REQUEST['is_enabled']) : 0; |
| 197 |
|
| 198 |
$data = $this->get_option(self::$jltma_menu_settings_key, []); |
| 199 |
$data['menu_location_' . $menu_id] = [ |
| 200 |
'is_enabled' => $is_enabled, |
| 201 |
]; |
| 202 |
|
| 203 |
$this->save_sanitized(self::$jltma_menu_settings_key, $data); |
| 204 |
|
| 205 |
$response = array( |
| 206 |
"status" => "success", |
| 207 |
"message" => esc_html__("Mega Menu Saved", "master-addons" ) |
| 208 |
); |
| 209 |
echo json_encode($response); |
| 210 |
die(); |
| 211 |
} |
| 212 |
|
| 213 |
public function get_option($key, $default = '') |
| 214 |
{ |
| 215 |
$data_all = get_option(self::$key); |
| 216 |
return (isset($data_all[$key]) && $data_all[$key] != '') ? $data_all[$key] : $default; |
| 217 |
} |
| 218 |
|
| 219 |
public function save_sanitized($key, $value = '', $senitize_func = 'sanitize_text_field') |
| 220 |
{ |
| 221 |
$data_all = get_option(self::$key); |
| 222 |
|
| 223 |
$value = self::sanitize($value, $senitize_func); |
| 224 |
|
| 225 |
$data_all[$key] = $value; |
| 226 |
update_option('masteraddons_options', $data_all); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
public static function sanitize($value, $senitize_func = 'sanitize_text_field') |
| 231 |
{ |
| 232 |
$senitize_func = (in_array($senitize_func, [ |
| 233 |
'sanitize_email', |
| 234 |
'sanitize_file_name', |
| 235 |
'sanitize_hex_color', |
| 236 |
'sanitize_hex_color_no_hash', |
| 237 |
'sanitize_html_class', |
| 238 |
'sanitize_key', |
| 239 |
'sanitize_meta', |
| 240 |
'sanitize_mime_type', |
| 241 |
'sanitize_sql_orderby', |
| 242 |
'sanitize_option', |
| 243 |
'sanitize_text_field', |
| 244 |
'sanitize_title', |
| 245 |
'sanitize_title_for_query', |
| 246 |
'sanitize_title_with_dashes', |
| 247 |
'sanitize_user', |
| 248 |
'esc_url_raw', |
| 249 |
'wp_filter_nohtml_kses', |
| 250 |
])) ? $senitize_func : 'sanitize_text_field'; |
| 251 |
|
| 252 |
if (!is_array($value)) { |
| 253 |
return $senitize_func($value); |
| 254 |
} else { |
| 255 |
return array_map(function ($inner_value) use ($senitize_func) { |
| 256 |
return self::sanitize($inner_value, $senitize_func); |
| 257 |
}, $value); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
public static function get_instance() |
| 262 |
{ |
| 263 |
if (is_null(self::$_instance)) { |
| 264 |
self::$_instance = new self(); |
| 265 |
} |
| 266 |
return self::$_instance; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if (!function_exists('jltma_megamenu_options')) { |
| 273 |
function jltma_megamenu_options() |
| 274 |
{ |
| 275 |
return Megamenu_Options::get_instance(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
jltma_megamenu_options(); |
| 280 |
|