';
break;
}
}
}
/**
* Search custom fields as well as content.
* @param WP_Query $wp
*/
public function shop_order_search_custom_fields( $wp ) {
global $pagenow, $wpdb;
if ( 'edit.php' != $pagenow || empty( $wp->query_vars['s'] ) || $wp->query_vars['post_type'] != 'shop_order' ) {
return;
}
$search_fields = array_map( 'wc_clean', apply_filters( 'woocommerce_shop_order_search_fields', array(
'_order_key',
'_billing_company',
'_billing_address_1',
'_billing_address_2',
'_billing_city',
'_billing_postcode',
'_billing_country',
'_billing_state',
'_billing_email',
'_billing_phone',
'_shipping_address_1',
'_shipping_address_2',
'_shipping_city',
'_shipping_postcode',
'_shipping_country',
'_shipping_state'
) ) );
$search_order_id = str_replace( 'Order #', '', $_GET['s'] );
if ( ! is_numeric( $search_order_id ) ) {
$search_order_id = 0;
}
// Search orders
$post_ids = array_unique( array_merge(
$wpdb->get_col(
$wpdb->prepare( "
SELECT DISTINCT p1.post_id
FROM {$wpdb->postmeta} p1
INNER JOIN {$wpdb->postmeta} p2 ON p1.post_id = p2.post_id
WHERE
( p1.meta_key = '_billing_first_name' AND p2.meta_key = '_billing_last_name' AND CONCAT(p1.meta_value, ' ', p2.meta_value) LIKE '%%%s%%' )
OR
( p1.meta_key = '_shipping_first_name' AND p2.meta_key = '_shipping_last_name' AND CONCAT(p1.meta_value, ' ', p2.meta_value) LIKE '%%%s%%' )
OR
( p1.meta_key IN ('" . implode( "','", $search_fields ) . "') AND p1.meta_value LIKE '%%%s%%' )
",
esc_attr( $_GET['s'] ), esc_attr( $_GET['s'] ), esc_attr( $_GET['s'] )
)
),
$wpdb->get_col(
$wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
WHERE order_item_name LIKE '%%%s%%'
",
esc_attr( $_GET['s'] )
)
),
array( $search_order_id )
) );
// Remove s - we don't want to search order name
unset( $wp->query_vars['s'] );
// so we know we're doing this
$wp->query_vars['shop_order_search'] = true;
// Search by found posts
$wp->query_vars['post__in'] = $post_ids;
}
/**
* Change the label when searching orders.
*
* @param mixed $query
* @return string
*/
public function shop_order_search_label( $query ) {
global $pagenow, $typenow;
if ( 'edit.php' != $pagenow ) {
return $query;
}
if ( $typenow != 'shop_order' ) {
return $query;
}
if ( ! get_query_var( 'shop_order_search' ) ) {
return $query;
}
return wp_unslash( $_GET['s'] );
}
/**
* Query vars for custom searches.
*
* @param mixed $public_query_vars
* @return array
*/
public function add_custom_query_var( $public_query_vars ) {
$public_query_vars[] = 'sku';
$public_query_vars[] = 'shop_order_search';
return $public_query_vars;
}
/**
* Filters for post types.
*/
public function restrict_manage_posts() {
global $typenow;
if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
$this->shop_order_filters();
} elseif ( 'product' == $typenow ) {
$this->product_filters();
} elseif( 'shop_coupon' == $typenow ) {
$this->shop_coupon_filters();
}
}
/**
* Show a category filter box.
*/
public function product_filters() {
global $wp_query;
// Category Filtering
wc_product_dropdown_categories();
// Type filtering
$terms = get_terms( 'product_type' );
$output = '';
echo apply_filters( 'woocommerce_product_filters', $output );
}
/**
* Show custom filters to filter coupons by type.
*/
public function shop_coupon_filters() {
?>
display_name ) . ' (#' . absint( $user->ID ) . ' – ' . esc_html( $user->user_email ) . ')';
}
?>
'_price',
'orderby' => 'meta_value_num'
) );
}
if ( 'featured' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_featured',
'orderby' => 'meta_value'
) );
}
if ( 'sku' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_sku',
'orderby' => 'meta_value'
) );
}
}
} elseif ( 'shop_coupon' === $typenow ) {
if ( ! empty( $_GET['coupon_type'] ) ) {
$vars['meta_key'] = 'discount_type';
$vars['meta_value'] = wc_clean( $_GET['coupon_type'] );
}
} elseif ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
// Filter the orders by the posted customer.
if ( isset( $_GET['_customer_user'] ) && $_GET['_customer_user'] > 0 ) {
$vars['meta_query'] = array(
array(
'key' => '_customer_user',
'value' => (int) $_GET['_customer_user'],
'compare' => '='
)
);
}
// Sorting
if ( isset( $vars['orderby'] ) ) {
if ( 'order_total' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_order_total',
'orderby' => 'meta_value_num'
) );
}
}
// Status
if ( ! isset( $vars['post_status'] ) ) {
$post_statuses = wc_get_order_statuses();
foreach ( $post_statuses as $status => $value ) {
if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
unset( $post_statuses[ $status ] );
}
}
$vars['post_status'] = array_keys( $post_statuses );
}
}
return $vars;
}
/**
* Filter the products in admin based on options.
*
* @param mixed $query
*/
public function product_filters_query( $query ) {
global $typenow, $wp_query;
if ( 'product' == $typenow ) {
if ( isset( $query->query_vars['product_type'] ) ) {
// Subtypes
if ( 'downloadable' == $query->query_vars['product_type'] ) {
$query->query_vars['product_type'] = '';
$query->is_tax = false;
$query->query_vars['meta_value'] = 'yes';
$query->query_vars['meta_key'] = '_downloadable';
} elseif ( 'virtual' == $query->query_vars['product_type'] ) {
$query->query_vars['product_type'] = '';
$query->is_tax = false;
$query->query_vars['meta_value'] = 'yes';
$query->query_vars['meta_key'] = '_virtual';
}
}
// Categories
if ( isset( $_GET['product_cat'] ) && '0' == $_GET['product_cat'] ) {
$query->query_vars['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => get_terms( 'product_cat', array( 'fields' => 'ids' ) ),
'operator' => 'NOT IN'
);
}
}
}
/**
* Search by SKU or ID for products.
*
* @param string $where
* @return string
*/
public function product_search( $where ) {
global $pagenow, $wpdb, $wp;
if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'product' != $wp->query_vars['post_type'] ) {
return $where;
}
$search_ids = array();
$terms = explode( ',', $wp->query_vars['s'] );
foreach ( $terms as $term ) {
if ( is_numeric( $term ) ) {
$search_ids[] = $term;
}
// Attempt to get a SKU
$sku_to_id = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_parent FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE meta_key='_sku' AND meta_value LIKE %s;", '%' . $wpdb->esc_like( wc_clean( $term ) ) . '%' ) );
$sku_to_id = array_merge( wp_list_pluck( $sku_to_id, 'ID' ), wp_list_pluck( $sku_to_id, 'post_parent' ) );
if ( $sku_to_id && sizeof( $sku_to_id ) > 0 ) {
$search_ids = array_merge( $search_ids, $sku_to_id );
}
}
$search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) );
if ( sizeof( $search_ids ) > 0 ) {
$where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where );
}
return $where;
}
/**
* Disable the auto-save functionality for Orders.
*/
public function disable_autosave() {
global $post;
if ( $post && in_array( get_post_type( $post->ID ), wc_get_order_types( 'order-meta-boxes' ) ) ) {
wp_dequeue_script( 'autosave' );
}
}
/**
* Removes variations etc belonging to a deleted post, and clears transients.
*
* @param mixed $id ID of post being deleted
*/
public function delete_post( $id ) {
global $woocommerce, $wpdb;
if ( ! current_user_can( 'delete_posts' ) ) {
return;
}
if ( $id > 0 ) {
$post_type = get_post_type( $id );
switch ( $post_type ) {
case 'product' :
$child_product_variations = get_children( 'post_parent=' . $id . '&post_type=product_variation' );
if ( ! empty( $child_product_variations ) ) {
foreach ( $child_product_variations as $child ) {
wp_delete_post( $child->ID, true );
}
}
$child_products = get_children( 'post_parent=' . $id . '&post_type=product' );
if ( ! empty( $child_products ) ) {
foreach ( $child_products as $child ) {
$child_post = array();
$child_post['ID'] = $child->ID;
$child_post['post_parent'] = 0;
wp_update_post( $child_post );
}
}
if ( $parent_id = wp_get_post_parent_id( $id ) ) {
wc_delete_product_transients( $parent_id );
}
break;
case 'product_variation' :
wc_delete_product_transients( wp_get_post_parent_id( $id ) );
break;
case 'shop_order' :
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
if ( ! is_null( $refunds ) ) {
foreach ( $refunds as $refund ) {
wp_delete_post( $refund->ID, true );
}
}
break;
}
}
}
/**
* woocommerce_trash_post function.
*
* @param mixed $id
*/
public function trash_post( $id ) {
global $wpdb;
if ( $id > 0 ) {
$post_type = get_post_type( $id );
if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) {
// Delete count - meta doesn't work on trashed posts
$user_id = get_post_meta( $id, '_customer_user', true );
if ( $user_id > 0 ) {
update_user_meta( $user_id, '_order_count', '' );
update_user_meta( $user_id, '_money_spent', '' );
}
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
foreach ( $refunds as $refund ) {
$wpdb->update( $wpdb->posts, array( 'post_status' => 'trash' ), array( 'ID' => $refund->ID ) );
}
delete_transient( 'woocommerce_processing_order_count' );
wc_delete_shop_order_transients( $id );
}
}
}
/**
* woocommerce_untrash_post function.
*
* @param mixed $id
*/
public function untrash_post( $id ) {
global $wpdb;
if ( $id > 0 ) {
$post_type = get_post_type( $id );
if ( in_array( $post_type, wc_get_order_types( 'order-count' ) ) ) {
// Delete count - meta doesn't work on trashed posts
$user_id = get_post_meta( $id, '_customer_user', true );
if ( $user_id > 0 ) {
update_user_meta( $user_id, '_order_count', '' );
update_user_meta( $user_id, '_money_spent', '' );
}
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
foreach ( $refunds as $refund ) {
$wpdb->update( $wpdb->posts, array( 'post_status' => 'wc-completed' ), array( 'ID' => $refund->ID ) );
}
delete_transient( 'woocommerce_processing_order_count' );
wc_delete_shop_order_transients( $id );
}
}
}
/**
* Remove item meta on permanent deletion.
*/
public function delete_order_items( $postid ) {
global $wpdb;
if ( in_array( get_post_type( $postid ), wc_get_order_types() ) ) {
do_action( 'woocommerce_delete_order_items', $postid );
$wpdb->query( "
DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta
FROM {$wpdb->prefix}woocommerce_order_items
JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id
WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}';
" );
do_action( 'woocommerce_deleted_order_items', $postid );
}
}
/**
* Remove downloadable permissions on permanent order deletion.
*/
public function delete_order_downloadable_permissions( $postid ) {
global $wpdb;
if ( in_array( get_post_type( $postid ), wc_get_order_types() ) ) {
do_action( 'woocommerce_delete_order_downloadable_permissions', $postid );
$wpdb->query( $wpdb->prepare( "
DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d
", $postid ) );
do_action( 'woocommerce_deleted_order_downloadable_permissions', $postid );
}
}
/**
* Change title boxes in admin.
* @param string $text
* @param object $post
* @return string
*/
public function enter_title_here( $text, $post ) {
switch ( $post->post_type ) {
case 'product' :
$text = __( 'Product name', 'woocommerce' );
break;
case 'shop_coupon' :
$text = __( 'Coupon code', 'woocommerce' );
break;
}
return $text;
}
/**
* Print coupon description textarea field.
* @param WP_Post $post
*/
public function edit_form_after_title( $post ) {
if ( 'shop_coupon' === $post->post_type ) {
?>
labels->singular_name );
$strings['uploadedToThisPost'] = sprintf( __( 'Uploaded to this %s', 'woocommerce' ), $obj->labels->singular_name );
}
return $strings;
}
/**
* Hidden default Meta-Boxes.
* @param array $hidden
* @param object $screen
* @return array
*/
public function hidden_meta_boxes( $hidden, $screen ) {
if ( 'product' === $screen->post_type && 'post' === $screen->base ) {
$hidden = array_merge( $hidden, array( 'postcustom' ) );
}
return $hidden;
}
/**
* Output product visibility options.
*/
public function product_data_visibility() {
global $post;
if ( 'product' != $post->post_type ) {
return;
}
$current_visibility = ( $current_visibility = get_post_meta( $post->ID, '_visibility', true ) ) ? $current_visibility : apply_filters( 'woocommerce_product_visibility_default' , 'visible' );
$current_featured = ( $current_featured = get_post_meta( $post->ID, '_featured', true ) ) ? $current_featured : 'no';
$visibility_options = apply_filters( 'woocommerce_product_visibility_options', array(
'visible' => __( 'Catalog/search', 'woocommerce' ),
'catalog' => __( 'Catalog', 'woocommerce' ),
'search' => __( 'Search', 'woocommerce' ),
'hidden' => __( 'Hidden', 'woocommerce' )
) );
?>
' . __( 'Choose where this product should be displayed in your catalog. The product will always be accessible directly.', 'woocommerce' ) . '';
foreach ( $visibility_options as $name => $label ) {
echo ' ';
}
echo '
' . __( 'Enable this option to feature this product.', 'woocommerce' ) . '
';
echo ' ';
?>
get_files() );
$updated_download_ids = array_keys( (array) $downloadable_files );
$new_download_ids = array_filter( array_diff( $updated_download_ids, $existing_download_ids ) );
$removed_download_ids = array_filter( array_diff( $existing_download_ids, $updated_download_ids ) );
if ( ! empty( $new_download_ids ) || ! empty( $removed_download_ids ) ) {
// determine whether downloadable file access has been granted via the typical order completion, or via the admin ajax method
$existing_permissions = $wpdb->get_results( $wpdb->prepare( "SELECT * from {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE product_id = %d GROUP BY order_id", $product_id ) );
foreach ( $existing_permissions as $existing_permission ) {
$order = wc_get_order( $existing_permission->order_id );
if ( ! empty( $order->id ) ) {
// Remove permissions
if ( ! empty( $removed_download_ids ) ) {
foreach ( $removed_download_ids as $download_id ) {
if ( apply_filters( 'woocommerce_process_product_file_download_paths_remove_access_to_old_file', true, $download_id, $product_id, $order ) ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->id, $product_id, $download_id ) );
}
}
}
// Add permissions
if ( ! empty( $new_download_ids ) ) {
foreach ( $new_download_ids as $download_id ) {
if ( apply_filters( 'woocommerce_process_product_file_download_paths_grant_access_to_new_file', true, $download_id, $product_id, $order ) ) {
// grant permission if it doesn't already exist
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT 1=1 FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s", $order->id, $product_id, $download_id ) ) ) {
wc_downloadable_file_permission( $download_id, $product_id, $order );
}
}
}
}
}
}
}
}
/**
* Disable DFW feature pointer.
*/
public function disable_dfw_feature_pointer() {
$screen = get_current_screen();
if ( 'product' === $screen->id && 'post' === $screen->base ) {
remove_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_wp410_dfw' ) );
}
}
}
endif;
new WC_Admin_Post_Types();