2013-07-24 16:01:36 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Admin Functions
2013-07-24 16:01:36 +00:00
*
2015-06-16 16:08:30 +00:00
* @ package WooCommerce / Admin / Functions
* @ version 2.4 . 0
2013-07-24 16:01:36 +00:00
*/
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2019-01-03 13:40:22 +00:00
exit ;
2014-09-20 19:52:30 +00:00
}
2013-07-24 16:01:36 +00:00
/**
2015-11-03 13:31:20 +00:00
* Get all WooCommerce screen ids .
2013-07-24 16:01:36 +00:00
*
* @ return array
*/
2013-07-25 15:29:25 +00:00
function wc_get_screen_ids () {
2014-11-30 06:52:32 +00:00
2014-02-14 10:57:48 +00:00
$wc_screen_id = sanitize_title ( __ ( 'WooCommerce' , 'woocommerce' ) );
2014-07-20 23:53:43 +00:00
$screen_ids = array (
'toplevel_page_' . $wc_screen_id ,
$wc_screen_id . '_page_wc-reports' ,
2015-12-08 12:45:18 +00:00
$wc_screen_id . '_page_wc-shipping' ,
2014-07-20 23:53:43 +00:00
$wc_screen_id . '_page_wc-settings' ,
$wc_screen_id . '_page_wc-status' ,
$wc_screen_id . '_page_wc-addons' ,
2015-02-24 12:29:03 +00:00
'toplevel_page_wc-reports' ,
2014-07-20 23:53:43 +00:00
'product_page_product_attributes' ,
2017-05-12 19:40:54 +00:00
'product_page_product_exporter' ,
2017-05-17 13:16:38 +00:00
'product_page_product_importer' ,
2014-07-20 23:53:43 +00:00
'edit-product' ,
'product' ,
'edit-shop_coupon' ,
'shop_coupon' ,
'edit-product_cat' ,
'edit-product_tag' ,
2015-02-15 20:36:18 +00:00
'profile' ,
2016-08-27 02:08:49 +00:00
'user-edit' ,
2014-07-20 23:53:43 +00:00
);
foreach ( wc_get_order_types () as $type ) {
$screen_ids [] = $type ;
$screen_ids [] = 'edit-' . $type ;
}
2019-01-03 13:40:22 +00:00
$attributes = wc_get_attribute_taxonomies ();
if ( $attributes ) {
2017-05-18 14:22:12 +00:00
foreach ( $attributes as $attribute ) {
$screen_ids [] = 'edit-' . wc_attribute_taxonomy_name ( $attribute -> attribute_name );
}
}
2014-07-20 23:53:43 +00:00
return apply_filters ( 'woocommerce_screen_ids' , $screen_ids );
2013-07-25 15:29:25 +00:00
}
/**
* Create a page and store the ID in an option .
*
2019-01-03 13:40:22 +00:00
* @ param mixed $slug Slug for the new page .
* @ param string $option Option name to store the page ' s ID .
* @ param string $page_title ( default : '' ) Title for the new page .
* @ param string $page_content ( default : '' ) Content for the new page .
* @ param int $post_parent ( default : 0 ) Parent for the new page .
* @ return int page ID .
2013-07-25 15:29:25 +00:00
*/
function wc_create_page ( $slug , $option = '' , $page_title = '' , $page_content = '' , $post_parent = 0 ) {
2014-07-20 23:53:43 +00:00
global $wpdb ;
2013-07-25 15:29:25 +00:00
2018-03-05 18:59:17 +00:00
$option_value = get_option ( $option );
2015-06-16 11:31:15 +00:00
2019-01-03 13:40:22 +00:00
if ( $option_value > 0 ) {
$page_object = get_post ( $option_value );
if ( $page_object && 'page' === $page_object -> post_type && ! in_array ( $page_object -> post_status , array ( 'pending' , 'trash' , 'future' , 'auto-draft' ), true ) ) {
// Valid page is already in place.
2015-09-07 13:17:22 +00:00
return $page_object -> ID ;
2015-06-16 11:31:15 +00:00
}
2014-09-22 18:30:07 +00:00
}
2015-06-16 16:08:30 +00:00
2014-07-20 23:53:43 +00:00
if ( strlen ( $page_content ) > 0 ) {
2019-01-03 13:40:22 +00:00
// Search for an existing page with the specified page content (typically a shortcode).
2018-11-19 16:25:06 +00:00
$shortcode = str_replace ( array ( '<!-- wp:shortcode -->' , '<!-- /wp:shortcode -->' ), '' , $page_content );
$valid_page_found = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1; " , " % { $shortcode } % " ) );
2014-07-20 23:53:43 +00:00
} else {
2019-01-03 13:40:22 +00:00
// Search for an existing page with the specified page slug.
2015-09-07 13:17:22 +00:00
$valid_page_found = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1; " , $slug ) );
2014-07-20 23:53:43 +00:00
}
2013-07-25 15:29:25 +00:00
2015-09-07 13:17:22 +00:00
$valid_page_found = apply_filters ( 'woocommerce_create_page_id' , $valid_page_found , $slug , $page_content );
2015-06-16 11:31:15 +00:00
2015-09-07 13:17:22 +00:00
if ( $valid_page_found ) {
if ( $option ) {
update_option ( $option , $valid_page_found );
2014-08-13 09:59:37 +00:00
}
2015-09-07 13:17:22 +00:00
return $valid_page_found ;
2014-07-20 23:53:43 +00:00
}
2015-09-07 13:17:22 +00:00
2019-01-03 13:40:22 +00:00
// Search for a matching valid trashed page.
2015-09-07 13:17:22 +00:00
if ( strlen ( $page_content ) > 0 ) {
2019-01-03 13:40:22 +00:00
// Search for an existing page with the specified page content (typically a shortcode).
2015-09-07 13:17:22 +00:00
$trashed_page_found = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1; " , " % { $page_content } % " ) );
} else {
2019-01-03 13:40:22 +00:00
// Search for an existing page with the specified page slug.
2015-09-07 13:17:22 +00:00
$trashed_page_found = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1; " , $slug ) );
2015-06-16 11:31:15 +00:00
}
2014-07-20 23:53:43 +00:00
2015-09-07 13:17:22 +00:00
if ( $trashed_page_found ) {
$page_id = $trashed_page_found ;
$page_data = array (
2018-03-05 18:59:17 +00:00
'ID' => $page_id ,
'post_status' => 'publish' ,
2015-09-07 13:17:22 +00:00
);
2018-03-05 18:59:17 +00:00
wp_update_post ( $page_data );
2015-09-07 13:17:22 +00:00
} else {
2015-06-16 11:31:15 +00:00
$page_data = array (
'post_status' => 'publish' ,
'post_type' => 'page' ,
'post_author' => 1 ,
'post_name' => $slug ,
'post_title' => $page_title ,
'post_content' => $page_content ,
'post_parent' => $post_parent ,
2016-08-27 01:46:45 +00:00
'comment_status' => 'closed' ,
2015-06-16 11:31:15 +00:00
);
2018-03-05 18:59:17 +00:00
$page_id = wp_insert_post ( $page_data );
2015-06-16 11:31:15 +00:00
}
2014-07-20 23:53:43 +00:00
2014-08-13 09:59:37 +00:00
if ( $option ) {
2014-07-20 23:53:43 +00:00
update_option ( $option , $page_id );
2014-08-13 09:59:37 +00:00
}
2014-07-20 23:53:43 +00:00
return $page_id ;
2013-07-26 14:36:28 +00:00
}
/**
* Output admin fields .
*
* Loops though the woocommerce options array and outputs each field .
*
2019-01-03 13:40:22 +00:00
* @ param array $options Opens array to output .
2013-07-26 14:36:28 +00:00
*/
function woocommerce_admin_fields ( $options ) {
2014-11-30 06:52:32 +00:00
2017-02-16 11:46:01 +00:00
if ( ! class_exists ( 'WC_Admin_Settings' , false ) ) {
2018-03-05 18:59:17 +00:00
include dirname ( __FILE__ ) . '/class-wc-admin-settings.php' ;
2014-09-22 18:30:07 +00:00
}
2013-07-26 14:36:28 +00:00
2014-07-20 23:53:43 +00:00
WC_Admin_Settings :: output_fields ( $options );
2013-07-26 14:36:28 +00:00
}
/**
* Update all settings which are passed .
*
2019-01-03 13:40:22 +00:00
* @ param array $options Option fields to save .
* @ param array $data Passed data .
2013-07-26 14:36:28 +00:00
*/
2016-06-13 14:47:09 +00:00
function woocommerce_update_options ( $options , $data = null ) {
2014-11-30 06:52:32 +00:00
2017-02-16 11:46:01 +00:00
if ( ! class_exists ( 'WC_Admin_Settings' , false ) ) {
2018-03-05 18:59:17 +00:00
include dirname ( __FILE__ ) . '/class-wc-admin-settings.php' ;
2014-09-22 18:30:07 +00:00
}
2013-07-26 14:36:28 +00:00
2016-06-13 14:47:09 +00:00
WC_Admin_Settings :: save_fields ( $options , $data );
2013-07-26 14:36:28 +00:00
}
/**
* Get a setting from the settings API .
*
2019-01-03 13:40:22 +00:00
* @ param mixed $option_name Option name to save .
* @ param mixed $default Default value to save .
2013-07-26 14:36:28 +00:00
* @ return string
*/
function woocommerce_settings_get_option ( $option_name , $default = '' ) {
2014-11-30 06:52:32 +00:00
2017-02-16 11:46:01 +00:00
if ( ! class_exists ( 'WC_Admin_Settings' , false ) ) {
2018-03-05 18:59:17 +00:00
include dirname ( __FILE__ ) . '/class-wc-admin-settings.php' ;
2014-09-22 18:30:07 +00:00
}
2013-07-26 14:36:28 +00:00
2014-07-20 23:53:43 +00:00
return WC_Admin_Settings :: get_option ( $option_name , $default );
2013-08-06 15:56:15 +00:00
}
2019-01-03 13:40:22 +00:00
/**
* Sees if line item stock has already reduced stock , and whether those values need adjusting e . g . after changing item qty .
*
* @ since 3.6 . 0
* @ param WC_Order_Item $item Item object .
* @ param integer $item_quantity Optional quantity to check against . Read from object if not passed .
* @ return boolean | array | WP_Error Array of changes or error object when stock is updated ( @ see wc_update_product_stock ) . False if nothing changes .
*/
function wc_maybe_adjust_line_item_product_stock ( $item , $item_quantity = - 1 ) {
if ( 'line_item' !== $item -> get_type () ) {
2019-02-18 16:45:55 +00:00
return false ;
2019-01-03 13:40:22 +00:00
}
$product = $item -> get_product ();
$item_quantity = wc_stock_amount ( $item_quantity >= 0 ? $item_quantity : $item -> get_quantity () );
$already_reduced_stock = wc_stock_amount ( $item -> get_meta ( '_reduced_stock' , true ) );
if ( ! $product || ! $product -> managing_stock () || ! $already_reduced_stock || $item_quantity === $already_reduced_stock ) {
2019-02-18 16:45:55 +00:00
return false ;
2019-01-03 13:40:22 +00:00
}
$diff = $item_quantity - $already_reduced_stock ;
if ( $diff < 0 ) {
$new_stock = wc_update_product_stock ( $product , $diff * - 1 , 'increase' );
} else {
$new_stock = wc_update_product_stock ( $product , $diff , 'decrease' );
}
if ( is_wp_error ( $new_stock ) ) {
return $new_stock ;
}
$item -> update_meta_data ( '_reduced_stock' , $item_quantity );
$item -> save ();
return array (
'from' => $new_stock + $diff ,
'to' => $new_stock ,
);
}
2014-07-17 20:17:54 +00:00
/**
2016-08-19 12:43:33 +00:00
* Save order items . Uses the CRUD .
2014-07-17 20:17:54 +00:00
*
* @ since 2.2
2019-01-03 13:40:22 +00:00
* @ param int $order_id Order ID .
* @ param array $items Order items to save .
2014-07-17 20:17:54 +00:00
*/
function wc_save_order_items ( $order_id , $items ) {
2017-08-11 15:16:22 +00:00
// Allow other plugins to check change in order items before they are saved.
2016-06-07 06:13:35 +00:00
do_action ( 'woocommerce_before_save_order_items' , $order_id , $items );
2019-01-03 13:40:22 +00:00
$qty_change_order_notes = array ();
2017-08-11 15:16:22 +00:00
// Line items and fees.
2014-07-17 20:17:54 +00:00
if ( isset ( $items [ 'order_item_id' ] ) ) {
2016-08-25 12:50:37 +00:00
$data_keys = array (
'line_tax' => array (),
'line_subtotal_tax' => array (),
'order_item_name' => null ,
'order_item_qty' => null ,
'order_item_tax_class' => null ,
'line_total' => null ,
'line_subtotal' => null ,
);
2014-12-01 23:55:48 +00:00
foreach ( $items [ 'order_item_id' ] as $item_id ) {
2019-01-03 13:40:22 +00:00
$item = WC_Order_Factory :: get_order_item ( absint ( $item_id ) );
if ( ! $item ) {
2016-08-19 12:43:33 +00:00
continue ;
}
2014-07-17 20:17:54 +00:00
2016-08-25 12:05:27 +00:00
$item_data = array ();
foreach ( $data_keys as $key => $default ) {
2018-10-05 19:58:50 +00:00
$item_data [ $key ] = isset ( $items [ $key ][ $item_id ] ) ? wc_check_invalid_utf8 ( wp_unslash ( $items [ $key ][ $item_id ] ) ) : $default ;
2016-08-25 12:05:27 +00:00
}
if ( '0' === $item_data [ 'order_item_qty' ] ) {
2019-01-03 13:40:22 +00:00
$changed_stock = wc_maybe_adjust_line_item_product_stock ( $item , 0 );
if ( $changed_stock && ! is_wp_error ( $changed_stock ) ) {
$qty_change_order_notes [] = $item -> get_name () . ' – ' . $changed_stock [ 'from' ] . '→' . $changed_stock [ 'to' ];
}
2016-08-24 15:09:39 +00:00
$item -> delete ();
continue ;
}
2016-08-24 15:02:19 +00:00
2018-03-05 18:59:17 +00:00
$item -> set_props (
array (
'name' => $item_data [ 'order_item_name' ],
'quantity' => $item_data [ 'order_item_qty' ],
'tax_class' => $item_data [ 'order_item_tax_class' ],
'total' => $item_data [ 'line_total' ],
'subtotal' => $item_data [ 'line_subtotal' ],
'taxes' => array (
'total' => $item_data [ 'line_tax' ],
'subtotal' => $item_data [ 'line_subtotal_tax' ],
),
)
);
2014-07-17 20:17:54 +00:00
2017-08-23 11:15:06 +00:00
if ( 'fee' === $item -> get_type () ) {
$item -> set_amount ( $item_data [ 'line_total' ] );
}
2016-08-19 12:43:33 +00:00
if ( isset ( $items [ 'meta_key' ][ $item_id ], $items [ 'meta_value' ][ $item_id ] ) ) {
foreach ( $items [ 'meta_key' ][ $item_id ] as $meta_id => $meta_key ) {
2018-02-12 22:39:33 +00:00
$meta_key = substr ( wp_unslash ( $meta_key ), 0 , 255 );
2018-03-05 18:59:17 +00:00
$meta_value = isset ( $items [ 'meta_value' ][ $item_id ][ $meta_id ] ) ? wp_unslash ( $items [ 'meta_value' ][ $item_id ][ $meta_id ] ) : '' ;
2016-08-19 12:43:33 +00:00
2016-08-25 16:10:59 +00:00
if ( '' === $meta_key && '' === $meta_value ) {
2016-08-25 12:05:27 +00:00
if ( ! strstr ( $meta_id , 'new-' ) ) {
$item -> delete_meta_data_by_mid ( $meta_id );
2016-08-19 12:43:33 +00:00
}
2016-08-25 12:05:27 +00:00
} elseif ( strstr ( $meta_id , 'new-' ) ) {
2016-08-19 12:43:33 +00:00
$item -> add_meta_data ( $meta_key , $meta_value , false );
} else {
$item -> update_meta_data ( $meta_key , $meta_value , $meta_id );
}
}
}
2018-06-11 18:56:40 +00:00
2018-06-11 18:27:32 +00:00
// Allow other plugins to change item object before it is saved.
2018-06-28 14:31:03 +00:00
do_action ( 'woocommerce_before_save_order_item' , $item );
2014-07-17 20:17:54 +00:00
2016-08-19 12:43:33 +00:00
$item -> save ();
2019-01-03 13:40:22 +00:00
$changed_stock = wc_maybe_adjust_line_item_product_stock ( $item );
if ( $changed_stock && ! is_wp_error ( $changed_stock ) ) {
$qty_change_order_notes [] = $item -> get_name () . ' (' . $changed_stock [ 'from' ] . '→' . $changed_stock [ 'to' ] . ')' ;
}
2015-06-15 15:22:53 +00:00
}
2014-07-17 20:17:54 +00:00
}
2019-01-03 13:40:22 +00:00
// Shipping Rows.
2014-07-17 20:17:54 +00:00
if ( isset ( $items [ 'shipping_method_id' ] ) ) {
2016-08-25 12:50:37 +00:00
$data_keys = array (
'shipping_method' => null ,
'shipping_method_title' => null ,
'shipping_cost' => 0 ,
'shipping_taxes' => array (),
);
2017-04-06 20:58:24 +00:00
2014-12-01 23:55:48 +00:00
foreach ( $items [ 'shipping_method_id' ] as $item_id ) {
2019-01-03 13:40:22 +00:00
$item = WC_Order_Factory :: get_order_item ( absint ( $item_id ) );
if ( ! $item ) {
2016-08-19 12:43:33 +00:00
continue ;
2014-07-20 23:53:43 +00:00
}
2016-08-24 15:02:19 +00:00
2016-08-25 12:50:37 +00:00
$item_data = array ();
foreach ( $data_keys as $key => $default ) {
2017-04-06 20:58:24 +00:00
$item_data [ $key ] = isset ( $items [ $key ][ $item_id ] ) ? wc_clean ( wp_unslash ( $items [ $key ][ $item_id ] ) ) : $default ;
2016-08-24 15:02:19 +00:00
}
2016-08-22 17:07:52 +00:00
2018-03-05 18:59:17 +00:00
$item -> set_props (
array (
'method_id' => $item_data [ 'shipping_method' ],
'method_title' => $item_data [ 'shipping_method_title' ],
'total' => $item_data [ 'shipping_cost' ],
'taxes' => array (
'total' => $item_data [ 'shipping_taxes' ],
),
)
);
2016-08-25 12:50:37 +00:00
2016-08-22 17:07:52 +00:00
if ( isset ( $items [ 'meta_key' ][ $item_id ], $items [ 'meta_value' ][ $item_id ] ) ) {
foreach ( $items [ 'meta_key' ][ $item_id ] as $meta_id => $meta_key ) {
2017-04-06 20:58:24 +00:00
$meta_value = isset ( $items [ 'meta_value' ][ $item_id ][ $meta_id ] ) ? wp_unslash ( $items [ 'meta_value' ][ $item_id ][ $meta_id ] ) : '' ;
2016-08-22 17:07:52 +00:00
2016-08-25 16:10:59 +00:00
if ( '' === $meta_key && '' === $meta_value ) {
2016-08-25 12:50:37 +00:00
if ( ! strstr ( $meta_id , 'new-' ) ) {
$item -> delete_meta_data_by_mid ( $meta_id );
2016-08-22 17:07:52 +00:00
}
2016-08-25 12:50:37 +00:00
} elseif ( strstr ( $meta_id , 'new-' ) ) {
2016-08-22 17:07:52 +00:00
$item -> add_meta_data ( $meta_key , $meta_value , false );
} else {
$item -> update_meta_data ( $meta_key , $meta_value , $meta_id );
}
}
}
2016-08-19 12:43:33 +00:00
$item -> save ();
2014-07-20 23:53:43 +00:00
}
}
2017-08-11 15:16:22 +00:00
$order = wc_get_order ( $order_id );
2019-01-03 13:40:22 +00:00
if ( ! empty ( $qty_change_order_notes ) ) {
/* translators: %s item name. */
$order -> add_order_note ( sprintf ( __ ( 'Adjusted stock: %s' , 'woocommerce' ), implode ( ', ' , $qty_change_order_notes ) ), false , true );
}
2016-08-19 12:43:33 +00:00
$order -> update_taxes ();
$order -> calculate_totals ( false );
2015-03-16 12:17:09 +00:00
2019-01-03 13:40:22 +00:00
// Inform other plugins that the items have been saved.
2014-09-19 16:08:54 +00:00
do_action ( 'woocommerce_saved_order_items' , $order_id , $items );
2014-07-17 20:17:54 +00:00
}
2017-11-20 22:43:37 +00:00
/**
* Get HTML for some action buttons . Used in list tables .
*
* @ since 3.3 . 0
* @ param array $actions Actions to output .
* @ return string
*/
function wc_render_action_buttons ( $actions ) {
$actions_html = '' ;
foreach ( $actions as $action ) {
if ( isset ( $action [ 'group' ] ) ) {
2017-11-22 16:28:11 +00:00
$actions_html .= '<div class="wc-action-button-group"><label>' . $action [ 'group' ] . '</label> <span class="wc-action-button-group__items">' . wc_render_action_buttons ( $action [ 'actions' ] ) . '</span></div>' ;
2017-11-20 22:43:37 +00:00
} elseif ( isset ( $action [ 'action' ], $action [ 'url' ], $action [ 'name' ] ) ) {
2018-02-15 12:46:47 +00:00
$actions_html .= sprintf ( '<a class="button wc-action-button wc-action-button-%1$s %1$s" href="%2$s" aria-label="%3$s" title="%3$s">%4$s</a>' , esc_attr ( $action [ 'action' ] ), esc_url ( $action [ 'url' ] ), esc_attr ( isset ( $action [ 'title' ] ) ? $action [ 'title' ] : $action [ 'name' ] ), esc_html ( $action [ 'name' ] ) );
2017-11-20 22:43:37 +00:00
}
}
return $actions_html ;
}
2019-03-25 13:30:04 +00:00
/**
* Shows a notice if variations are missing prices .
*
* @ since 3.6 . 0
* @ param WC_Product $product_object Product object .
*/
function wc_render_invalid_variation_notice ( $product_object ) {
global $wpdb ;
$variation_ids = $product_object ? $product_object -> get_children () : array ();
if ( empty ( $variation_ids ) ) {
return ;
}
$variation_count = count ( $variation_ids );
// Check if a variation exists without pricing data.
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$invalid_variation_count = $wpdb -> get_var (
"
SELECT count ( post_id ) FROM { $wpdb -> postmeta }
WHERE post_id in ( " . implode( ',', array_map( 'absint', $variation_ids ) ) . " )
AND meta_key = '_price'
AND meta_value >= 0
2019-07-03 10:37:28 +00:00
AND meta_value != ''
2019-03-25 13:30:04 +00:00
"
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
if ( 0 < ( $variation_count - $invalid_variation_count ) ) {
?>
< div id = " message " class = " inline notice woocommerce-message woocommerce-notice-invalid-variation " >
< p >
< ? php
echo wp_kses_post (
sprintf (
/* Translators: %d variation count. */
_n ( '%d variation does not have a price.' , '%d variations do not have prices.' , ( $variation_count - $invalid_variation_count ), 'woocommerce' ),
( $variation_count - $invalid_variation_count )
) . ' ' .
__ ( 'Variations (and their attributes) that do not have prices will not be shown in your store.' , 'woocommerce' )
);
?>
</ p >
</ div >
< ? php
}
}