woocommerce/admin/admin-init.php

184 lines
5.4 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
/**
2011-08-10 17:11:11 +00:00
* WooCommerce Admin
2011-08-09 15:16:18 +00:00
*
2011-08-10 17:11:11 +00:00
* Main admin file which loads all settings panels and sets up admin menus.
2011-08-09 15:16:18 +00:00
*
2011-08-10 17:11:11 +00:00
* @author WooThemes
2011-08-09 15:16:18 +00:00
* @category Admin
2011-08-10 17:11:11 +00:00
* @package WooCommerce
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
include_once( 'admin-settings.php' );
include_once( 'admin-install.php' );
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
function woocommerce_admin_init() {
include_once( 'admin-attributes.php' );
include_once( 'admin-dashboard.php' );
include_once( 'admin-import.php' );
include_once( 'admin-post-types.php' );
include_once( 'writepanels/writepanels-init.php' );
2011-08-09 15:16:18 +00:00
}
2011-08-10 17:11:11 +00:00
add_action('admin_init', 'woocommerce_admin_init');
2011-08-09 15:16:18 +00:00
/**
* Admin Menus
*
* Sets up the admin menus in wordpress.
*/
2011-08-10 17:11:11 +00:00
function woocommerce_admin_menu() {
2011-08-09 15:16:18 +00:00
global $menu;
2011-08-10 17:11:11 +00:00
$menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator' );
2011-08-09 15:16:18 +00:00
2011-08-19 14:48:41 +00:00
add_menu_page(__('WooCommerce'), __('WooCommerce'), 'manage_woocommerce', 'woocommerce' , 'woocommerce_settings', woocommerce::plugin_url() . '/assets/images/icons/menu_icons.png', 56);
add_submenu_page('woocommerce', __('General Settings', 'woothemes'), __('Settings', 'woothemes') , 'manage_woocommerce', 'woocommerce', 'woocommerce_settings');
2011-08-13 16:07:10 +00:00
add_submenu_page('edit.php?post_type=product', __('Attributes', 'woothemes'), __('Attributes', 'woothemes'), 'manage_woocommerce', 'attributes', 'woocommerce_attributes');
2011-08-09 15:16:18 +00:00
}
2011-08-10 17:11:11 +00:00
function woocommerce_admin_menu_order( $menu_order ) {
2011-08-09 15:16:18 +00:00
// Initialize our custom order array
2011-08-10 17:11:11 +00:00
$woocommerce_menu_order = array();
2011-08-09 15:16:18 +00:00
// Get the index of our custom separator
2011-08-10 17:11:11 +00:00
$woocommerce_separator = array_search( 'separator-woocommerce', $menu_order );
2011-08-09 15:16:18 +00:00
// Loop through menu order and do some rearranging
foreach ( $menu_order as $index => $item ) :
2011-08-10 17:11:11 +00:00
if ( ( ( 'woocommerce' ) == $item ) ) :
$woocommerce_menu_order[] = 'separator-woocommerce';
unset( $menu_order[$woocommerce_separator] );
2011-08-09 15:16:18 +00:00
endif;
2011-08-10 17:11:11 +00:00
if ( !in_array( $item, array( 'separator-woocommerce' ) ) ) :
$woocommerce_menu_order[] = $item;
2011-08-09 15:16:18 +00:00
endif;
endforeach;
// Return order
2011-08-10 17:11:11 +00:00
return $woocommerce_menu_order;
2011-08-09 15:16:18 +00:00
}
2011-08-10 17:11:11 +00:00
function woocommerce_admin_custom_menu_order() {
2011-08-09 15:16:18 +00:00
if ( !current_user_can( 'manage_options' ) ) return false;
return true;
}
2011-08-15 16:48:24 +00:00
add_action('admin_menu', 'woocommerce_admin_menu', 9);
2011-08-10 17:11:11 +00:00
add_action('menu_order', 'woocommerce_admin_menu_order');
add_action('custom_menu_order', 'woocommerce_admin_custom_menu_order');
2011-08-09 15:16:18 +00:00
/**
* Admin Head
*
2011-08-10 17:11:11 +00:00
* Outputs some styles in the admin <head> to show icons on the woocommerce admin pages
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function woocommerce_admin_head() {
2011-08-09 15:16:18 +00:00
?>
<style type="text/css">
<?php if ( isset($_GET['taxonomy']) && $_GET['taxonomy']=='product_cat' ) : ?>
.icon32-posts-product { background-position: -243px -5px !important; }
<?php elseif ( isset($_GET['taxonomy']) && $_GET['taxonomy']=='product_tag' ) : ?>
.icon32-posts-product { background-position: -301px -5px !important; }
<?php endif; ?>
</style>
<?php
}
2011-08-10 17:11:11 +00:00
add_action('admin_head', 'woocommerce_admin_head');
2011-08-09 15:16:18 +00:00
2011-08-13 13:57:48 +00:00
2011-08-10 17:11:11 +00:00
/**
* Feature a product from admin
*/
function woocommerce_feature_product() {
2011-08-09 15:16:18 +00:00
if( !is_admin() ) die;
if( !current_user_can('edit_posts') ) wp_die( __('You do not have sufficient permissions to access this page.') );
2011-08-10 17:11:11 +00:00
if( !check_admin_referer()) wp_die( __('You have taken too long. Please go back and retry.', 'woothemes') );
2011-08-09 15:16:18 +00:00
$post_id = isset($_GET['product_id']) && (int)$_GET['product_id'] ? (int)$_GET['product_id'] : '';
if(!$post_id) die;
$post = get_post($post_id);
if(!$post) die;
if($post->post_type !== 'product') die;
2011-08-10 17:11:11 +00:00
$product = new woocommerce_product($post->ID);
2011-08-09 15:16:18 +00:00
if ($product->is_featured()) update_post_meta($post->ID, 'featured', 'no');
else update_post_meta($post->ID, 'featured', 'yes');
$sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
wp_safe_redirect( $sendback );
}
2011-08-10 17:11:11 +00:00
add_action('wp_ajax_woocommerce-feature-product', 'woocommerce_feature_product');
2011-08-09 15:16:18 +00:00
/**
* Returns proper post_type
*/
2011-08-10 17:11:11 +00:00
function woocommerce_get_current_post_type() {
2011-08-09 15:16:18 +00:00
global $post, $typenow, $current_screen;
if( $current_screen && @$current_screen->post_type ) return $current_screen->post_type;
if( $typenow ) return $typenow;
if( !empty($_REQUEST['post_type']) ) return sanitize_key( $_REQUEST['post_type'] );
if ( !empty($post) && !empty($post->post_type) ) return $post->post_type;
if( ! empty($_REQUEST['post']) && (int)$_REQUEST['post'] ) {
$p = get_post( $_REQUEST['post'] );
return $p ? $p->post_type : '';
}
return '';
}
/**
2011-08-10 17:11:11 +00:00
* Categories ordering scripts
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function woocommerce_categories_scripts () {
2011-08-09 15:16:18 +00:00
if( !isset($_GET['taxonomy']) || $_GET['taxonomy'] !== 'product_cat') return;
2011-08-10 17:11:11 +00:00
wp_register_script('woocommerce-categories-ordering', woocommerce::plugin_url() . '/assets/js/categories-ordering.js', array('jquery-ui-sortable'));
wp_print_scripts('woocommerce-categories-ordering');
2011-08-09 15:16:18 +00:00
}
2011-08-10 17:11:11 +00:00
add_action('admin_footer-edit-tags.php', 'woocommerce_categories_scripts');
2011-08-09 15:16:18 +00:00
/**
* Ajax request handling for categories ordering
*/
2011-08-10 17:11:11 +00:00
function woocommerce_categories_ordering() {
2011-08-09 15:16:18 +00:00
global $wpdb;
$id = (int)$_POST['id'];
$next_id = isset($_POST['nextid']) && (int) $_POST['nextid'] ? (int) $_POST['nextid'] : null;
if( ! $id || ! $term = get_term_by('id', $id, 'product_cat') ) die(0);
2011-08-10 17:11:11 +00:00
woocommerce_order_categories( $term, $next_id );
2011-08-09 15:16:18 +00:00
$children = get_terms('product_cat', "child_of=$id&menu_order=ASC&hide_empty=0");
if( $term && sizeof($children) ) {
echo 'children';
die;
}
}
2011-08-10 17:11:11 +00:00
add_action('wp_ajax_woocommerce-categories-ordering', 'woocommerce_categories_ordering');