| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Modules\MenuDuplicator; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Utils; |
| 6 |
|
| 7 |
// no direct access allowed |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* @package WP Adminify |
| 14 |
* Module: Menu Duplicator |
| 15 |
* |
| 16 |
* @author Jewel Theme <support@jeweltheme.com> |
| 17 |
*/ |
| 18 |
|
| 19 |
class MenuDuplicator |
| 20 |
{ |
| 21 |
|
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
add_action('admin_footer', [$this, 'jltwp_adminify_add_duplicate_button'], 11); |
| 25 |
add_action('admin_footer', [$this, 'jltwp_adminify_creating_menu_duplicate'], 6); |
| 26 |
|
| 27 |
// Duplicate Nav Menu Item |
| 28 |
add_action('admin_footer', [$this, 'jltwp_adminify_duplicate_nav_menu_item'], 12); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Duplicate Menu Button |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function jltwp_adminify_add_duplicate_button() |
| 37 |
{ |
| 38 |
$current_screen = get_current_screen(); |
| 39 |
$current_menu = get_user_option('nav_menu_recently_edited'); |
| 40 |
if ($current_screen->id == 'nav-menus' && $_GET['menu'] != '0') { |
| 41 |
$return = ''; |
| 42 |
$return .= '<div class="AdminifyDuplicateMenuSpinenr spinner"></div><a class="AdminifyDuplicateMenuClick button button-primary button-large menu-save" href="?adminify-menu-duplicate=' . $current_menu . '">' . __('Duplicate Menu', 'adminify') . '</a>'; |
| 43 |
?> |
| 44 |
<script type="text/javascript"> |
| 45 |
var update_menu_form = jQuery('#update-nav-menu'); |
| 46 |
update_menu_form.find('.publishing-action').append('<?php echo Utils::wp_kses_custom($return); ?>'); |
| 47 |
jQuery('.AdminifyDuplicateMenuClick').click(function() { |
| 48 |
jQuery('.AdminifyDuplicateMenuSpinenr').addClass('is-active').show(); |
| 49 |
}); |
| 50 |
</script> |
| 51 |
|
| 52 |
<?php |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Creating Menu Duplication |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function jltwp_adminify_creating_menu_duplicate() |
| 62 |
{ |
| 63 |
$current_screen = get_current_screen(); |
| 64 |
if ($current_screen->id == 'nav-menus') { |
| 65 |
if (isset($_GET['adminify-menu-duplicate'])) { |
| 66 |
$id = intval(wp_unslash($_GET['adminify-menu-duplicate'])); |
| 67 |
$source = wp_get_nav_menu_object($id); |
| 68 |
$duplicate = $this->render_menu_duplicate(sanitize_text_field(wp_unslash($_GET['adminify-menu-duplicate'])), $source->name . ' ' . __('(Copy)', 'adminify')); |
| 69 |
if ($duplicate) { |
| 70 |
?> |
| 71 |
<script type="text/javascript"> |
| 72 |
window.location.replace("<?php echo esc_url(admin_url('nav-menus.php?action=edit&menu=' . $duplicate)); ?>"); |
| 73 |
</script> |
| 74 |
<?php |
| 75 |
} else { |
| 76 |
?> |
| 77 |
<script type="text/javascript"> |
| 78 |
window.location.replace("<?php echo esc_url(admin_url('nav-menus.php')); ?>"); |
| 79 |
</script> |
| 80 |
<?php |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Render Menu Duplicate |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function render_menu_duplicate($id = null, $name = null) |
| 93 |
{ |
| 94 |
|
| 95 |
// Sanity check |
| 96 |
if (empty($id) || empty($name)) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$id = intval($id); |
| 101 |
$name = sanitize_text_field($name); |
| 102 |
$source = wp_get_nav_menu_object($id); |
| 103 |
$source_items = wp_get_nav_menu_items($id); |
| 104 |
|
| 105 |
$menu_exists = wp_get_nav_menu_object($name); |
| 106 |
|
| 107 |
if (!$menu_exists) { |
| 108 |
$new_id = wp_create_nav_menu($name); |
| 109 |
} else { |
| 110 |
return $new_id = $this->render_menu_duplicate($id, $name . ' ' . __('(Copy)', 'adminify')); |
| 111 |
} |
| 112 |
|
| 113 |
if (!$new_id || is_array($new_id)) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Key is the original db ID, val is the new |
| 118 |
$rel = []; |
| 119 |
|
| 120 |
$i = 1; |
| 121 |
foreach ($source_items as $menu_item) { |
| 122 |
$args = [ |
| 123 |
'menu-item-db-id' => $menu_item->db_id, |
| 124 |
'menu-item-object-id' => $menu_item->object_id, |
| 125 |
'menu-item-object' => $menu_item->object, |
| 126 |
'menu-item-position' => $i, |
| 127 |
'menu-item-type' => $menu_item->type, |
| 128 |
'menu-item-title' => $menu_item->title, |
| 129 |
'menu-item-url' => $menu_item->url, |
| 130 |
'menu-item-description' => $menu_item->description, |
| 131 |
'menu-item-attr-title' => $menu_item->attr_title, |
| 132 |
'menu-item-target' => $menu_item->target, |
| 133 |
'menu-item-classes' => implode(' ', $menu_item->classes), |
| 134 |
'menu-item-xfn' => $menu_item->xfn, |
| 135 |
'menu-item-status' => $menu_item->post_status, |
| 136 |
]; |
| 137 |
|
| 138 |
$parent_id = wp_update_nav_menu_item($new_id, 0, $args); |
| 139 |
|
| 140 |
$rel[$menu_item->db_id] = $parent_id; |
| 141 |
|
| 142 |
// did it have a parent? if so, we need to update with the NEW ID |
| 143 |
if ($menu_item->menu_item_parent) { |
| 144 |
$args['menu-item-parent-id'] = $rel[$menu_item->menu_item_parent]; |
| 145 |
$parent_id = wp_update_nav_menu_item($new_id, $parent_id, $args); |
| 146 |
} |
| 147 |
|
| 148 |
// allow developers to run any custom functionality they'd like |
| 149 |
do_action('duplicate_menu_item', $menu_item, $args); |
| 150 |
|
| 151 |
$i++; |
| 152 |
} |
| 153 |
|
| 154 |
return $new_id; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Duplicate Nav Menu Item |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function jltwp_adminify_duplicate_nav_menu_item() |
| 164 |
{ |
| 165 |
global $pagenow; |
| 166 |
|
| 167 |
if ($pagenow != 'nav-menus.php' || !current_user_can('edit_theme_options')) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
?> |
| 172 |
<script type="text/javascript"> |
| 173 |
(function($) { |
| 174 |
|
| 175 |
"use strict"; |
| 176 |
|
| 177 |
function adminifyGetMenuID(el) { |
| 178 |
return el.attr('id').replace('menu-item-', '') * 1; |
| 179 |
} |
| 180 |
|
| 181 |
function adminifyGetMenu(el) { |
| 182 |
return el.get(0).className.split('menu-item-depth-')[1].split(' ')[0]; |
| 183 |
} |
| 184 |
|
| 185 |
$(document).on('mouseover', 'li.menu-item:not(.adminify-duplicator-enabled)', function(e) { |
| 186 |
|
| 187 |
var menu_item_enable = $(this).addClass('adminify-duplicator-enabled'); |
| 188 |
|
| 189 |
menu_item_enable.find('.menu-item-actions').append('<span class="meta-sep hide-if-no-js"> | </span><a class="item-duplicate-this submitcancel hide-if-no-js" href="#">Duplicate</a>'); |
| 190 |
|
| 191 |
}); |
| 192 |
|
| 193 |
$(document).on('click', 'li.menu-item.adminify-duplicator-enabled .item-duplicate-this', function(e) { |
| 194 |
|
| 195 |
e.preventDefault(); |
| 196 |
var li = $(this).closest('li.menu-item'), |
| 197 |
depth = 'menu-item-depth-' + adminifyGetMenu(li), |
| 198 |
availables = $('#menu-to-edit li[id^=menu-item-]'), |
| 199 |
ids = [], |
| 200 |
nowId = adminifyGetMenuID(li), |
| 201 |
newId, newEl, form_data; |
| 202 |
|
| 203 |
form_data = { |
| 204 |
action: 'add-menu-item', |
| 205 |
menu: $('#menu').val(), |
| 206 |
'menu-settings-column-nonce': $('#menu-settings-column-nonce').val(), |
| 207 |
'menu-item': { |
| 208 |
'-1': { |
| 209 |
'menu-item-db-id': 0, |
| 210 |
'menu-item-object-id': li.find('input.menu-item-data-object-id').val(), |
| 211 |
'menu-item-object': li.find('input.menu-item-data-object').val(), |
| 212 |
'menu-item-parent-id': li.find('input.menu-item-data-parent-id').val(), |
| 213 |
'menu-item-type': li.find('input.menu-item-data-type').val(), |
| 214 |
'menu-item-title': li.find('input.edit-menu-item-title').val(), |
| 215 |
'menu-item-url': li.find('input.edit-menu-item-url').val(), |
| 216 |
'menu-item-description': li.find('textarea.edit-menu-item-description').val(), |
| 217 |
'menu-item-attr-title': li.find('input.edit-menu-item-attr-title').val(), |
| 218 |
'menu-item-target': li.find('.field-link-target input[type=checkbox]').is(':checked') ? '_blank' : '', |
| 219 |
'menu-item-classes': li.find('input.edit-menu-item-classes').val(), |
| 220 |
'menu-item-xfn': li.find('input.edit-menu-item-xfn').val() |
| 221 |
} |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
$.post(ajaxurl, form_data, function(menuMarkup) { |
| 226 |
// console.log($(menuMarkup)); |
| 227 |
|
| 228 |
var newElement = $(menuMarkup); |
| 229 |
|
| 230 |
$('.hide-column-tog').not(':checked').each(function() { |
| 231 |
newElement.find('.field-' + $(this).val()).addClass('hidden-field'); |
| 232 |
}); |
| 233 |
|
| 234 |
newElement.removeClass('menu-item-depth-0'); |
| 235 |
newElement.addClass(depth); |
| 236 |
|
| 237 |
newElement = newElement.wrap('<div>').parent().html(); |
| 238 |
|
| 239 |
if (li.next().hasClass(depth) || li.parent().children('li').last().get(0) === li.get(0)) { |
| 240 |
li.after(newElement); |
| 241 |
} else if (adminifyGetMenu(li.next()) < adminifyGetMenu(li)) { |
| 242 |
li.after(newElement); |
| 243 |
} else { |
| 244 |
if (adminifyGetMenu(li) != 0) { |
| 245 |
depth = 'menu-item-depth-' + (adminifyGetMenu(li) - 1); |
| 246 |
} |
| 247 |
li.nextUntil('.' + depth).last().after(newElement); |
| 248 |
} |
| 249 |
}); |
| 250 |
|
| 251 |
}); |
| 252 |
|
| 253 |
})(jQuery) |
| 254 |
</script> |
| 255 |
<?php |
| 256 |
} |
| 257 |
} |
| 258 |
|