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

Added URL field #171

Merged
merged 6 commits into from
Nov 3, 2012
Merged
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
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
85 changes: 19 additions & 66 deletions inc/fields/select-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;

// Make sure "select" field is loaded
require_once RWMB_FIELDS_DIR . 'select.php';

if ( !class_exists( 'RWMB_Select_Advanced_Field' ) )
{
class RWMB_Select_Advanced_Field
class RWMB_Select_Advanced_Field extends RWMB_Select_Field
{
/**
* Enqueue scripts and styles
Expand All @@ -13,10 +16,11 @@ class RWMB_Select_Advanced_Field
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'select2', RWMB_CSS_URL . 'select2-css/select2.css', array(), '3.2' );
wp_enqueue_style( 'select2', RWMB_CSS_URL . 'select2/select2.css', array(), '3.2' );
wp_enqueue_style( 'rwmb-select-advanced', RWMB_CSS_URL . 'select-advanced.css', array(), RWMB_VER );
wp_register_script( 'select2', RWMB_JS_URL . 'select2-js/select2.js', array(), '3.2', true );
wp_enqueue_script( 'select_advanced', RWMB_JS_URL . 'select-advanced.js', array('select2'), RWMB_VER, true );

wp_register_script( 'select2', RWMB_JS_URL . 'select2/select2.min.js', array(), '3.2', true );
wp_enqueue_script( 'rwmb-select-advanced', RWMB_JS_URL . 'select-advanced.js', array( 'select2' ), RWMB_VER, true );
}

/**
Expand All @@ -31,12 +35,15 @@ static function admin_enqueue_scripts()
static function html( $html, $meta, $field )
{
$html = sprintf(
'<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options ="%s">',
'<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options="%s">',
$field['field_name'],
$field['id'],
$field['multiple'] ? ' multiple="multiple"' : '',
esc_attr( json_encode( $field['js_options'] ))
esc_attr( json_encode( $field['js_options'] ) )
);
if ( !empty( $field['js_options']['placeholder'] ) )
$html .= '<option></option>';

$option = '<option value="%s" %s>%s</option>';

foreach ( $field['options'] as $value => $label )
Expand All @@ -53,59 +60,6 @@ static function html( $html, $meta, $field )
return $html;
}

/**
* Get meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* @see "save" method for better understanding
*
* TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility
*
* @param $meta
* @param $post_id
* @param $saved
* @param $field
*
* @return array
*/
static function meta( $meta, $post_id, $saved, $field )
{
$single = $field['clone'] || !$field['multiple'];
$meta = get_post_meta( $post_id, $field['id'], $single );
$meta = ( !$saved && '' === $meta || array() === $meta ) ? $field['std'] : $meta;

$meta = array_map( 'esc_attr', (array) $meta );

return $meta;
}

/**
* Save meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility
*
* @param $new
* @param $old
* @param $post_id
* @param $field
*/
static function save( $new, $old, $post_id, $field )
{
if ( !$field['clone'] )
{
RW_Meta_Box::save( $new, $old, $post_id, $field );
return;
}

if ( empty( $new ) )
delete_post_meta( $post_id, $field['id'] );
else
update_post_meta( $post_id, $field['id'], $new );
}

/**
* Normalize parameters for field
*
Expand All @@ -115,19 +69,18 @@ static function save( $new, $old, $post_id, $field )
*/
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );

$field = wp_parse_args( $field, array(
'js_options' => array(),
) );

$field['js_options'] = wp_parse_args( $field['js_options'], array(
'allowClear' => true,
'width' => 'resolve',
'placeholder' => "Select a Value"
'allowClear' => true,
'width' => 'resolve',
'placeholder' => __( 'Select a value', 'rwmb' )
) );

$field['field_name'] = $field['id'];
if ( !$field['clone'] && $field['multiple'] )
$field['field_name'] .= '[]';

return $field;
}
}
Expand Down
3 changes: 1 addition & 2 deletions inc/fields/time.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static function admin_enqueue_scripts( )
static function html( $html, $meta, $field )
{
return sprintf(
'<input type="text" class="rwmb-datetime" name="%s" value="%s" id="%s" size="%s" data-options="%s" />',
'<input type="text" class="rwmb-time" name="%s" value="%s" id="%s" size="%s" data-options="%s" />',
$field['field_name'],
$meta,
isset( $field['clone'] ) && $field['clone'] ? '' : $field['id'],
Expand All @@ -63,7 +63,6 @@ static function normalize_field( $field )
// Deprecate 'format', but keep it for backward compatible
// Use 'js_options' instead
$field['js_options'] = wp_parse_args( $field['js_options'], array(
'timeOnly' => true,
'showButtonPanel' => true,
'timeFormat' => empty( $field['format'] ) ? 'hh:mm:ss' : $field['format'],
) );
Expand Down
49 changes: 49 additions & 0 deletions inc/fields/url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;

// Make sure "text" field is loaded
require_once RWMB_FIELDS_DIR . 'text.php';

if ( ! class_exists( 'RWMB_URL_Field' ) )
{
class RWMB_URL_Field
{
/**
* Get field HTML
*
* @param string $html
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $html, $meta, $field )
{
return sprintf(
'<input type="url" class="rwmb-url" name="%s" id="%s" value="%s" size="%s" />',
$field['field_name'],
$field['id'],
$meta,
$field['size']
);
}


/**
* Sanitizes url
*
* @param $post_id
* @param $field
* @param $old
* @param $new
*
* @return $new
*/
static function value( $new, $old, $post_id, $field)
{
$new = esc_url($new);
return $new;
}
}
}
2 changes: 1 addition & 1 deletion js/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function rwmb_update_datetime_picker()
options = $this.data( 'options' );

$this.siblings( '.ui-datepicker-append' ).remove(); // Remove appended text
$this.removeClass( 'hasDatepicker' ).datepicker( options );
$this.removeClass( 'hasDatepicker' ).datetimepicker( options );
} );
}

Expand Down
8 changes: 4 additions & 4 deletions js/select-advanced.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* Update date picker element
* Update select2
* Used for static & dynamic added elements (when clone)
*/
function rwmb_update_select_advanced()
{
var $ = jQuery;

$( '.rwmb-select-advanced' ).each( function()
$( '.rwmb-select-advanced' ).each( function ()
{
var $this = $( this ),
options = $this.data( 'options' );
$this.select2(options);
$this.select2( options );
} );
}

jQuery( document ).ready( function()
jQuery( document ).ready( function ()
{
rwmb_update_select_advanced();
} );
Loading