Generate a zone name if left blank

@coderkevin
This commit is contained in:
Mike Jolley 2016-05-24 18:59:35 +01:00
parent 3893d0ec19
commit 22291e3ead
3 changed files with 55 additions and 39 deletions

View File

@ -239,7 +239,6 @@
size = _.size( zones ),
newRow = _.extend( {}, data.default_zone, {
zone_id : 'new-' + size + '-' + Date.now(),
zone_name: data.strings.default_zone_name,
editing : true
} );

View File

@ -241,7 +241,6 @@ class WC_Settings_Shipping extends WC_Settings_Page {
'strings' => array(
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),
'default_zone_name' => __( 'Zone', 'woocommerce' ),
),
) );
wp_enqueue_script( 'wc-shipping-zones' );

View File

@ -19,7 +19,7 @@ class WC_Shipping_Zone extends WC_Data {
* Zone Data
* @var array
*/
protected $_data = array(
protected $_data = array(
'zone_id' => 0,
'zone_name' => '',
'zone_order' => 0,
@ -36,9 +36,9 @@ class WC_Shipping_Zone extends WC_Data {
* Constructor for zones
* @param int|object $zone Zone ID to load from the DB (optional) or already queried data.
*/
public function __construct( $zone = 0 ) {
public function __construct( $zone = 0 ) {
if ( is_numeric( $zone ) && ! empty( $zone ) ) {
$this->read( $zone );
$this->read( $zone );
} elseif ( is_object( $zone ) ) {
$this->set_zone_id( $zone->zone_id );
$this->set_zone_name( $zone->zone_name );
@ -50,20 +50,20 @@ class WC_Shipping_Zone extends WC_Data {
} else {
$this->set_zone_name( __( 'Zone', 'woocommerce' ) );
}
}
}
/**
* Get ID
* @return int
*/
public function get_id() {
return $this->get_zone_id();
}
public function get_id() {
return $this->get_zone_id();
}
/**
* Insert zone into the database
*/
public function create() {
* Insert zone into the database
*/
public function create() {
global $wpdb;
$wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zones', array(
'zone_name' => $this->get_zone_name(),
@ -87,36 +87,40 @@ class WC_Shipping_Zone extends WC_Data {
}
}
/**
* Update zone in the database
*/
public function update() {
global $wpdb;
/**
* Update zone in the database
*/
public function update() {
global $wpdb;
$wpdb->update( $wpdb->prefix . 'woocommerce_shipping_zones', array(
'zone_name' => $this->get_zone_name(),
'zone_order' => $this->get_zone_order(),
), array( 'zone_id' => $this->get_zone_id() ) );
}
}
/**
* Delete a zone.
* @since 2.6.0
*/
public function delete() {
public function delete() {
if ( $this->get_id() ) {
global $wpdb;
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'zone_id' => $this->get_id() ) );
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $this->get_id() ) );
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $this->get_id() ) );
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zones', array( 'zone_id' => $this->get_id() ) );
WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
$this->set_zone_id( 0 );
}
}
}
/**
* Save zone data to the database.
*/
public function save() {
if ( empty( $this->get_zone_name() ) ) {
$this->set_zone_name( $this->generate_zone_name() );
}
if ( ! $this->get_zone_id() ) {
$this->create();
} else {
@ -134,33 +138,47 @@ class WC_Shipping_Zone extends WC_Data {
* Get zone ID
* @return int
*/
public function get_zone_id() {
return absint( $this->_data['zone_id'] );
}
public function get_zone_id() {
return absint( $this->_data['zone_id'] );
}
/**
* Get zone name
* @return string
*/
public function get_zone_name() {
return $this->_data['zone_name'];
}
public function get_zone_name() {
return $this->_data['zone_name'];
}
/**
* Get zone order
* @return int
*/
public function get_zone_order() {
return absint( $this->_data['zone_order'] );
}
return absint( $this->_data['zone_order'] );
}
/**
* Get zone locations
* @return array of zone objects
*/
public function get_zone_locations() {
return $this->_data['zone_locations'];
}
return $this->_data['zone_locations'];
}
/**
* Generate a zone name based on location.
* @return string
*/
protected function generate_zone_name() {
$zone_name = $this->get_formatted_location();
if ( empty( $zone_name ) ) {
$zone_name = __( 'Zone', 'woocommerce' );
}
return $zone_name;
}
/**
* Return a text string representing what this zone is for.
@ -214,7 +232,7 @@ class WC_Shipping_Zone extends WC_Data {
global $wpdb;
$raw_methods_sql = $enabled_only ? "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d AND is_enabled = 1 order by method_order ASC;" : "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d order by method_order ASC;";
$raw_methods = $wpdb->get_results( $wpdb->prepare( $raw_methods_sql, $this->get_zone_id() ) );
$raw_methods = $wpdb->get_results( $wpdb->prepare( $raw_methods_sql, $this->get_zone_id() ) );
$wc_shipping = WC_Shipping::instance();
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
$methods = array();
@ -292,25 +310,25 @@ class WC_Shipping_Zone extends WC_Data {
* @access private
* @param int $set
*/
private function set_zone_id( $set ) {
$this->_data['zone_id'] = absint( $set );
}
private function set_zone_id( $set ) {
$this->_data['zone_id'] = absint( $set );
}
/**
* Set zone name
* @param string $set
*/
public function set_zone_name( $set ) {
public function set_zone_name( $set ) {
$this->_data['zone_name'] = wc_clean( $set );
}
}
/**
* Set zone order
* @param int $set
*/
public function set_zone_order( $set ) {
$this->_data['zone_order'] = absint( $set );
}
$this->_data['zone_order'] = absint( $set );
}
/**
* Is passed location type valid?