function tt_render_list_page() { global $SUCCESS; //Create an instance of our package class... $zoneListTable = new Zone_List_Table(); //Fetch, prepare, sort, and filter our data... $zoneListTable->prepare_items(); ?> <div class="wrap woocommerce"> <div id="icon-users" class="icon32"><br/></div> <h2>Shipping Zones <a href="?page=<?php echo $_REQUEST['page']; ?> &tab=shipping_zones&action=new" class="add-new-h2">Add New</a></h2> </form> <?php if (isset($_GET['upgrade']) && $_GET['upgrade'] == 'true') { ?> <div class="updated" style="font-weight:bold;"> <p><?php _e('Your zones have been updated. Please test your forms to ensure that everything is in working order.', 'be_table_rate'); ?> </p> </div> <?php } ?> <?php if (isset($_GET['action']) && $_GET['action'] == 'delete') { ?> <?php if (isset($SUCCESS) && $SUCCESS) { ?> <div class="updated" style="font-weight:bold;"> <p><?php _e('The selected zone(s) have been deleted.', 'be_table_rate'); ?> </p> </div> <?php } else { ?> <div class="error" style="font-weight:bold;"> <p><?php _e('An error has occurred and the selected zone(s) were not deleted.', 'be_table_rate'); ?> </p> </div> <?php } ?> <?php } ?> <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions --> <form id="zones-filter" method="post" action=""> <!-- For plugins, we also need to ensure that the form posts back to our current page --> <input type="hidden" name="page" value="<?php echo $_REQUEST['page']; ?> " /> <!-- Now we can render the completed list table --> <?php $zoneListTable->display(); ?> <?php wp_nonce_field('woocommerce-settings', '_wpnonce', true, true); ?> <p class="submit"> <input name="save" class="button-primary" type="submit" value="Save changes" /> <input type="hidden" name="subtab" id="last_tab" /> </p> </form> </div> <script> jQuery(function() { var fixHelperModified = function(e, tr) { var $originals = tr.children(); var $helper = tr.clone(); $helper.children().each(function(index) { jQuery(this).width($originals.eq(index).width()) }); return $helper; }; jQuery("#the-list").sortable({ helper: fixHelperModified }).disableSelection(); }); </script> <?php }
function be_save_new_zone() { global $woocommerce, $SUCCESS; $shipping_zones = array_filter((array) get_option('be_woocommerce_shipping_zones')); if (isset($_POST['action'])) { if ('delete' === Zone_List_Table::current_action()) { if (is_array($_POST['zone']) && count($_POST['zone']) > 0) { foreach ($_POST['zone'] as $value) { if (isset($shipping_zones[$value]) && is_array($shipping_zones[$value])) { unset($shipping_zones[$value]); $SUCCESS = true; } } update_option('be_woocommerce_shipping_zones', $shipping_zones); } $redirect = add_query_arg(array('saved' => $SUCCESS, 'action' => false, 'zone' => false)); } else { $i = 1; if (count($_POST['zone_id']) > 0) { $new_order = array(); foreach ($_POST['zone_id'] as $value) { $zone_id = (int) $value; if (isset($shipping_zones[$zone_id]) && is_array($shipping_zones[$zone_id])) { $new_order[$zone_id] = $shipping_zones[$zone_id]; $new_order[$zone_id]['zone_order'] = $i; $i++; } } update_option('be_woocommerce_shipping_zones', $new_order); // reorder table of rates to reflect new zone order $currentRates = array_filter((array) get_option('woocommerce_table_rates')); foreach ($currentRates as $key => $value) { $currentRates[$key]['zone_order'] = $new_order[$value['zone']]['zone_order']; } $newRates = BE_Table_Rate_Shipping::sort_table_rates($currentRates); update_option('woocommerce_table_rates', $newRates); } $SUCCESS = true; $redirect = add_query_arg(array('saved' => 'true', 'action' => false, 'zone' => false)); } } elseif (isset($_GET['action']) && $_GET['action'] == 'delete') { if (isset($_GET['zone'])) { $zone_id = (int) $_GET['zone']; if (isset($shipping_zones[$zone_id]) && is_array($shipping_zones[$zone_id])) { unset($shipping_zones[$zone_id]); update_option('be_woocommerce_shipping_zones', $shipping_zones); $SUCCESS = true; $redirect = add_query_arg(array('saved' => 'true', 'action' => false, 'zone' => false)); } } } elseif (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) { $totalitems = count($shipping_zones); $max_keys = array(); $zone_id_posted = (int) $_POST['zone_id']; $zone_enabled = isset($_POST['zone_enabled']) ? '1' : '0'; $zone_title = sanitize_text_field($_POST['zone_title']); $zone_description = sanitize_text_field($_POST['zone_description']); $zone_type = sanitize_text_field($_POST['zone_type']); if (!isset($shipping_zones[$zone_id_posted]) || !isset($shipping_zones[$zone_id_posted]['zone_order'])) { if (count($shipping_zones) > 0) { foreach ($shipping_zones as $value) { $max_keys[] = $value['zone_order']; } $zone_order_max = max($max_keys); } else { $zone_order_max = 0; } } else { $zone_order_max = $shipping_zones[$zone_id_posted]['zone_order']; } if ($zone_type == 'countries') { $zone_country = isset($_POST['location_countries']) ? (array) $_POST['location_countries'] : array(); $zone_country = implode(',', $zone_country); $zone_country_except = isset($_POST['location_countries_exceptS']) ? (array) $_POST['location_countries_exceptS'] : array(); $zone_country_except = implode(',', $zone_country_except); $zone_postal_except = sanitize_text_field($_POST['location_countries_except']); //$zone_postal_except = preg_replace( '/\s+/', '', $zone_postal_except ); $zone_except = array('states' => $zone_country_except, 'postals' => $zone_postal_except); $zone_postal = ''; } elseif ($zone_type == 'postal') { $zone_country = sanitize_text_field($_POST['location_country']); $zone_postal = sanitize_text_field($_POST['location_codes']); //$zone_postal = preg_replace( '/\s+/', '', $zone_postal ); $zone_except = isset($_POST['location_postal_except']) ? sanitize_text_field($_POST['location_postal_except']) : ''; //$zone_except = preg_replace( '/\s+/', '', $zone_except ); } else { $zone_country = $zone_postal = ""; $zone_except = isset($_POST['location_everywhere_except']) ? (array) $_POST['location_everywhere_except'] : array(); $zone_except = implode(',', $zone_except); } $shipping_zones[$zone_id_posted] = array('zone_id' => $zone_id_posted, 'zone_enabled' => $zone_enabled, 'zone_title' => $zone_title, 'zone_description' => $zone_description, 'zone_type' => $zone_type, 'zone_country' => $zone_country, 'zone_postal' => $zone_postal, 'zone_except' => $zone_except, 'zone_order' => $_GET['action'] == 'edit' ? $shipping_zones[$zone_id_posted]['zone_order'] : $zone_order_max + 1); update_option('be_woocommerce_shipping_zones', $shipping_zones); // Clear any unwanted data wc_delete_product_transients(); if (!isset($zone_id) || $zone_id == 0) { $zone_id = $zone_id_posted; } delete_transient('woocommerce_cache_excluded_uris'); $zone_id = $zone_id_posted == 0 ? $zone_id_max : $zone_id_posted; // Redirect back to the settings page $SUCCESS = true; $redirect = add_query_arg(array('saved' => 'true', 'action' => 'edit', 'zone' => $zone_id)); } //wp_safe_redirect( $redirect ); //exit; }