woocommerce/includes/admin/wc-admin-functions.php

278 lines
9.1 KiB
PHP
Raw Normal View History

<?php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Admin Functions
*
* @author WooThemes
* @category Core
* @package WooCommerce/Admin/Functions
* @version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
2015-11-03 13:31:20 +00:00
* Get all WooCommerce screen ids.
*
* @return array
*/
2013-07-25 15:29:25 +00:00
function wc_get_screen_ids() {
$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',
$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',
'toplevel_page_wc-reports',
2014-07-20 23:53:43 +00:00
'product_page_product_attributes',
'edit-product',
'product',
'edit-shop_coupon',
'shop_coupon',
'edit-product_cat',
'edit-product_tag',
2015-02-15 20:36:18 +00:00
'profile',
'user-edit'
2014-07-20 23:53:43 +00:00
);
foreach ( wc_get_order_types() as $type ) {
$screen_ids[] = $type;
$screen_ids[] = 'edit-' . $type;
}
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.
*
* @param mixed $slug Slug for the new page
2014-09-07 23:37:55 +00:00
* @param string $option Option name to store the page's ID
2013-07-25 15:29:25 +00:00
* @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
*/
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
$option_value = get_option( $option );
if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) {
if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
// Valid page is already in place
return $page_object->ID;
}
}
2014-07-20 23:53:43 +00:00
if ( strlen( $page_content ) > 0 ) {
// Search for an existing page with the specified page content (typically a shortcode)
$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;", "%{$page_content}%" ) );
2014-07-20 23:53:43 +00:00
} else {
// Search for an existing page with the specified page slug
$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
$valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content );
if ( $valid_page_found ) {
if ( $option ) {
update_option( $option, $valid_page_found );
}
return $valid_page_found;
2014-07-20 23:53:43 +00:00
}
// Search for a matching valid trashed page
if ( strlen( $page_content ) > 0 ) {
// Search for an existing page with the specified page content (typically a shortcode)
$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 {
// Search for an existing page with the specified page slug
$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 ) );
}
2014-07-20 23:53:43 +00:00
if ( $trashed_page_found ) {
$page_id = $trashed_page_found;
$page_data = array(
'ID' => $page_id,
'post_status' => 'publish',
);
wp_update_post( $page_data );
} else {
$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,
'comment_status' => 'closed'
);
$page_id = wp_insert_post( $page_data );
}
2014-07-20 23:53:43 +00:00
if ( $option ) {
2014-07-20 23:53:43 +00:00
update_option( $option, $page_id );
}
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.
*
* @param array $options Opens array to output
*/
function woocommerce_admin_fields( $options ) {
if ( ! class_exists( 'WC_Admin_Settings' ) ) {
include( dirname( __FILE__ ) . '/class-wc-admin-settings.php' );
}
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.
*
* @param array $options
* @param array $data
2013-07-26 14:36:28 +00:00
*/
function woocommerce_update_options( $options, $data = null ) {
if ( ! class_exists( 'WC_Admin_Settings' ) ) {
include( dirname( __FILE__ ) . '/class-wc-admin-settings.php' );
}
2013-07-26 14:36:28 +00:00
WC_Admin_Settings::save_fields( $options, $data );
2013-07-26 14:36:28 +00:00
}
/**
* Get a setting from the settings API.
*
2014-09-07 23:37:55 +00:00
* @param mixed $option_name
* @param mixed $default
2013-07-26 14:36:28 +00:00
* @return string
*/
function woocommerce_settings_get_option( $option_name, $default = '' ) {
if ( ! class_exists( 'WC_Admin_Settings' ) ) {
include( dirname( __FILE__ ) . '/class-wc-admin-settings.php' );
}
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 );
}
/**
2016-08-19 12:43:33 +00:00
* Save order items. Uses the CRUD.
*
* @since 2.2
* @param int $order_id Order ID
* @param array $items Order items to save
*/
function wc_save_order_items( $order_id, $items ) {
// Allow other plugins to check change in order items before they are saved
do_action( 'woocommerce_before_save_order_items', $order_id, $items );
2016-08-19 12:43:33 +00:00
$order = wc_get_order( $order_id );
2016-08-19 12:43:33 +00:00
// Line items and fees
if ( isset( $items['order_item_id'] ) ) {
foreach ( $items['order_item_id'] as $item_id ) {
2016-08-19 12:43:33 +00:00
if ( ! $item = $order->get_item( absint( $item_id ) ) ) {
continue;
}
if ( isset( $items['order_item_name'][ $item_id ] ) ) {
2016-08-19 12:43:33 +00:00
$item->set_name( wc_clean( wp_unslash( $items['order_item_name'][ $item_id ] ) ) );
}
2016-08-19 12:43:33 +00:00
if ( isset( $items['order_item_qty'][ $item_id ] ) && is_callable( array( $item, 'set_quantity' ) ) ) {
$item->set_quantity( $items['order_item_qty'][ $item_id ] );
}
if ( isset( $items['order_item_tax_class'][ $item_id ] ) ) {
2016-08-19 12:43:33 +00:00
$item->set_tax_class( wc_clean( $items['order_item_tax_class'][ $item_id ] ) );
}
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 ) {
$meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? $items['meta_value'][ $item_id ][ $meta_id ] : '';
if ( strstr( $meta_id, 'new-' ) ) {
if ( $meta_key === '' && $meta_value === '' ) {
continue;
}
$item->add_meta_data( $meta_key, $meta_value, false );
} elseif ( $meta_key === '' && $meta_value === '' ) {
$item->delete_meta_data_by_mid( $meta_id );
} else {
$item->update_meta_data( $meta_key, $meta_value, $meta_id );
}
}
}
2016-08-19 12:43:33 +00:00
$line_tax = isset( $items['line_tax'][ $item_id ] ) ? $items['line_tax'][ $item_id ]: array();
$line_subtotal_tax = isset( $items['line_subtotal_tax'][ $item_id ] ) ? $items['line_subtotal_tax'][ $item_id ]: $line_tax;
$item->set_total( isset( $items['line_total'][ $item_id ] ) ? $items['line_total'][ $item_id ] : 0 );
$item->set_total_tax( array_sum( $line_tax ) );
2016-08-22 15:48:19 +00:00
if ( is_callable( array( $item, 'set_subtotal' ) ) ) {
$item->set_subtotal( isset( $items['line_subtotal'][ $item_id ] ) ? $items['line_subtotal'][ $item_id ] : $item->get_total() );
$item->set_subtotal_tax( array_sum( $line_subtotal_tax ) );
}
2016-08-19 12:43:33 +00:00
$item->set_taxes( array( 'total' => $line_tax, 'subtotal' => $line_subtotal_tax ) );
$item->save();
}
}
// Shipping Rows
if ( isset( $items['shipping_method_id'] ) ) {
foreach ( $items['shipping_method_id'] as $item_id ) {
2016-08-19 12:43:33 +00:00
if ( ! $item = $order->get_item( absint( $item_id ) ) ) {
continue;
2014-07-20 23:53:43 +00:00
}
2016-08-19 12:43:33 +00:00
$item->set_method_id( isset( $items['shipping_method'][ $item_id ] ) ? wc_clean( $items['shipping_method'][ $item_id ] ) : '' );
$item->set_method_title( isset( $items['shipping_method_title'][ $item_id ] ) ? wc_clean( wp_unslash( $items['shipping_method_title'][ $item_id ] ) ) : '' );
$item->set_total( isset( $items['shipping_cost'][ $item_id ] ) ? $items['shipping_cost'][ $item_id ] : '' );
2016-08-22 15:48:19 +00:00
$item->set_taxes( array( 'total' => isset( $items['shipping_taxes'][ $item_id ] ) ? $items['shipping_taxes'][ $item_id ] : array() ) );
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 ) {
$meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? $items['meta_value'][ $item_id ][ $meta_id ] : '';
if ( strstr( $meta_id, 'new-' ) ) {
if ( $meta_key === '' && $meta_value === '' ) {
continue;
}
$item->add_meta_data( $meta_key, $meta_value, false );
} elseif ( $meta_key === '' && $meta_value === '' ) {
$item->delete_meta_data_by_mid( $meta_id );
} 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
}
}
2016-08-19 12:43:33 +00:00
// Updates tax totals
$order->update_taxes();
2016-08-19 12:43:33 +00:00
// Calc totals - this also triggers save
$order->calculate_totals( false );
// Inform other plugins that the items have been saved
do_action( 'woocommerce_saved_order_items', $order_id, $items );
}