Skip to content
Snippets Groups Projects
Commit c48871ff authored by dyszczo's avatar dyszczo
Browse files

init 2.x. From ShopMagic plugin

parent f5322738
No related branches found
No related tags found
1 merge request!2Feature/field and templates
Pipeline #3860 failed
Showing
with 757 additions and 1 deletion
......@@ -4,12 +4,18 @@
{
"name": "Marcin",
"email": "marcin@wpdesk.pl"
},
{
"name": "Krzysiek",
"email": "krzysiek@wpdesk.pl"
}
],
"require": {
"php": ">=5.6",
"ext-curl": "*",
"ext-json": "*"
"ext-json": "*",
"wpdesk/wp-persistence": "2.0-beta",
"wpdesk/wp-view": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "<7",
......
<?php
namespace WPDesk\Forms;
interface Escaper {
/**
* @param mixed $value
*
* @return string
*/
public function escape( $value );
}
<?php
namespace WPDesk\Forms;
/**
* The idea is that from the moment the factory returns this interface it's values cannot be changed.
* And that is why here are only the getters.
*
* The: Validation, Serialization, Sanitization features are provided trough delegated classes (get_validator, get_serializer ...)
*
* @package WPDesk\Forms
*/
interface Field {
/** @return string */
public function get_name();
/** @return mixed */
public function get_default_value();
/** @return string */
public function get_template_name();
/**
* When this field is used on form this field will force it's own template.
*
* return bool
*/
public function should_override_form_template();
/**
* HTML label.
*
* @return string
*/
public function get_label();
/** bool */
public function has_label();
/**
* Description for field. It can be shown near the field.
*
* @return string
*/
public function get_description();
/**
* Additional field description that should be shown in optional hover tip.
*
* @return string
*/
public function get_description_tip();
/** @return bool */
public function has_description_tip();
/** @return bool */
public function has_description();
/**
* @return bool
*/
public function is_readonly();
/** @return bool */
public function is_disabled();
/** @return string */
public function get_id();
/** @bool */
public function is_required();
/** @return bool */
public function has_placeholder();
/** @return string */
public function get_placeholder();
/**
* @param string[] $except
*
* @return string[] name->value
*/
public function get_attributes( $except = [] );
/**
* @param string $name
* @param string $default
*
* @return string
*/
public function get_attribute( $name, $default = null );
/** @return bool */
public function is_attribute_set( $name );
/**
* @param string $name
*
* @return string
*/
public function get_meta_value( $name );
/** @return bool */
public function is_meta_value_set( $name );
/**
* @return string
*/
public function get_classes();
/** bool */
public function has_classes();
/** @return bool */
public function is_class_set( $name );
/**
* @return mixed
*/
public function get_possible_values();
/**
* @return bool
*/
public function is_multiple();
/**
* @return Validator
*/
public function get_validator();
/**
* @return Sanitizer
*/
public function get_sanitizer();
/** @return Serializer */
public function get_serializer();
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer\NoSanitize;
use WPDesk\Forms\Serializer;
use WPDesk\Forms\Serializer\NoSerialize;
use WPDesk\Forms\Validator\ChainValidator;
use WPDesk\Forms\Validator\RequiredValidator;
/**
* Base class for fields. Is responsible for settings all required field values and provides standard implementation for
* the field interface.
*
* @package WPDesk\Forms
*/
abstract class BasicField implements Field {
/** @var array[] */
protected $attributes;
/** @var array[] */
protected $meta;
protected $default_value;
public function __construct() {
$this->meta['class'] = [];
}
public function get_label() {
return $this->meta['label'];
}
/**
* @param string $value
*
* @return $this
*/
public function set_label( $value ) {
$this->meta['label'] = $value;
return $this;
}
public function get_description_tip() {
return $this->meta['description_tip'];
}
public function has_description_tip() {
return isset( $this->meta['description_tip'] );
}
public function should_override_form_template() {
return isset( $this->attributes['overrite_template'] ) ? $this->attributes['overrite_template'] : false;
}
public function get_description() {
return $this->meta['description'];
}
public function has_label() {
return isset( $this->meta['label'] );
}
public function has_description() {
return isset( $this->meta['description'] );
}
public function set_description( $value ) {
$this->meta['description'] = $value;
return $this;
}
public function set_description_tip( $value ) {
$this->meta['description_tip'] = $value;
return $this;
}
/**
* @return array
*
* @deprecated not sure if needed. TODO: Check later.
*/
public function get_type() {
return $this->attributes['type'];
}
/**
* @param string $value
*
* @return $this
*/
public function set_placeholder( $value ) {
$this->meta['placeholder'] = $value;
return $this;
}
public function has_placeholder() {
return isset( $this->meta['placeholder'] );
}
public function get_placeholder() {
return $this->meta['placeholder'];
}
/**
* @param string $name
*
* @return $this
*/
public function set_name( $name ) {
$this->attributes['name'] = $name;
return $this;
}
public function get_attributes( $except = [] ) {
return array_filter( $this->attributes, function ( $value, $key ) use ( $except ) {
return ! in_array( $key, $except );
}, ARRAY_FILTER_USE_BOTH );
}
public function get_meta_value( $name ) {
return $this->meta[ $name ];
}
public function get_classes() {
return implode( ' ', $this->meta['class'] );
}
public function has_classes() {
return ! empty( $this->meta['class'] );
}
public function get_possible_values() {
return isset( $this->meta['possible_values'] ) ? $this->meta['possible_values'] : [];
}
public function get_id() {
return isset( $this->attributes['id'] ) ? $this->attributes['id'] : sanitize_title( $this->get_name() );
}
public function get_name() {
return $this->attributes['name'];
}
public function is_multiple() {
return isset( $this->attributes['multiple'] ) ? $this->attributes['multiple'] : false;
}
/**
* @return $this
*/
public function set_disabled() {
$this->attributes['disabled'] = true;
return $this;
}
public function is_disabled() {
return isset( $this->attributes['disabled'] ) ? $this->attributes['disabled'] : false;
}
/**
* @return $this
*/
public function set_readonly() {
$this->attributes['readonly'] = true;
return $this;
}
public function is_readonly() {
return isset( $this->attributes['readonly'] ) ? $this->attributes['readonly'] : false;
}
/**
* @return $this
*/
public function set_required() {
$this->meta['required'] = true;
return $this;
}
/**
* @param string $class_name
*
* @return $this
*/
public function add_class( $class_name ) {
$this->meta['class'][ $class_name ] = $class_name;
return $this;
}
/**
* @param string $class_name
*
* @return $this
*/
public function unset_class( $class_name ) {
unset( $this->meta['class'][ $class_name ] );
return $this;
}
/**
* @param $name
* @param $value
*
* @return $this
*/
public function set_attribute( $name, $value ) {
$this->attributes[ $name ] = $value;
return $this;
}
public function unset_attribute( $name ) {
unset( $this->attributes[ $name ] );
return $this;
}
public function is_attribute_set( $name ) {
return isset( $this->attributes[ $name ] );
}
public function is_meta_value_set( $name ) {
return isset( $this->meta[ $name ] );
}
public function is_class_set( $name ) {
return isset( $this->meta['class'][ $name ] );
}
public function get_default_value() {
return $this->default_value;
}
/**
* @param string $value
*
* @return $this
*/
public function set_default_value( $value ) {
$this->default_value = $value;
return $this;
}
/**
* @return ChainValidator
*/
public function get_validator() {
$chain = new ChainValidator();
if ( $this->is_required() ) {
$chain->attach( new RequiredValidator() );
}
return $chain;
}
public function is_required() {
return isset( $this->meta['required'] ) ? $this->meta['required'] : false;
}
public function get_attribute( $name, $default = null ) {
return isset( $this->attributes[ $name ] ) ?: $default;
}
public function get_sanitizer() {
return new NoSanitize();
}
/**
* @return Serializer
*/
public function get_serializer() {
if ( isset( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) {
return $this->meta['serializer'];
}
return new NoSerialize();
}
public function set_serializer( Serializer $serializer ) {
$this->meta['serializer'] = $serializer;
return $this;
}
}
<?php
namespace WPDesk\Forms\Field;
class CheckboxField extends BasicField {
const VALUE_TRUE = 'yes';
const VALUE_FALSE = 'no';
public function __construct() {
parent::__construct();
$this->set_attribute( 'type', 'checkbox' );
}
public function get_template_name() {
return 'input-checkbox';
}
public function get_sublabel() {
return $this->meta['sublabel'];
}
public function set_sublabel( $value ) {
$this->meta['sublabel'] = $value;
return $this;
}
public function has_sublabel() {
return isset( $this->meta['sublabel'] );
}
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\NoValueField;
class Header extends NoValueField {
public function __construct() {
$this->meta['header_size'] = '';
}
public function get_template_name() {
return 'header';
}
public function should_override_form_template() {
return true;
}
public function set_header_size( $value ) {
$this->meta['header_size'] = $value;
return $this;
}
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class HiddenField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
$this->set_attribute( 'type', 'hidden' );
}
public function get_sanitizer() {
return new TextFieldSanitizer();
}
public function get_template_name() {
return 'input-hidden';
}
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Sanitizer\TextFieldSanitizer;
class InputTextField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
$this->set_attribute( 'type', 'text' );
}
public function get_sanitizer() {
return new TextFieldSanitizer();
}
public function get_template_name() {
return 'input-text';
}
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\Validator\NonceValidator;
class NoOnceField extends BasicField {
public function __construct( $action_name ) {
parent::__construct();
$this->meta['action'] = $action_name;
}
public function get_validator() {
return new NonceValidator( $this->get_meta_value( 'action' ) );
}
public function get_template_name() {
return 'noonce';
}
}
<?php
namespace WPDesk\Forms\Field;
/**
* Base class for Fields that can show itself on form but cannot process any value.
*
* @package WPDesk\Forms
*/
abstract class NoValueField extends BasicField {
public function get_name() {
return '';
}
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\NoValueField;
class Paragraph extends NoValueField {
public function get_template_name() {
return 'paragraph';
}
public function should_override_form_template() {
return true;
}
}
<?php
namespace WPDesk\Forms\Field;
class ProductSelect extends SelectField {
public function __construct() {
parent::__construct();
$this->set_multiple();
}
public function get_template_name() {
return 'product-select';
}
}
<?php
namespace WPDesk\Forms\Field;
class RadioField extends BasicField {
public function get_template_name() {
return 'input-radio';
}
}
<?php
namespace WPDesk\Forms\Field;
class SelectField extends BasicField {
public function get_template_name() {
return 'select';
}
public function set_options( $options ) {
$this->meta['possible_values'] = $options;
return $this;
}
public function set_multiple() {
$this->attributes['multiple'] = true;
return $this;
}
}
<?php
namespace WPDesk\Forms\Field;
use WPDesk\Forms\NoValueField;
class SubmitField extends NoValueField {
public function get_template_name() {
return 'input-submit';
}
public function get_type() {
return 'submit';
}
public function should_override_form_template() {
return true;
}
}
<?php
namespace WPDesk\Forms\Field;
class TextAreaField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
}
public function get_template_name() {
return 'textarea';
}
}
<?php
namespace WPDesk\Forms\Field;
class WooSelect extends SelectField {
public function __construct() {
parent::__construct();
$this->set_multiple();
$this->add_class( 'wc-enhanced-select' );
}
public function get_template_name() {
return 'woo-select';
}
}
<?php
namespace WPDesk\Forms\Field;
class WyswigField extends BasicField {
public function __construct() {
parent::__construct();
$this->set_default_value( '' );
}
public function get_template_name() {
return 'wyswig';
}
public function should_override_form_template() {
return true;
}
}
<?php
namespace WPDesk\Forms;
/**
* FieldProvider is owner of FormFields. These fields can be used to render forms and process values.
*/
interface FieldProvider {
/**
* Returns owned fields.
*
* @return Field[]
*/
public function get_fields();
}
<?php
namespace WPDesk\Forms;
/**
* @class FieldRenderer
*/
interface FieldRenderer {
/**
* @param FieldProvider $provider
* @param array $fields_data
* @param string $name_prefix
*
* @return string|array String or normalized array
*/
public function render_fields( FieldProvider $provider, array $fields_data, $name_prefix = '' );
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment