Skip to content
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

Add new field type: decimal number with two digits of precision #20

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 23 additions & 13 deletions bp-xprofile-custom-fields-type.php
Expand Up @@ -58,17 +58,7 @@ public function init()
'doc', 'docx', 'pdf'
));
$this->files_max_filesize = apply_filters('bxcft_files_max_filesize', Bxcft_Plugin::BXCFT_MAX_FILESIZE);

$locale = apply_filters( 'bxcft_load_load_textdomain_get_locale', get_locale() );
if ( !empty( $locale ) ) {
$mofile_default = sprintf( '%slang/%s.mo', plugin_dir_path(__FILE__), $locale );
$mofile = apply_filters( 'bxcft_load_textdomain_mofile', $mofile_default );

if ( file_exists( $mofile ) ) {
load_textdomain( "bxcft", $mofile );
}
}


/** Includes **/
require_once( 'classes/Bxcft_Field_Type_Birthdate.php' );
require_once( 'classes/Bxcft_Field_Type_Email.php' );
Expand All @@ -80,6 +70,9 @@ public function init()
require_once( 'classes/Bxcft_Field_Type_Image.php' );
require_once( 'classes/Bxcft_Field_Type_File.php' );
require_once( 'classes/Bxcft_Field_Type_Color.php' );
require_once( 'classes/Bxcft_Field_Type_DecimalNumber.php' );
require_once( 'classes/Bxcft_Field_Type_Gender.php' );
require_once( 'classes/Bxcft_Field_Type_Diet.php' );

if (bp_is_user_profile_edit() || bp_is_register_page()) {
wp_enqueue_script('bxcft-modernizr', plugin_dir_url(__FILE__) . 'js/modernizr.js', array(), '2.6.2', false);
Expand Down Expand Up @@ -136,6 +129,9 @@ public function bxcft_get_field_types($fields)
'image' => 'Bxcft_Field_Type_Image',
'file' => 'Bxcft_Field_Type_File',
'color' => 'Bxcft_Field_Type_Color',
'decimal_number' => 'Bxcft_Field_Type_DecimalNumber',
'gender' => 'Bxcft_Field_Type_Gender',
'diet' => 'Bxcft_Field_Type_Diet',
);
$fields = array_merge($fields, $new_fields);

Expand Down Expand Up @@ -604,6 +600,10 @@ public function bxcft_map($field_type, $field)
case 'color':
$field_type = 'textbox';
break;

case 'decimal_number':
$field_type = 'number';
break;

case 'select_custom_post_type':
case 'multiselect_custom_post_type':
Expand All @@ -614,9 +614,19 @@ public function bxcft_map($field_type, $field)

return $field_type;
}

public function bxcft_update()
{
$locale = apply_filters( 'bxcft_load_load_textdomain_get_locale', get_locale() );
if ( !empty( $locale ) ) {
$mofile_default = sprintf( '%slang/%s.mo', plugin_dir_path(__FILE__), $locale );
$mofile = apply_filters( 'bxcft_load_textdomain_mofile', $mofile_default );

if ( file_exists( $mofile ) ) {
load_textdomain( "bxcft", $mofile );
}
}

if (!get_option('bxcft_activated')) {
add_option('bxcft_activated', 1);
}
Expand All @@ -643,4 +653,4 @@ public static function deactivate()
register_activation_hook(__FILE__, array('Bxcft_Plugin', 'activate'));
register_deactivation_hook(__FILE__, array('Bxcft_Plugin', 'deactivate'));
$bxcft_plugin = new Bxcft_Plugin();
}
}
72 changes: 72 additions & 0 deletions classes/Bxcft_Field_Type_DecimalNumber.php
@@ -0,0 +1,72 @@
<?php
/**
* DecimalNumber Type
*/
if (!class_exists('Bxcft_Field_Type_DecimalNumber'))
{
class Bxcft_Field_Type_DecimalNumber extends BP_XProfile_Field_Type
{
public function __construct() {
parent::__construct();

$this->name = __( 'Decimal number (HTML5 field)', 'bxcft' );

$this->set_format( '/^\d+|-\d+\.?\d*$/', 'replace' );

do_action( 'bp_xprofile_field_type_decimal_number', $this );
}

public function admin_field_html (array $raw_properties = array ())
{
$html = $this->get_edit_field_html_elements( array_merge(
array(
'type' => 'number',
'step' => '0.01',
),
$raw_properties
) );
?>
<input <?php echo $html; ?> />
<?php
}

public function edit_field_html (array $raw_properties = array ())
{
if ( isset( $raw_properties['user_id'] ) ) {
unset( $raw_properties['user_id'] );
}

// HTML5 required attribute.
if ( bp_get_the_profile_field_is_required() ) {
$raw_properties['required'] = 'required';
}

$html = $this->get_edit_field_html_elements( array_merge(
array(
'type' => 'number',
'step' => '0.01',
'value' => bp_get_the_profile_field_edit_value(),
),
$raw_properties
) );

$label = sprintf(
'<label for="%s">%s%s</label>',
bp_get_the_profile_field_input_name(),
bp_get_the_profile_field_name(),
(bp_get_the_profile_field_is_required()) ?
' ' . esc_html__( '(required)', 'buddypress' ) : ''
);
// Label.
echo apply_filters('bxcft_field_label', $label, bp_get_the_profile_field_id(), bp_get_the_profile_field_type(), bp_get_the_profile_field_input_name(), bp_get_the_profile_field_name(), bp_get_the_profile_field_is_required());
// Errors.
do_action( bp_get_the_profile_field_errors_action() );
// Input.
?>
<input <?php echo $html; ?> />
<?php
}

public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
}
}