From f029f48459516e9d4ad948ca62aff1a3e7fc1db9 Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:14:53 +0100 Subject: [PATCH 01/27] Update version + remove unused function --- ACF_City_Selector.php | 17 +++-------------- README.md | 3 +++ readme.txt | 9 ++++++--- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/ACF_City_Selector.php b/ACF_City_Selector.php index 6005de8..6c6c80c 100755 --- a/ACF_City_Selector.php +++ b/ACF_City_Selector.php @@ -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 Requires PHP: 7.0 Author: Beee Author URI: https://berryplasman.com @@ -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' ) ) { @@ -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 */ diff --git a/README.md b/README.md index cf018a0..29f109a 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,9 @@ I got the idea for this plugin through [Fabrizio Sabato](https://github.com/fab0 ### Changelog +1.10.0 +* ? + 1.9.1 * fixed incorrect version diff --git a/readme.txt b/readme.txt index e4c6d28..b887e47 100755 --- a/readme.txt +++ b/readme.txt @@ -3,12 +3,12 @@ Tags: acf, custom, fields, custom fields, select, country, city, state, province Contributors: beee Requires at least: 3.6.0 Requires PHP: 7.0 -Tested up to: 6.0.2 -Stable tag: 1.9.1 +Tested up to: 6.1 +Stable tag: 1.10.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html -This plugins adds a new (ACF) field to select a city depending on country and state/province. +This plugin adds a new (ACF) field to select a city depending on country and state/province. == Description == @@ -41,6 +41,9 @@ A. Please read the FAQ @ [https://acf-city-selector.com/documentation/](https:// == Changelog == += 1.10.0 = +* ? + = 1.9.1 = * fix incorrect version From b4a1f41b92c51ea8d422188acab7712c32298648 Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:17:36 +0100 Subject: [PATCH 02/27] Remove string --- admin/acfcs-info.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/admin/acfcs-info.php b/admin/acfcs-info.php index 4179b1d..ee912cf 100755 --- a/admin/acfcs-info.php +++ b/admin/acfcs-info.php @@ -26,8 +26,6 @@ function acfcs_info_page() { %s', esc_html__( 'Info', 'acf-city-selector' ) ); ?>

-
-

From ae6cdc2b765470654cec497c91fe67a0174db47c Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:25:39 +0100 Subject: [PATCH 03/27] Sprintf settings link --- ACF_City_Selector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACF_City_Selector.php b/ACF_City_Selector.php index 6c6c80c..5453589 100755 --- a/ACF_City_Selector.php +++ b/ACF_City_Selector.php @@ -258,7 +258,7 @@ public function acfcs_include_field_types( $version = false ) { * @return array */ public function acfcs_settings_link( $links ) { - $settings_link = [ 'settings' => '' . esc_html__( 'Settings', 'acf-city-selector' ) . '' ]; + $settings_link = [ 'settings' => sprintf( '%s', admin_url( 'options-general.php?page=acfcs-dashboard' ), esc_html__( 'Settings', 'acf-city-selector' ) ) ]; return array_merge( $settings_link, $links ); } From 6ff77fed11c6651ab2c159a2ea66521823e8351e Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:27:57 +0100 Subject: [PATCH 04/27] Remove static --- ACF_City_Selector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ACF_City_Selector.php b/ACF_City_Selector.php index 5453589..1e15bc9 100755 --- a/ACF_City_Selector.php +++ b/ACF_City_Selector.php @@ -371,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 ); } From 5910cb0c246c40f45694bbcb5bfa5f4ed35c91b9 Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:36:23 +0100 Subject: [PATCH 05/27] Merge IFs --- inc/acfcs-ajax.php | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/inc/acfcs-ajax.php b/inc/acfcs-ajax.php index bcaaf28..f24b077 100644 --- a/inc/acfcs-ajax.php +++ b/inc/acfcs-ajax.php @@ -27,11 +27,9 @@ function acfcs_get_states_call() { } } - if ( ! isset( $field[ 'show_labels' ] ) ) { - if ( isset( $_POST[ 'show_labels' ] ) ) { - $field[ 'show_labels' ] = ( '1' == sanitize_text_field( $_POST[ 'show_labels' ] ) ) ? true : false; - } - } + if ( ! isset( $field[ 'show_labels' ] ) && isset( $_POST[ 'show_labels' ] ) ) { + $field[ 'show_labels' ] = ( '1' == sanitize_text_field( $_POST[ 'show_labels' ] ) ) ? true : false; + } if ( isset( $country_code ) ) { $states_transient = acfcs_get_states( $country_code, true, $field ); @@ -82,15 +80,13 @@ function acfcs_get_cities_call() { } } - if ( ! isset( $field[ 'show_labels' ] ) ) { - if ( isset( $_POST[ 'show_labels' ] ) ) { - $show_labels = sanitize_text_field( $_POST[ 'show_labels' ] ); - if ( '1' == $show_labels ) { - $field[ 'show_labels' ] = true; - } elseif ( '0' == $show_labels ) { - $field[ 'show_labels' ] = false; - } - } + if ( ! isset( $field[ 'show_labels' ] ) && isset( $_POST[ 'show_labels' ] ) ) { + $show_labels = sanitize_text_field( $_POST[ 'show_labels' ] ); + if ( '1' == $show_labels ) { + $field[ 'show_labels' ] = true; + } elseif ( '0' == $show_labels ) { + $field[ 'show_labels' ] = false; + } } if ( 6 <= strlen( $posted_state_code ) ) { From 92cf69d96aa435792c5abeb069c235971b2472a3 Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:36:29 +0100 Subject: [PATCH 06/27] Add wpdb->prepare --- inc/form-handling.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/inc/form-handling.php b/inc/form-handling.php index a4ed9d9..e6ea677 100755 --- a/inc/form-handling.php +++ b/inc/form-handling.php @@ -118,10 +118,11 @@ function acfcs_delete_rows() { $cities[] = $split[ 1 ]; } } - $cities = implode( ', ', $cities ); - $row_ids = implode( ',', $ids ); - $query = "DELETE FROM " . $wpdb->prefix . "cities WHERE id IN (" . $row_ids . ")"; - $amount = $wpdb->query( $query ); + $cities = implode( ', ', $cities ); + $row_ids = implode( ',', $ids ); + $table = $wpdb->prefix . 'cities'; + $query = $wpdb->prepare( "DELETE FROM {$table} WHERE id IN (%s)", $row_ids ); + $amount = $wpdb->query( $query ); if ( $amount > 0 ) { ACF_City_Selector::acfcs_errors()->add( 'success_row_delete', sprintf( _n( 'You have deleted the city %s.', 'You have deleted the following cities: %s.', $amount, 'acf-city-selector' ), $cities ) ); From dc0a05d24f16886983fc00044d73d069a8ad5716 Mon Sep 17 00:00:00 2001 From: Beee Date: Fri, 4 Nov 2022 19:52:15 +0100 Subject: [PATCH 07/27] Esc string --- admin/acfcs-countries.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/acfcs-countries.php b/admin/acfcs-countries.php index a7b5955..66b127c 100755 --- a/admin/acfcs-countries.php +++ b/admin/acfcs-countries.php @@ -51,7 +51,7 @@ function acfcs_country_page() { %s

', 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' ) ); ?>

- %s', esc_url( ACFCS_WEBSITE_URL . '/get-countries/' ), __( 'here', 'acf-city-selector' ) ) ); ?> + %s', esc_url( ACFCS_WEBSITE_URL . '/get-countries/' ), esc_html__( 'here', 'acf-city-selector' ) ) ); ?>

From 9349146e1f08467e5619fe125761e2015df5fc6b Mon Sep 17 00:00:00 2001 From: Beee Date: Sat, 5 Nov 2022 22:05:32 +0100 Subject: [PATCH 08/27] Typo --- admin/acf-city-selector-v5.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/acf-city-selector-v5.php b/admin/acf-city-selector-v5.php index 2537a35..292d635 100755 --- a/admin/acf-city-selector-v5.php +++ b/admin/acf-city-selector-v5.php @@ -25,7 +25,7 @@ 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 */ From e2614ca053ff725d68775662d9ddb1f522197345 Mon Sep 17 00:00:00 2001 From: Beee Date: Sat, 5 Nov 2022 22:19:37 +0100 Subject: [PATCH 09/27] Table as var --- admin/acf-city-selector-v5.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/admin/acf-city-selector-v5.php b/admin/acf-city-selector-v5.php index 292d635..dc32e9d 100755 --- a/admin/acf-city-selector-v5.php +++ b/admin/acf-city-selector-v5.php @@ -48,6 +48,7 @@ function __construct( $settings ) { } + /** * render_field_settings() * @@ -106,6 +107,7 @@ function render_field_settings( $field ) { ) ); } + /** * render_field() * @@ -230,11 +232,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; From 8163c851c607e46da118676218e92f5ae2935d19 Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 17 Nov 2022 20:02:35 +0100 Subject: [PATCH 10/27] Version bump --- ACF_City_Selector.php | 2 +- README.md | 6 +++--- readme.txt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ACF_City_Selector.php b/ACF_City_Selector.php index 1e15bc9..9482ab7 100755 --- a/ACF_City_Selector.php +++ b/ACF_City_Selector.php @@ -4,7 +4,7 @@ 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.10.0 - Tested up to: 6.1 + Tested up to: 6.1.1 Requires PHP: 7.0 Author: Beee Author URI: https://berryplasman.com diff --git a/README.md b/README.md index 29f109a..dd6e98d 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Welcome to the City Selector plugin, which is an extension for [Advanced Custom ### Version -1.9.1 - released 13.10.22 +1.10.1 - released 1x.11.22 ### Description @@ -183,8 +183,8 @@ This ACF field type is compatible/tested with ACF 5 (Pro). It's slightly tested ### 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.4 * [X] Advanced Custom Fields 4.4.12 diff --git a/readme.txt b/readme.txt index b887e47..e35244e 100755 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Tags: acf, custom, fields, custom fields, select, country, city, state, province Contributors: beee Requires at least: 3.6.0 Requires PHP: 7.0 -Tested up to: 6.1 +Tested up to: 6.1.1 Stable tag: 1.10.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html From 462e87bde622426702acaffb7d54f785c5f8cd3d Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 17 Nov 2022 20:05:23 +0100 Subject: [PATCH 11/27] Render checkbox for single --- inc/acfcs-actions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/inc/acfcs-actions.php b/inc/acfcs-actions.php index ccc5358..7bb5a5b 100755 --- a/inc/acfcs-actions.php +++ b/inc/acfcs-actions.php @@ -47,3 +47,24 @@ function acfcs_reimport_cities( $country_code = false ) { } } add_action( 'acfcs_after_success_import', 'acfcs_reimport_cities' ); + + function acfcs_save_single( $value, $post_id ) { + if ( isset( $value[ 'save_single' ] ) && 1 == $value[ 'save_single' ] ) { + if ( ! empty( $value[ 'countryCode' ] ) ) { + update_post_meta( $post_id, 'acfcs_search_country', $value[ 'countryCode' ] ); + } + if ( ! empty( $value[ 'stateCode' ] ) ) { + update_post_meta( $post_id, 'acfcs_search_state', $value[ 'stateCode' ] ); + } + if ( ! empty( $value[ 'cityName' ] ) ) { + update_post_meta( $post_id, 'acfcs_search_city', $value[ 'cityName' ] ); + } + } else { + // remove meta + delete_post_meta( $post_id, 'acfcs_search_country' ); + delete_post_meta( $post_id, 'acfcs_search_state' ); + delete_post_meta( $post_id, 'acfcs_search_city' ); + } + // error_log(print_r($value,true)); + } + add_action( 'acfcs_store_meta', 'acfcs_save_single', 10, 2 ); From 9ec4dfc95856631c7bd59cec6736857fc98a4c85 Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 17 Nov 2022 20:05:31 +0100 Subject: [PATCH 12/27] Style checkbox --- assets/css/general.css | 52 +++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/assets/css/general.css b/assets/css/general.css index 4b7ce58..da8c427 100644 --- a/assets/css/general.css +++ b/assets/css/general.css @@ -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; } From 76dc90d4397e668397fdb4d26cd0e1c3ecf5be06 Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 17 Nov 2022 20:10:46 +0100 Subject: [PATCH 13/27] Render checkbox --- inc/acfcs-functions.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index 2251684..5ff7df3 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -588,6 +588,37 @@ function acfcs_render_dropdown( $type, $field, $stored_value, $prefill_values ) } + function acfcs_render_checkbox( $field, $stored_value ) { + $faq_explanation = ACFCS_WEBSITE_URL . '/faq/#'; + $field_id = $field[ 'id' ]; + $field_name = $field[ 'name' ]; + $field_label = __( 'Save as single meta', 'acf-city-selector' ); + $field_suffix = 'save_single'; + + ob_start(); + ?> +
+
+ /> +
+
+ + | + + + +
+
+ Date: Thu, 17 Nov 2022 20:11:43 +0100 Subject: [PATCH 14/27] Prepare field to save meta --- admin/acf-city-selector-v5.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/admin/acf-city-selector-v5.php b/admin/acf-city-selector-v5.php index dc32e9d..c33c52f 100755 --- a/admin/acf-city-selector-v5.php +++ b/admin/acf-city-selector-v5.php @@ -117,16 +117,17 @@ function render_field_settings( $field ) { */ 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; + $save_as_single = ( isset( $field[ 'value' ][ 'save_single' ] ) ) ? $field[ 'value' ][ 'save_single' ] : false; + $show_first = true; + $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 ); @@ -167,6 +168,10 @@ 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' ] ) ) { + echo acfcs_render_checkbox( $field, $save_as_single ); } } @@ -318,6 +323,10 @@ function update_value( $value, $post_id, $field ) { } } + if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) ) { + do_action( 'acfcs_store_meta', $value, $post_id ); + } + return $value; } From 5903a2817112dffe03564718bd38b019b9e65177 Mon Sep 17 00:00:00 2001 From: Beee Date: Sun, 11 Dec 2022 19:18:54 +0100 Subject: [PATCH 15/27] Make link work --- inc/acfcs-functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index 5ff7df3..faf579d 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -589,7 +589,7 @@ function acfcs_render_dropdown( $type, $field, $stored_value, $prefill_values ) function acfcs_render_checkbox( $field, $stored_value ) { - $faq_explanation = ACFCS_WEBSITE_URL . '/faq/#'; + $faq_explanation = ACFCS_WEBSITE_URL . '/faq/store-city-selector-values-as-single-meta/'; $field_id = $field[ 'id' ]; $field_name = $field[ 'name' ]; $field_label = __( 'Save as single meta', 'acf-city-selector' ); @@ -606,7 +606,7 @@ function acfcs_render_checkbox( $field, $stored_value ) { | - + From 2f5214db33fd236f2f52a0e87e2ca64cbba4af82 Mon Sep 17 00:00:00 2001 From: Beee Date: Sun, 11 Dec 2022 19:32:34 +0100 Subject: [PATCH 16/27] Clean up --- inc/acfcs-actions.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/inc/acfcs-actions.php b/inc/acfcs-actions.php index 7bb5a5b..a731d76 100755 --- a/inc/acfcs-actions.php +++ b/inc/acfcs-actions.php @@ -48,6 +48,15 @@ function acfcs_reimport_cities( $country_code = false ) { } add_action( 'acfcs_after_success_import', 'acfcs_reimport_cities' ); + + /** + * Save location as single meta values + * + * @param $value + * @param $post_id + * + * @return void + */ function acfcs_save_single( $value, $post_id ) { if ( isset( $value[ 'save_single' ] ) && 1 == $value[ 'save_single' ] ) { if ( ! empty( $value[ 'countryCode' ] ) ) { @@ -59,12 +68,11 @@ function acfcs_save_single( $value, $post_id ) { if ( ! empty( $value[ 'cityName' ] ) ) { update_post_meta( $post_id, 'acfcs_search_city', $value[ 'cityName' ] ); } - } else { + } elseif ( $post_id ) { // remove meta delete_post_meta( $post_id, 'acfcs_search_country' ); delete_post_meta( $post_id, 'acfcs_search_state' ); delete_post_meta( $post_id, 'acfcs_search_city' ); } - // error_log(print_r($value,true)); } add_action( 'acfcs_store_meta', 'acfcs_save_single', 10, 2 ); From 9d5d6eb71fb8edd143e991be6188da45b18bc69c Mon Sep 17 00:00:00 2001 From: Beee Date: Sun, 11 Dec 2022 19:32:47 +0100 Subject: [PATCH 17/27] Optimize sql queries --- inc/acfcs-functions.php | 51 +++++++++++++---------------------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index faf579d..cc07d90 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -29,11 +29,7 @@ function acfcs_get_countries( $show_first = true, $field = false, $force = false } global $wpdb; - $results = $wpdb->get_results( " - SELECT * FROM {$wpdb->prefix}cities - GROUP BY country - ORDER BY country ASC - " ); + $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}cities GROUP BY country ORDER BY country ASC" ); if ( ! empty( $results ) ) { $country_results = array(); @@ -80,15 +76,9 @@ function acfcs_get_states( $country_code = false, $show_first = true, $field = f $order = "ORDER BY LENGTH(state_name), state_name"; } - global $wpdb; - $sql = $wpdb->prepare( " - SELECT * - FROM {$wpdb->prefix}cities - WHERE country_code = %s - GROUP BY state_code - " . $order, strtoupper( $country_code ) - ); - $results = $wpdb->get_results( $sql ); + global $wpdb; + $sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}cities WHERE country_code = %s GROUP BY state_code" . $order, strtoupper( $country_code ) ); + $results = $wpdb->get_results( $sql ); $state_results = array(); foreach ( $results as $data ) { @@ -197,8 +187,10 @@ function acfcs_get_cities( $country_code = false, $state_code = false, $field = */ function acfcs_get_country_name( $country_code = false ) { if ( false != $country_code ) { - global $wpdb; - $country = $wpdb->get_row( $wpdb->prepare( "SELECT country FROM {$wpdb->prefix}cities WHERE country_code = %s", $country_code ) ); + global $wpdb; + $query = $wpdb->prepare( "SELECT country FROM {$wpdb->prefix}cities WHERE country_code = %s", $country_code ); + $country = $wpdb->get_row( $query ); + if ( isset( $country->country ) ) { return $country->country; } else { @@ -438,20 +430,13 @@ function acfcs_get_packages( $endpoint = 'single' ) { */ function acfcs_get_countries_info() { global $wpdb; - $results = $wpdb->get_results( " - SELECT country_code FROM {$wpdb->prefix}cities - GROUP BY country_code - ORDER BY country_code ASC - " ); + $results = $wpdb->get_results( "SELECT country_code FROM {$wpdb->prefix}cities GROUP BY country_code ORDER BY country_code ASC" ); $acfcs_info = array(); foreach ( $results as $data ) { - $country_code = $data->country_code; - $results = $wpdb->get_results( $wpdb->prepare( " - SELECT * FROM {$wpdb->prefix}cities - WHERE country_code = %s - ORDER BY country_code ASC - ", $country_code ) ); + $country_code = $data->country_code; + $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}cities WHERE country_code = %s ORDER BY country_code ASC", $country_code ); + $results = $wpdb->get_results( $query ); $acfcs_info[ $country_code ] = [ 'country_code' => $country_code, @@ -615,7 +600,6 @@ function acfcs_render_checkbox( $field, $stored_value ) { $input = ob_get_clean(); return $input; - } @@ -757,7 +741,7 @@ function acfcs_delete_country( $countries ) { if ( ! empty( $sanitized_country_codes ) ) { global $wpdb; $country_string = strtoupper( "'" . implode( "', '", $sanitized_country_codes ) . "'" ); - $query = "DELETE FROM {$wpdb->prefix}cities WHERE `country_code` IN ({$country_string})"; + $query = $wpdb->prepare( "DELETE FROM {$wpdb->prefix}cities WHERE `country_code` IN (%s)", $country_string ); $result = $wpdb->query( $query ); if ( $result > 0 ) { ACF_City_Selector::acfcs_errors()->add( 'success_country_remove', sprintf( esc_html__( 'You have successfully removed all entries for %s.', 'acf-city-selector' ), $country_names_and ) ); @@ -935,12 +919,9 @@ function acfcs_get_searched_cities() { $orderby = 'ORDER BY city_name ASC, state_name ASC'; } - $sql = "SELECT * FROM {$wpdb->prefix}cities - " . $where . " - " . $orderby . " - " . $search_limit . " - "; - $cities = $wpdb->get_results( $sql ); + $table = $wpdb->prefix . 'cities'; + $query = $wpdb->prepare( "SELECT * FROM $table %s %s %s", $where, $orderby, $search_limit ); + $cities = $wpdb->get_results( $query ); return $cities; } From 2d923dd649bdcd28a3867404291efc880fa6c9fe Mon Sep 17 00:00:00 2001 From: Beee Date: Sun, 11 Dec 2022 19:32:53 +0100 Subject: [PATCH 18/27] Update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dd6e98d..8aace68 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Welcome to the City Selector plugin, which is an extension for [Advanced Custom ### Version -1.10.1 - released 1x.11.22 +1.10.0 - released 1x.12.22 ### Description @@ -184,7 +184,7 @@ This ACF field type is compatible/tested with ACF 5 (Pro). It's slightly tested ### Tested with * [X] Wordpress 6.1.1 -* [X] Advanced Custom Fields Pro 6.0.4 +* [X] Advanced Custom Fields Pro 6.0.5 * [X] Advanced Custom Fields 4.4.12 @@ -229,7 +229,7 @@ I got the idea for this plugin through [Fabrizio Sabato](https://github.com/fab0 ### Changelog 1.10.0 -* ? +* add single meta values for location 1.9.1 * fixed incorrect version From bdb5e99672997aada06f92aa0dc8f12d03be70b6 Mon Sep 17 00:00:00 2001 From: Beee Date: Sun, 11 Dec 2022 19:40:34 +0100 Subject: [PATCH 19/27] Set table as var --- inc/acfcs-functions.php | 42 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index cc07d90..23a1551 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -28,8 +28,9 @@ function acfcs_get_countries( $show_first = true, $field = false, $force = false } } - global $wpdb; - $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}cities GROUP BY country ORDER BY country ASC" ); + global $wpdb; + $table = $wpdb->prefix . 'cities'; + $results = $wpdb->get_results( "SELECT * FROM $table GROUP BY country ORDER BY country ASC" ); if ( ! empty( $results ) ) { $country_results = array(); @@ -77,7 +78,8 @@ function acfcs_get_states( $country_code = false, $show_first = true, $field = f } global $wpdb; - $sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}cities WHERE country_code = %s GROUP BY state_code" . $order, strtoupper( $country_code ) ); + $table = $wpdb->prefix . 'cities'; + $sql = $wpdb->prepare( "SELECT * FROM $table WHERE country_code = %s GROUP BY state_code" . $order, strtoupper( $country_code ) ); $results = $wpdb->get_results( $sql ); $state_results = array(); @@ -140,7 +142,8 @@ function acfcs_get_cities( $country_code = false, $state_code = false, $field = if ( $set_transient ) { if ( false !== $country_code ) { global $wpdb; - $query = "SELECT * FROM {$wpdb->prefix}cities"; + $table = $wpdb->prefix . 'cities'; + $query = "SELECT * FROM $table"; if ( $country_code && $state_code ) { if ( 3 < strlen( $state_code ) ) { $state_code = substr( $state_code, 3 ); @@ -188,7 +191,8 @@ function acfcs_get_cities( $country_code = false, $state_code = false, $field = function acfcs_get_country_name( $country_code = false ) { if ( false != $country_code ) { global $wpdb; - $query = $wpdb->prepare( "SELECT country FROM {$wpdb->prefix}cities WHERE country_code = %s", $country_code ); + $table = $wpdb->prefix . 'cities'; + $query = $wpdb->prepare( "SELECT country FROM $table WHERE country_code = %s", $country_code ); $country = $wpdb->get_row( $query ); if ( isset( $country->country ) ) { @@ -214,9 +218,10 @@ function acfcs_get_country_name( $country_code = false ) { */ function acfcs_has_cities( $country_code = false ) { global $wpdb; - $query = "SELECT * FROM {$wpdb->prefix}cities LIMIT 1"; + $table = $wpdb->prefix . 'cities'; + $query = "SELECT * FROM $table LIMIT 1"; if ( $country_code ) { - $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}cities WHERE country_code = %s LIMIT 1", $country_code ); + $query = $wpdb->prepare( "SELECT * FROM $table WHERE country_code = %s LIMIT 1", $country_code ); } $results = $wpdb->get_results( $query ); @@ -430,12 +435,13 @@ function acfcs_get_packages( $endpoint = 'single' ) { */ function acfcs_get_countries_info() { global $wpdb; - $results = $wpdb->get_results( "SELECT country_code FROM {$wpdb->prefix}cities GROUP BY country_code ORDER BY country_code ASC" ); + $table = $wpdb->prefix . 'cities'; + $results = $wpdb->get_results( "SELECT country_code FROM $table GROUP BY country_code ORDER BY country_code ASC" ); $acfcs_info = array(); foreach ( $results as $data ) { $country_code = $data->country_code; - $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}cities WHERE country_code = %s ORDER BY country_code ASC", $country_code ); + $query = $wpdb->prepare( "SELECT * FROM $table WHERE country_code = %s ORDER BY country_code ASC", $country_code ); $results = $wpdb->get_results( $query ); $acfcs_info[ $country_code ] = [ @@ -740,9 +746,11 @@ function acfcs_delete_country( $countries ) { if ( ! empty( $sanitized_country_codes ) ) { global $wpdb; - $country_string = strtoupper( "'" . implode( "', '", $sanitized_country_codes ) . "'" ); - $query = $wpdb->prepare( "DELETE FROM {$wpdb->prefix}cities WHERE `country_code` IN (%s)", $country_string ); - $result = $wpdb->query( $query ); + $table = $wpdb->prefix . 'cities'; + $country_string = strtoupper( "'" . implode( "', '", $sanitized_country_codes ) . "'" ); + $query = $wpdb->prepare( "DELETE FROM $table WHERE `country_code` IN (%s)", $country_string ); + $result = $wpdb->query( $query ); + if ( $result > 0 ) { ACF_City_Selector::acfcs_errors()->add( 'success_country_remove', sprintf( esc_html__( 'You have successfully removed all entries for %s.', 'acf-city-selector' ), $country_names_and ) ); foreach( $countries as $country_code ) { @@ -831,9 +839,11 @@ function acfcs_get_states_optgroup() { // get states for these countries if ( ! empty( $countries ) ) { - global $wpdb; - $states = array(); - foreach ( $countries as $country ) { + global $wpdb; + $table = $wpdb->prefix . 'cities'; + $states = []; + + foreach ( $countries as $country ) { $states[] = array( 'state' => 'open_optgroup', 'name' => esc_attr__( acfcs_get_country_name( $country[ 'code' ] ), 'acf-city-selector' ), @@ -844,7 +854,7 @@ function acfcs_get_states_optgroup() { $order = 'ORDER BY LENGTH(state_name), state_name'; } - $query = "SELECT * FROM {$wpdb->prefix}cities WHERE country_code = %s GROUP BY state_code " . $order; + $query = "SELECT * FROM $table WHERE country_code = %s GROUP BY state_code " . $order; $sql = $wpdb->prepare( $query, $country[ 'code' ] ); $results = $wpdb->get_results( $sql ); From f5e52eaf9d7376b79502de476d7104e0a9cc64ac Mon Sep 17 00:00:00 2001 From: Beee Date: Sun, 11 Dec 2022 19:44:39 +0100 Subject: [PATCH 20/27] Optimize returns --- inc/acfcs-functions.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index 23a1551..529d518 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -42,7 +42,6 @@ function acfcs_get_countries( $show_first = true, $field = false, $force = false $countries = array_merge( $countries, $country_results ); } - return $countries; } @@ -210,7 +209,7 @@ function acfcs_get_country_name( $country_code = false ) { /** - * Checks if there any cities in the database (for page availability) + * Checks if there are any cities in the database (for page availability) * * @param $country_code * @@ -240,8 +239,10 @@ function acfcs_has_cities( $country_code = false ) { * @return array */ function acfcs_check_if_files() { - $target_dir = acfcs_upload_folder(); - if ( is_dir( $target_dir ) ) { + $actual_files = []; + $target_dir = acfcs_upload_folder(); + + if ( is_dir( $target_dir ) ) { $file_index = scandir( $target_dir ); $excluded_files = [ '.', @@ -251,7 +252,6 @@ function acfcs_check_if_files() { ]; if ( is_array( $file_index ) ) { - $actual_files = array(); foreach ( $file_index as $file ) { if ( ! in_array( $file, $excluded_files ) ) { $actual_files[] = $file; @@ -263,7 +263,7 @@ function acfcs_check_if_files() { } } - return array(); + return $actual_files; } @@ -785,7 +785,8 @@ function acfcs_upload_folder( $suffix = false ) { * @return false|string */ function acfcs_render_preview_results( $csv_data = [] ) { - if ( ! empty( $csv_data ) ) { + $table = ''; + if ( ! empty( $csv_data ) ) { $table_columns = [ esc_html__( 'City', 'acf-city-selector' ), esc_html__( 'State code', 'acf-city-selector' ), @@ -811,11 +812,9 @@ function acfcs_render_preview_results( $csv_data = [] ) { $table_rows = ob_get_clean(); $table_body = sprintf( '%s', $table_rows ); $table = sprintf( '%s%s
', $table_headers, $table_body ); - - return $table; } - return ''; + return $table; } @@ -828,6 +827,8 @@ function acfcs_render_preview_results( $csv_data = [] ) { */ function acfcs_get_states_optgroup() { $results = acfcs_get_countries( false ); + $states = []; + // if there is at least 1 country if ( ! empty( $results ) ) { foreach ( $results as $country_code => $label ) { @@ -841,7 +842,6 @@ function acfcs_get_states_optgroup() { if ( ! empty( $countries ) ) { global $wpdb; $table = $wpdb->prefix . 'cities'; - $states = []; foreach ( $countries as $country ) { $states[] = array( @@ -872,12 +872,10 @@ function acfcs_get_states_optgroup() { 'name' => '', ); } - - return $states; } } - return []; + return $states; } From ffc20d5e0de468a55274366b19d57db93302bb0f Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 29 Dec 2022 18:44:44 +0100 Subject: [PATCH 21/27] Only in admin --- admin/acf-city-selector-v5.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/acf-city-selector-v5.php b/admin/acf-city-selector-v5.php index c33c52f..ad571f9 100755 --- a/admin/acf-city-selector-v5.php +++ b/admin/acf-city-selector-v5.php @@ -170,7 +170,7 @@ function render_field( $field ) { echo acfcs_render_dropdown( 'city', $field, $selected_city, $prefill_values ); } - if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) ) { + if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) && is_admin() ) { echo acfcs_render_checkbox( $field, $save_as_single ); } } From 3e571767ffe1ca528cc25387c51b9f3ccc1166de Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 29 Dec 2022 19:03:17 +0100 Subject: [PATCH 22/27] Change checkbox to hidden field, defined in settings --- admin/acf-city-selector-v5.php | 27 +++++++++++++++++++-------- inc/acfcs-actions.php | 6 +++--- inc/acfcs-functions.php | 31 ++++--------------------------- 3 files changed, 26 insertions(+), 38 deletions(-) diff --git a/admin/acf-city-selector-v5.php b/admin/acf-city-selector-v5.php index ad571f9..2d8eb43 100755 --- a/admin/acf-city-selector-v5.php +++ b/admin/acf-city-selector-v5.php @@ -35,6 +35,7 @@ function __construct( $settings ) { $this->label = 'City Selector'; $this->category = esc_attr__( 'Choice', 'acf-city-selector' ); $this->defaults = array( + 'store_meta' => 0, 'show_labels' => 1, 'which_fields' => 'all', 'use_select2' => 0, @@ -72,7 +73,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' ), @@ -82,6 +83,16 @@ function render_field_settings( $field ) { 'value' => $field[ 'use_select2' ], ) ); + acf_render_field_setting( $field, [ + 'choices' => $select_options, + 'instructions' => esc_html__( 'Store location as single meta values to make querying easier', 'acf-city-selector' ), + 'label' => esc_html__( 'Single meta values', '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, @@ -118,12 +129,12 @@ function render_field_settings( $field ) { function render_field( $field ) { $default_country = ( isset( $field[ 'default_country' ] ) && ! empty( $field[ 'default_country' ] ) ) ? $field[ 'default_country' ] : false; + $store_meta = isset( $field[ 'store_meta' ] )? $field[ 'store_meta' ] : 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; - $save_as_single = ( isset( $field[ 'value' ][ 'save_single' ] ) ) ? $field[ 'value' ][ 'save_single' ] : false; $show_first = true; $which_fields = ( isset( $field[ 'which_fields' ] ) ) ? $field[ 'which_fields' ] : 'all'; @@ -169,9 +180,8 @@ 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' ] ) && is_admin() ) { - echo acfcs_render_checkbox( $field, $save_as_single ); + if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) && $store_meta ) { + echo acfcs_render_hidden_field( 'acfcs_store_meta', '1' ); } } @@ -197,9 +207,10 @@ function input_admin_enqueue_scripts() { $js_vars[ 'ajaxurl' ] = admin_url( 'admin-ajax.php' ); $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[ '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'; + $js_vars[ 'show_labels' ] = ( ! empty( $all_info[ 'show_labels' ] ) ) ? $all_info[ 'show_labels' ] : apply_filters( 'acfcs_show_labels', true ); + $js_vars[ 'store_meta' ] = ( ! empty( $all_info[ 'store_meta' ] ) ) ? $all_info[ 'store_meta' ] : 0; + $js_vars[ 'use_select2' ] = ( ! empty( $all_info[ 'use_select2' ] ) ) ? $all_info[ 'use_select2' ] : false; + $js_vars[ 'which_fields' ] = ( ! empty( $all_info[ 'which_fields' ] ) ) ? $all_info[ 'which_fields' ] : 'all'; wp_localize_script( 'acfcs-process', 'city_selector_vars', $js_vars ); diff --git a/inc/acfcs-actions.php b/inc/acfcs-actions.php index a731d76..a8890f9 100755 --- a/inc/acfcs-actions.php +++ b/inc/acfcs-actions.php @@ -57,8 +57,8 @@ function acfcs_reimport_cities( $country_code = false ) { * * @return void */ - function acfcs_save_single( $value, $post_id ) { - if ( isset( $value[ 'save_single' ] ) && 1 == $value[ 'save_single' ] ) { + function acfcs_save_single_meta( $value, $post_id ) { + if ( isset( $value[ 'acfcs_store_meta' ] ) && 1 == $value[ 'acfcs_store_meta' ] ) { if ( ! empty( $value[ 'countryCode' ] ) ) { update_post_meta( $post_id, 'acfcs_search_country', $value[ 'countryCode' ] ); } @@ -75,4 +75,4 @@ function acfcs_save_single( $value, $post_id ) { delete_post_meta( $post_id, 'acfcs_search_city' ); } } - add_action( 'acfcs_store_meta', 'acfcs_save_single', 10, 2 ); + add_action( 'acfcs_store_meta', 'acfcs_save_single_meta', 10, 2 ); diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index 529d518..9261caf 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -579,33 +579,10 @@ function acfcs_render_dropdown( $type, $field, $stored_value, $prefill_values ) } - function acfcs_render_checkbox( $field, $stored_value ) { - $faq_explanation = ACFCS_WEBSITE_URL . '/faq/store-city-selector-values-as-single-meta/'; - $field_id = $field[ 'id' ]; - $field_name = $field[ 'name' ]; - $field_label = __( 'Save as single meta', 'acf-city-selector' ); - $field_suffix = 'save_single'; - - ob_start(); - ?> -
-
- /> -
-
- - | - - - -
-
- ', $name, $value ); + } } From 9f45da574a4fe27fc415c49e0b2fbbd48dcdc5fe Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 29 Dec 2022 19:31:19 +0100 Subject: [PATCH 23/27] Change IF to store meta --- inc/acfcs-actions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/acfcs-actions.php b/inc/acfcs-actions.php index a731d76..e48fbe4 100755 --- a/inc/acfcs-actions.php +++ b/inc/acfcs-actions.php @@ -58,7 +58,7 @@ function acfcs_reimport_cities( $country_code = false ) { * @return void */ function acfcs_save_single( $value, $post_id ) { - if ( isset( $value[ 'save_single' ] ) && 1 == $value[ 'save_single' ] ) { + if ( isset( $_POST[ 'store_meta' ] ) && 1 == $_POST[ 'store_meta' ] ) { if ( ! empty( $value[ 'countryCode' ] ) ) { update_post_meta( $post_id, 'acfcs_search_country', $value[ 'countryCode' ] ); } From 95f77d39d5301f5831b5674b65812f63f69065c8 Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 29 Dec 2022 19:31:57 +0100 Subject: [PATCH 24/27] Add new setting + render it --- admin/acf-city-selector-v5.php | 31 +++++++++++++++---------------- inc/acfcs-functions.php | 9 +++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/admin/acf-city-selector-v5.php b/admin/acf-city-selector-v5.php index ad571f9..3d62e0a 100755 --- a/admin/acf-city-selector-v5.php +++ b/admin/acf-city-selector-v5.php @@ -10,7 +10,6 @@ * Main class */ class acf_field_city_selector extends acf_field { - /* * Function index * - construct( $settings ) @@ -30,12 +29,12 @@ class acf_field_city_selector extends acf_field { * @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, ); @@ -45,7 +44,6 @@ function __construct( $settings ) { $this->settings = $settings; parent::__construct(); - } @@ -57,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' ) @@ -82,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, @@ -116,15 +123,14 @@ 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 = []; $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; - $save_as_single = ( isset( $field[ 'value' ][ 'save_single' ] ) ) ? $field[ 'value' ][ 'save_single' ] : 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 ) { @@ -169,9 +175,8 @@ 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' ] ) && is_admin() ) { - echo acfcs_render_checkbox( $field, $save_as_single ); + if ( ! isset( $field[ 'parent_layout' ] ) && ! isset( $field[ 'parent_repeater' ] ) && $store_meta ) { + echo acfcs_render_hidden_field( 'store_meta', '1' ); } } @@ -183,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' ]; @@ -198,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 ); - } @@ -220,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; @@ -262,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' ] ) ) { @@ -328,7 +330,6 @@ function update_value( $value, $post_id, $field ) { } return $value; - } @@ -347,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' ); @@ -395,5 +395,4 @@ function validate_value( $valid, $value, $field, $input ) { } new acf_field_city_selector( $this->settings ); - } diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index 529d518..a008702 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -579,6 +579,15 @@ function acfcs_render_dropdown( $type, $field, $stored_value, $prefill_values ) } + function acfcs_render_hidden_field( $name, $value ) { + if ( $name && $value ) { + return sprintf( '', $name, $value ); + } + + return false; + } + + function acfcs_render_checkbox( $field, $stored_value ) { $faq_explanation = ACFCS_WEBSITE_URL . '/faq/store-city-selector-values-as-single-meta/'; $field_id = $field[ 'id' ]; From 3341527e0884c00b4a3ba4e066e9fe77ffcffbd0 Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 29 Dec 2022 19:32:12 +0100 Subject: [PATCH 25/27] Fix concatenated query --- inc/acfcs-functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index a008702..cde5ea3 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -71,9 +71,9 @@ function acfcs_get_states( $country_code = false, $show_first = true, $field = f if ( false != $country_code ) { $transient = get_transient( 'acfcs_states_' . strtolower( $country_code ) ); if ( false == $transient || is_array( $transient ) && empty( $transient ) ) { - $order = 'ORDER BY state_name ASC'; + $order = ' ORDER BY state_name ASC'; if ( 'FR' == $country_code ) { - $order = "ORDER BY LENGTH(state_name), state_name"; + $order = " ORDER BY LENGTH(state_name), state_name"; } global $wpdb; From d18916a438b68cb3a704b11079c20a0f914cf6ab Mon Sep 17 00:00:00 2001 From: Beee Date: Thu, 29 Dec 2022 19:35:36 +0100 Subject: [PATCH 26/27] Remove unused function --- inc/acfcs-functions.php | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/inc/acfcs-functions.php b/inc/acfcs-functions.php index cde5ea3..90260ec 100755 --- a/inc/acfcs-functions.php +++ b/inc/acfcs-functions.php @@ -588,36 +588,6 @@ function acfcs_render_hidden_field( $name, $value ) { } - function acfcs_render_checkbox( $field, $stored_value ) { - $faq_explanation = ACFCS_WEBSITE_URL . '/faq/store-city-selector-values-as-single-meta/'; - $field_id = $field[ 'id' ]; - $field_name = $field[ 'name' ]; - $field_label = __( 'Save as single meta', 'acf-city-selector' ); - $field_suffix = 'save_single'; - - ob_start(); - ?> -
-
- /> -
-
- - | - - - -
-
- Date: Thu, 29 Dec 2022 19:38:38 +0100 Subject: [PATCH 27/27] Changelog --- README.md | 5 +++-- readme.txt | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8aace68..c245f21 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Welcome to the City Selector plugin, which is an extension for [Advanced Custom ### Version -1.10.0 - released 1x.12.22 +1.10.0 - released 29.12.22 ### Description @@ -229,7 +229,8 @@ I got the idea for this plugin through [Fabrizio Sabato](https://github.com/fab0 ### Changelog 1.10.0 -* add single meta values for location +* add option to store location as single meta values +* fix ordering for states (which caused some issues) 1.9.1 * fixed incorrect version diff --git a/readme.txt b/readme.txt index e35244e..f6b5d94 100755 --- a/readme.txt +++ b/readme.txt @@ -42,7 +42,8 @@ A. Please read the FAQ @ [https://acf-city-selector.com/documentation/](https:// == Changelog == = 1.10.0 = -* ? +* Add option to store location as single meta values +* Fix ordering for states (which caused some issues) = 1.9.1 = * fix incorrect version