';
}
}
}
/**
* Search custom fields as well as content.
*
* @access public
* @param WP_Query $wp
* @return void
*/
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 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.
*
* @access public
* @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.
*
* @access public
* @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, $wp_query;
switch ( $typenow ) {
case 'product' :
$this->product_filters();
break;
case 'shop_coupon' :
$this->shop_coupon_filters();
break;
case 'shop_order' :
$this->shop_order_filters();
break;
default :
break;
}
}
/**
* 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() {
?>
'_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 ( 'shop_order' === $typenow ) {
// Filter the orders by the posted customer.
if ( isset( $_GET['_customer_user'] ) && $_GET['_customer_user'] > 0 ) {
$vars['meta_key'] = '_customer_user';
$vars['meta_value'] = (int) $_GET['_customer_user'];
}
// 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'] ) ) {
$vars['post_status'] = array_keys( wc_get_order_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->query_vars['meta_value'] = 'yes';
$query->query_vars['meta_key'] = '_downloadable';
} elseif ( 'virtual' == $query->query_vars['product_type'] ) {
$query->query_vars['product_type'] = '';
$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_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_sku' AND meta_value LIKE '%%%s%%';", wc_clean( $term ) ) );
if ( $sku_to_id && sizeof( $sku_to_id ) > 0 ) {
$search_ids = array_merge( $search_ids, $sku_to_id );
}
}
$search_ids = array_filter( array_map( 'absint', $search_ids ) );
if ( sizeof( $search_ids ) > 0 ) {
$where = str_replace( ')))', ") OR ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . "))))", $where );
}
return $where;
}
/**
* Change messages when a post type is updated.
*
* @param array $messages
* @return array
*/
public function post_updated_messages( $messages ) {
global $post, $post_ID;
$messages['product'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __( 'Product updated. View Product', 'woocommerce' ), esc_url( get_permalink($post_ID) ) ),
2 => __( 'Custom field updated.', 'woocommerce' ),
3 => __( 'Custom field deleted.', 'woocommerce' ),
4 => __( 'Product updated.', 'woocommerce' ),
5 => isset($_GET['revision']) ? sprintf( __( 'Product restored to revision from %s', 'woocommerce' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __( 'Product published. View Product', 'woocommerce' ), esc_url( get_permalink($post_ID) ) ),
7 => __( 'Product saved.', 'woocommerce' ),
8 => sprintf( __( 'Product submitted. Preview Product', 'woocommerce' ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __( 'Product scheduled for: %1$s. Preview Product', 'woocommerce' ),
date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __( 'Product draft updated. Preview Product', 'woocommerce' ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
$messages['shop_order'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Order updated.', 'woocommerce' ),
2 => __( 'Custom field updated.', 'woocommerce' ),
3 => __( 'Custom field deleted.', 'woocommerce' ),
4 => __( 'Order updated.', 'woocommerce' ),
5 => isset($_GET['revision']) ? sprintf( __( 'Order restored to revision from %s', 'woocommerce' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Order updated.', 'woocommerce' ),
7 => __( 'Order saved.', 'woocommerce' ),
8 => __( 'Order submitted.', 'woocommerce' ),
9 => sprintf( __( 'Order scheduled for: %1$s.', 'woocommerce' ),
date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( $post->post_date ) ) ),
10 => __( 'Order draft updated.', 'woocommerce' )
);
$messages['shop_coupon'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Coupon updated.', 'woocommerce' ),
2 => __( 'Custom field updated.', 'woocommerce' ),
3 => __( 'Custom field deleted.', 'woocommerce' ),
4 => __( 'Coupon updated.', 'woocommerce' ),
5 => isset($_GET['revision']) ? sprintf( __( 'Coupon restored to revision from %s', 'woocommerce' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Coupon updated.', 'woocommerce' ),
7 => __( 'Coupon saved.', 'woocommerce' ),
8 => __( 'Coupon submitted.', 'woocommerce' ),
9 => sprintf( __( 'Coupon scheduled for: %1$s.', 'woocommerce' ),
date_i18n( __( 'M j, Y @ G:i', 'woocommerce' ), strtotime( $post->post_date ) ) ),
10 => __( 'Coupon draft updated.', 'woocommerce' )
);
return $messages;
}
/**
* Disable the auto-save functionality for Orders.
*
* @access public
* @return void
*/
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
*
* @access public
* @param mixed $id ID of post being deleted
* @return void
*/
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 ( $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 ( $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 ) );
foreach ( $refunds as $refund ) {
wp_delete_post( $refund->ID, true );
}
break;
}
}
}
/**
* woocommerce_trash_post function.
*
* @access public
* @param mixed $id
* @return void
*/
public function trash_post( $id ) {
global $wpdb;
if ( $id > 0 ) {
$post_type = get_post_type( $id );
if ( 'shop_order' == $post_type ) {
// 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' );
}
}
}
/**
* woocommerce_untrash_post function.
*
* @access public
* @param mixed $id
* @return void
*/
public function untrash_post( $id ) {
global $wpdb;
if ( $id > 0 ) {
$post_type = get_post_type( $id );
if ( 'shop_order' == $post_type ) {
// 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' );
}
}
}
/**
* Remove item meta on permanent deletion
*
* @access public
* @return void
**/
public function delete_order_items( $postid ) {
global $wpdb;
if ( get_post_type( $postid ) == 'shop_order' ) {
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 );
}
}
/**
* Change text without slow getext filter
*/
public function change_featured_image_text() {
global $l10n;
if ( isset( $l10n['default'] ) && isset( $l10n['default']->entries ) ) {
foreach ( $l10n['default']->entries as $entry_key => $entries ) {
foreach ( $entries->translations as $key => $value ) {
if ( 'Featured Image' == $value ) {
$l10n['default']->entries[ $entry_key ]->translations[ $key ] = __( 'Product Image', 'woocommerce' );
} elseif ( 'Remove featured image' == $value ) {
$l10n['default']->entries[ $entry_key ]->translations[ $key ] = __( 'Remove product image', 'woocommerce' );
} elseif ( 'Set featured image' == $value ) {
$l10n['default']->entries[ $entry_key ]->translations[ $key ] = __( 'Set product image', 'woocommerce' );
}
}
}
}
}
/**
* Change "Featured Image" to "Product Image" throughout media modals.
*
* @param array $strings Array of strings to translate.
* @param object $post
* @return array
*/
public function media_view_strings( $strings = array(), $post = null ) {
if ( isset( $post->post_type ) && 'product' == $post->post_type ) {
$strings['setFeaturedImageTitle'] = __( 'Set product image', 'woocommerce' );
$strings['setFeaturedImage'] = __( 'Set product image', 'woocommerce' );
}
return $strings;
}
/**
* 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' :
return __( 'Product name', 'woocommerce' );
break;
case 'shop_coupon' :
return __( '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;
}
/**
* Output product visibility options.
*
* @access public
* @return void
*/
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' )
) );
?>
' . __( 'Define the loops this product should be visible in. The product will still 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 ( $new_download_ids || $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 ( $order->id ) {
// Remove permissions
if ( $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 ( $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 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 );
}
}
}
}
}
}
}
}
}
endif;
new WC_Admin_Post_Types();