Skip to content

#55-US State field #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/wpum-fields/class-wpum-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function load() {
'hidden',
'taxonomy',
'user',
'states',
] );

foreach ( $fields as $field ) {
Expand Down
65 changes: 65 additions & 0 deletions includes/wpum-fields/types/class-wpum-field-states.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Registers a States field for the forms.
*
* @package wp-user-manager
* @copyright Copyright (c) 2021, WP User Manager
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

/**
* Register a dropdown field type.
*/
class WPUM_Field_States extends WPUM_Field_Type {

public function __construct() {
$this->name = esc_html__( 'US States', 'wp-user-manager' );
$this->type = 'states';
$this->icon = 'dashicons-location-alt';
$this->group = 'advanced';
$this->label = 'State';
$this->allow_default = false;
$this->min_addon_version = '2.3';
}

public function get_data_keys() {
$keys = parent::get_data_keys();

return array_merge( $keys, array_keys( $this->get_editor_settings()['general'] ) );
}

/**
* @return array
*/
public function get_editor_settings() {
return [
'general' => [
'allow_multiple' => array(
'type' => 'checkbox',
'label' => esc_html__( 'Allow multiple selection', 'wp-user-manager' ),
'model' => 'allow_multiple',
'default' => false,
)
],
];
}

/**
* Format the output onto the profiles for the taxonomy field.
*
* @param object $field
* @param mixed $value
* @return string
*/
function get_formatted_output( $field, $value ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}

return implode( ', ', wp_list_pluck( $value ) );
}

}
21 changes: 21 additions & 0 deletions templates/form-fields/states-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* The template for displaying the states field.
*
* This template can be overridden by copying it to yourtheme/wpum/form-fields/states-field.php
*
* HOWEVER, on occasion WPUM will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @version 1.0.0
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

$field_type = empty( $data->allow_multiple ) ? 'select' : 'multiselect';

WPUM()->templates->set_template_data( $data )->get_template_part( 'form-fields/' . $field_type, 'field' );