Created
August 18, 2018 04:13
-
-
Save imranhoshain/0d4500061bcfd1d22509d97e1502914e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(!function_exists('spy_get_page_id')) { | |
function spy_get_page_id() { | |
global $post; | |
$page = array( | |
'id' => 0, | |
'type' => 'page' | |
); | |
if(isset($post->ID) && is_singular('page')) { | |
$page = array( | |
'id' => $post->ID, | |
'type' => 'page' | |
); | |
} else if( is_home() || is_archive('post') || is_search() ) { | |
$page = array( | |
'id' => $id = get_option( 'page_for_posts' ), | |
'type' => 'blog' | |
); | |
} else if( get_post_type() == 'etheme_portfolio' || is_singular( 'etheme_portfolio' ) ) { | |
$page = array( | |
'id' => etheme_tpl2id( 'portfolio.php' ), | |
'type' => 'portfolio' | |
); | |
} | |
if(class_exists('WooCommerce') && (is_shop() || is_product_category() || is_product_tag() || is_singular( "product" ))) { | |
$page = array( | |
'id' => get_option('woocommerce_shop_page_id'), | |
'type' => 'shop' | |
); | |
} | |
return $page; | |
} | |
} | |
/** | |
* Adding custom sidebar ajax | |
*/ | |
if(!function_exists('spy_add_sidebar_action')) { | |
function spy_add_sidebar_action(){ | |
if (!wp_verify_nonce($_GET['_wpnonce_piko_widgets'],'piko-add-sidebar-widgets') ) die( 'Security check' ); | |
if($_GET['piko_sidebar_name'] == '') die('Empty Name'); | |
$option_name = 'spy_custom_sidebars'; | |
if(!get_option($option_name) || get_option($option_name) == '') delete_option($option_name); | |
$new_sidebar = $_GET['piko_sidebar_name']; | |
$result = spy_add_sidebar($new_sidebar); | |
if($result) die($result); | |
else die('error'); | |
} | |
} | |
if( ! function_exists('spy_add_sidebar') ) { | |
function spy_add_sidebar($name) { | |
$option_name = 'spy_custom_sidebars'; | |
if(get_option($option_name)) { | |
$custom_sidebars = spy_get_stored_sidebar(); | |
$custom_sidebars[] = trim($name); | |
$result = update_option($option_name, $custom_sidebars); | |
}else{ | |
$custom_sidebars[] = $name; | |
$result2 = add_option($option_name, $custom_sidebars); | |
} | |
if($result) return 'Updated'; | |
elseif($result2) return 'added'; | |
else die('error'); | |
} | |
} | |
/** | |
* deleting custom sidebar in ajax | |
*/ | |
if(!function_exists('spy_delete_sidebar')) { | |
function spy_delete_sidebar(){ | |
$option_name = 'spy_custom_sidebars'; | |
$del_sidebar = trim($_GET['piko_sidebar_name']); | |
if(get_option($option_name)) { | |
$custom_sidebars = spy_get_stored_sidebar(); | |
foreach($custom_sidebars as $key => $value){ | |
if($value == $del_sidebar) | |
unset($custom_sidebars[$key]); | |
} | |
$result = update_option($option_name, $custom_sidebars); | |
} | |
if($result) die('Deleted'); | |
else die('error'); | |
} | |
} | |
/** | |
* detault registering sidebars similar custom sidebar | |
*/ | |
if(!function_exists('spy_register_stored_sidebar')) { | |
function spy_register_stored_sidebar(){ | |
$custom_sidebars = spy_get_stored_sidebar(); | |
if(is_array($custom_sidebars)) { | |
foreach($custom_sidebars as $name){ | |
register_sidebar( array( | |
'name' => ''.$name.'', | |
'id' => str_replace(' ','',strtolower ($name)), | |
'class' => 'piko_custom_sidebar', | |
'before_widget' => '<section id="%1$s" class="widget %2$s">', | |
'after_widget' => '</section>', | |
'before_title' => '<h2 class="widget-title">', | |
'after_title' => '</h2>', | |
) ); | |
} | |
} | |
} | |
} | |
/** | |
* Stored all sidebar in array | |
*/ | |
if(!function_exists('spy_get_stored_sidebar')) { | |
function spy_get_stored_sidebar(){ | |
$option_name = 'spy_custom_sidebars'; | |
return get_option($option_name); | |
} | |
} | |
/** | |
* Add form custom widgets | |
*/ | |
if(!function_exists('spy_sidebar_form')) { | |
function spy_sidebar_form(){ | |
?> | |
<form action="<?php echo admin_url( 'widgets.php' ); ?>" method="post" id="piko_add_sidebar_form"> | |
<h2>Custom Sidebar</h2> | |
<?php wp_nonce_field( 'piko-add-sidebar-widgets', '_wpnonce_piko_widgets', false ); ?> | |
<input type="text" name="piko_sidebar_name" id="piko_sidebar_name" /> | |
<button type="submit" class="button-primary" value="add-sidebar">Add Sidebar</button> | |
</form> | |
<script type="text/javascript"> | |
var sidebarForm = jQuery('#piko_add_sidebar_form'); | |
var sidebarFormNew = sidebarForm.clone(); | |
sidebarForm.remove(); | |
jQuery('#widgets-right').append('<div style="clear:both;"></div>'); | |
jQuery('#widgets-right').append(sidebarFormNew); | |
sidebarFormNew.submit(function(e){ | |
e.preventDefault(); | |
var data = { | |
'action':'spy_add_sidebar', | |
'_wpnonce_piko_widgets': jQuery('#_wpnonce_piko_widgets').val(), | |
'piko_sidebar_name': jQuery('#piko_sidebar_name').val(), | |
}; | |
//console.log(data); | |
jQuery.ajax({ | |
url: ajaxurl, | |
data: data, | |
success: function(response){ | |
console.log(response); | |
window.location.reload(true); | |
}, | |
error: function(data) { | |
console.log('error'); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'sidebar_admin_page', 'spy_sidebar_form', 30 ); | |
add_action('wp_ajax_spy_add_sidebar', 'spy_add_sidebar_action'); | |
add_action('wp_ajax_spy_delete_sidebar', 'spy_delete_sidebar'); | |
add_action( 'widgets_init', 'spy_register_stored_sidebar' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment