Location: PHPKode > scripts > ColorShop > colorshop/admin/post-types/product.php
<?php
/**
 * Admin functions for the shop_product post type
 *
 * @author 		ColorVila
 * @category 	Admin
 * @package 	ColorShop/Admin/Products
 * @version     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Duplicate a product link on products list
 *
 * @access public
 * @param mixed $actions
 * @param mixed $post
 * @return array
 */
function colorshop_duplicate_product_link_row($actions, $post) {

	if ( function_exists( 'duplicate_post_plugin_activation' ) )
		return $actions;

	if ( ! current_user_can( 'manage_colorshop' ) ) return $actions;

	if ( $post->post_type != 'product' )
		return $actions;

	$actions['duplicate'] = '<a href="' . wp_nonce_url( admin_url( 'admin.php?action=duplicate_product&amp;post=' . $post->ID ), 'colorshop-duplicate-product_' . $post->ID ) . '" title="' . __( 'Make a duplicate from this product', 'colorshop' )
		. '" rel="permalink">' .  __( 'Duplicate', 'colorshop' ) . '</a>';

	return $actions;
}

add_filter( 'post_row_actions', 'colorshop_duplicate_product_link_row',10,2 );
add_filter( 'page_row_actions', 'colorshop_duplicate_product_link_row',10,2 );


/**
 *  Duplicate a product link on edit screen
 *
 * @access public
 * @return void
 */
function colorshop_duplicate_product_post_button() {
	global $post;

	if ( function_exists( 'duplicate_post_plugin_activation' ) ) return;

	if ( ! current_user_can( 'manage_colorshop' ) ) return;

	if ( ! is_object( $post ) ) return;

	if ( $post->post_type != 'product' ) return;

	if ( isset( $_GET['post'] ) ) {
		$notifyUrl = wp_nonce_url( admin_url( "admin.php?action=duplicate_product&post=" . absint( $_GET['post'] ) ), 'colorshop-duplicate-product_' . $_GET['post'] );
		?>
		<div id="duplicate-action"><a class="submitduplicate duplication" href="<?php echo esc_url( $notifyUrl ); ?>"><?php _e( 'Copy to a new draft', 'colorshop' ); ?></a></div>
		<?php
	}
}

add_action( 'post_submitbox_start', 'colorshop_duplicate_product_post_button' );


/**
 * Columns for Products page
 *
 * @access public
 * @param mixed $columns
 * @return array
 */
function colorshop_edit_product_columns( $existing_columns ) {
	global $colorshop;

	if ( empty( $existing_columns ) && ! is_array( $existing_columns ) )
		$existing_columns = array();

	unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'] );

	$columns = array();
	$columns["cb"] = "<input type=\"checkbox\" />";
	$columns["thumb"] = '<img src="' . $colorshop->plugin_url() . '/assets/images/image.png" alt="' . __( 'Image', 'colorshop' ) . '" class="tips" data-tip="' . __( 'Image', 'colorshop' ) . '" width="14" height="14" />';

	$columns["name"] = __( 'Name', 'colorshop' );

	if (get_option('colorshop_enable_sku', true) == 'yes')
		$columns["sku"] = __( 'SKU', 'colorshop' );

	if (get_option('colorshop_manage_stock')=='yes')
		$columns["is_in_stock"] = __( 'Stock', 'colorshop' );

	$columns["price"] = __( 'Price', 'colorshop' );

	$columns["product_cat"] = __( 'Categories', 'colorshop' );
	$columns["product_tag"] = __( 'Tags', 'colorshop' );
	$columns["featured"] = '<img src="' . $colorshop->plugin_url() . '/assets/images/featured.png" alt="' . __( 'Featured', 'colorshop' ) . '" class="tips" data-tip="' . __( 'Featured', 'colorshop' ) . '" width="12" height="12" />';
	$columns["product_type"] = '<img src="' . $colorshop->plugin_url() . '/assets/images/product_type_head.png" alt="' . __( 'Type', 'colorshop' ) . '" class="tips" data-tip="' . __( 'Type', 'colorshop' ) . '" width="14" height="12" />';
	$columns["date"] = __( 'Date', 'colorshop' );

	return array_merge( $columns, $existing_columns );
}

add_filter( 'manage_edit-product_columns', 'colorshop_edit_product_columns' );


/**
 * Custom Columns for Products page
 *
 * @access public
 * @param mixed $column
 * @return void
 */
function colorshop_custom_product_columns( $column ) {
	global $post, $colorshop, $the_product;

	if ( empty( $the_product ) || $the_product->id != $post->ID )
		$the_product = get_product( $post );

	switch ($column) {
		case "thumb" :
			echo '<a href="' . get_edit_post_link( $post->ID ) . '">' . $the_product->get_image() . '</a>';
		break;
		case "name" :
			$edit_link = get_edit_post_link( $post->ID );
			$title = _draft_or_post_title();
			$post_type_object = get_post_type_object( $post->post_type );
			$can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID );

			echo '<strong><a class="row-title" href="'.$edit_link.'">' . $title.'</a>';

			_post_states( $post );

			echo '</strong>';

			if ( $post->post_parent > 0 )
				echo '&nbsp;&nbsp;&larr; <a href="'. get_edit_post_link($post->post_parent) .'">'. get_the_title($post->post_parent) .'</a>';

			// Excerpt view
			if (isset($_GET['mode']) && $_GET['mode']=='excerpt') echo apply_filters('the_excerpt', $post->post_excerpt);

			// Get actions
			$actions = array();

			$actions['id'] = 'ID: ' . $post->ID;

			if ( $can_edit_post && 'trash' != $post->post_status ) {
				$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item' ) ) . '">' . __( 'Edit' ) . '</a>';
				$actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr( __( 'Edit this item inline' ) ) . '">' . __( 'Quick&nbsp;Edit' ) . '</a>';
			}
			if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
				if ( 'trash' == $post->post_status )
					$actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
				elseif ( EMPTY_TRASH_DAYS )
					$actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
				if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
					$actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently' ) . "</a>";
			}
			if ( $post_type_object->public ) {
				if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
					if ( $can_edit_post )
						$actions['view'] = '<a href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
				} elseif ( 'trash' != $post->post_status ) {
					$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
				}
			}

			$actions = apply_filters( 'post_row_actions', $actions, $post );

			echo '<div class="row-actions">';

			$i = 0;
			$action_count = sizeof($actions);

			foreach ( $actions as $action => $link ) {
				++$i;
				( $i == $action_count ) ? $sep = '' : $sep = ' | ';
				echo "<span class='$action'>$link$sep</span>";
			}
			echo '</div>';

			get_inline_data( $post );

			/* Custom inline data for colorshop */
			echo '
				<div class="hidden" id="colorshop_inline_' . $post->ID . '">
					<div class="menu_order">' . $post->menu_order . '</div>
					<div class="sku">' . $the_product->sku . '</div>
					<div class="regular_price">' . $the_product->regular_price . '</div>
					<div class="sale_price">' . $the_product->sale_price . '</div>
					<div class="weight">' . $the_product->weight . '</div>
					<div class="length">' . $the_product->length . '</div>
					<div class="width">' . $the_product->width . '</div>
					<div class="height">' . $the_product->height . '</div>
					<div class="visibility">' . $the_product->visibility . '</div>
					<div class="stock_status">' . $the_product->stock_status . '</div>
					<div class="stock">' . $the_product->stock . '</div>
					<div class="manage_stock">' . $the_product->manage_stock . '</div>
					<div class="featured">' . $the_product->featured . '</div>
					<div class="product_type">' . $the_product->product_type . '</div>
					<div class="product_is_virtual">' . $the_product->virtual . '</div>
				</div>
			';

		break;
		case "sku" :
			if ($the_product->get_sku()) echo $the_product->get_sku(); else echo '<span class="na">&ndash;</span>';
		break;
		case "product_type" :
			if( $the_product->product_type == 'grouped' ):
				echo '<span class="product-type tips '.$the_product->product_type.'" data-tip="' . __( 'Grouped', 'colorshop' ) . '"></span>';
			elseif ( $the_product->product_type == 'external' ):
				echo '<span class="product-type tips '.$the_product->product_type.'" data-tip="' . __( 'External/Affiliate', 'colorshop' ) . '"></span>';
			elseif ( $the_product->product_type == 'simple' ):

				if ($the_product->is_virtual()) {
					echo '<span class="product-type tips virtual" data-tip="' . __( 'Virtual', 'colorshop' ) . '"></span>';
				} elseif ($the_product->is_downloadable()) {
					echo '<span class="product-type tips downloadable" data-tip="' . __( 'Downloadable', 'colorshop' ) . '"></span>';
				} else {
					echo '<span class="product-type tips '.$the_product->product_type.'" data-tip="' . __( 'Simple', 'colorshop' ) . '"></span>';
				}

			elseif ( $the_product->product_type == 'variable' ):
				echo '<span class="product-type tips '.$the_product->product_type.'" data-tip="' . __( 'Variable', 'colorshop' ) . '"></span>';
			else:
				// Assuming that we have other types in future
				echo '<span class="product-type tips '.$the_product->product_type.'" data-tip="' . ucwords($the_product->product_type) . '"></span>';
			endif;
		break;
		case "price":
			if ($the_product->get_price_html()) echo $the_product->get_price_html(); else echo '<span class="na">&ndash;</span>';
		break;
		case "product_cat" :
		case "product_tag" :
			if ( ! $terms = get_the_terms( $post->ID, $column ) ) {
				echo '<span class="na">&ndash;</span>';
			} else {
				foreach ( $terms as $term ) {
					$termlist[] = '<a href="' . admin_url( 'edit.php?' . $column . '=' . $term->slug . '&post_type=product' ) . ' ">' . $term->name . '</a>';
				}

				echo implode( ', ', $termlist );
			}
		break;
		case 'featured':
			$url = wp_nonce_url( admin_url( 'admin-ajax.php?action=colorshop-feature-product&product_id=' . $post->ID ), 'colorshop-feature-product' );
			echo '<a href="' . $url . '" title="'. __( 'Toggle featured', 'colorshop' ) . '">';
			if ( $the_product->is_featured() ) {
				echo '<img src="' . $colorshop->plugin_url() . '/assets/images/featured.png" alt="'. __( 'yes', 'colorshop' ) . '" height="14" width="14" />';
			} else {
				echo '<img src="' . $colorshop->plugin_url() . '/assets/images/featured-off.png" alt="'. __( 'no', 'colorshop' ) . '" height="14" width="14" />';
			}
			echo '</a>';
		break;
		case "is_in_stock" :

			if ($the_product->is_in_stock()) {
				echo '<mark class="instock">' . __( 'In stock', 'colorshop' ) . '</mark>';
			} else {
				echo '<mark class="outofstock">' . __( 'Out of stock', 'colorshop' ) . '</mark>';
			}

			if ( $the_product->managing_stock() ) :
				echo ' &times; ' . $the_product->get_total_stock();
			endif;

		break;
	}
}

add_action('manage_product_posts_custom_column', 'colorshop_custom_product_columns', 2 );


/**
 * Make product columns sortable
 * https://gist.github.com/906872
 **/

/**
 * Make product columns sortable
 *
 * https://gist.github.com/906872
 *
 * @access public
 * @param mixed $columns
 * @return array
 */
function colorshop_custom_product_sort($columns) {
	$custom = array(
		'price'			=> 'price',
		'featured'		=> 'featured',
		'sku'			=> 'sku',
		'name'			=> 'title'
	);
	return wp_parse_args( $custom, $columns );
}

add_filter( 'manage_edit-product_sortable_columns', 'colorshop_custom_product_sort');


/**
 * Product column orderby
 *
 * http://scribu.net/wordpress/custom-sortable-columns.html#comment-4732
 *
 * @access public
 * @param mixed $vars
 * @return array
 */
function colorshop_custom_product_orderby( $vars ) {
	if (isset( $vars['orderby'] )) :
		if ( 'price' == $vars['orderby'] ) :
			$vars = array_merge( $vars, array(
				'meta_key' 	=> '_price',
				'orderby' 	=> 'meta_value_num'
			) );
		endif;
		if ( 'featured' == $vars['orderby'] ) :
			$vars = array_merge( $vars, array(
				'meta_key' 	=> '_featured',
				'orderby' 	=> 'meta_value'
			) );
		endif;
		if ( 'sku' == $vars['orderby'] ) :
			$vars = array_merge( $vars, array(
				'meta_key' 	=> '_sku',
				'orderby' 	=> 'meta_value'
			) );
		endif;
	endif;

	return $vars;
}

add_filter( 'request', 'colorshop_custom_product_orderby' );


/**
 * Filter products by category, uses slugs for option values.
 *
 * Code adapted by Andrew Benbow
 *
 * @access public
 * @return void
 */
function colorshop_products_by_category() {
	global $typenow, $wp_query;
    if ($typenow=='product') :
    	colorshop_product_dropdown_categories();
    endif;
}

add_action('restrict_manage_posts','colorshop_products_by_category');


/**
 * Filter products by type
 *
 * @access public
 * @return void
 */
function colorshop_products_by_type() {
    global $typenow, $wp_query;
    if ($typenow=='product') :

    	// Types
		$terms = get_terms('product_type');
		$output = "<select name='product_type' id='dropdown_product_type'>";
		$output .= '<option value="">'.__( 'Show all product types', 'colorshop' ).'</option>';
		foreach($terms as $term) :
			$output .="<option value='" . sanitize_title( $term->name ) . "' ";
			if ( isset( $wp_query->query['product_type'] ) ) $output .=selected($term->slug, $wp_query->query['product_type'], false);
			$output .=">";

				// Its was dynamic but did not support the translations
				if( $term->name == 'grouped' ):
					$output .= __( 'Grouped product', 'colorshop' );
				elseif ( $term->name == 'external' ):
					$output .= __( 'External/Affiliate product', 'colorshop' );
				elseif ( $term->name == 'simple' ):
					$output .= __( 'Simple product', 'colorshop' );
				elseif ( $term->name == 'variable' ):
					$output .= __( 'Variable', 'colorshop' );
				else:
					// Assuming that we have other types in future
					$output .= ucwords($term->name);
				endif;

			$output .=" ($term->count)</option>";
		endforeach;
		$output .="</select>";

		// Downloadable/virtual
		$output .= "<select name='product_subtype' id='dropdown_product_subtype'>";
		$output .= '<option value="">'.__( 'Show all sub-types', 'colorshop' ).'</option>';

		$output .="<option value='downloadable' ";
		if ( isset( $_GET['product_subtype'] ) ) $output .= selected('downloadable', $_GET['product_subtype'], false);
		$output .=">".__( 'Downloadable', 'colorshop' )."</option>";

		$output .="<option value='virtual' ";
		if ( isset( $_GET['product_subtype'] ) ) $output .= selected('virtual', $_GET['product_subtype'], false);
		$output .=">".__( 'Virtual', 'colorshop' )."</option>";

		$output .="</select>";

		echo $output;
    endif;
}

add_action('restrict_manage_posts', 'colorshop_products_by_type');


/**
 * Filter the products in admin based on options
 *
 * @access public
 * @param mixed $query
 * @return void
 */
function colorshop_admin_product_filter_query( $query ) {
	global $typenow, $wp_query;

    if ( $typenow == 'product' ) {

    	// Subtypes
    	if ( ! empty( $_GET['product_subtype'] ) ) {
	    	if ( $_GET['product_subtype'] == 'downloadable' ) {
	        	$query->query_vars['meta_value'] 	= 'yes';
	        	$query->query_vars['meta_key'] 		= '_downloadable';
	        } elseif ( $_GET['product_subtype'] == 'virtual' ) {
	        	$query->query_vars['meta_value'] 	= 'yes';
	        	$query->query_vars['meta_key'] 		= '_virtual';
	        }
        }

        // Categories
        if ( isset( $_GET['product_cat'] ) && $_GET['product_cat'] == '0' ) {

        	$query->query_vars['tax_query'][] = array(
        		'taxonomy' => 'product_cat',
        		'field' => 'id',
				'terms' => get_terms( 'product_cat', array( 'fields' => 'ids' ) ),
				'operator' => 'NOT IN'
        	);

        }

	}

}

add_filter( 'parse_query', 'colorshop_admin_product_filter_query' );


/**
 * Search by SKU or ID for products. Adapted from code by BenIrvin (Admin Search by ID)
 *
 * @access public
 * @param mixed $wp
 * @return void
 */
function colorshop_admin_product_search( $wp ) {
    global $pagenow, $wpdb;

	if( 'edit.php' != $pagenow ) return;
	if( !isset( $wp->query_vars['s'] ) ) return;
	if ($wp->query_vars['post_type']!='product') return;

	if( '#' == substr( $wp->query_vars['s'], 0, 1 ) ) :

		$id = absint( substr( $wp->query_vars['s'], 1 ) );

		if( !$id ) return;

		unset( $wp->query_vars['s'] );
		$wp->query_vars['p'] = $id;

	elseif( 'SKU:' == substr( $wp->query_vars['s'], 0, 4 ) ) :

		$sku = trim( substr( $wp->query_vars['s'], 4 ) );

		if( !$sku ) return;

		$ids = $wpdb->get_col( 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE meta_key="_sku" AND meta_value LIKE "%' . $sku . '%";' );

		if ( ! $ids ) return;

		unset( $wp->query_vars['s'] );
		$wp->query_vars['post__in'] = $ids;
		$wp->query_vars['sku'] = $sku;

	endif;
}


/**
 * Label for the search by ID/SKU feature
 *
 * @access public
 * @param mixed $query
 * @return void
 */
function colorshop_admin_product_search_label($query) {
	global $pagenow, $typenow, $wp;

    if ( 'edit.php' != $pagenow ) return $query;
    if ( $typenow!='product' ) return $query;

	$s = get_query_var( 's' );
	if ($s) return $query;

	$sku = get_query_var( 'sku' );
	if($sku) {
		$post_type = get_post_type_object($wp->query_vars['post_type']);
		return sprintf(__( '[%s with SKU of %s]', 'colorshop' ), $post_type->labels->singular_name, $sku);
	}

	$p = get_query_var( 'p' );
	if ($p) {
		$post_type = get_post_type_object($wp->query_vars['post_type']);
		return sprintf(__( '[%s with ID of %d]', 'colorshop' ), $post_type->labels->singular_name, $p);
	}

	return $query;
}

if ( is_admin() ) {
	add_action( 'parse_request', 'colorshop_admin_product_search' );
	add_filter( 'get_search_query', 'colorshop_admin_product_search_label' );
}


/**
 * Custom quick edit - form
 *
 * @access public
 * @param mixed $column_name
 * @param mixed $post_type
 * @return void
 */
function colorshop_admin_product_quick_edit( $column_name, $post_type ) {
	if ($column_name != 'price' || $post_type != 'product') return;
	?>
    <fieldset class="inline-edit-col-left">
		<div id="colorshop-fields" class="inline-edit-col">

			<h4><?php _e( 'Product Data', 'colorshop' ); ?></h4>

			<?php if( get_option('colorshop_enable_sku', true) !== 'no' ) : ?>

				<label>
				    <span class="title"><?php _e( 'SKU', 'colorshop' ); ?></span>
				    <span class="input-text-wrap">
						<input type="text" name="_sku" class="text sku" value="">
					</span>
				</label>
				<br class="clear" />

			<?php endif; ?>

			<div class="price_fields">
				<label>
				    <span class="title"><?php _e( 'Price', 'colorshop' ); ?></span>
				    <span class="input-text-wrap">
						<input type="text" name="_regular_price" class="text regular_price" placeholder="<?php _e( 'Regular price', 'colorshop' ); ?>" value="">
					</span>
				</label>
				<br class="clear" />
				<label>
				    <span class="title"><?php _e( 'Sale', 'colorshop' ); ?></span>
				    <span class="input-text-wrap">
						<input type="text" name="_sale_price" class="text sale_price" placeholder="<?php _e( 'Sale price', 'colorshop' ); ?>" value="">
					</span>
				</label>
				<br class="clear" />
			</div>

			<?php if ( get_option('colorshop_enable_weight') == "yes" || get_option('colorshop_enable_dimensions') == "yes" ) : ?>
			<div class="dimension_fields">

				<?php if ( get_option('colorshop_enable_weight') == "yes" ) : ?>
					<label>
					    <span class="title"><?php _e( 'Weight', 'colorshop' ); ?></span>
					    <span class="input-text-wrap">
							<input type="text" name="_weight" class="text weight" placeholder="0.00" value="">
						</span>
					</label>
					<br class="clear" />
				<?php endif; ?>

				<?php if ( get_option('colorshop_enable_dimensions') == "yes" ) : ?>
					<div class="inline-edit-group dimensions">
						<div>
						    <span class="title"><?php _e( 'L/W/H', 'colorshop' ); ?></span>
						    <span class="input-text-wrap">
								<input type="text" name="_length" class="text length" placeholder="<?php _e( 'Length', 'colorshop' ); ?>" value="">
								<input type="text" name="_width" class="text width" placeholder="<?php _e( 'Width', 'colorshop' ); ?>" value="">
								<input type="text" name="_height" class="text height" placeholder="<?php _e( 'Height', 'colorshop' ); ?>" value="">
							</span>
						</div>
					</div>
				<?php endif; ?>

			</div>
			<?php endif; ?>

			<label class="alignleft">
			    <span class="title"><?php _e( 'Visibility', 'colorshop' ); ?></span>
			    <span class="input-text-wrap">
			    	<select class="visibility" name="_visibility">
					<?php
						$options = array(
							'visible' => __( 'Catalog &amp; search', 'colorshop' ),
							'catalog' => __( 'Catalog', 'colorshop' ),
							'search' => __( 'Search', 'colorshop' ),
							'hidden' => __( 'Hidden', 'colorshop' )
						);
						foreach ($options as $key => $value) {
							echo '<option value="'.$key.'">'. $value .'</option>';
						}
					?>
					</select>
				</span>
			</label>
			<label class="alignleft featured">
				<input type="checkbox" name="_featured" value="1">
				<span class="checkbox-title"><?php _e( 'Featured', 'colorshop' ); ?></span>
			</label>
			<br class="clear" />
			<label class="alignleft">
			    <span class="title"><?php _e( 'In stock?', 'colorshop' ); ?></span>
			    <span class="input-text-wrap">
			    	<select class="stock_status" name="_stock_status">
					<?php
						$options = array(
							'instock' => __( 'In stock', 'colorshop' ),
							'outofstock' => __( 'Out of stock', 'colorshop' )
						);
						foreach ($options as $key => $value) {
							echo '<option value="'.$key.'">'. $value .'</option>';
						}
					?>
					</select>
				</span>
			</label>

			<div class="stock_fields">

				<?php if (get_option('colorshop_manage_stock')=='yes') : ?>
					<label class="alignleft manage_stock">
						<input type="checkbox" name="_manage_stock" value="1">
						<span class="checkbox-title"><?php _e( 'Manage stock?', 'colorshop' ); ?></span>
					</label>
					<br class="clear" />
					<label class="stock_qty_field">
					    <span class="title"><?php _e( 'Stock Qty', 'colorshop' ); ?></span>
					    <span class="input-text-wrap">
							<input type="text" name="_stock" class="text stock" value="">
						</span>
					</label>
				<?php endif; ?>

			</div>

			<input type="hidden" name="colorshop_quick_edit_nonce" value="<?php echo wp_create_nonce( 'colorshop_quick_edit_nonce' ); ?>" />
		</div>
	</fieldset>
	<?php
}

add_action( 'quick_edit_custom_box',  'colorshop_admin_product_quick_edit', 10, 2 );


/**
 * Custom quick edit - script
 *
 * @access public
 * @param mixed $hook
 * @return void
 */
function colorshop_admin_product_quick_edit_scripts( $hook ) {
	global $colorshop, $post_type;

	if ( $hook == 'edit.php' && $post_type == 'product' )
    	wp_enqueue_script( 'colorshop_quick-edit', $colorshop->plugin_url() . '/assets/js/admin/quick-edit.js', array('jquery') );
}

add_action( 'admin_enqueue_scripts', 'colorshop_admin_product_quick_edit_scripts', 10 );


/**
 * Custom quick edit - save
 *
 * @access public
 * @param mixed $post_id
 * @param mixed $post
 * @return void
 */
function colorshop_admin_product_quick_edit_save( $post_id, $post ) {

	if ( ! $_POST || is_int( wp_is_post_revision( $post_id ) ) || is_int( wp_is_post_autosave( $post_id ) ) ) return $post_id;
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
	if ( ! isset( $_POST['colorshop_quick_edit_nonce'] ) || ! wp_verify_nonce( $_POST['colorshop_quick_edit_nonce'], 'colorshop_quick_edit_nonce' ) ) return $post_id;
	if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;
	if ( $post->post_type != 'product' ) return $post_id;

	global $colorshop, $wpdb;

	$product           = get_product( $post );
	$old_regular_price = $product->regular_price;
	$old_sale_price    = $product->sale_price;

	// Save fields
	if ( isset( $_POST['_sku'] ) ) update_post_meta( $post_id, '_sku', colorshop_clean( $_POST['_sku'] ) );
	if ( isset( $_POST['_weight'] ) ) update_post_meta( $post_id, '_weight', colorshop_clean( $_POST['_weight'] ) );
	if ( isset( $_POST['_length'] ) ) update_post_meta( $post_id, '_length', colorshop_clean( $_POST['_length'] ) );
	if ( isset( $_POST['_width'] ) ) update_post_meta( $post_id, '_width', colorshop_clean( $_POST['_width'] ) );
	if ( isset( $_POST['_height'] ) ) update_post_meta( $post_id, '_height', colorshop_clean( $_POST['_height'] ) );
	if ( isset( $_POST['_stock_status'] ) ) update_post_meta( $post_id, '_stock_status', colorshop_clean( $_POST['_stock_status'] ) );
	if ( isset( $_POST['_visibility'] ) ) update_post_meta( $post_id, '_visibility', colorshop_clean( $_POST['_visibility'] ) );
	if ( isset( $_POST['_featured'] ) ) update_post_meta( $post_id, '_featured', 'yes' ); else update_post_meta( $post_id, '_featured', 'no' );

	if ( $product->is_type('simple') || $product->is_type('external') ) {

		if ( isset( $_POST['_regular_price'] ) ) update_post_meta( $post_id, '_regular_price', colorshop_clean( $_POST['_regular_price'] ) );
		if ( isset( $_POST['_sale_price'] ) ) update_post_meta( $post_id, '_sale_price', colorshop_clean( $_POST['_sale_price'] ) );

		// Handle price - remove dates and set to lowest
		$price_changed = false;

		if ( isset( $_POST['_regular_price'] ) && colorshop_clean( $_POST['_regular_price'] ) != $old_regular_price ) $price_changed = true;
		if ( isset( $_POST['_sale_price'] ) && colorshop_clean( $_POST['_sale_price'] ) != $old_sale_price ) $price_changed = true;

		if ( $price_changed ) {
			update_post_meta( $post_id, '_sale_price_dates_from', '' );
			update_post_meta( $post_id, '_sale_price_dates_to', '' );

			if ( isset( $_POST['_sale_price'] ) && $_POST['_sale_price'] != '' ) {
				update_post_meta( $post_id, '_price', colorshop_clean( $_POST['_sale_price'] ) );
			} else {
				update_post_meta( $post_id, '_price', colorshop_clean( $_POST['_regular_price'] ) );
			}
		}
	}

	// Handle stock
	if ( ! $product->is_type('grouped') ) {
		if ( isset( $_POST['_manage_stock'] ) ) {
			update_post_meta( $post_id, '_manage_stock', 'yes' );
			update_post_meta( $post_id, '_stock', (int) $_POST['_stock'] );
		} else {
			update_post_meta( $post_id, '_manage_stock', 'no' );
			update_post_meta( $post_id, '_stock', '0' );
		}
	}

	// Clear transient
	$colorshop->clear_product_transients( $post_id );
}

add_action( 'save_post', 'colorshop_admin_product_quick_edit_save', 10, 2 );


/**
 * Custom bulk edit - form
 *
 * @access public
 * @param mixed $column_name
 * @param mixed $post_type
 * @return void
 */
function colorshop_admin_product_bulk_edit( $column_name, $post_type ) {
	if ($column_name != 'price' || $post_type != 'product') return;
	?>
    <fieldset class="inline-edit-col-right">
		<div id="colorshop-fields-bulk" class="inline-edit-col">

			<h4><?php _e( 'Product Data', 'colorshop' ); ?></h4>

			<?php do_action( 'colorshop_product_bulk_edit_start' ); ?>

			<div class="inline-edit-group">
				<label class="alignleft">
					<span class="title"><?php _e( 'Price', 'colorshop' ); ?></span>
				    <span class="input-text-wrap">
				    	<select class="change_regular_price change_to" name="change_regular_price">
						<?php
							$options = array(
								'' 	=> __( '鈥� No Change 鈥�', 'colorshop' ),
								'1' => __( 'Change to:', 'colorshop' ),
								'2' => __( 'Increase by (fixed amount or %):', 'colorshop' ),
								'3' => __( 'Decrease by (fixed amount or %):', 'colorshop' )
							);
							foreach ($options as $key => $value) {
								echo '<option value="' . $key . '">' . $value . '</option>';
							}
						?>
						</select>
					</span>
				</label>
			    <label class="alignright">
			    	<input type="text" name="_regular_price" class="text regular_price" placeholder="<?php _e( 'Enter price', 'colorshop' ); ?>" value="" />
			    </label>
			</div>

			<div class="inline-edit-group">
				<label class="alignleft">
				    <span class="title"><?php _e( 'Sale', 'colorshop' ); ?></span>
				    <span class="input-text-wrap">
				    	<select class="change_sale_price change_to" name="change_sale_price">
						<?php
							$options = array(
								'' 	=> __( '鈥� No Change 鈥�', 'colorshop' ),
								'1' => __( 'Change to:', 'colorshop' ),
								'2' => __( 'Increase by (fixed amount or %):', 'colorshop' ),
								'3' => __( 'Decrease by (fixed amount or %):', 'colorshop' ),
								'4' => __( 'Decrease regular price by (fixed amount or %):', 'colorshop' )
							);
							foreach ($options as $key => $value) {
								echo '<option value="' . $key . '">' . $value . '</option>';
							}
						?>
						</select>
					</span>
				</label>
				<label class="alignright">
					<input type="text" name="_sale_price" class="text sale_price" placeholder="<?php _e( 'Enter price', 'colorshop' ); ?>" value="" />
				</label>
			</div>

			<?php if ( get_option('colorshop_enable_weight') == "yes" ) : ?>
				<div class="inline-edit-group">
					<label class="alignleft">
					    <span class="title"><?php _e( 'Weight', 'colorshop' ); ?></span>
					    <span class="input-text-wrap">
					    	<select class="change_weight change_to" name="change_weight">
							<?php
								$options = array(
									'' 	=> __( '鈥� No Change 鈥�', 'colorshop' ),
									'1' => __( 'Change to:', 'colorshop' )
								);
								foreach ($options as $key => $value) {
									echo '<option value="'.$key.'">'. $value .'</option>';
								}
							?>
							</select>
						</span>
					</label>
					<label class="alignright">
						<input type="text" name="_weight" class="text weight" placeholder="0.00" value="">
					</label>
				</div>
			<?php endif; ?>

			<?php if ( get_option('colorshop_enable_dimensions') == "yes" ) : ?>
				<div class="inline-edit-group dimensions">
					<label class="alignleft">
					    <span class="title"><?php _e( 'L/W/H', 'colorshop' ); ?></span>
					    <span class="input-text-wrap">
					    	<select class="change_dimensions change_to" name="change_dimensions">
							<?php
								$options = array(
									'' 	=> __( '鈥� No Change 鈥�', 'colorshop' ),
									'1' => __( 'Change to:', 'colorshop' )
								);
								foreach ($options as $key => $value) {
									echo '<option value="'.$key.'">'. $value .'</option>';
								}
							?>
							</select>
						</span>
					</label>
					<div class="alignright">
						<input type="text" name="_length" class="text length" placeholder="<?php _e( 'Length', 'colorshop' ); ?>" value="">
						<input type="text" name="_width" class="text width" placeholder="<?php _e( 'Width', 'colorshop' ); ?>" value="">
						<input type="text" name="_height" class="text height" placeholder="<?php _e( 'Height', 'colorshop' ); ?>" value="">
					</div>
				</div>
			<?php endif; ?>

			<label>
			    <span class="title"><?php _e( 'Visibility', 'colorshop' ); ?></span>
			    <span class="input-text-wrap">
			    	<select class="visibility" name="_visibility">
					<?php
						$options = array(
							'' => __( '鈥� No Change 鈥�', 'colorshop' ),
							'visible' => __( 'Catalog &amp; search', 'colorshop' ),
							'catalog' => __( 'Catalog', 'colorshop' ),
							'search' => __( 'Search', 'colorshop' ),
							'hidden' => __( 'Hidden', 'colorshop' )
						);
						foreach ($options as $key => $value) {
							echo '<option value="'.$key.'">'. $value .'</option>';
						}
					?>
					</select>
				</span>
			</label>
			<label>
			    <span class="title"><?php _e( 'Featured', 'colorshop' ); ?></span>
			    <span class="input-text-wrap">
			    	<select class="featured" name="_featured">
					<?php
						$options = array(
							'' => __( '鈥� No Change 鈥�', 'colorshop' ),
							'yes' => __( 'Yes', 'colorshop' ),
							'no' => __( 'No', 'colorshop' )
						);
						foreach ($options as $key => $value) {
							echo '<option value="'.$key.'">'. $value .'</option>';
						}
					?>
					</select>
				</span>
			</label>

			<label>
			    <span class="title"><?php _e( 'In stock?', 'colorshop' ); ?></span>
			    <span class="input-text-wrap">
			    	<select class="stock_status" name="_stock_status">
					<?php
						$options = array(
							'' => __( '鈥� No Change 鈥�', 'colorshop' ),
							'instock' => __( 'In stock', 'colorshop' ),
							'outofstock' => __( 'Out of stock', 'colorshop' )
						);
						foreach ($options as $key => $value) {
							echo '<option value="'.$key.'">'. $value .'</option>';
						}
					?>
					</select>
				</span>
			</label>
			<?php if (get_option('colorshop_manage_stock')=='yes') : ?>
				<label>
				    <span class="title"><?php _e( 'Manage stock?', 'colorshop' ); ?></span>
				    <span class="input-text-wrap">
				    	<select class="manage_stock" name="_manage_stock">
						<?php
							$options = array(
								'' => __( '鈥� No Change 鈥�', 'colorshop' ),
								'yes' => __( 'Yes', 'colorshop' ),
								'no' => __( 'No', 'colorshop' )
							);
							foreach ($options as $key => $value) {
								echo '<option value="'.$key.'">'. $value .'</option>';
							}
						?>
						</select>
					</span>
				</label>

				<div class="inline-edit-group dimensions">
					<label class="alignleft stock_qty_field">
					    <span class="title"><?php _e( 'Stock Qty', 'colorshop' ); ?></span>
					    <span class="input-text-wrap">
					    	<select class="change_stock change_to" name="change_stock">
							<?php
								$options = array(
									'' 	=> __( '鈥� No Change 鈥�', 'colorshop' ),
									'1' => __( 'Change to:', 'colorshop' )
								);
								foreach ($options as $key => $value) {
									echo '<option value="'.$key.'">'. $value .'</option>';
								}
							?>
							</select>
						</span>
					</label>
					<label class="alignright">
						<input type="text" name="_stock" class="text stock" placeholder="<?php _e( 'Stock Qty', 'colorshop' ); ?>" value="">
					</label>
				</div>

			<?php endif; ?>

			do_action( 'colorshop_product_bulk_edit_end' );

			<input type="hidden" name="colorshop_bulk_edit_nonce" value="<?php echo wp_create_nonce( 'colorshop_bulk_edit_nonce' ); ?>" />
		</div>
	</fieldset>
	<?php
}

add_action('bulk_edit_custom_box',  'colorshop_admin_product_bulk_edit', 10, 2);


/**
 * Custom bulk edit - save
 *
 * @access public
 * @param mixed $post_id
 * @param mixed $post
 * @return void
 */
function colorshop_admin_product_bulk_edit_save( $post_id, $post ) {

	if ( is_int( wp_is_post_revision( $post_id ) ) ) return;
	if ( is_int( wp_is_post_autosave( $post_id ) ) ) return;
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
	if ( ! isset( $_REQUEST['colorshop_bulk_edit_nonce'] ) || ! wp_verify_nonce( $_REQUEST['colorshop_bulk_edit_nonce'], 'colorshop_bulk_edit_nonce' ) ) return $post_id;
	if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;
	if ( $post->post_type != 'product' ) return $post_id;

	global $colorshop, $wpdb;

	$product           = get_product( $post );
	$old_regular_price = $product->regular_price;
	$old_sale_price    = $product->sale_price;

	// Save fields
	if ( ! empty( $_REQUEST['change_weight'] ) && isset( $_REQUEST['_weight'] ) )
		update_post_meta( $post_id, '_weight', esc_html( stripslashes( $_REQUEST['_weight'] ) ) );

	if ( ! empty( $_REQUEST['change_dimensions'] ) ) {
		if ( isset( $_REQUEST['_length'] ) )
			update_post_meta( $post_id, '_length', esc_html( stripslashes( $_REQUEST['_length'] ) ) );
		if ( isset( $_REQUEST['_width'] ) )
			update_post_meta( $post_id, '_width', esc_html( stripslashes( $_REQUEST['_width'] ) ) );
		if ( isset( $_REQUEST['_height'] ) )
			update_post_meta( $post_id, '_height', esc_html( stripslashes( $_REQUEST['_height'] ) ) );
	}

	if ( ! empty( $_REQUEST['_stock_status'] ) )
		update_post_meta( $post_id, '_stock_status', stripslashes( $_REQUEST['_stock_status'] ) );

	if ( ! empty( $_REQUEST['_visibility'] ) )
		update_post_meta( $post_id, '_visibility', stripslashes( $_REQUEST['_visibility'] ) );

	if ( ! empty( $_REQUEST['_featured'] ) )
		update_post_meta( $post_id, '_featured', stripslashes( $_REQUEST['_featured'] ) );

	// Handle price - remove dates and set to lowest
	if ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) ) {

		$price_changed = false;

		if ( ! empty( $_REQUEST['change_regular_price'] ) ) {

			$change_regular_price = absint( $_REQUEST['change_regular_price'] );
			$regular_price = esc_attr( stripslashes( $_REQUEST['_regular_price'] ) );

			switch ( $change_regular_price ) {
				case 1 :
					$new_price = $regular_price;
				break;
				case 2 :
					if ( strstr( $regular_price, '%' ) ) {
						$percent = str_replace( '%', '', $regular_price ) / 100;
						$new_price = $old_regular_price + ( $old_regular_price * $percent );
					} else {
						$new_price = $old_regular_price + $regular_price;
					}
				break;
				case 3 :
					if ( strstr( $regular_price, '%' ) ) {
						$percent = str_replace( '%', '', $regular_price ) / 100;
						$new_price = $old_regular_price - ( $old_regular_price * $percent );
					} else {
						$new_price = $old_regular_price - $regular_price;
					}
				break;
			}

			if ( isset( $new_price ) && $new_price != $old_regular_price ) {
				$price_changed = true;
				update_post_meta( $post_id, '_regular_price', $new_price );
				$product->regular_price = $new_price;
			}
		}

		if ( ! empty( $_REQUEST['change_sale_price'] ) ) {

			$change_sale_price = absint( $_REQUEST['change_sale_price'] );
			$sale_price = esc_attr( stripslashes( $_REQUEST['_sale_price'] ) );

			switch ( $change_sale_price ) {
				case 1 :
					$new_price = $sale_price;
				break;
				case 2 :
					if ( strstr( $sale_price, '%' ) ) {
						$percent = str_replace( '%', '', $sale_price ) / 100;
						$new_price = $old_sale_price + ( $old_sale_price * $percent );
					} else {
						$new_price = $old_sale_price + $sale_price;
					}
				break;
				case 3 :
					if ( strstr( $sale_price, '%' ) ) {
						$percent = str_replace( '%', '', $sale_price ) / 100;
						$new_price = $old_sale_price - ( $old_sale_price * $percent );
					} else {
						$new_price = $old_sale_price - $sale_price;
					}
				break;
				case 4 :
					if ( strstr( $sale_price, '%' ) ) {
						$percent = str_replace( '%', '', $sale_price ) / 100;
						$new_price = $product->regular_price - ( $product->regular_price * $percent );
					} else {
						$new_price = $product->regular_price - $sale_price;
					}
				break;
			}

			if ( isset( $new_price ) && $new_price != $old_sale_price ) {
				$price_changed = true;
				update_post_meta( $post_id, '_sale_price', $new_price );
				$product->sale_price = $new_price;
			}
		}

		if ( $price_changed ) {
			update_post_meta( $post_id, '_sale_price_dates_from', '' );
			update_post_meta( $post_id, '_sale_price_dates_to', '' );

			if ( $product->regular_price < $product->sale_price ) {
				$product->sale_price = '';
				update_post_meta( $post_id, '_sale_price', '' );
			}

			if ( $product->sale_price ) {
				update_post_meta( $post_id, '_price', $product->sale_price );
			} else {
				update_post_meta( $post_id, '_price', $product->regular_price );
			}
		}
	}

	// Handle stock
	if ( ! $product->is_type( 'grouped' ) ) {

		if ( ! empty( $_REQUEST['change_stock'] ) ) {
			update_post_meta( $post_id, '_stock', (int) $_REQUEST['_stock'] );
			update_post_meta( $post_id, '_manage_stock', 'yes' );
		}

		if ( ! empty( $_REQUEST['_manage_stock'] ) ) {

			if ( $_REQUEST['_manage_stock'] == 'yes' ) {
				update_post_meta( $post_id, '_manage_stock', 'yes' );
			} else {
				update_post_meta( $post_id, '_manage_stock', 'no' );
				update_post_meta( $post_id, '_stock', '0' );
			}
		}

	}

	do_action( 'colorshop_product_bulk_edit_save', $product );

	// Clear transient
	$colorshop->clear_product_transients( $post_id );
}

add_action( 'save_post', 'colorshop_admin_product_bulk_edit_save', 10, 2 );


/**
 * Product sorting link
 *
 * Based on Simple Page Ordering by 10up (http://wordpress.org/extend/plugins/simple-page-ordering/)
 *
 * @access public
 * @param mixed $views
 * @return void
 */
function colorshop_default_sorting_link( $views ) {
	global $post_type, $wp_query;

	if ( ! current_user_can('edit_others_pages') ) return $views;
	$class = ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == 'menu_order title' ) ? 'current' : '';
	$query_string = remove_query_arg(array( 'orderby', 'order' ));
	$query_string = add_query_arg( 'orderby', urlencode('menu_order title'), $query_string );
	$query_string = add_query_arg( 'order', urlencode('ASC'), $query_string );
	$views['byorder'] = '<a href="'. $query_string . '" class="' . $class . '">' . __( 'Sort Products', 'colorshop' ) . '</a>';

	return $views;
}

add_filter( 'views_edit-product', 'colorshop_default_sorting_link' );


/**
 * colorshop_disable_checked_ontop function.
 *
 * @access public
 * @param mixed $args
 * @param mixed $post_id
 * @return void
 */
function colorshop_disable_checked_ontop( $args ) {

	if ( $args['taxonomy'] == 'product_cat' ) {
		$args['checked_ontop'] = false;
	}

	return $args;
}

add_filter( 'wp_terms_checklist_args', 'colorshop_disable_checked_ontop' );

/**
 * Change label for insert buttons.
 *
 * @access public
 * @param mixed $translation
 * @param mixed $original
 * @return void
 */
function colorshop_change_insert_into_post( $strings ) {
	global $post_type;

	if ( $post_type == 'product' ) {
		$strings['insertIntoPost']     = __( 'Insert into product', 'colorshop' );
		$strings['uploadedToThisPost'] = __( 'Uploaded to this product', 'colorshop' );
	}

	return $strings;
}

add_filter( 'media_view_strings', 'colorshop_change_insert_into_post' );
Return current item: ColorShop