Skip to content

Commit

Permalink
Merge pull request #99 from swaponline/support27
Browse files Browse the repository at this point in the history
Support #27 - Allow add custom menu items
  • Loading branch information
noxonsu authored Feb 2, 2022
2 parents dfab87c + 01a341a commit 374c9eb
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 1 deletion.
2 changes: 2 additions & 0 deletions includes/admin_page/admin_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function mcwallet_page() {
<a href="#mcwallet-tab-3" class="nav-tab"><?php esc_html_e( 'Custom HTML', 'multi-currency-wallet' ); ?></a>
<a href="#mcwallet-tab-4" class="nav-tab"><?php esc_html_e( 'Strings Editor', 'multi-currency-wallet' ); ?></a>
<a href="#mcwallet-tab-editfaq" class="nav-tab"><?php esc_html_e( 'Edit FAQ', 'multi-currency-wallet' ); ?></a>
<a href="#mcwallet-tab-editmenu" class="nav-tab"><?php esc_html_e( 'Edit Menu items', 'multi-currency-wallet' ); ?></a>
</h2><!-- .nav-tab-wrapper -->

<div class="mcwallet-panel-tab panel-tab-active" id="mcwallet-tab-1">
Expand Down Expand Up @@ -190,6 +191,7 @@ function mcwallet_page() {

</div><!-- .mcwallet-panel-tab -->
<?php include "admin_page_editfaq.php"; ?>
<?php include "admin_page_editmenu.php"; ?>
<div class="welcome-panel-column-container mcwallet-panel-tab mcwallet-form-options" id="mcwallet-tab-2">
<div class="mcwallet-shortcode-panel-row">

Expand Down
2 changes: 1 addition & 1 deletion includes/admin_page/admin_page_editfaq.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function render_faq_rows($rows, $type) {
<tr>
<td colspan="3" align="center" style="background: #e9e9e9">
<strong>
<?php esc_html_e('Default FAQ block (&quot;How are my private keys stored&quot;, &quot;Waht are the fees involved&quot;, &quot;Why minning fee is to high&quot;)'); ?>
<?php esc_html_e('Default FAQ block (&quot;How are my private keys stored&quot;, &quot;What are the fees involved&quot;, &quot;Why minning fee is to high&quot;)'); ?>
</strong>
</td>
</tr>
Expand Down
270 changes: 270 additions & 0 deletions includes/admin_page/admin_page_editmenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
<div class="welcome-panel-column-container mcwallet-panel-tab mcwallet-form-options" id="mcwallet-tab-editmenu">
<div class="mcwallet-shortcode-panel-row">
<?php
$own_before_menus = get_option( 'mcwallet_own_before_menus' , array() );
$own_after_menus = get_option( 'mcwallet_own_after_menus', array() );

function render_menu_rows($rows, $type) {
if (count($rows)) {
foreach ($rows as $k=>$own_menu) {
?>
<tr class="mcwallet-own-menu-row">
<td>
<input type="text" data-mcwallet-target="mcwallet-menu-title" value="<?php esc_attr_e($own_menu['title'])?>" />
</td>
<td>
<input type="text" data-mcwallet-target="mcwallet-menu-link" value="<?php esc_attr_e($own_menu['link']);?>" />
</td>
<td>
<a href="#" data-mcwallet-action="mcwallet_menu_move_up">[Up]</a>
<a href="#" data-mcwallet-action="mcwallet_menu_move_down">[Down]</a>
<a href="#" data-mcwallet-action="mcwallet_menu_remove">[Delete]</a>
</td>
</tr>
<?php
}
}
?>
<tr class="<?php echo (count($rows)) ? '-mc-hidden' : ''?>" data-mcwallet-role="empty-row">
<td colspan="3" align="center"><?php esc_html_e('Empty') ?></td>
</tr>
<?php
}
?>
<h3><?php esc_html_e( 'Edit menu items', 'multi-currency-wallet' );?></h3>
<style type="text/css">
.mcwallet-menu-list STRONG {
font-weight: bold;
}
.mcwallet-own-menu-row INPUT {
width: 100%;
}
.mcwallet-menu-list .-mc-hidden {
display: none;
}
</style>
<table class="mcwallet-menu-list wp-list-table widefat striped">
<thead>
<tr>
<td width="25%"><strong><?php esc_html_e('Title', 'multi-currency-wallet'); ?></strong></td>
<td><strong><?php esc_html_e('Link', 'multi-currency-wallet'); ?></strong></td>
<td width="250px"><strong><?php esc_html_e('Actions', 'multi-currency-wallet'); ?></strong></td>
</tr>
</thead>
<tbody id="mcwallet-menu-before" data-mcwallet-role="menu-before-holder">
<?php render_menu_rows($own_before_menus, 'before'); ?>
</tbody>
<tbody>
<tr>
<td colspan="3" align="center" style="background: #e9e9e9">
<strong>
<?php esc_html_e('Default menu items (&quot;Wallet&quot;, &quot;Transactions&quot;, &quot;Exchange&quot;)'); ?>
</strong>
</td>
</tr>
</tbody>
<tbody id="mcwallet-menu-after" data-mcwallet-role="menu-after-holder">
<?php render_menu_rows($own_after_menus, 'after'); ?>
</tbody>
<thead>
<tr>
<td colspan="3">
<h3><?php esc_html_e( 'Add new menu item', 'multi-currency-wallet' );?></h3>
</td>
</tr>
</thead>
<tbody>
<tr class="mcwallet-own-menu-row">
<td>
<input type="text" data-mcwallet-role="mcwallet-addmenu-title" value="" />
</td>
<td>
<input type="text" data-mcwallet-role="mcwallet-addmenu-link" value="" />
</td>
<td>
<a href="#" data-mcwallet-action="mcwallet_menu_add">[Add menu]</a>
</td>
</tr>
</tbody>
<tbody style="display: none" data-mcwallet-role="menu_template">
<tr class="mcwallet-own-menu-row">
<td>
<input type="text" data-mcwallet-target="mcwallet-menu-title" value="" />
</td>
<td>
<input type="text" data-mcwallet-target="mcwallet-menu-link" value="" />
</td>
<td>
<a href="#" data-mcwallet-action="mcwallet_menu_move_up">[Up]</a>
<a href="#" data-mcwallet-action="mcwallet_menu_move_down">[Down]</a>
<a href="#" data-mcwallet-action="mcwallet_menu_remove">[Delete]</a>
</td>
</tr>
</tbody>
</table>

<table class="form-table">
<tr>
<th scope="row"></th>
<td>
<input type="submit" name="mcwallet-update-menu" id="mcwallet-update-menu" class="button button-primary" value="Update menu items" >
<span class="spinner"></span>
</td>
</tr>
</table>
<script type="text/javascript">
(($) => {
const $beforeHolder = $('#mcwallet-menu-before')
const $afterHolder = $('#mcwallet-menu-after')

$('#mcwallet-update-menu').on('click', function (e) {
e.preventDefault()

const $beforemenusRows = $beforeHolder.find('TR.mcwallet-own-menu-row')
const $aftermenusRows = $afterHolder.find('TR.mcwallet-own-menu-row')

const ajaxData = {
action: 'mcwallet_update_menus',
nonce: mcwallet.nonce,
menusBefore: [],
menusAfter: []
}
$beforemenusRows.each((i, rowholder) => {
const title = $($(rowholder).find('INPUT[data-mcwallet-target="mcwallet-menu-title"]')[0]).val()
const link = $($(rowholder).find('INPUT[data-mcwallet-target="mcwallet-menu-link"]')[0]).val()
ajaxData.menusBefore.push({
title,
link
})
})
$aftermenusRows.each((i, rowholder) => {
const title = $($(rowholder).find('INPUT[data-mcwallet-target="mcwallet-menu-title"]')[0]).val()
const link = $($(rowholder).find('INPUT[data-mcwallet-target="mcwallet-menu-link"]')[0]).val()
ajaxData.menusAfter.push({
title,
link
})
})
const thisBtn = $(this)
mcwallet.showSpinner(thisBtn)
$.post( mcwallet.ajaxurl, ajaxData, function(response) {
if( response.status == 'success' ) {
mcwallet.showNotice( mcwallet.notices.updated, 'success')
}
if ( response.status == 'false' ) {
mcwallet.showNotice( mcwallet.notices.wrong, 'error')
}
mcwallet.showSpinner(thisBtn)
});
})
$('A[data-mcwallet-action="mcwallet_menu_add"]').on('click', function (e) {
e.preventDefault()
const title = $('INPUT[data-mcwallet-role="mcwallet-addmenu-title"]').val()
const link = $('INPUT[data-mcwallet-role="mcwallet-addmenu-link"]').val()
const $newRow = $('TBODY[data-mcwallet-role="menu_template"]>TR').clone()
$('INPUT[data-mcwallet-role="mcwallet-addmenu-title"]').val('')
$('INPUT[data-mcwallet-role="mcwallet-addmenu-link"]').val('')
$newRow.css({ opacity: 0 })
$($newRow.find('INPUT[data-mcwallet-target="mcwallet-menu-title"]')[0]).val(title)
$($newRow.find('INPUT[data-mcwallet-target="mcwallet-menu-link"]')[0]).val(link)
$afterHolder.append($newRow)
$afterHolder.find('TR[data-mcwallet-role="empty-row"]').addClass('-mc-hidden')
$newRow.animate( { opacity: 1 }, 500)
})
$('TABLE.mcwallet-menu-list').on('click', 'A[data-mcwallet-action="mcwallet_menu_move_up"]', function (e) {
e.preventDefault();
const menuHolder = $($(e.target).parents('TR')[0])
const topHolder = $($(e.target).parents('TBODY')[0])
let menuPrev = $(menuHolder.prev('TR')[0])
if (menuPrev.length && menuPrev.data('mcwallet-role') === 'empty-row') {
menuPrev = $(menuPrev.prev('TR')[0])
}
if (menuPrev.length) {
menuHolder
.animate({
opacity: 0
},
500,
() => {
menuHolder
.insertBefore(menuPrev)
.animate({ opacity: 1, duration: 500 })
})
} else {
if (topHolder.data('mcwallet-role') === 'menu-after-holder') {
menuHolder
.animate({
opacity: 0
},
500,
() => {
$beforeHolder.find('TR[data-mcwallet-role="empty-row"]').addClass('-mc-hidden')
$beforeHolder.append(menuHolder)
menuHolder.animate({ opacity: 1, duration: 500 })
const aftermenus = $afterHolder.find('TR.mcwallet-own-menu-row')
if (!aftermenus.length) {
$afterHolder.find('TR[data-mcwallet-role="empty-row"]').removeClass('-mc-hidden')
}
})
}
}
})
$('TABLE.mcwallet-menu-list').on('click', 'A[data-mcwallet-action="mcwallet_menu_move_down"]', function (e) {
e.preventDefault();
const menuHolder = $($(e.target).parents('TR')[0])
const topHolder = $($(e.target).parents('TBODY')[0])
let menuNext = $(menuHolder.next('TR')[0])
if (menuNext.length && menuNext.data('mcwallet-role') === 'empty-row') {
menuNext = $(menuNext.next('TR')[0])
}
if (menuNext.length) {
menuHolder
.animate({
opacity: 0
},
500,
() => {
menuHolder
.insertAfter(menuNext)
.animate({ opacity: 1, duration: 500 })
})
} else {
if (topHolder.data('mcwallet-role') === 'menu-before-holder') {
menuHolder
.animate({
opacity: 0
},
500,
() => {
$afterHolder.find('TR[data-mcwallet-role="empty-row"]').addClass('-mc-hidden')
$afterHolder.prepend(menuHolder)
menuHolder.animate({ opacity: 1, duration: 500 })
const beforemenus = $beforeHolder.find('TR.mcwallet-own-menu-row')
if (!beforemenus.length) {
$beforeHolder.find('TR[data-mcwallet-role="empty-row"]').removeClass('-mc-hidden')
}
})
}
}
})
$('TABLE.mcwallet-menu-list').on('click', 'A[data-mcwallet-action="mcwallet_menu_remove"]', function (e) {
e.preventDefault();
if (confirm('Confirm delete')) {
const menuHolder = $($(e.target).parents('TR')[0])
const topHolder = $($(e.target).parents('TBODY')[0])
menuHolder.animate({
opacity: 0
},
500,
() => {
menuHolder.remove()
if(!topHolder.find('TR.mcwallet-own-menu-row').length) {
topHolder.find('TR[data-mcwallet-role="empty-row"]').removeClass('-mc-hidden')
}
})
}
})
})(jQuery)
</script>
</div>
</div>
46 changes: 46 additions & 0 deletions includes/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,52 @@ function mcwallet_update_faqs() {
));
}
add_action( 'wp_ajax_mcwallet_update_faqs', 'mcwallet_update_faqs' );

/**
* Update menu items
*/
function mcwallet_update_menus() {
/* Check nonce */
check_ajax_referer( 'mcwallet-nonce', 'nonce' );

/* Stop if the current user is not an admin or do not have administrative access */
if ( ! current_user_can( 'manage_options' ) ) {
die();
}

$status = 'false';

$own_before_menus = array();
if (isset($_POST['menusBefore']) and is_array($_POST['menusBefore'])) {
foreach ($_POST['menusBefore'] as $k=>$menuData) {
if (is_array($menuData) and isset($menuData['title']) and isset($menuData['link']) and ($menuData['title'] !== '') and ($menuData['link'] !== '')) {
$own_before_menus[] = array(
'title' => sanitize_text_field($menuData['title']),
'link' => sanitize_text_field($menuData['link'])
);
}
}
}
$own_after_menus = array();
if (isset($_POST['menusAfter']) and is_array($_POST['menusAfter'])) {
foreach ($_POST['menusAfter'] as $k=>$menuData) {
if (is_array($menuData) and isset($menuData['title']) and isset($menuData['link']) and ($menuData['title'] !== '') and ($menuData['link'] !== '')) {
$own_after_menus[] = array(
'title' => sanitize_text_field($menuData['title']),
'link' => sanitize_text_field($menuData['link'])
);
}
}
}
update_option( 'mcwallet_own_before_menus', $own_before_menus);
update_option( 'mcwallet_own_after_menus', $own_after_menus);
wp_send_json( array(
'status' => 'success'
));
}
add_action( 'wp_ajax_mcwallet_update_menus', 'mcwallet_update_menus' );


/**
* Update options
*/
Expand Down
8 changes: 8 additions & 0 deletions includes/enqueue_scripts/wp_enqueue_scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,14 @@ function mcwallet_inline_script() {

$script .= 'window.SO_FaqBeforeTabs = ' . wp_json_encode( $own_before_faqs , JSON_PRETTY_PRINT ) . ';' . "\n\n";
$script .= 'window.SO_FaqAfterTabs = ' . wp_json_encode( $own_after_faqs , JSON_PRETTY_PRINT ) . ';' . "\n\n";

// menu items
$own_before_menus = get_option( 'mcwallet_own_before_menus' , array() );
$own_after_menus = get_option( 'mcwallet_own_after_menus', array() );

$script .= 'window.SO_MenuItemsBefore = ' . wp_json_encode( $own_before_menus , JSON_PRETTY_PRINT ) . ';' . "\n\n";
$script .= 'window.SO_MenuItemsAfter = ' . wp_json_encode( $own_after_menus , JSON_PRETTY_PRINT ) . ';' . "\n\n";

$args = array(
'post_type' => 'mcwallet_banner',
'posts_per_page' => -1,
Expand Down

0 comments on commit 374c9eb

Please sign in to comment.