Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Countries, currencies and regions infrastructure update #1865

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wp-shopping-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ public function setup_table_names() {
global $wpdb;
$wpdb->wpsc_meta = WPSC_TABLE_META;
$wpdb->wpsc_also_bought = WPSC_TABLE_ALSO_BOUGHT;
$wpdb->wpsc_region_tax = WPSC_TABLE_REGION_TAX;
$wpdb->wpsc_coupon_codes = WPSC_TABLE_COUPON_CODES;
$wpdb->wpsc_cart_contents = WPSC_TABLE_CART_CONTENTS;
$wpdb->wpsc_claimed_stock = WPSC_TABLE_CLAIMED_STOCK;
$wpdb->wpsc_currency_list = WPSC_TABLE_CURRENCY_LIST;
$wpdb->wpsc_purchase_logs = WPSC_TABLE_PURCHASE_LOGS;
$wpdb->wpsc_checkout_forms = WPSC_TABLE_CHECKOUT_FORMS;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these tables removed? We talked in Slack about back compat concerns here...I might be missing something though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as deprecated is true they are still defined, and even installed if it a brand new installation. Also, ( when I finish up the last routine ) and changes made through $wpdb calls will also be automatically picked up by the code.

At some point when we want to turn off deprecated the the "legacy" database structures and constants will quietly go away without further action

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the constants are defined (https://github.com/wp-e-commerce/WP-e-Commerce/pull/1865/files#diff-66e9d8bfe5d295da98d32c2d9c5a8d5cR251), but the WPDB properties are not?

$wpdb->wpsc_product_rating = WPSC_TABLE_PRODUCT_RATING;
Expand All @@ -137,6 +135,8 @@ public function setup_table_names() {
$wpdb->wpsc_purchasemeta = WPSC_TABLE_PURCHASE_META;
$wpdb->wpsc_visitors = WPSC_TABLE_VISITORS;
$wpdb->wpsc_visitormeta = WPSC_TABLE_VISITOR_META;

do_action( 'wpsc_setup_table_names' );
}

/**
Expand Down
22 changes: 15 additions & 7 deletions wpsc-admin/ajax-and-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,24 @@ function wpsc_change_currency() {
return;
}

global $wpdb;

// it appears that what is called 'currencyid' here is really the country id
if ( is_numeric( $_POST['currencyid'] ) ) {
$currency_data = $wpdb->get_results( $wpdb->prepare( "SELECT `symbol`,`symbol_html`,`code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`=%d LIMIT 1", $_POST['currencyid'] ), ARRAY_A );
$wpsc_country = wpsc_get_country_object( $_POST['currencyid'] );

$price_out = null;
if ( $currency_data[0]['symbol'] != '' ) {
$currency_sign = $currency_data[0]['symbol_html'];
} else {
$currency_sign = $currency_data[0]['code'];

// we are going to look for currency code as html, if that not found, then the
// symbol, if that not cound we will go with the isocode
$currency_sign = $wpsc_country->get_currency_symbol_html();

if ( empty( $currency_sign ) ) {
$currency_sign = $wpsc_country->get_currency_symbol();
}

if ( empty( $currency_sign ) ) {
$currency_sign = $wpsc_country->get_currency_code();
}

echo $currency_sign;
}
}
Expand Down
18 changes: 8 additions & 10 deletions wpsc-admin/includes/display-items-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,10 @@ function wpsc_price_control_forms() {
}

$product_data['meta']['_wpsc_price'] = wpsc_format_number( $product_data['meta']['_wpsc_price'] );

$currency_data = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` ORDER BY `country` ASC", ARRAY_A );
$currency_data = wpsc_get_all_countries();

/* Get country name and symbol */
$currency_type = get_option( 'currency_type' );
$country = new WPSC_Country( $currency_type );
$country = wpsc_get_currency_type_country_object();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a massive fan of ending function names in data types, e.g. WPSC_Countries::get_countries_array() or this, wpsc_get_currency_type_country_object(). Seems to me it'd be better to just be wpsc_get_currency_type_country?


$ct_code = $country->get_currency_code(); // Country currency code
$ct_symb = $country->get_currency_symbol(); // Country symbol
Expand Down Expand Up @@ -197,9 +195,9 @@ function wpsc_price_control_forms() {
<td class="remove"><a href="#" class="wpsc_delete_currency_layer<?php echo $currency_delete_class; ?>" rel="<?php echo $iso; ?>"><?php echo $currency_delete_text; ?></a></td>
<td>
<select name="newCurrency[]" class="newCurrency">
<?php foreach ( $currency_data as $currency ) : ?>
<option value="<?php echo absint( $currency['id'] ); ?>" <?php selected( $iso, $currency['isocode'] ); ?>>
<?php echo esc_html( $currency['country'] ); ?> (<?php echo esc_html( $currency['currency'] ); ?>)
<?php foreach ( $currency_data as $country_id => $wpsc_country ) : ?>
<option value="<?php echo absint( $country_id); ?>" <?php selected( $iso, $wpsc_country->get_isocode() ); ?>>
<?php echo esc_html( $wpsc_country->get_name() ); ?> (<?php echo esc_html( $wpsc_country->get_currency() ); ?>)
</option>
<?php endforeach; ?>
</select>
Expand All @@ -214,9 +212,9 @@ function wpsc_price_control_forms() {
<td class="remove"><a href="#" class="wpsc_delete_currency_layer<?php echo $currency_delete_class; ?>"><?php echo $currency_delete_text; ?></a></td>
<td>
<select name="newCurrency[]" class="newCurrency">
<?php foreach ( (array) $currency_data as $currency ) { ?>
<option value="<?php echo absint( $currency['id'] ); ?>">
<?php echo esc_html( $currency['country'] ); ?>
<?php foreach ( $currency_data as $country_id => $wpsc_country ) { ?>
<option value="<?php echo absint( $country_id ); ?>">
<?php echo esc_html( $wpsc_country->get_name() ); ?>
</option>
<?php } ?>
</select>
Expand Down
39 changes: 26 additions & 13 deletions wpsc-admin/includes/product-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,24 +778,37 @@ function wpsc_edit_product_variations($product_id, $post_data) {

}

function wpsc_update_alt_product_currency($product_id, $newCurrency, $newPrice){
/**
* @param int $product_id
* @param int $new_currency_country_id
* @param float $new_price
*/
function wpsc_update_alt_product_currency($product_id, $new_currency_country_id, $new_price ){
global $wpdb;

$old_curr = get_product_meta($product_id, 'currency',true);
$sql = $wpdb->prepare( "SELECT `isocode` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id`= %d", $newCurrency );
$isocode = $wpdb->get_var($sql);

$newCurrency = 'currency';
$old_curr[$isocode] = $newPrice;
if(($newPrice != '') && ($newPrice > 0.00)){
update_product_meta($product_id, $newCurrency, $old_curr);
$old_currency_original_meta_value = get_product_meta( $product_id, 'currency', true );
if ( !is_array( $old_currency_original_meta_value ) ) {
$new_meta_value = array();
} else {
if((empty($old_curr[$isocode]) || 0.00 == $old_curr[$isocode]) && is_array($old_curr))
unset($old_curr[$isocode]);
update_product_meta($product_id, $newCurrency, $old_curr);

$new_meta_value = $old_currency_original_meta_value;
}

if ( ! empty( $new_currency_country_id ) ) {
$wpsc_country_new_currency = wpsc_get_country_object( $new_currency_country_id );
if ( $wpsc_country_new_currency ) {

$new_meta_value[ $wpsc_country_new_currency->get_isocode() ] = $new_price;

if ( ! empty( $new_price ) ) {
update_product_meta( $product_id, 'currency', $new_meta_value );
} else {
if ( ( empty( $new_meta_value[ $wpsc_country_new_currency->get_isocode() ] ) || 0.00 == $new_meta_value[ $wpsc_country_new_currency->get_isocode() ] ) && is_array( $new_meta_value ) ) {
unset( $new_meta_value[ $wpsc_country_new_currency->get_isocode() ] );
}
update_product_meta( $product_id, 'currency', $new_meta_value );
}
}
}
}

/**
Expand Down
7 changes: 4 additions & 3 deletions wpsc-admin/includes/save-data.functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,14 @@ function wpsc_save_category_set( $category_id, $tt_id ) {

if ( ! empty( $_POST['countrylist2'] ) && ( $category_id > 0 ) ) {
$AllSelected = false;
$countryList = $wpdb->get_col( "SELECT `id` FROM `" . WPSC_TABLE_CURRENCY_LIST . "`" );
$countryList = wpsc_get_country_objects();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LIkewise, here, seems wpsc_get_countries() would make more sense.


if ( $AllSelected != true ){
$all_counntry_ids = array_keys( $countryList );
$posted_countries = array_map( 'intval', $_POST['countrylist2'] );
$unselectedCountries = array_diff( $countryList, $posted_countries );
$unselectedCountries = array_diff( $all_counntry_ids, $posted_countries );
//find the countries that are selected
$selectedCountries = array_intersect( $countryList, $posted_countries );
$selectedCountries = array_intersect( $all_counntry_ids, $posted_countries );
wpsc_update_categorymeta( $category_id, 'target_market', $selectedCountries );
}

Expand Down
67 changes: 35 additions & 32 deletions wpsc-admin/includes/settings-tabs/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ public function __construct() {
}

private function get_regions() {
global $wpdb;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['country'] ) )
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['country'] ) ) {
$base_country = $_POST['country'];
else
} else {
$base_country = get_option( 'base_country' );
$from = WPSC_TABLE_REGION_TAX . ' AS r';
$join = WPSC_TABLE_CURRENCY_LIST . ' AS c';
$sql = $wpdb->prepare( "
SELECT r.id, r.name
FROM {$from}
INNER JOIN {$join} ON r.country_id = c.id AND c.isocode = %s
", $base_country );
$this->regions = $wpdb->get_results( $sql );
}

if ( ! empty( $base_country ) ) {
$wpsc_country = wpsc_get_country_object( $base_country );

if ( $wpsc_country && $wpsc_country->has_regions() ) {
$this->regions = $wpsc_country->get_regions();
}
}
}

public function display_region_drop_down() {
$base_region = get_option( 'base_region' );
if ( ! empty( $this->regions ) ):
?>
<select name='wpsc_options[base_region]'>
<?php foreach ( $this->regions as $region ): ?>
<option value='<?php echo esc_attr( $region->id ); ?>' <?php selected( $region->id, $base_region ); ?>><?php echo esc_html( $region->name ); ?></option>
<?php foreach ( $this->regions as $region_id => $wpsc_region ): ?>
<option value='<?php echo esc_attr( $region_id ); ?>' <?php selected( $region_id, $base_region ); ?>><?php echo esc_html( $wpsc_region->get_name() ); ?></option>
<?php endforeach ?>
</select>
<?php
endif;
}

public function display() {
global $wpdb;
$all_countries = wpsc_get_all_countries();
?>
<h3><?php echo esc_html_e( 'General Settings', 'wpsc' ); ?></h3>
<table class='wpsc_options form-table'>
Expand All @@ -59,10 +59,6 @@ public function display() {
</td>
</tr>

<?php
/* START OF TARGET MARKET SELECTION */
$countrylist = WPSC_Countries::get_countries_array( true, true );
?>
<tr>
<th scope="row">
<?php esc_html_e( 'Target Markets', 'wpsc' ); ?>
Expand All @@ -78,13 +74,13 @@ public function display() {
<?php printf( __( 'Select: <a href="%1$s" class="wpsc-select-all" title="All">All</a> <a href="%2$s" class="wpsc-select-none" title="None">None</a>' , 'wpsc') , esc_url( add_query_arg( array( 'selected_all' => 'all' ) ) ), esc_url( add_query_arg( array( 'selected_all' => 'none' ) ) ) ); ?>
</span><br />
<div id='wpsc-target-markets' class='ui-widget-content multiple-select'>
<?php foreach ( (array)$countrylist as $country ) : ?>
<?php if ( $country['visible'] == 1 ) : ?>
<input type='checkbox' id="countrylist2-<?php echo $country['id']; ?>" name='countrylist2[]' value='<?php echo $country['id']; ?>' checked='checked' />
<label for="countrylist2-<?php echo $country['id']; ?>"><?php esc_html_e( $country['country'] ); ?></label><br />
<?php foreach ( $all_countries as $country_id => $wpsc_country ) : ?>
<?php if ( $wpsc_country->is_visible() ) : ?>
<input type='checkbox' id="countrylist2-<?php echo $country_id; ?>" name='countrylist2[]' value='<?php echo $country_id; ?>' checked='checked' />
<label for="countrylist2-<?php echo $country_id; ?>"><?php esc_html_e( $wpsc_country->get_name() ); ?></label><br />
<?php else : ?>
<input type='checkbox' id="countrylist2-<?php echo $country['id']; ?>" name='countrylist2[]' value='<?php echo $country['id']; ?>' />
<label for="countrylist2-<?php echo $country['id']; ?>"><?php esc_html_e( $country['country'] ); ?></label><br />
<input type='checkbox' id="countrylist2-<?php echo $country_id; ?>" name='countrylist2[]' value='<?php echo $country_id; ?>' />
<label for="countrylist2-<?php echo $country_id; ?>"><?php esc_html_e( $wpsc_country->get_name() ); ?></label><br />
<?php endif; ?>
<?php endforeach; ?>
</div>
Expand Down Expand Up @@ -133,27 +129,34 @@ public function display() {
<h3 class="form_group"><?php esc_html_e( 'Currency Settings', 'wpsc' ); ?></h3>
<table class='wpsc_options form-table'>
<?php
$currency_data = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` ORDER BY `country` ASC", ARRAY_A );
$currency_type = esc_attr( get_option( 'currency_type' ) );
$currency_type_country_id = wpsc_get_currency_type_country_id();
?>
<tr>
<th scope="row"><label for="wpsc_options_currency_type"><?php esc_html_e( 'Currency Type', 'wpsc' ); ?></label></th>
<td>
<select id="wpsc_options_currency_type" name='wpsc_options[currency_type]' onchange='getcurrency(this.options[this.selectedIndex].value);'>
<?php foreach ( $currency_data as $currency ) : ?>
<option value='<?php echo $currency['id']; ?>' <?php selected( $currency['id'], $currency_type ); ?>><?php esc_html_e( $currency['country'] ); ?> (<?php echo $currency['currency']; ?>)</option>
<?php foreach ( $all_countries as $country_id => $wpsc_country ) : ?>
<option value='<?php echo $country_id; ?>' <?php selected( $country_id, $currency_type_country_id ); ?>><?php esc_html_e( $wpsc_country->get_name() ); ?> (<?php echo $wpsc_country->get_currency_name(); ?>)</option>
<?php endforeach; ?>
</select>
</td>
</tr>

<?php
$currency_data = $wpdb->get_row( "SELECT `symbol`,`symbol_html`,`code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`='" . esc_attr( get_option( 'currency_type' ) ) . "' LIMIT 1", ARRAY_A );
$wpsc_country_of_currency_type = wpsc_get_currency_type_country_object();

if ( $wpsc_country_of_currency_type ) {
$currency_sign = $wpsc_country_of_currency_type->get_currency_symbol_html();

if ( empty( $currency_sign ) ) {
$currency_sign = $wpsc_country_of_currency_type->get_currency_symbol();
}

if ( $currency_data['symbol'] != '' ) {
$currency_sign = esc_attr( $currency_data['symbol_html'] );
if ( empty( $currency_sign ) ) {
$currency_sign = $wpsc_country_of_currency_type->get_currency_code();
}
} else {
$currency_sign = esc_attr( $currency_data['code'] );
$currency_sign = '';
}

$currency_sign_location = esc_attr( get_option( 'currency_sign_location' ) );
Expand Down
13 changes: 8 additions & 5 deletions wpsc-admin/includes/tax_and_shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
?>
<form action='' method='post' name='regional_tax' class='wpsc_form_track'>
<?php
$country_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode` IN(%s) LIMIT 1", $country_isocode ), ARRAY_A );
if(($country_data['has_regions'] == 1))
{
$region_data = $wpdb->get_results("SELECT `".WPSC_TABLE_REGION_TAX."`.* FROM `".WPSC_TABLE_REGION_TAX."` WHERE `".WPSC_TABLE_REGION_TAX."`.`country_id` IN('".$country_data['id']."') ",ARRAY_A) ;
$wpsc_country = wpsc_get_country_object( $country_isocode );
$country_data = $wpsc_country->get_array();

if( $wpsc_country->has_regions() ) {
$region_data = $wpsc_country->get_regions( true );

$region_data = array_chunk($region_data, 14);

echo "<table>\n\r";
Expand All @@ -32,8 +34,9 @@
{
echo " <td style='vertical-align: top; padding-right: 3em;'>\n\r";
echo "<table>\n\r";
foreach($region_col as $region)
foreach($region_col as $wpsc_region)
{
$region = $wpsc_region->as_array();
$tax_percentage = $region['tax'];
echo " <tr>\n\r";
if($region['id'] == $base_region)
Expand Down
17 changes: 13 additions & 4 deletions wpsc-admin/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,10 +866,19 @@ function wpsc_delete_coupon(){
add_action( 'admin_init', 'wpsc_delete_coupon' );
}

function _wpsc_action_update_option_base_country( $old_value, $new_value ) {
global $wpdb;
$region_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(`regions`.`id`) FROM `" . WPSC_TABLE_REGION_TAX . "` AS `regions` INNER JOIN `" . WPSC_TABLE_CURRENCY_LIST . "` AS `country` ON `country`.`id` = `regions`.`country_id` WHERE `country`.`isocode` IN('%s')", $new_value ) );
if ( ! $region_count )
/**
* Check to see if the base region need to be updated when the base country option is updated
* @param string $old_base_country_iso_code string
* @param string $new_base_country_iso_code string
*
*/
function _wpsc_action_update_option_base_country( $old_base_country_iso_code, $new_base_country_iso_code ) {
$wpsc_country = wpsc_get_country_object( $new_base_country_iso_code );

if ( ! $wpsc_country->has_regions() ) {
update_option( 'base_region', '' );
}

}

add_action( 'update_option_base_country', '_wpsc_action_update_option_base_country', 10, 2 );
Loading