postal-search-tool.php 1.63 KB
<?php
/**
 * Quotes Block Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during AJAX preview.
 * @param   (int|string) $post_id The post ID this block is saved to.
 */

// Create id attribute allowing for custom "anchor" value.
$id = 'postal-search-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = 'postal-search';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
}
if( $is_preview ) {
    $className .= ' is-admin';
}

$postal_lookup = [];
$row = 0;

if (($handle = fopen(wp_get_upload_dir()['basedir']."/terriroties.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $row++;
        if($row == 1) {
            continue;
        }
        $postal_lookup[] = ['GEOKEY'=>$data[0],'PREFERRED'=>$data[3]];
    }
    fclose($handle);
}

?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">


    <div class='postal-code-inputs'>
        <label>
            <span class='label'>Enter Full Postal Code:</span>
            <input type='text' class='postal-code-search' />
        </label>

        <label>
            <span class='label'>Preferred?</span>
            <input type='text' class='preferred' />

        </label>
    </div>

    <input type='hidden' class='postal-search-data' value='<?= json_encode($postal_lookup) ?>' />

</div>