/** * Create a custom metabox to input the location for this post. */ function wanderlist_geolocation_box() { global $post; wp_nonce_field(basename(__FILE__), 'meta-box-nonce'); // Input field (also includes our user's Mapbox key so JS can grab it) // @todo: Spit out an error message if the API key isn't properly set. $options = get_option('wanderlist_settings'); ?> <input id="wanderlist-geolocation-input" data-mapboxkey="<?php esc_attr_e($options['wanderlist_mapbox_key']); ?> " type="hidden" name="wanderlist-geolocation"> <div id="wanderlist-geolocation-map"> <a href="#" id="wanderlist-locate-user"><span class="screen-reader-text">Find me</span></a> <div class="wanderlist-loader"> <span class="wanderlist-loader-circle"></span> <span class="wanderlist-loader-circle"></span> </div> </div> <?php // Message field to show what's going on behind-the-scenes. ?> <div id="wanderlist-geocoder-message" class="wanderlist-message"> <?php printf('<span class="success-message">%s <strong class="place"></strong>.</span>', esc_html__('Your location has been set to', 'wanderlist')); ?> <span class="error-message"></span> </div> <?php // Hidden fields into which we can input data returned from our geocoder. ?> <input id="wanderlist-city" name="wanderlist-city" type="hidden" value="<?php esc_attr_e(get_post_meta($post->ID, 'wanderlist-city', true)); ?> " /> <input id="wanderlist-region" name="wanderlist-region" type="hidden" value="<?php esc_attr_e(get_post_meta($post->ID, 'wanderlist-region', true)); ?> " /> <input id="wanderlist-lng" name="wanderlist-lng" type="hidden" value="<?php esc_attr_e(get_post_meta($post->ID, 'wanderlist-lng', true)); ?> " /> <input id="wanderlist-lat" name="wanderlist-lat" type="hidden" value="<?php esc_attr_e(get_post_meta($post->ID, 'wanderlist-lat', true)); ?> " /> <input id="wanderlist-country" name="wanderlist-country" type="hidden" value="<?php esc_attr_e(wanderlist_place_data('country')); ?> " /> <?php }
/** * Count something. Right now, we're using it for countries, continents, and places, * but we'll probably expand in the future. */ function wanderlist_count($thing) { switch ($thing) { // Get every place we've visited up to today. case 'places': $args = array('post_type' => 'wanderlist-location', 'meta_key' => 'wanderlist-arrival-date', 'meta_value' => wanderlist_today(), 'meta_compare' => '<=', 'posts_per_page' => -1); $place_query = new WP_Query($args); // Create an array of unique places. $places = array(); while ($place_query->have_posts()) { $place_query->the_post(); $city_name = get_post_meta(get_the_ID(), 'wanderlist-city', true); // If we've already been to this place, don't add it to our array. if (!in_array($city_name, $places)) { $places[] = get_post_meta(get_the_ID(), 'wanderlist-city', true); } wp_reset_postdata(); } return count($places); break; case 'countries': // Count all countries in the database. $countries = get_terms('wanderlist-country', array('hide_empty' => true, 'childless' => true)); // Get an array of places we're visiting in the future. $options = get_option('wanderlist_settings'); $args = array('posts_per_page' => -1, 'post_type' => 'wanderlist-location', 'meta_key' => 'wanderlist-arrival-date', 'meta_value' => wanderlist_today(), 'meta_compare' => '>'); $future_country_query = new WP_Query($args); // For each place we're visiting in the future, get a list of countries. $future_countries = array(); while ($future_country_query->have_posts()) { $future_country_query->the_post(); $country = wanderlist_place_data('country', get_the_ID()); // Make sure each country only appears once in the array. if (!in_array($country, $future_countries)) { $future_countries[] = $country; } wp_reset_postdata(); } // For each of these future countries, check to see if we have any places visited in the past. foreach ($future_countries as $future_country) { $args = array('posts_per_page' => -1, 'post_type' => 'wanderlist-location', 'meta_key' => 'wanderlist-arrival-date', 'meta_value' => wanderlist_today(), 'meta_compare' => '<=', 'tax_query' => array(array('taxonomy' => 'wanderlist-country', 'field' => 'slug', 'terms' => sanitize_title($future_country)))); $visited_country_query = new WP_Query($args); while ($visited_country_query->have_posts()) { $visited_country_query->the_post(); $country = wanderlist_place_data('country', get_the_ID()); // Remove countries we've already visited from our array of future countries. if (($key = array_search($country, $future_countries)) !== false) { unset($future_countries[$key]); } wp_reset_postdata(); } } // Finally, return a total count of countries, minus those countries we're only visiting in the future. return count($countries) - count($future_countries); break; // Count all continents visited // @todo: Account for edge cases, of which there are many! // Count all continents visited // @todo: Account for edge cases, of which there are many! case 'continents': // Get our countries to start $countries = get_terms('wanderlist-country', array('hide_empty' => true, 'childless' => true)); // Create an array of all continents we've visited $continents = array(); foreach ($countries as $country) { $continent = wanderlist_iso_data($country->name, 'continent'); // If we've found a continent and it's not already in our array, add it if ($continent && !in_array($continent, $continents)) { $continents[] = $continent; } } return count($continents); break; } }