Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
atanas-dev committed Aug 18, 2017
2 parents 63a01d1 + f1e4879 commit 333b8c3
Show file tree
Hide file tree
Showing 26 changed files with 114 additions and 79 deletions.
4 changes: 2 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
1. You should now have changes ONLY in the /assets/dist/ directory - any changes outside this directory mean that you have a dirty working copy. Never commit anything else into `release` except /assets/dist/ changes.
1. Commit the /assets/dist/ changes to `release`
1. `git push --all`
1. Create a new release in [Github](https://github.com/htmlburger/carbon-fields-plugin/releases/new) from the `release` branch
1. Create a new release in [Github](https://github.com/htmlburger/carbon-fields/releases/new) from the `release` branch
1. Enter the new version you set in `config.php` for `Tag version` and `Title`
1. Add a changelog for `Description`
1. Click `Publish release`

## `htmlburger/carbon-fields-plugin`

1. Update the version in `carbon-fields-plugin.php` and `composer.json` for `htmlburger/carbon-fields` to match the newly released version
1. Update the version in `carbon-fields-plugin.php`, `readme.txt` and `composer.json` for `htmlburger/carbon-fields` to match the newly released version
1. Commit to `master`
1. Create a new release in [Github](https://github.com/htmlburger/carbon-fields-plugin/releases/new) from the `master` branch
1. Enter the new version for `Tag version` and `Title` (you can skip the changelog)
Expand Down
25 changes: 20 additions & 5 deletions assets/js/fields/components/datetime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const DateTimeField = ({
field,
options,
buttonText,
handleManualInput,
formatManualInput,
}) => {
return <Field field={field}>
<Flatpickr options={options} className="carbon-field-group-holder">
Expand All @@ -44,7 +46,8 @@ export const DateTimeField = ({
name={name}
value={field.value}
disabled={!field.ui.is_visible}
onChange={()=>{}} /* Use a noop as Flatpickr events are handled instead */
onChange={handleManualInput}
onBlur={formatManualInput}
className="regular-text carbon-field-group-input"
data-input
{...field.attributes} />
Expand Down Expand Up @@ -111,6 +114,18 @@ export const enhance = compose(
field.picker = instance;
},

handleManualInput: ({ field, setFieldValue }) => e => {
let value = e.target.value;
if (value !== field.value) {
setFieldValue(field.id, value);
}
},

formatManualInput: ({ field, setFieldValue }) => e => {
let value = e.target.value;
field.picker.setDate(value, true);
},

handleChange: ({ field, setFieldValue }) => ([ selectedDate ], selectedDateStr, instance) => {
instance._selectedDateStr = selectedDateStr;

Expand All @@ -128,9 +143,7 @@ export const enhance = compose(
const { value } = instance._input;

if (value) {
if (_selectedDateStr && value !== _selectedDateStr) {
instance.setDate(value, true);
}
instance.setDate(value, true);
} else {
instance.clear();
}
Expand All @@ -140,7 +153,7 @@ export const enhance = compose(
/**
* Pass some props to the component.
*/
withProps(({ field, handleReady, handleChange, handleClose }) => {
withProps(({ field, handleReady, handleManualInput, formatManualInput, handleChange, handleClose }) => {
const buttonText = field.type === TYPE_TIME
? carbonFieldsL10n.field.selectTime
: carbonFieldsL10n.field.selectDate;
Expand All @@ -156,6 +169,8 @@ export const enhance = compose(
return {
options,
buttonText,
handleManualInput,
formatManualInput,
};
}),
);
Expand Down
4 changes: 2 additions & 2 deletions assets/js/fields/components/radio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const RadioField = ({
value={option.value}
checked={isChecked(option)}
disabled={!field.ui.is_visible}
onChange={handleChange}
onChange={handleChange(option)}
{...field.attributes} />

{option.name}
Expand Down Expand Up @@ -110,7 +110,7 @@ export const enhance = compose(
* Pass some handlers to the component.
*/
withHandlers({
handleChange: ({ field, setFieldValue }) => ({ target: { value } }) => setFieldValue(field.id, value),
handleChange: ({ field, setFieldValue }) => ({ value }) => () => setFieldValue(field.id, value),
isChecked: ({ field }) => option => option.value === field.value,
})
),
Expand Down
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Define version constant
if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
define( __NAMESPACE__ . '\VERSION', '2.0.4' );
define( __NAMESPACE__ . '\VERSION', '2.0.5-beta.1' );
}

# Define root directory
Expand Down
19 changes: 9 additions & 10 deletions core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

namespace Carbon_Fields;

use Carbon_Fields\Container\Container as Abstract_Container;

/**
* Container proxy factory class.
* Used for shorter namespace access when creating a container.
*/
class Container {

/**
* A proxy for the abstract container factory method.
*
* @see Carbon_Fields\Container\Container::factory()
* @return Container
* @see Carbon_Fields\Container\Container::factory()
* @return Carbon_Fields\Container\Container
*/
public static function factory( $type, $name ) {
return Abstract_Container::factory( $type, $name );
public static function factory() {
return call_user_func_array( array( '\Carbon_Fields\Container\Container', 'factory' ), func_get_args() );
}

/**
* An alias of factory().
*
* @see Container::factory()
* @return Container
* @see Container::factory()
* @return Carbon_Fields\Container\Container
*/
public static function make( $type, $name ) {
return static::factory( $type, $name );
public static function make() {
return call_user_func_array( array( get_class(), 'factory' ), func_get_args() );
}
}
2 changes: 1 addition & 1 deletion core/Container/Comment_Meta_Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function is_valid_attach_for_object( $object_id = null ) {
*/
public function attach() {
add_meta_box(
$this->id,
$this->get_id(),
$this->title,
array( $this, 'render' ),
'comment',
Expand Down
30 changes: 15 additions & 15 deletions core/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ abstract class Container implements Datastore_Holder_Interface {
/**
* Create a new container of type $type and name $name.
*
* @param string $raw_type
* @param string $id Unique id for the container. Optional
* @param string $name Human-readable name of the container
* @return object $container
* @param string $raw_type
* @param string $id Unique id for the container. Optional
* @param string $name Human-readable name of the container
* @return Container $container
*/
public static function factory( $raw_type, $id, $name = '' ) {
// no name provided - switch input around as the id is optionally generated based on the name
Expand All @@ -151,10 +151,7 @@ public static function factory( $raw_type, $id, $name = '' ) {
$type = Helper::normalize_type( $raw_type );
$repository = Carbon_Fields::resolve( 'container_repository' );
$container = null;

if ( $id === '' ) {
$id = $repository->get_unique_container_id( $name );
}
$id = $repository->get_unique_container_id( ( $id !== '' ) ? $id : $name );

if ( ! Helper::is_valid_entity_id( $id ) ) {
Incorrect_Syntax_Exception::raise( 'Container IDs can only contain lowercase alphanumeric characters, dashes and underscores ("' . $id . '" passed).' );
Expand Down Expand Up @@ -194,10 +191,11 @@ public static function factory( $raw_type, $id, $name = '' ) {
/**
* An alias of factory().
*
* @see Container::factory()
* @see Container::factory()
* @return Container
*/
public static function make( $type, $name ) {
return static::factory( $type, $name );
public static function make() {
return call_user_func_array( array( get_class(), 'factory' ), func_get_args() );
}

/**
Expand Down Expand Up @@ -484,16 +482,18 @@ public function get_root_field_by_name( $field_name ) {
* @return string
*/
protected function get_field_pattern_regex() {
$field_name_characters = Helper::get_field_name_characters_pattern();

// matches:
// field_name
// field_name[0]
// field_name[0]:group_name
// field_name:group_name
$regex = '/
\A
(?P<field_name>[a-z0-9_]+)
(?P<field_name>[' . $field_name_characters . ']+)
(?:\[(?P<group_index>\d+)\])?
(?:' . preg_quote( static::HIERARCHY_GROUP_SEPARATOR, '/' ). '(?P<group_name>[a-z0-9_]+))?
(?:' . preg_quote( static::HIERARCHY_GROUP_SEPARATOR, '/' ). '(?P<group_name>[' . $field_name_characters . ']+))?
\z
/x';
return $regex;
Expand Down Expand Up @@ -617,7 +617,7 @@ public function get_datastore() {
* @return string
*/
protected function get_nonce_name() {
return 'carbon_fields_container_' . $this->id . '_nonce';
return $this->get_id() . '_nonce';
}

/**
Expand Down Expand Up @@ -763,7 +763,7 @@ public function to_json( $load ) {
$conditions = $this->condition_translator->fulfillable_to_foreign( $conditions );

$container_data = array(
'id' => $this->id,
'id' => $this->get_id(),
'type' => $this->type,
'title' => $this->title,
'classes' => $this->get_classes(),
Expand Down
2 changes: 1 addition & 1 deletion core/Container/Nav_Menu_Item_Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected function get_clone_for_menu_item( $menu_item_id, $load = true ) {
$custom_fields[] = $tmp_field;
}

$container = Container::factory( $this->type, $menu_item_field_prefix . $this->id )
$container = Container::factory( $this->type, $menu_item_field_prefix . $this->get_id() )
->set_datastore( $menu_item_datastore, true )
->add_fields( $custom_fields )
->init( $menu_item_id );
Expand Down
5 changes: 3 additions & 2 deletions core/Container/Post_Meta_Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ public function attach() {

foreach ( $this->post_types as $post_type ) {
add_meta_box(
$this->id,
$this->get_id(),
$this->title,
array( $this, 'render' ),
$post_type,
$this->settings['meta_box_context'],
$this->settings['meta_box_priority']
);

add_filter( "postbox_classes_{$post_type}_{$this->id}", array( $this, 'add_postbox_classes' ) );
$container_id = $this->get_id();
add_filter( "postbox_classes_{$post_type}_{$container_id}", array( $this, 'add_postbox_classes' ) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/Container/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Repository {
* @return array
*/
public function register_container( Container $container ) {
$this->register_unique_container_id( $container->id );
$this->register_unique_container_id( $container->get_id() );
$this->containers[] = $container;
$this->pending_containers[] = $container;
}
Expand Down
6 changes: 3 additions & 3 deletions core/Datastore/Datastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public static function factory( $raw_type ) {
/**
* An alias of factory().
*
* @see Datastore::factory()
* @see Datastore::factory()
* @return Datastore_Interface
*/
public static function make( $type ) {
return static::factory( $type );
public static function make() {
return call_user_func_array( array( get_class(), 'factory' ), func_get_args() );
}
}
5 changes: 3 additions & 2 deletions core/Datastore/Key_Value_Datastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ protected function value_tree_array_to_value_tree( $value_tree_array, Field $fie
$hierarchy_index = $field->get_hierarchy_index();

foreach ( $hierarchy as $index => $parent_field ) {
if ( isset( $value_tree[ $parent_field ][ $hierarchy_index[ $index ] ] ) ) {
$value_tree = $value_tree[ $parent_field ][ $hierarchy_index[ $index ] ];
$hierarchy_index_value = isset( $hierarchy_index[ $index ] ) ? $hierarchy_index[ $index ] : 0;
if ( isset( $value_tree[ $parent_field ][ $hierarchy_index_value ] ) ) {
$value_tree = $value_tree[ $parent_field ][ $hierarchy_index_value ];
}
}

Expand Down
17 changes: 9 additions & 8 deletions core/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

namespace Carbon_Fields;

use Carbon_Fields\Field\Field as Abstract_Field;

/**
* Field proxy factory class.
* Used for shorter namespace access when creating a field.
*/
class Field {

/**
* A proxy for the abstract field factory method.
*
* @see Carbon_Fields\Field\Field::factory()
* @see Carbon_Fields\Field\Field::factory()
* @return Carbon_Fields\Field\Field
*/
public static function factory( $type, $name, $label = null ) {
return Abstract_Field::factory( $type, $name, $label );
public static function factory() {
return call_user_func_array( array( '\Carbon_Fields\Field\Field', 'factory' ), func_get_args() );
}

/**
* An alias of factory().
*
* @see Field::factory()
* @see Field::factory()
* @return Carbon_Fields\Field\Field
*/
public static function make( $type, $name, $label = null ) {
return static::factory( $type, $name, $label );
public static function make() {
return call_user_func_array( array( get_class(), 'factory' ), func_get_args() );
}
}
9 changes: 5 additions & 4 deletions core/Field/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ public static function factory( $raw_type, $name, $label = null ) {
/**
* An alias of factory().
*
* @see Field::factory()
* @see Field::factory()
* @return Field
*/
public static function make( $type, $name, $label = null ) {
return static::factory( $type, $name, $label );
public static function make() {
return call_user_func_array( array( get_class(), 'factory' ), func_get_args() );
}

/**
Expand Down Expand Up @@ -648,7 +648,8 @@ public function set_name( $name ) {
}

// symbols ][ are supported in a hidden way - required for widgets to work (WP imposes dashes and square brackets on field names)
$regex = '/\A[a-z0-9_\-\[\]]+\z/';
$field_name_characters = Helper::get_field_name_characters_pattern();
$regex = '/\A[' . $field_name_characters . '\[\]]+\z/';
if ( ! preg_match( $regex, $name ) ) {
Incorrect_Syntax_Exception::raise( 'Field names can only contain lowercase alphanumeric characters, dashes and underscores ("' . $name . '" passed).' );
return $this;
Expand Down
11 changes: 9 additions & 2 deletions core/Field/Select_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ public function to_json( $load ) {
$options = $this->parse_options( $this->get_options() );
$values = wp_list_pluck( $options, 'value' );
$value = $this->get_formatted_value();
if ( ! in_array( $value, $values ) && ! empty( $values ) ) {
$value = $values[0];
if ( ! empty( $values ) ) {
// this roundabout way is required in order to keep proper value types
// as values taken from the database are always strings
$value_index = array_search( $value, $values );
if ( $value_index !== false ) {
$value = $values[ $value_index ];
} else {
$value = $values[0];
}
}

$field_data = array_merge( $field_data, array(
Expand Down
9 changes: 9 additions & 0 deletions core/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,13 @@ public static function sanitize_classes( $classes ) {
public static function is_valid_entity_id( $id ) {
return ! empty( $id ) && preg_match( '/\A[a-z0-9_\-]+\z/', $id );
}

/**
* Return a partial regex pettern matching allowed field name characters
*
* @return string
*/
public static function get_field_name_characters_pattern() {
return 'a-z0-9_\-';
}
}
Loading

0 comments on commit 333b8c3

Please sign in to comment.