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