Skip to content

Commit

Permalink
Merge pull request #58 from Beee4life/feature/0.22.2
Browse files Browse the repository at this point in the history
Feature/0.22.2
  • Loading branch information
Beee4life authored Aug 19, 2020
2 parents b06e1c7 + 9346e02 commit 5524810
Show file tree
Hide file tree
Showing 10 changed files with 3,775 additions and 3,720 deletions.
180 changes: 105 additions & 75 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.22.1
Version: 0.22.2
Author: Beee
Author URI: https://berryplasman.com
Text Domain: acf-city-selector
Expand Down Expand Up @@ -33,7 +33,8 @@ class ACF_City_Selector {
public function __construct() {

$this->settings = array(
'version' => '0.22.1',
'db_version' => '1.0',
'version' => '0.22.2',
'url' => plugin_dir_url( __FILE__ ),
'path' => plugin_dir_path( __FILE__ ),
'upload_folder' => wp_upload_dir()[ 'basedir' ] . '/acfcs/',
Expand All @@ -42,6 +43,21 @@ public function __construct() {
define( 'ACFCS_WEBSITE_URL', 'https://acfcs.berryplasman.com' );
}

if ( ! defined( 'ACFCS_PLUGIN_URL' ) ) {
$plugin_url = plugins_url( '/', __FILE__ );
define( 'ACFCS_PLUGIN_URL', $plugin_url );
}

if ( ! defined( 'ACFCS_PLUGIN_PATH' ) ) {
$plugin_path = dirname( __FILE__ );
define( 'ACFCS_PLUGIN_PATH', $plugin_path );
}

if ( ! defined( 'ACFCS_PLUGIN_SETTINGS' ) ) {
$settings_url = admin_url( 'admin.php?page=b3-onboarding' );
define( 'ACFCS_PLUGIN_SETTINGS', $settings_url );
}

// set text domain
load_plugin_textdomain( 'acf-city-selector', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

Expand Down Expand Up @@ -69,6 +85,7 @@ public function __construct() {
add_action( 'plugins_loaded', array( $this, 'acfcs_change_plugin_order' ), 5 );
add_action( 'plugins_loaded', array( $this, 'acfcs_check_for_acf' ), 6 );
add_action( 'plugins_loaded', array( $this, 'acfcs_check_acf_version' ) );
add_action( 'plugins_loaded', array( $this, 'acfcs_set_db_version' ) );

// Plugin's own actions
add_action( 'acfcs_after_success_country_remove', array( $this, 'acfcs_delete_transients' ) );
Expand All @@ -92,64 +109,11 @@ public function __construct() {
}


/*
* Change plugin order so ACFCS loads after ACF
*/
public function acfcs_change_plugin_order() {
$active_plugins = get_option( 'active_plugins' );
$acfcs_key = array_search( 'acf-city-selector/ACF_City_Selector.php', $active_plugins );
$acf_key = array_search( 'advanced-custom-fields-pro/acf.php', $active_plugins );
if ( false !== $acf_key && false !== $acfcs_key ) {
if ( $acfcs_key < $acf_key ) {
$this->acfcs_move_array_element( $active_plugins, $acfcs_key, $acf_key );
update_option( 'active_plugins', $active_plugins, true );
}
}
}


/*
* Check if ACF is active and if not add an admin notice
*/
public function acfcs_check_for_acf() {
if ( ! class_exists( 'acf' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="error"><p>';
echo sprintf( __( '"%s" is not activated. This plugin <strong>must</strong> be activated, because without it "%s" won\'t work. Activate it <a href="%s">here</a>.', 'acf-city-selector' ),
'Advanced Custom Fields',
'ACF City Selector',
esc_url( admin_url( 'plugins.php?s=acf&plugin_status=inactive' ) ) );
echo '</p></div>';
});
}
}


/**
* Add admin notice when ACF version < 5
*/
public function acfcs_check_acf_version() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();

if ( isset( $plugins[ 'advanced-custom-fields-pro/acf.php' ] ) ) {
if ( $plugins[ 'advanced-custom-fields-pro/acf.php' ][ 'Version' ] < 5 && is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="error"><p>';
echo sprintf( __( '<b>Warning</b>: The "%s" plugin will not work properly (anymore) with %s v4.x. Please upgrade to PRO.', 'acf-city-selector' ), 'City Selector', 'Advanced Custom Fields' );
echo '</p></div>';
} );
}
}
}


/*
* Do stuff upon plugin activation
*/
public function acfcs_plugin_activation() {
$this->acfcs_set_db_version();
if ( false == get_option( 'acfcs_preserve_settings' ) ) {
$this->acfcs_create_fill_db();
}
Expand Down Expand Up @@ -184,24 +148,25 @@ public function acfcs_create_fill_db() {
* Check if table exists
*/
public function acfcs_check_table() {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
ob_start();
global $wpdb;
?>
CREATE TABLE <?php echo $wpdb->prefix; ?>cities (
id int(6) unsigned NOT NULL auto_increment,
city_name varchar(50) NULL,
state_code varchar(3) NULL,
state_name varchar(50) NULL,
country_code varchar(2) NULL,
country varchar(50) NULL,
PRIMARY KEY (id)
)
COLLATE <?php echo $wpdb->collate; ?>;
<?php
$sql = ob_get_clean();
dbDelta( $sql );

if ( get_option( 'b3_db_version', false ) != $this->settings[ 'db_version' ] ) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
ob_start();
global $wpdb;
?>
CREATE TABLE <?php echo $wpdb->prefix; ?>cities (
id int(6) unsigned NOT NULL auto_increment,
city_name varchar(50) NULL,
state_code varchar(3) NULL,
state_name varchar(50) NULL,
country_code varchar(2) NULL,
country varchar(50) NULL,
PRIMARY KEY (id)
)
COLLATE <?php echo $wpdb->collate; ?>;
<?php
$sql = ob_get_clean();
dbDelta( $sql );
}
}


Expand Down Expand Up @@ -625,7 +590,7 @@ public static function acfcs_show_admin_notices() {
}
echo $message;
echo '</div>';
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . esc_html__( 'Dismiss this notice', 'action-logger' ) . '</span></button>';
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . esc_html__( 'Dismiss this notice', 'acf-city-selector' ) . '</span></button>';
}
echo '</div>';
}
Expand Down Expand Up @@ -723,6 +688,70 @@ public static function acfcs_admin_menu() {
return $menu;
}

/*
* Check if ACF is active and if not add an admin notice
*/
public function acfcs_check_for_acf() {
if ( ! class_exists( 'acf' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="error"><p>';
echo sprintf( __( '"%s" is not activated. This plugin <strong>must</strong> be activated, because without it "%s" won\'t work. Activate it <a href="%s">here</a>.', 'acf-city-selector' ),
'Advanced Custom Fields',
'ACF City Selector',
esc_url( admin_url( 'plugins.php?s=acf&plugin_status=inactive' ) ) );
echo '</p></div>';
});
}
}


/*
* Add admin notice when ACF version < 5
*/
public function acfcs_check_acf_version() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugins = get_plugins();

if ( isset( $plugins[ 'advanced-custom-fields-pro/acf.php' ] ) ) {
if ( $plugins[ 'advanced-custom-fields-pro/acf.php' ][ 'Version' ] < 5 && is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="error"><p>';
echo sprintf( __( '<b>Warning</b>: The "%s" plugin will not work properly (anymore) with %s v4.x. Please upgrade to PRO.', 'acf-city-selector' ), 'City Selector', 'Advanced Custom Fields' );
echo '</p></div>';
} );
}
}
}


/*
* Change plugin order so ACFCS loads after ACF
*/
public function acfcs_change_plugin_order() {
$active_plugins = get_option( 'active_plugins' );
$acfcs_key = array_search( 'acf-city-selector/ACF_City_Selector.php', $active_plugins );
$acf_key = array_search( 'advanced-custom-fields-pro/acf.php', $active_plugins );
if ( false !== $acf_key && false !== $acfcs_key ) {
if ( $acfcs_key < $acf_key ) {
$this->acfcs_move_array_element( $active_plugins, $acfcs_key, $acf_key );
update_option( 'active_plugins', $active_plugins, true );
}
}
}


/*
* Set DB version
*/
public function acfcs_set_db_version() {
if ( get_option( 'b3_db_version', false ) != $this->settings[ 'db_version' ] ) {
update_option( 'b3_db_version', $this->settings[ 'db_version' ] );
}
}


/**
* Move array element to specific position
*
Expand All @@ -735,6 +764,7 @@ public static function acfcs_move_array_element( &$array, $from_index, $to_index
array_splice( $array, $to_index, 0, $out );
}


/*
* Adds admin pages
*/
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

0.22.1 - released 13.06.20
0.22.2 - released 20.08.20

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

[X] Wordpress 5.4.1
[X] Advanced Custom Fields Pro 5.8.9
[X] Wordpress 5.5
[X] Advanced Custom Fields Pro 5.9.0
[X] Advanced Custom Fields 4.4.12
[X] Chrome (latest version)
[ ] Firefox (latest version)
Expand Down Expand Up @@ -225,6 +225,10 @@ I got the idea for this plugin through [Fabrizio Sabato](https://github.com/fab0
<a name="changelog"></a>
### Changelog

0.22.2
* changed var name which prevented storing of some fields
* added constants

0.22.1
* added undefined index when no criteria are used to search (in admin)
* added isset for new values
Expand Down
20 changes: 14 additions & 6 deletions fields/acf-city-selector-v5.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,36 +334,44 @@ function update_value( $value, $post_id, $field ) {

$required = $field[ 'required' ];
if ( 0 == $required ) {
if ( isset( $fields[ 'which_fields' ] ) && 'all' == $fields[ 'which_fields' ] || ! isset( $fields[ 'which_fields' ] ) ) {
if ( isset( $field[ 'which_fields' ] ) && 'all' == $field[ 'which_fields' ] || ! isset( $field[ 'which_fields' ] ) ) {
// if nothing is selected, set value to false
if ( empty( $value[ 'countryCode' ] ) && empty( $value[ 'stateCode' ] ) && empty( $value[ 'cityName' ] ) ) {
$value = false;
} elseif ( empty( $value[ 'stateCode' ] ) && empty( $value[ 'cityName' ] ) ) {
$value = false;
}
} elseif ( isset( $fields[ 'which_fields' ] ) && 'country_state' == $fields[ 'which_fields' ] ) {
} elseif ( isset( $field[ 'which_fields' ] ) && 'country_only' == $field[ 'which_fields' ] ) {
if ( empty( $value[ 'countryCode' ] ) ) {
$value = false;
}
} elseif ( isset( $field[ 'which_fields' ] ) && 'country_state' == $field[ 'which_fields' ] ) {
if ( empty( $value[ 'countryCode' ] ) || empty( $value[ 'stateCode' ] ) ) {
$value = false;
}
} elseif ( isset( $fields[ 'which_fields' ] ) && 'country_city' == $fields[ 'which_fields' ] ) {
} elseif ( isset( $field[ 'which_fields' ] ) && 'country_city' == $field[ 'which_fields' ] ) {
if ( empty( $value[ 'countryCode' ] ) || empty( $value[ 'cityName' ] ) ) {
$value = false;
}
}
} else {
// field == required
if ( isset( $fields[ 'which_fields' ] ) && 'all' == $fields[ 'which_fields' ] || ! isset( $fields[ 'which_fields' ] ) ) {
if ( isset( $field[ 'which_fields' ] ) && 'all' == $field[ 'which_fields' ] || ! isset( $field[ 'which_fields' ] ) ) {
// if nothing is selected, set value to false
if ( empty( $value[ 'countryCode' ] ) && empty( $value[ 'stateCode' ] ) && empty( $value[ 'cityName' ] ) ) {
$value = false;
} elseif ( empty( $value[ 'countryCode' ] ) || empty( $value[ 'stateCode' ] ) || empty( $value[ 'cityName' ] ) ) {
$value = false;
}
} elseif ( isset( $fields[ 'which_fields' ] ) && 'country_state' == $fields[ 'which_fields' ] ) {
} elseif ( isset( $field[ 'which_fields' ] ) && 'country_only' == $field[ 'which_fields' ] ) {
if ( empty( $value[ 'countryCode' ] ) ) {
$value = false;
}
} elseif ( isset( $field[ 'which_fields' ] ) && 'country_state' == $field[ 'which_fields' ] ) {
if ( empty( $value[ 'countryCode' ] ) || empty( $value[ 'stateCode' ] ) ) {
$value = false;
}
} elseif ( isset( $fields[ 'which_fields' ] ) && 'country_city' == $fields[ 'which_fields' ] ) {
} elseif ( isset( $field[ 'which_fields' ] ) && 'country_city' == $field[ 'which_fields' ] ) {
if ( empty( $value[ 'countryCode' ] ) || empty( $value[ 'cityName' ] ) ) {
$value = false;
}
Expand Down
17 changes: 13 additions & 4 deletions inc/acfcs-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function acfcs_populate_city_select( $country_code = false, $state_code = false,
*
* @return array
*/
function acfcs_get_countries( $show_first = false, $show_labels = false ) {
function acfcs_get_countries( $show_first = false, $show_labels = false, $show_count = false ) {

$countries = [];
if ( false !== $show_first ) {
Expand All @@ -80,7 +80,15 @@ function acfcs_get_countries( $show_first = false, $show_labels = false ) {

$country_results = [];
foreach ( $results as $data ) {
$country_results[ $data->country_code ] = __( $data->country, 'acf-city-selector' );
$count_results = false;
if ( false != $show_count ) {
$city_results = $wpdb->get_results( '
SELECT * FROM ' . $wpdb->prefix . 'cities
WHERE country_code = \'' . $data->country_code . '\'
' );
$count_results = ' (' . count( $city_results ) . ')';
}
$country_results[ $data->country_code ] = __( $data->country, 'acf-city-selector' ) . $count_results;
}

set_transient( 'acfcs_countries', $country_results, DAY_IN_SECONDS );
Expand Down Expand Up @@ -467,8 +475,9 @@ function acfcs_get_country_info() {
ORDER BY country_code ASC
' );
$acfcs_info[ $country_code ] = [
'count' => count( $results ),
'name' => acfcs_get_country_name( $country_code ),
'country_code' => $country_code,
'count' => count( $results ),
'name' => acfcs_get_country_name( $country_code ),
];
}

Expand Down
4 changes: 3 additions & 1 deletion inc/acfcs-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function acfcs_info_page() {
</div>

<div class="acfcs__section acfcs__section--countries">
<h2><?php esc_html_e( 'Countries in database', 'acf-city-selector' ); ?></h2>
<?php if ( ! empty( $countries ) ) { ?>
<h2><?php esc_html_e( 'Countries in database', 'acf-city-selector' ); ?></h2>
<table class="acfcs__table acfcs__table--info">
<thead>
<tr>
Expand All @@ -57,6 +57,8 @@ function acfcs_info_page() {
<?php } ?>
</tbody>
</table>
<?php } else { ?>
<?php $prepare_json[ 'countries' ] = 'none'; ?>
<?php } ?>

<h2><?php esc_html_e( 'Server info', 'acf-city-selector' ); ?></h2>
Expand Down
Loading

0 comments on commit 5524810

Please sign in to comment.