Skip to content

Commit

Permalink
Merge pull request #50 from Beee4life/feature/0.21
Browse files Browse the repository at this point in the history
Feature/0.21
  • Loading branch information
Beee4life authored May 21, 2020
2 parents 164e041 + 5fb67a2 commit f2e6eab
Show file tree
Hide file tree
Showing 19 changed files with 19,544 additions and 438 deletions.
20 changes: 13 additions & 7 deletions ACF_City_Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: ACF City Selector
Plugin URI: https://acfcs.berryplasman.com
Description: An extension for ACF which allows you to select a city based on country and province/state.
Version: 0.20
Version: 0.21
Author: Beee
Author URI: https://berryplasman.com
Text Domain: acf-city-selector
Expand Down Expand Up @@ -33,7 +33,7 @@ class ACF_City_Selector {
public function __construct() {

$this->settings = array(
'version' => '0.20',
'version' => '0.21',
'url' => plugin_dir_url( __FILE__ ),
'path' => plugin_dir_path( __FILE__ ),
'upload_folder' => wp_upload_dir()[ 'basedir' ] . '/acfcs/',
Expand Down Expand Up @@ -707,15 +707,18 @@ public static function acfcs_admin_menu() {
}

if ( ! empty ( acfcs_check_if_files() ) ) {
// $current_page = ( isset( $acfcs_subpage ) && 'preview' == $acfcs_subpage ) ? ' class="current_page"' : false;
// $preview = ' | <a href="' . $admin_url . 'acfcs-preview"' . $current_page . '>' . esc_html__( 'Preview', 'acf-city-selector' ) . '</a>';
$current_page = ( isset( $acfcs_subpage ) && 'preview' == $acfcs_subpage ) ? ' class="current_page"' : false;
$preview = ' | <a href="' . $admin_url . 'acfcs-preview"' . $current_page . '>' . esc_html__( 'Preview', 'acf-city-selector' ) . '</a>';
}

$current_page = ( isset( $acfcs_subpage ) && 'info' == $acfcs_subpage ) ? ' class="current_page"' : false;
$info = ' | <a href="' . $admin_url . 'acfcs-info"' . $current_page . '>' . esc_html__( 'Info', 'acf-city-selector' ) . '</a>';

if ( true === $show_countries ) {
$countries = ' | <a href="' . $admin_url . 'acfcs-countries" class="cta">' . esc_html__( 'Get more countries', 'acf-city-selector' ) . '</a>';
}

$menu = '<p class="acfcs-admin-menu">' . $dashboard . $search . $preview . $settings . $countries . '</p>';
$menu = '<p class="acfcs-admin-menu">' . $dashboard . $search . $preview . $settings . $info . $countries . '</p>';

return $menu;
}
Expand All @@ -740,8 +743,8 @@ public function acfcs_add_admin_pages() {
include( 'inc/acfcs-dashboard.php' );
add_options_page( 'ACF City Selector', 'City Selector', 'manage_options', 'acfcs-dashboard', 'acfcs_dashboard' );

// include( 'inc/acfcs-preview.php' );
// add_submenu_page( null, 'Preview data', 'Preview data', 'manage_options', 'acfcs-preview', 'acfcs_preview_page' );
include( 'inc/acfcs-preview.php' );
add_submenu_page( null, 'Preview data', 'Preview data', 'manage_options', 'acfcs-preview', 'acfcs_preview_page' );

include( 'inc/acfcs-settings.php' );
add_submenu_page( null, 'Settings', 'Settings', 'manage_options', 'acfcs-settings', 'acfcs_settings' );
Expand All @@ -751,6 +754,9 @@ public function acfcs_add_admin_pages() {
add_submenu_page( null, 'City Overview', 'City Overview', 'manage_options', 'acfcs-search', 'acfcs_search' );
}

include( 'inc/acfcs-info.php' );
add_submenu_page( null, 'Info', 'Info', 'manage_options', 'acfcs-info', 'acfcs_info_page' );

include( 'inc/acfcs-countries.php' );
add_submenu_page( null, 'Get countries', 'Get countries', 'manage_options', 'acfcs-countries', 'acfcs_country_page' );
}
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Welcome to the City Selector plugin, which is an extension for [Advanced Custom
<a name="version"></a>
### Version

0.20 - released 14.05.20
0.21 - released 21.05.20

<a name="description"></a>
### Description
Expand Down Expand Up @@ -193,6 +193,9 @@ The plugin works in the following situations:

The plugin hasn't been tested yet in the following situations:
* as a repeater field on a user page
* as a clone field
* on taxonomy pages
* on settings pages
* most front-end usage (except single use)
* with the Gutenberg editor (and don't hold your breath either, I hate it)

Expand All @@ -202,19 +205,27 @@ This is very random and unpredictable.
<a name="todo"></a>
### TODO

- [] Add select2 to dropdowns (including a search, like with a post object field)
- [] Select which fields to use; all, country + state or country + city or state city
- [X] Select which fields to use; all, country + state or country + city or state city
- [ ] Add explanation about how the field validation works
- [ ] Add select2 to dropdowns (including a search, like with a post object field)

<a name="credit"></a>
### Credit

I got the idea for this plugin through [Fabrizio Sabato](https://github.com/fab01) who used it a bit differently, which can ben seen [here](http://www.deskema.it/en/articles/multi-level-country-state-city-cascading-select-wordpress).

Since I couldn't fix the Javascript for this plugin, [Jarah de Jong](https://github.com/inquota) took care of the JS basics.
[Jarah de Jong](https://github.com/inquota) helped me out with the JS basics and [John McDonald](https://github.com/mrjohnmc) with the German translation.

<a name="changelog"></a>
### Changelog

0.21
* fixed error in verification on preview page + added page back
* added natural sorting for cities
* added option to select which fields to use (all/country only/country + state/country + city)
* added an info page with info for debug/support
* added German translation

0.20
* removed a check on length state code which falsed on countries like France, Spain and Australia
* temporarily removed preview page since it incorrectly deleted files
Expand Down
33 changes: 27 additions & 6 deletions assets/css/acf-city-selector.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
margin-bottom: 1em;
}

.acf-field.acf-field-acf-city-selector select {
padding: 4px;
}

.acf-field-acf-city-selector .acf-input-header {
margin-bottom: 10px;
}
Expand All @@ -26,12 +30,12 @@
.admin_left {
float: left;
position: relative;
width: 70%;
width: 75%;
}

.admin_right {
float: right;
max-width: 25%;
max-width: 22.5%;
}

.acfcs input[type="checkbox"] {
Expand Down Expand Up @@ -81,8 +85,8 @@
padding: 0 0 0 5px;
}

.acfcs__search-criteria--limit {
margin-right: 1em;
.acfcs__search-criteria--orderby {
margin-right: 1.25em;
}

.acfcs__search-criteria--or,
Expand All @@ -97,12 +101,24 @@
width: 100%;
}

.acfcs table.acfcs__table--packages,
.acfcs table.acfcs__table--preview-form,
.acfcs table.acfcs__table--search,
.acfcs table.acfcs__table--uploaded {
max-width: 60%;
}

.acfcs table.acfcs__table--packages {
max-width: 75%;
}

.acfcs table.acfcs__table--info {
margin-bottom: 2em;
max-width: 450px;
}

.acfcs table.acfcs__table--info:last-child {
margin-bottom: 0;
}

.acfcs table.acfcs__table--uploaded {
margin-bottom: 1em;
}
Expand All @@ -112,6 +128,11 @@
margin-bottom: 1em;
}

.acfcs table.acfcs__table--search tr td:nth-child(1),
.acfcs table.acfcs__table--search tr td:nth-child(2) {
width: 60px;
}

.acfcs table.acfcs__table th {
text-align: left;
}
Expand Down
93 changes: 67 additions & 26 deletions assets/js/city-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
if (countries.length) {
countries.on('change', function () {
const response_cities = []
const response_states = []
var $this = $(this);
var country_code = $this.val();
Expand All @@ -45,42 +46,78 @@
var city_field_id = country_field_id.replace( 'countryCode', 'cityName' );
var changed_state = $('select[id="' + state_field_id + '"]');
var changed_city = $('select[id="' + city_field_id + '"]');
const d = get_states(country_code);
response_states.push(d)

Promise.all(response_states).then(function(jsonResults) {
for (i = 0; i < jsonResults.length; i++) {
var obj = JSON.parse(jsonResults[i]);
var len = obj.length;
var $stateValues = '';

changed_city.empty();
changed_city.fadeIn();
changed_state.empty();
changed_state.fadeIn();
for (j = 0; j < len; j++) {
$selected = '';
var state = obj[j];
$stateValues += '<option value="' + state.country_state + '">' + state.state_name + '</option>';
var which_fields = 'all';

if(typeof(city_selector_vars) != "undefined" && city_selector_vars !== null) {
var which_fields = city_selector_vars[ 'which_fields' ];
}

if ( jQuery.inArray(which_fields, [ 'country_state', 'all' ] ) !== -1 ) {
const d = get_states(country_code);
response_states.push(d)

Promise.all(response_states).then(function(jsonResults) {
for (i = 0; i < jsonResults.length; i++) {
var obj = JSON.parse(jsonResults[i]);
var len = obj.length;
var $stateValues = '';

changed_city.empty();
changed_city.fadeIn();
changed_state.empty();
changed_state.fadeIn();
for (j = 0; j < len; j++) {
$selected = '';
var state = obj[j];
$stateValues += '<option value="' + state.country_state + '">' + state.state_name + '</option>';
}
changed_state.append($stateValues);
// this string 'comes' from fields/acf-city-selector-v5.php
var i18n_select_city = acf._e('acf_city_selector', 'i18n_select_city');
$select_city = '<option value="">' + i18n_select_city + '</option>';
changed_city.append($select_city);
}
changed_state.append($stateValues);
// this string 'comes' from fields/acf-city-selector-v5.php
var i18n_select_city = acf._e('acf_city_selector', 'i18n_select_city');
$select_city = '<option value="">' + i18n_select_city + '</option>';
changed_city.append($select_city);
}
});
});

} else if ( jQuery.inArray(which_fields, [ 'country_city' ] ) !== -1 ) {
const d = get_cities(country_code);
response_cities.push(d)

Promise.all(response_cities).then(function(jsonResults) {
for (i = 0; i < jsonResults.length; i++) {
var obj = JSON.parse(jsonResults);
var len = obj.length;
var $cityValues = '';

changed_city.empty();
changed_city.fadeIn();
for (j = 0; j < len; j++) {
var city = obj[j];
if ( j === 0 ) {
$cityValues += '<option value="">' + city.city_name + '</option>';
} else {
$cityValues += '<option value="' + city.city_name + '">' + city.city_name + '</option>';
}
}
changed_city.append($cityValues);
}
});
}

});
}

// if there are any selects with name*=stateCode
if (state.length) {
state.on('change', function () {

// @TODO: add if for when city isn't needed

const response_cities = []
var $this = $(this);
var state_code = $this.val();
var state_field_id = $this.attr('id');
var city_field_id = state_field_id.replace( 'stateCode', 'cityName' );
var city_field_id = state_field_id.replace('stateCode', 'cityName');
var changed_city = $('select[id="' + city_field_id + '"]');
const d = get_cities(state_code);
response_cities.push(d)
Expand All @@ -95,7 +132,11 @@
changed_city.fadeIn();
for (j = 0; j < len; j++) {
var city = obj[j];
$cityValues += '<option value="' + city.city_name + '">' + city.city_name + '</option>';
if ( j === 0 ) {
$cityValues += '<option value="">' + city.city_name + '</option>';
} else {
$cityValues += '<option value="' + city.city_name + '">' + city.city_name + '</option>';
}
}
changed_city.append($cityValues);
}
Expand Down
Loading

0 comments on commit f2e6eab

Please sign in to comment.