PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.0
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.0
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / Modules / MenuDuplicator / MenuDuplicator.php

MenuDuplicator.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.0, at Inc/Modules/MenuDuplicator/MenuDuplicator.php

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