Site Builder
Editing:
Geo-JSON-Exporter2.txt
writable 0666
<?php /* Plugin Name: Geo JSON Exporter Description: Exports a JSON file per US state from the city table for fast AJAX geo navigation. Output goes to /geo/json-data/{STATE}.json. Also exports /geo/json-data/states-bounds.json for fast geolocation. Version: 1.2 Author: BestDealOn Dev Team */ if ( ! defined('ABSPATH') ) exit; define('GJE_TABLE', 'kVTr1OdFj_city'); // Change to your table name if needed define('GJE_OUTDIR', $_SERVER['DOCUMENT_ROOT'] . '/geo/json-data/'); if (!is_dir(GJE_OUTDIR)) mkdir(GJE_OUTDIR, 0775, true); add_action('admin_menu', function() { add_management_page( 'Geo JSON Exporter', 'Geo JSON Exporter', 'manage_options', 'geo_json_exporter', 'gje_admin_page' ); }); function gje_admin_page() { global $wpdb; $states = $wpdb->get_col("SELECT DISTINCT subdiv_id FROM " . GJE_TABLE . " WHERE country_id='USA' ORDER BY subdiv_id"); ?> <div class="wrap" id="gje-app"> <h1>Geo JSON Exporter</h1> <p>This tool exports one <code>.json</code> file per state into <code>/geo/json-data/</code> for ultra-fast city/county navigation.<br> After exporting states, click <b>Generate State Boundaries JSON</b> to build <code>states-bounds.json</code> for fast geolocation.</p> <div> <label for="gje-state"><b>State:</b></label> <select id="gje-state"> <option value="">All States</option> <?php foreach($states as $state): ?> <option value="<?php echo esc_attr($state); ?>"><?php echo esc_html($state); ?></option> <?php endforeach; ?> </select> <button id="gje-export" class="button button-primary">Export</button> <button id="gje-export-bounds" class="button">Generate State Boundaries JSON</button> </div> <div id="gje-status" style="margin-top:20px;font-family:monospace;white-space:pre;"></div> </div> <script> (function($){ $('#gje-export').on('click', function(){ var state = $('#gje-state').val(); $('#gje-status').text('Working...'); $.post(ajaxurl, { action: 'gje_export_json', state: state }, function(data){ if (data.success) { $('#gje-status').text(data.data.status); } else { $('#gje-status').text('Error: ' + (data.data ? data.data.status : 'Unknown error')); } }); }); $('#gje-export-bounds').on('click', function(){ $('#gje-status').text('Generating boundaries...'); $.post(ajaxurl, { action: 'gje_export_state_bounds' }, function(data){ if (data.success) { $('#gje-status').text(data.data.status); } else { $('#gje-status').text('Error: ' + (data.data ? data.data.status : 'Unknown error')); } }); }); })(jQuery); </script> <?php } // AJAX: Export JSON (single state or all) add_action('wp_ajax_gje_export_json', function() { global $wpdb; $state = isset($_POST['state']) ? sanitize_text_field($_POST['state']) : ''; $outdir = GJE_OUTDIR; $status = ''; $states = $state ? [$state] : $wpdb->get_col("SELECT DISTINCT subdiv_id FROM " . GJE_TABLE . " WHERE country_id='USA'"); foreach ($states as $st) { $rows = $wpdb->get_results($wpdb->prepare( "SELECT city_title, city_lat, city_lon, city_id FROM " . GJE_TABLE . " WHERE country_id='USA' AND subdiv_id=%s", $st )); $arr = []; foreach($rows as $r) { $slug = preg_replace('/[^a-z0-9]+/i','-', strtolower($r->city_title)); $arr[] = [ 'city' => $r->city_title, 'lat' => floatval($r->city_lat), 'lon' => floatval($r->city_lon), 'slug' => $slug, 'url' => "/geo/USA/{$st}/{$slug}/" ]; } $outfile = $outdir . $st . '.json'; file_put_contents($outfile, json_encode($arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); $status .= "Exported " . count($arr) . " cities to $outfile\n"; } wp_send_json_success(['status' => $status]); }); // AJAX: Export state boundaries JSON (50 states + DC only) add_action('wp_ajax_gje_export_state_bounds', function() { global $wpdb; $table = GJE_TABLE; $outdir = GJE_OUTDIR; $states = $wpdb->get_col("SELECT DISTINCT subdiv_id FROM $table WHERE country_id='USA'"); $out = []; $valid_states = [ "AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO", "MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","DC" ]; $state_names = [ "AL"=>"Alabama","AK"=>"Alaska","AZ"=>"Arizona","AR"=>"Arkansas","CA"=>"California","CO"=>"Colorado","CT"=>"Connecticut", "DE"=>"Delaware","FL"=>"Florida","GA"=>"Georgia","HI"=>"Hawaii","ID"=>"Idaho","IL"=>"Illinois","IN"=>"Indiana","IA"=>"Iowa", "KS"=>"Kansas","KY"=>"Kentucky","LA"=>"Louisiana","ME"=>"Maine","MD"=>"Maryland","MA"=>"Massachusetts","MI"=>"Michigan", "MN"=>"Minnesota","MS"=>"Mississippi","MO"=>"Missouri","MT"=>"Montana","NE"=>"Nebraska","NV"=>"Nevada","NH"=>"New Hampshire", "NJ"=>"New Jersey","NM"=>"New Mexico","NY"=>"New York","NC"=>"North Carolina","ND"=>"North Dakota","OH"=>"Ohio","OK"=>"Oklahoma", "OR"=>"Oregon","PA"=>"Pennsylvania","RI"=>"Rhode Island","SC"=>"South Carolina","SD"=>"South Dakota","TN"=>"Tennessee", "TX"=>"Texas","UT"=>"Utah","VT"=>"Vermont","VA"=>"Virginia","WA"=>"Washington","WV"=>"West Virginia","WI"=>"Wisconsin","WY"=>"Wyoming", "DC"=>"District of Columbia" ]; foreach($states as $st) { if (!in_array($st, $valid_states)) continue; $res = $wpdb->get_row($wpdb->prepare( "SELECT MIN(city_lat) as minLat, MAX(city_lat) as maxLat, MIN(city_lon) as minLon, MAX(city_lon) as maxLon FROM $table WHERE country_id='USA' AND subdiv_id=%s", $st )); $out[] = [ "state" => $st, "name" => isset($state_names[$st]) ? $state_names[$st] : $st, "minLat" => round(floatval($res->minLat), 6), "maxLat" => round(floatval($res->maxLat), 6), "minLon" => round(floatval($res->minLon), 6), "maxLon" => round(floatval($res->maxLon), 6) ]; } $outfile = $outdir . 'states-bounds.json'; file_put_contents($outfile, json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); wp_send_json_success(['status' => 'Exported boundaries for ' . count($out) . ' states to ' . $outfile]); });
Save changes
Create folder
writable 0777
Create
Cancel