Rearrange things to get ajax working.

Spread them out throughout WooCommerce.
This commit is contained in:
George Stephanis 2015-08-13 17:49:59 -04:00
parent c6d7e6ba26
commit 2aa1650f54
4 changed files with 115 additions and 104 deletions

View File

@ -63,8 +63,7 @@
return rates;
},
save : function() {
$.post( ajaxurl + '?action=wc_tax_rates_save_changes', {
// action : 'wc_tax_rates_save_changes',
$.post( ajaxurl + '?action=woocommerce_tax_rates_save_changes', {
current_class : data.current_class,
wc_tax_nonce : data.wc_tax_nonce,
changes : this.changes

View File

@ -25,8 +25,6 @@ class WC_Settings_Tax extends WC_Settings_Page {
* Constructor.
*/
public function __construct() {
add_action( 'wp_ajax_wc_tax_rates_save_changes', array( __CLASS__, 'wp_ajax_wc_tax_rates_save_changes' ) );
$this->label = __( 'Tax', 'woocommerce' );
parent::__construct();
}
@ -102,32 +100,6 @@ class WC_Settings_Tax extends WC_Settings_Page {
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_tax_rates_%') OR `option_name` LIKE ('_transient_timeout_wc_tax_rates_%')" );
}
public static function get_rates_for_tax_class( $tax_class ) {
global $wpdb;
// Get all the rates and locations. Snagging all at once should significantly cut down on the number of queries.
$rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}woocommerce_tax_rates` WHERE `tax_rate_class` = %s ORDER BY `tax_rate_order`;", sanitize_title( $tax_class ) ) );
$locations = $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}woocommerce_tax_rate_locations`" );
// Set the rates keys equal to their ids.
$rates = array_combine( wp_list_pluck( $rates, 'tax_rate_id' ), $rates );
// Drop the locations into the rates array.
foreach ( $locations as $location ) {
// Don't set them for unexistent rates.
if ( ! isset( $rates[ $location->tax_rate_id ] ) ) {
continue;
}
// If the rate exists, initialize the array before appending to it.
if ( ! isset( $rates[ $location->tax_rate_id ]->{$location->location_type} ) ) {
$rates[ $location->tax_rate_id ]->{$location->location_type} = array();
}
$rates[ $location->tax_rate_id ]->{$location->location_type}[] = $location->location_code;
}
return $rates;
}
/**
* Output tax rate tables
*/
@ -166,7 +138,7 @@ class WC_Settings_Tax extends WC_Settings_Page {
'current_class' => $current_class,
'wc_tax_nonce' => wp_create_nonce( 'wc_tax_nonce-class:' . $current_class ),
'base_url' => $base_url,
'rates' => array_values( self::get_rates_for_tax_class( $current_class ) ),
'rates' => array_values( WC_Tax::get_rates_for_tax_class( $current_class ) ),
'page' => ! empty( $_GET['p'] ) ? absint( $_GET['p'] ) : 1,
'limit' => 100,
'countries' => $countries,
@ -205,76 +177,6 @@ class WC_Settings_Tax extends WC_Settings_Page {
include( 'views/html-settings-tax.php' );
}
/**
* Handle ajax tax rate submissions.
*/
private static function wp_ajax_wc_tax_rates_save_changes() {
global $wpdb;
if ( ! isset( $_POST['current_class'], $_POST['wc_tax_nonce'], $_POST['changes'] ) ) {
wp_send_json_error( 'missing_fields' );
exit;
}
$current_class = $_POST['current_class']; // This is sanitized seven lines later.
if ( ! wp_verify_nonce( $_POST['wc_tax_nonce'], 'wc_tax_nonce-class:' . $current_class ) ) {
wp_send_json_error( 'bad_nonce' );
exit;
}
$current_class = WC_Tax::format_tax_rate_class( $current_class );
// Check User Caps
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
exit;
}
$changes = $_POST['changes'];
foreach ( $changes as $tax_rate_id => $data ) {
if ( isset( $data['deleted'] ) ) {
if ( isset( $data['newRow'] ) ) {
// So the user added and deleted a new row.
// That's fine, it's not in the database anyways. NEXT!
continue;
}
WC_Tax::_delete_tax_rate( $tax_rate_id );
}
$tax_rate = array_intersect_key( $data, array(
'tax_rate_country' => 1,
'tax_rate_state' => 1,
'tax_rate' => 1,
'tax_rate_name' => 1,
'tax_rate_priority' => 1,
'tax_rate_compound' => 1,
'tax_rate_shipping' => 1,
'tax_rate_order' => 1,
) );
if ( isset( $data['newRow'] ) ) {
// Hurrah, shiny and new!
$tax_rate['tax_rate_class'] = $current_class;
$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
} else {
// Updating an existing rate ...
WC_Tax::_update_tax_rate( $tax_rate_id, $tax_rate );
}
if ( isset( $data['postcode'] ) ) {
WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, wc_clean( $data['postcode'] ) );
}
if ( isset( $data['city'] ) ) {
WC_Tax::_update_tax_rate_cities( $tax_rate_id, wc_clean( $data['postcode'] ) );
}
}
wp_send_json_success( array(
'rates' => self::get_rates_for_tax_class( $current_class ),
) );
}
/**
* Get tax class being edited
* @return string

View File

@ -122,7 +122,8 @@ class WC_AJAX {
'get_customer_location' => true,
'load_variations' => false,
'save_variations' => false,
'bulk_edit_variations' => false
'bulk_edit_variations' => false,
'tax_rates_save_changes' => false,
);
foreach ( $ajax_events as $ajax_event => $nopriv ) {
@ -2972,6 +2973,76 @@ class WC_AJAX {
wc_delete_product_transients( $product_id );
die();
}
/**
* Handle submissions from assets/js/settings-views-html-settings-tax.js Backbone model.
*/
public static function tax_rates_save_changes() {
if ( ! isset( $_POST['current_class'], $_POST['wc_tax_nonce'], $_POST['changes'] ) ) {
wp_send_json_error( 'missing_fields' );
exit;
}
$current_class = $_POST['current_class']; // This is sanitized seven lines later.
if ( ! wp_verify_nonce( $_POST['wc_tax_nonce'], 'wc_tax_nonce-class:' . $current_class ) ) {
wp_send_json_error( 'bad_nonce' );
exit;
}
$current_class = WC_Tax::format_tax_rate_class( $current_class );
// Check User Caps
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
exit;
}
$changes = $_POST['changes'];
foreach ( $changes as $tax_rate_id => $data ) {
if ( isset( $data['deleted'] ) ) {
if ( isset( $data['newRow'] ) ) {
// So the user added and deleted a new row.
// That's fine, it's not in the database anyways. NEXT!
continue;
}
WC_Tax::_delete_tax_rate( $tax_rate_id );
}
$tax_rate = array_intersect_key( $data, array(
'tax_rate_country' => 1,
'tax_rate_state' => 1,
'tax_rate' => 1,
'tax_rate_name' => 1,
'tax_rate_priority' => 1,
'tax_rate_compound' => 1,
'tax_rate_shipping' => 1,
'tax_rate_order' => 1,
) );
if ( isset( $data['newRow'] ) ) {
// Hurrah, shiny and new!
$tax_rate['tax_rate_class'] = $current_class;
$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
} else {
// Updating an existing rate ...
if ( ! empty( $tax_rate ) ) {
WC_Tax::_update_tax_rate( $tax_rate_id, $tax_rate );
}
}
if ( isset( $data['postcode'] ) ) {
WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, array_map( 'wc_clean', $data['postcode'] ) );
}
if ( isset( $data['city'] ) ) {
WC_Tax::_update_tax_rate_cities( $tax_rate_id, array_map( 'wc_clean', $data['city'] ) );
}
}
wp_send_json_success( array(
'rates' => WC_Tax::get_rates_for_tax_class( $current_class ),
) );
}
}
WC_AJAX::init();

View File

@ -755,7 +755,10 @@ class WC_Tax {
* @return string
*/
public static function _update_tax_rate_postcodes( $tax_rate_id, $postcodes ) {
$postcodes = array_filter( array_diff( array_map( array( __CLASS__, 'format_tax_rate_postcode' ), explode( ';', $postcodes ) ), array( '*' ) ) );
if ( ! is_array( $postcodes ) ) {
$postcodes = explode( ';', $postcodes );
}
$postcodes = array_filter( array_diff( array_map( array( __CLASS__, 'format_tax_rate_postcode' ), $postcodes ), array( '*' ) ) );
$postcodes = self::_get_expanded_numeric_ranges_from_array( $postcodes );
self::_update_tax_rate_locations( $tax_rate_id, $postcodes, 'postcode' );
@ -774,7 +777,10 @@ class WC_Tax {
* @return string
*/
public static function _update_tax_rate_cities( $tax_rate_id, $cities ) {
$cities = array_filter( array_diff( array_map( array( __CLASS__, 'format_tax_rate_city' ), explode( ';', $cities ) ), array( '*' ) ) );
if ( ! is_array( $cities ) ) {
$cities = explode( ';', $cities );
}
$cities = array_filter( array_diff( array_map( array( __CLASS__, 'format_tax_rate_city' ), $cities ), array( '*' ) ) );
self::_update_tax_rate_locations( $tax_rate_id, $cities, 'city' );
}
@ -864,5 +870,38 @@ class WC_Tax {
}
return $postcodes;
}
/**
* Used by admin settings page.
*
* @param $tax_class
*
* @return array|null|object
*/
public static function get_rates_for_tax_class( $tax_class ) {
global $wpdb;
// Get all the rates and locations. Snagging all at once should significantly cut down on the number of queries.
$rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}woocommerce_tax_rates` WHERE `tax_rate_class` = %s ORDER BY `tax_rate_order`;", sanitize_title( $tax_class ) ) );
$locations = $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}woocommerce_tax_rate_locations`" );
// Set the rates keys equal to their ids.
$rates = array_combine( wp_list_pluck( $rates, 'tax_rate_id' ), $rates );
// Drop the locations into the rates array.
foreach ( $locations as $location ) {
// Don't set them for unexistent rates.
if ( ! isset( $rates[ $location->tax_rate_id ] ) ) {
continue;
}
// If the rate exists, initialize the array before appending to it.
if ( ! isset( $rates[ $location->tax_rate_id ]->{$location->location_type} ) ) {
$rates[ $location->tax_rate_id ]->{$location->location_type} = array();
}
$rates[ $location->tax_rate_id ]->{$location->location_type}[] = $location->location_code;
}
return $rates;
}
}
WC_Tax::init();