Changeset 3441475
- Timestamp:
- 01/17/2026 07:49:05 AM (7 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
shipping-rate-by-zipcodes/trunk/zipcoderate-method-class.php
r3195397 r3441475 50 50 <th> 51 51 52 <form method="post" action=""> 53 <input type="hidden" name="export_csv" value="1"> 54 <button type="submit" name="submitexport" class="button button-primary">Export Zipcodes</button> 55 </form> 52 <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"> 53 <input type="hidden" name="action" value="zipcoderate_export"> 54 <?php wp_nonce_field( 'zipcoderate_export', 'zipcoderate_nonce' ); ?> 55 <button class="button button-primary">Export CSV</button> 56 </form> 56 57 57 58 … … 94 95 global $wpdb; 95 96 96 if(isset( $_POST['zipcodes'] )){97 97 98 $this->update_zipcodes(); 98 }99 99 100 100 101 $cities = $this->getZipcodes(); … … 183 184 </th> 184 185 <td> 185 <form method="post" enctype="multipart/form-data"> 186 <input type="file" name="import_file" id="ImportFile" accept=".csv"> 187 <button class="button-primary" type="submit" name="importsubmit" id="ImportBtn"> Import</button> 188 </form> 186 <form method="post" enctype="multipart/form-data" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"> 187 <input type="hidden" name="action" value="zipcoderate_import"> 188 <?php wp_nonce_field( 'zipcoderate_import', 'zipcoderate_nonce' ); ?> 189 <input type="file" name="import_file" accept=".csv" required> 190 <button class="button button-primary">Import CSV</button> 191 </form> 192 189 193 </td> 190 194 <tr> … … 215 219 } 216 220 217 function update_zipcodes(){ 218 global $wpdb; 219 220 $othercities = array_map( 'sanitize_text_field',$_POST['otherzipcode']); 221 $othercityfees = array_map( 'sanitize_text_field', $_POST['other_zipcode_fee']); 222 223 $table = $wpdb->prefix . "shiprate_zipcodes"; 224 foreach($othercities as $id1 => $otherciti){ 225 226 $othercity = [ 227 'zip_code' => $otherciti, 228 'rate' => $othercityfees[$id1] 229 ]; 230 231 $check1 = $wpdb->get_results("SELECT id FROM $table where id = '$id1' ORDER BY id ASC", OBJECT); 232 233 if($check1) 234 $result1 = $wpdb->update($table, $othercity, ['id' => $id1]); 235 else 236 $result1 = $wpdb->insert($table, $othercity); 237 } 238 239 $cities = array_map( 'sanitize_text_field', $_POST['zipcodes']); 240 $fees = array_map( 'sanitize_text_field', $_POST['zipcodes_fee'] ); 241 242 foreach($cities as $id => $citi){ 243 244 $city = [ 245 'zip_code' => $citi, 246 'rate' => $fees[$id] 247 ]; 248 $check = $wpdb->get_results("SELECT id FROM $table where id = '$id' ORDER BY id ASC", OBJECT); 249 250 if($check) 251 $result = $wpdb->update($table, $city, ['id' => $id]); 252 else 253 $result = $wpdb->insert($table, $city); 254 } 255 256 257 if(isset($_POST['delcity'])){ 258 $delcity = array_map( 'sanitize_text_field', $_POST['delcity']); 259 foreach($delcity as $del){ 260 $wpdb->delete( $table, ['id' => $del] ); 261 } 262 } 263 264 265 } 221 function update_zipcodes() { 222 global $wpdb; 223 224 if ( ! current_user_can( 'manage_woocommerce' ) ) { 225 return; 226 } 227 228 if ( 229 ! isset( $_POST['zipcoderate_nonce'] ) || 230 ! wp_verify_nonce( $_POST['zipcoderate_nonce'], 'zipcoderate_save_zipcodes' ) 231 ) { 232 return; 233 } 234 235 $table = $wpdb->prefix . 'shiprate_zipcodes'; 236 237 /* OTHER ZIPCODE */ 238 if ( isset( $_POST['otherzipcode'], $_POST['other_zipcode_fee'] ) ) { 239 240 $other_zipcodes = array_map( 'sanitize_text_field', wp_unslash( $_POST['otherzipcode'] ) ); 241 $other_fees = array_map( 'floatval', wp_unslash( $_POST['other_zipcode_fee'] ) ); 242 243 foreach ( $other_zipcodes as $id => $zipcode ) { 244 $id = absint( $id ); 245 246 if ( empty( $zipcode ) ) { 247 continue; 248 } 249 250 $exists = $wpdb->get_var( 251 $wpdb->prepare( "SELECT id FROM {$table} WHERE id = %d", $id ) 252 ); 253 254 if ( $exists ) { 255 $wpdb->update( 256 $table, 257 array( 'rate' => $other_fees[ $id ] ), 258 array( 'id' => $id ), 259 array( '%f' ), 260 array( '%d' ) 261 ); 262 } 263 } 264 } 265 266 /* ZIPCODES */ 267 if ( isset( $_POST['zipcodes'], $_POST['zipcodes_fee'] ) ) { 268 269 $zipcodes = array_map( 'sanitize_text_field', wp_unslash( $_POST['zipcodes'] ) ); 270 $fees = array_map( 'floatval', wp_unslash( $_POST['zipcodes_fee'] ) ); 271 272 foreach ( $zipcodes as $id => $zipcode ) { 273 $id = absint( $id ); 274 275 if ( empty( $zipcode ) ) { 276 continue; 277 } 278 279 $exists = $wpdb->get_var( 280 $wpdb->prepare( "SELECT id FROM {$table} WHERE id = %d", $id ) 281 ); 282 283 if ( $exists ) { 284 $wpdb->update( 285 $table, 286 array( 287 'zip_code' => $zipcode, 288 'rate' => $fees[ $id ], 289 ), 290 array( 'id' => $id ), 291 array( '%s', '%f' ), 292 array( '%d' ) 293 ); 294 } else { 295 $wpdb->insert( 296 $table, 297 array( 298 'zip_code' => $zipcode, 299 'rate' => $fees[ $id ], 300 ), 301 array( '%s', '%f' ) 302 ); 303 } 304 } 305 } 306 307 /* DELETE */ 308 if ( isset( $_POST['delcity'] ) ) { 309 $delete_ids = array_map( 'absint', wp_unslash( $_POST['delcity'] ) ); 310 311 foreach ( $delete_ids as $id ) { 312 $wpdb->delete( $table, array( 'id' => $id ), array( '%d' ) ); 313 } 314 } 315 } 316 266 317 267 318 /** … … 369 420 370 421 public function getZipcodeFee($city_name){ 371 global $wpdb; 372 $table = $wpdb->prefix . "shiprate_zipcodes"; 373 return $wpdb->get_row("SELECT rate FROM $table where zip_code = '$city_name'", ARRAY_A); 374 } 422 global $wpdb; 423 $table = $wpdb->prefix . "shiprate_zipcodes"; 424 return $wpdb->get_row("SELECT rate FROM $table where zip_code = '$city_name'", ARRAY_A); 425 } 426 375 427 376 428 public function getOtherZipcodeFee($city_name){ 377 global $wpdb; 378 $table = $wpdb->prefix . "shiprate_zipcodes"; 379 return $wpdb->get_row("SELECT rate FROM $table where zip_code = 'Other Zipcode'", ARRAY_A); 380 } 429 global $wpdb; 430 $table = $wpdb->prefix . "shiprate_zipcodes"; 431 return $wpdb->get_row("SELECT rate FROM $table where zip_code = 'Other Zipcode'", ARRAY_A); 432 } 433 381 434 } 382 435 }
Note: See TracChangeset
for help on using the changeset viewer.