Skip to content

Commit

Permalink
Merge pull request #120 from Beee4life/feature/v1.10.0
Browse files Browse the repository at this point in the history
Feature/v1.10.0
  • Loading branch information
Beee4life authored Dec 29, 2022
2 parents 84d0184 + b018fa0 commit 61af52c
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 150 deletions.
21 changes: 5 additions & 16 deletions ACF_City_Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Plugin Name: ACF City Selector
Plugin URI: https://acf-city-selector.com
Description: An extension for ACF which allows you to select a city based on country and province/state.
Version: 1.9.1
Tested up to: 6.0.2
Version: 1.10.0
Tested up to: 6.1.1
Requires PHP: 7.0
Author: Beee
Author URI: https://berryplasman.com
Expand Down Expand Up @@ -35,7 +35,7 @@ public function __construct() {
$this->settings = array(
'db_version' => '1.0',
'url' => plugin_dir_url( __FILE__ ),
'version' => '1.9.1',
'version' => '1.10.0',
);

if ( ! class_exists( 'ACFCS_WEBSITE_URL' ) ) {
Expand Down Expand Up @@ -111,17 +111,6 @@ public function acfcs_plugin_deactivation() {
}


/*
* Prepare database upon plugin activation
*/
public function acfcs_fill_database() {
$countries = array( 'nl', 'be' );
foreach( $countries as $country ) {
acfcs_import_data( $country . '.csv', ACFCS_PLUGIN_PATH . 'import/' );
}
}


/*
* Check if table exists
*/
Expand Down Expand Up @@ -269,7 +258,7 @@ public function acfcs_include_field_types( $version = false ) {
* @return array
*/
public function acfcs_settings_link( $links ) {
$settings_link = [ 'settings' => '<a href="options-general.php?page=acfcs-dashboard">' . esc_html__( 'Settings', 'acf-city-selector' ) . '</a>' ];
$settings_link = [ 'settings' => sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=acfcs-dashboard' ), esc_html__( 'Settings', 'acf-city-selector' ) ) ];

return array_merge( $settings_link, $links );
}
Expand Down Expand Up @@ -382,7 +371,7 @@ public function acfcs_change_plugin_order() {
* @param $from_index
* @param $to_index
*/
public static function acfcs_move_array_element( &$array, $from_index, $to_index ) {
public function acfcs_move_array_element( &$array, $from_index, $to_index ) {
$splice = array_splice( $array, $from_index, 1 );
array_splice( $array, $to_index, 0, $splice );
}
Expand Down
10 changes: 7 additions & 3 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

1.9.1 - released 13.10.22
1.10.0 - released 29.12.22

<a name="description"></a>
### Description
Expand Down Expand Up @@ -183,8 +183,8 @@ This ACF field type is compatible/tested with ACF 5 (Pro). It's slightly tested
<a name="tested"></a>
### Tested with

* [X] Wordpress 6.0.2
* [X] Advanced Custom Fields Pro 6.0.2
* [X] Wordpress 6.1.1
* [X] Advanced Custom Fields Pro 6.0.5
* [X] Advanced Custom Fields 4.4.12

<a name="support"></a>
Expand Down Expand Up @@ -228,6 +228,10 @@ I got the idea for this plugin through [Fabrizio Sabato](https://github.com/fab0
<a name="changelog"></a>
### Changelog

1.10.0
* add option to store location as single meta values
* fix ordering for states (which caused some issues)

1.9.1
* fixed incorrect version

Expand Down
69 changes: 40 additions & 29 deletions admin/acf-city-selector-v5.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Main class
*/
class acf_field_city_selector extends acf_field {

/*
* Function index
* - construct( $settings )
Expand All @@ -25,17 +24,17 @@ class acf_field_city_selector extends acf_field {
/**
* acf_field_city_selector constructor
*
* This function will setup the class functionality
* This function will set up the class functionality
*
* @param $settings
*/
function __construct( $settings ) {

$this->name = 'acf_city_selector';
$this->label = 'City Selector';
$this->category = esc_attr__( 'Choice', 'acf-city-selector' );
$this->defaults = array(
'show_labels' => 1,
'store_meta' => 0,
'which_fields' => 'all',
'use_select2' => 0,
);
Expand All @@ -45,9 +44,9 @@ function __construct( $settings ) {
$this->settings = $settings;

parent::__construct();

}


/**
* render_field_settings()
*
Expand All @@ -56,7 +55,6 @@ function __construct( $settings ) {
* @param $field (array) the $field being edited
*/
function render_field_settings( $field ) {

$select_options = array(
1 => esc_attr__( 'Yes', 'acf-city-selector' ),
0 => esc_attr__( 'No', 'acf-city-selector' )
Expand All @@ -71,7 +69,7 @@ function render_field_settings( $field ) {
'value' => $field[ 'show_labels' ],
) );

acf_render_field_setting( $field, array(
acf_render_field_setting( $field, array(
'choices' => $select_options,
'instructions' => esc_html__( 'Use select2 for dropdowns', 'acf-city-selector' ),
'label' => esc_html__( 'Select2', 'acf-city-selector' ),
Expand All @@ -81,6 +79,16 @@ function render_field_settings( $field ) {
'value' => $field[ 'use_select2' ],
) );

acf_render_field_setting( $field, array(
'choices' => $select_options,
'instructions' => esc_html__( 'Store location as single meta values', 'acf-city-selector' ),
'label' => esc_html__( 'Store meta', 'acf-city-selector' ),
'layout' => 'horizontal',
'name' => 'store_meta',
'type' => 'radio',
'value' => $field[ 'store_meta' ],
) );

$countries = acfcs_get_countries( true, false, true );
acf_render_field_setting( $field, array(
'choices' => $countries,
Expand All @@ -106,6 +114,7 @@ function render_field_settings( $field ) {
) );
}


/**
* render_field()
*
Expand All @@ -114,17 +123,17 @@ function render_field_settings( $field ) {
* @param $field (array) the $field being edited
*/
function render_field( $field ) {

$default_country = ( isset( $field[ 'default_country' ] ) && ! empty( $field[ 'default_country' ] ) ) ? $field[ 'default_country' ] : false;
$prefill_cities = array();
$prefill_states = array();
$selected_country = ( isset( $field[ 'value' ][ 'countryCode' ] ) ) ? $field[ 'value' ][ 'countryCode' ] : false;
$selected_state = ( isset( $field[ 'value' ][ 'stateCode' ] ) ) ? $field[ 'value' ][ 'stateCode' ] : false;
$selected_city = ( isset( $field[ 'value' ][ 'cityName' ] ) ) ? $field[ 'value' ][ 'cityName' ] : false;
$show_first = true;
$which_fields = ( isset( $field[ 'which_fields' ] ) ) ? $field[ 'which_fields' ] : 'all';

if ( false !== $default_country && false == $selected_country ) {
$default_country = ( isset( $field[ 'default_country' ] ) && ! empty( $field[ 'default_country' ] ) ) ? $field[ 'default_country' ] : false;
$prefill_cities = [];
$prefill_states = [];
$selected_country = ( isset( $field[ 'value' ][ 'countryCode' ] ) ) ? $field[ 'value' ][ 'countryCode' ] : false;
$selected_state = ( isset( $field[ 'value' ][ 'stateCode' ] ) ) ? $field[ 'value' ][ 'stateCode' ] : false;
$selected_city = ( isset( $field[ 'value' ][ 'cityName' ] ) ) ? $field[ 'value' ][ 'cityName' ] : false;
$show_first = true;
$store_meta = ( isset( $field[ 'value' ][ 'store_meta' ] ) ) ? $field[ 'value' ][ 'store_meta' ] : false;
$which_fields = ( isset( $field[ 'which_fields' ] ) ) ? $field[ 'which_fields' ] : 'all';

if ( false !== $default_country && false == $selected_country ) {
// New post with default country
if ( in_array( $which_fields, [ 'all', 'country_state', 'state_city' ] ) ) {
$prefill_states = acfcs_get_states( $default_country, $show_first, $field );
Expand Down Expand Up @@ -165,6 +174,9 @@ function render_field( $field ) {
}
if ( 'all' == $which_fields || strpos( $which_fields, 'city' ) !== false ) {
echo acfcs_render_dropdown( 'city', $field, $selected_city, $prefill_values );
}
if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) && $store_meta ) {
echo acfcs_render_hidden_field( 'store_meta', '1' );
}
}

Expand All @@ -176,7 +188,6 @@ function render_field( $field ) {
* Use this action to add CSS + JavaScript to assist your render_field() action.
*/
function input_admin_enqueue_scripts() {

$plugin_url = $this->settings[ 'url' ];
$plugin_version = $this->settings[ 'version' ];

Expand All @@ -191,11 +202,11 @@ function input_admin_enqueue_scripts() {
$js_vars[ 'default_country' ] = ( isset( $all_info[ 'default_country' ] ) && false != $all_info[ 'default_country' ] ) ? $all_info[ 'default_country' ] : false;
$js_vars[ 'post_id' ] = ( isset( $_GET[ 'post' ] ) ) ? (int) $_GET[ 'post' ] : false;
$js_vars[ 'show_labels' ] = ( isset( $all_info[ 'show_labels' ] ) ) ? $all_info[ 'show_labels' ] : apply_filters( 'acfcs_show_labels', true );
$js_vars[ 'store_meta' ] = ( isset( $all_info[ 'store_meta' ] ) ) ? $all_info[ 'store_meta' ] : false;
$js_vars[ 'use_select2' ] = ( isset( $all_info[ 'use_select2' ] ) ) ? $all_info[ 'use_select2' ] : false;
$js_vars[ 'which_fields' ] = ( isset( $all_info[ 'which_fields' ] ) ) ? $all_info[ 'which_fields' ] : 'all';

wp_localize_script( 'acfcs-process', 'city_selector_vars', $js_vars );

}


Expand All @@ -213,7 +224,6 @@ function input_admin_enqueue_scripts() {
*
*/
function load_value( $value, $post_id, $field ) {

$state_code = false;
$country_code = ( isset( $value[ 'countryCode' ] ) ) ? $value[ 'countryCode' ] : false;

Expand All @@ -230,11 +240,12 @@ function load_value( $value, $post_id, $field ) {

if ( strlen( $country_code ) == 2 && false != $state_code ) {
global $wpdb;
$sql_query = $wpdb->prepare( "SELECT country, state_name FROM {$wpdb->prefix}cities WHERE country_code= %s AND state_code= %s", $country_code, $state_code );
$row = $wpdb->get_row( $sql_query );
$value[ 'stateCode' ] = $state_code;
$value[ 'stateName' ] = ( isset( $row->state_name ) ) ? $row->state_name : false;
$value[ 'countryName' ] = ( isset( $row->country ) ) ? $row->country : false;
$table = $wpdb->prefix . 'cities';
$sql_query = $wpdb->prepare( "SELECT country, state_name FROM {$table} WHERE country_code = %s AND state_code = %s", $country_code, $state_code );
$row = $wpdb->get_row( $sql_query );
$value[ 'stateCode' ] = $state_code;
$value[ 'stateName' ] = ( isset( $row->state_name ) ) ? $row->state_name : false;
$value[ 'countryName' ] = ( isset( $row->country ) ) ? $row->country : false;
}

return $value;
Expand All @@ -254,7 +265,6 @@ function load_value( $value, $post_id, $field ) {
* @return $value
*/
function update_value( $value, $post_id, $field ) {

$required = $field[ 'required' ];
if ( 0 == $required ) {
if ( isset( $field[ 'which_fields' ] ) && 'all' == $field[ 'which_fields' ] || ! isset( $field[ 'which_fields' ] ) ) {
Expand Down Expand Up @@ -315,8 +325,11 @@ function update_value( $value, $post_id, $field ) {
}
}

return $value;
if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) ) {
do_action( 'acfcs_store_meta', $value, $post_id );
}

return $value;
}


Expand All @@ -335,7 +348,6 @@ function update_value( $value, $post_id, $field ) {
* @return $valid
*/
function validate_value( $valid, $value, $field, $input ) {

if ( 1 == $field[ 'required' ] ) {
$nothing = esc_html__( "You didn't select anything.", 'acf-city-selector' );
$no_city = esc_html__( "You didn't select a city.", 'acf-city-selector' );
Expand Down Expand Up @@ -383,5 +395,4 @@ function validate_value( $valid, $value, $field, $input ) {
}

new acf_field_city_selector( $this->settings );

}
2 changes: 1 addition & 1 deletion admin/acfcs-countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function acfcs_country_page() {
<?php echo sprintf( '<p>%s</p>', esc_html__( 'Default the plugin comes with 2 countries included, the Netherlands and Belgium but you might want to add more countries to choose from.', 'acf-city-selector' ) ); ?>
<p>
<?php esc_html_e( "And now you can !! We have created several 'country packages' for you to import 'as is'.", 'acf-city-selector' ); ?>
<?php echo sprintf( __( 'Download them %s.', 'acf-city-selector' ), sprintf( '<a href="%s">%s</a>', esc_url( ACFCS_WEBSITE_URL . '/get-countries/' ), __( 'here', 'acf-city-selector' ) ) ); ?>
<?php echo sprintf( esc_html__( 'Download them %s.', 'acf-city-selector' ), sprintf( '<a href="%s" target="_blank" rel="noopener">%s</a>', esc_url( ACFCS_WEBSITE_URL . '/get-countries/' ), esc_html__( 'here', 'acf-city-selector' ) ) ); ?>
</p>
</div>

Expand Down
2 changes: 0 additions & 2 deletions admin/acfcs-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ function acfcs_info_page() {
<?php echo sprintf( '<h2>%s</h2>', esc_html__( 'Info', 'acf-city-selector' ) ); ?>
<p>
<?php esc_html_e( 'This page shows real-time info about your site and settings.', 'acf-city-selector' ); ?>
<br />
<?php esc_html_e( 'We might ask for this info if support is helping you fix a problem.', 'acf-city-selector' ); ?>
</p>
</div>

Expand Down
52 changes: 33 additions & 19 deletions assets/css/general.css
Original file line number Diff line number Diff line change
@@ -1,58 +1,72 @@
/* errors */
.acfcs .notice--error {
border-left-color: #dc3232;
border-left-color: #dc3232;
}

.acfcs .notice--success {
border-left-color: #43bc71;
border-left-color: #43bc71;
}

.acfcs .notice--warning {
border-left-color: #ff9000;
border-left-color: #ff9000;
}

.acfcs .notice--info {
border-left-color: #00a0d2;
border-left-color: #00a0d2;
}

.acfcs .field__message {
background-color: #f9f9f9;
border-left: 2px solid #ccd0d4;
border-top: 1px solid #ccd0d4;
border-right: 1px solid #ccd0d4;
border-bottom: 1px solid #ccd0d4;
margin-bottom: 1rem;
padding: 5px;
background-color: #f9f9f9;
border-left: 2px solid #ccd0d4;
border-top: 1px solid #ccd0d4;
border-right: 1px solid #ccd0d4;
border-bottom: 1px solid #ccd0d4;
margin-bottom: 1rem;
padding: 5px;
}

.acfcs .field__message--error {
border-left-color: #dc3232;
border-left-color: #dc3232;
}

.acfcs .field__message--warning {
border-left-color: #ff9000;
border-left-color: #ff9000;
}

/* edit post screens v4 */
/* not actively supported anymore */
.field_type-acf_city_selector .acfcs__dropdown-box {
margin-bottom: 1rem;
margin-bottom: 1rem;
}

.field_type-acf_city_selector span.acf-input-header {
display: block;
margin-bottom: 10px;
display: block;
margin-bottom: 10px;
}

/* edit post screens v5 */
.acf-field-acf-city-selector .acfcs__dropdown-box {
margin-bottom: 1rem;
margin-bottom: 1rem;
}

.acf-field-acf-city-selector .acfcs__checkbox {
display: flex;
flex-flow: row nowrap;
align-items: center;
}

.acfcs__checkbox input[type="checkbox"] {
margin-right: 0.5rem;
}

.acfcs__checkbox label {
vertical-align: initial;
}

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

.acf-field-acf-city-selector .acf-input-header {
margin-bottom: 10px;
margin-bottom: 10px;
}
Loading

0 comments on commit 61af52c

Please sign in to comment.