Skip to content

Commit

Permalink
#55-US State field
Browse files Browse the repository at this point in the history
  • Loading branch information
reygcalantaol committed Jun 27, 2021
1 parent f5d07ff commit 6f56ca3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
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';

This comment has been minimized.

Copy link
@polevaultweb

polevaultweb Jun 28, 2021

Contributor

Note to self, needs to be 2.4

}

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

0 comments on commit 6f56ca3

Please sign in to comment.