@@ -177,6 +177,8 @@ |
||
| 177 | 177 | * @param string $response |
| 178 | 178 | * @param string (optional) $field |
| 179 | 179 | * @param string (optional) $value |
| 180 | + * @param string $field |
|
| 181 | + * @param string $value |
|
| 180 | 182 | * @return string|null |
| 181 | 183 | */ |
| 182 | 184 | protected function string_or_null( $response, $field = null, $value = null ) { |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Abstraction\Composable; |
| 6 | 6 | use WFV\Contract\ArtisanInterface; |
@@ -30,9 +30,9 @@ discard block |
||
| 30 | 30 | * @param ArtisanInterface $builder |
| 31 | 31 | * @param string $action |
| 32 | 32 | */ |
| 33 | - public function __construct( ArtisanInterface $builder, $action ) { |
|
| 33 | + public function __construct(ArtisanInterface $builder, $action) { |
|
| 34 | 34 | $this->alias = $action; |
| 35 | - $this->install( $builder->collection ); |
|
| 35 | + $this->install($builder->collection); |
|
| 36 | 36 | $this->validator = $builder->validator; |
| 37 | 37 | } |
| 38 | 38 | |
@@ -45,8 +45,8 @@ discard block |
||
| 45 | 45 | * @param string $value Value to compare against. |
| 46 | 46 | * @return string|null |
| 47 | 47 | */ |
| 48 | - public function checked_if( $field = null, $value = null ) { |
|
| 49 | - return $this->string_or_null( 'checked', $field, $value ); |
|
| 48 | + public function checked_if($field = null, $value = null) { |
|
| 49 | + return $this->string_or_null('checked', $field, $value); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
@@ -60,8 +60,8 @@ discard block |
||
| 60 | 60 | * @param callable (optional) $callback |
| 61 | 61 | * @return string |
| 62 | 62 | */ |
| 63 | - public function display( $field = null, callable $callback = null ) { |
|
| 64 | - echo $input = $this->utilize('input')->escape( $field ); |
|
| 63 | + public function display($field = null, callable $callback = null) { |
|
| 64 | + echo $input = $this->utilize('input')->escape($field); |
|
| 65 | 65 | return $input; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -99,10 +99,10 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | public function is_valid() { |
| 101 | 101 | $is_valid = $this->validator->is_valid(); |
| 102 | - if( false === $is_valid ) { |
|
| 103 | - $this->utilize('errors')->set_errors( $this->validator->errors() ); |
|
| 102 | + if (false === $is_valid) { |
|
| 103 | + $this->utilize('errors')->set_errors($this->validator->errors()); |
|
| 104 | 104 | } |
| 105 | - $this->trigger_post_validate_action( $is_valid ); |
|
| 105 | + $this->trigger_post_validate_action($is_valid); |
|
| 106 | 106 | return $is_valid; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -137,8 +137,8 @@ discard block |
||
| 137 | 137 | * @param string $value Value to compare against. |
| 138 | 138 | * @return string|null |
| 139 | 139 | */ |
| 140 | - public function selected_if( $field = null, $value = null ) { |
|
| 141 | - return $this->string_or_null( 'selected', $field, $value ); |
|
| 140 | + public function selected_if($field = null, $value = null) { |
|
| 141 | + return $this->string_or_null('selected', $field, $value); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | /** |
@@ -150,9 +150,9 @@ discard block |
||
| 150 | 150 | */ |
| 151 | 151 | public function token_fields() { |
| 152 | 152 | // TODO - Move markup into something - perhaps a renderable interface? |
| 153 | - $token_name = $this->alias . '_token'; |
|
| 154 | - echo $nonce_field = wp_nonce_field( $this->alias, $token_name, false, false ); |
|
| 155 | - echo $action_field = '<input type="hidden" name="action" value="'. $this->alias .'">'; |
|
| 153 | + $token_name = $this->alias.'_token'; |
|
| 154 | + echo $nonce_field = wp_nonce_field($this->alias, $token_name, false, false); |
|
| 155 | + echo $action_field = '<input type="hidden" name="action" value="'.$this->alias.'">'; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | /** |
@@ -163,9 +163,9 @@ discard block |
||
| 163 | 163 | * @param ValidateInterface $rule |
| 164 | 164 | * @param string $field |
| 165 | 165 | */ |
| 166 | - public function validate( ValidateInterface $rule, $field ) { |
|
| 167 | - $input = $this->field_value( $field ); |
|
| 168 | - $this->validator->validate( $rule, $field, $input ); |
|
| 166 | + public function validate(ValidateInterface $rule, $field) { |
|
| 167 | + $input = $this->field_value($field); |
|
| 168 | + $this->validator->validate($rule, $field, $input); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -176,11 +176,11 @@ discard block |
||
| 176 | 176 | * |
| 177 | 177 | * @param string $field |
| 178 | 178 | */ |
| 179 | - protected function field_value( $field ) { |
|
| 179 | + protected function field_value($field) { |
|
| 180 | 180 | $input = $this->utilize('input'); |
| 181 | - if( $input->has( $field ) ) { |
|
| 182 | - $input = $input->get_array( false ); |
|
| 183 | - return $input[ $field ]; |
|
| 181 | + if ($input->has($field)) { |
|
| 182 | + $input = $input->get_array(false); |
|
| 183 | + return $input[$field]; |
|
| 184 | 184 | } |
| 185 | 185 | return null; |
| 186 | 186 | } |
@@ -196,8 +196,8 @@ discard block |
||
| 196 | 196 | * @param string (optional) $value |
| 197 | 197 | * @return string|null |
| 198 | 198 | */ |
| 199 | - protected function string_or_null( $response, $field = null, $value = null ) { |
|
| 200 | - return ( $this->input( $field )->contains( $field, $value ) ) ? $response : null; |
|
| 199 | + protected function string_or_null($response, $field = null, $value = null) { |
|
| 200 | + return ($this->input($field)->contains($field, $value)) ? $response : null; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -208,8 +208,8 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @param bool $is_valid |
| 210 | 210 | */ |
| 211 | - protected function trigger_post_validate_action( $is_valid = false ) { |
|
| 212 | - $action = ( true === $is_valid ) ? $this->alias : $this->alias .'_fail'; |
|
| 213 | - do_action( $action, $this ); |
|
| 211 | + protected function trigger_post_validate_action($is_valid = false) { |
|
| 212 | + $action = (true === $is_valid) ? $this->alias : $this->alias.'_fail'; |
|
| 213 | + do_action($action, $this); |
|
| 214 | 214 | } |
| 215 | 215 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) || die(); |
|
| 2 | +defined('ABSPATH') || die(); |
|
| 3 | 3 | /* |
| 4 | 4 | Plugin Name: WFV - Form Validation |
| 5 | 5 | Plugin URI: https://macder.github.io/wfv/ |
@@ -11,12 +11,12 @@ discard block |
||
| 11 | 11 | License URI: https://github.com/macder/wp-form-validation/blob/master/LICENSE |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -define( 'WFV_VALIDATE_VERSION', '0.11.0' ); |
|
| 15 | -define( 'WFV_VALIDATE__MINIMUM_WP_VERSION', '3.7' ); |
|
| 16 | -define( 'WFV_VALIDATE__PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
|
| 17 | -define( 'WFV_VALIDATE__ACTION_POST', 'validate_form' ); |
|
| 14 | +define('WFV_VALIDATE_VERSION', '0.11.0'); |
|
| 15 | +define('WFV_VALIDATE__MINIMUM_WP_VERSION', '3.7'); |
|
| 16 | +define('WFV_VALIDATE__PLUGIN_DIR', plugin_dir_path(__FILE__)); |
|
| 17 | +define('WFV_VALIDATE__ACTION_POST', 'validate_form'); |
|
| 18 | 18 | |
| 19 | -require_once WFV_VALIDATE__PLUGIN_DIR . '/vendor/autoload.php'; |
|
| 19 | +require_once WFV_VALIDATE__PLUGIN_DIR.'/vendor/autoload.php'; |
|
| 20 | 20 | |
| 21 | 21 | use WFV\FormComposite; |
| 22 | 22 | use WFV\Agent\InspectionAgent; |
@@ -33,21 +33,21 @@ discard block |
||
| 33 | 33 | * @param array $form Form arguments |
| 34 | 34 | * @param bool $trim Trim whitespace from beginning and end of string |
| 35 | 35 | */ |
| 36 | -function wfv_create( $action, array &$form, $trim = true ) { |
|
| 37 | - $inspect = new InspectionAgent( $action ); |
|
| 38 | - $input = ( true === $inspect->safe_submit() ) ? $_POST : array(); |
|
| 36 | +function wfv_create($action, array &$form, $trim = true) { |
|
| 37 | + $inspect = new InspectionAgent($action); |
|
| 38 | + $input = (true === $inspect->safe_submit()) ? $_POST : array(); |
|
| 39 | 39 | |
| 40 | - $builder = new FormArtisan( $form ); |
|
| 41 | - $form = ( new Director( $action ) ) |
|
| 42 | - ->with( 'input', [ $input, $trim ] ) |
|
| 43 | - ->with( 'rules' ) |
|
| 44 | - ->with( 'errors' ) |
|
| 45 | - ->with( 'messages' ) |
|
| 46 | - ->with( 'validator' ) |
|
| 47 | - ->compose( $builder ); |
|
| 40 | + $builder = new FormArtisan($form); |
|
| 41 | + $form = (new Director($action)) |
|
| 42 | + ->with('input', [$input, $trim]) |
|
| 43 | + ->with('rules') |
|
| 44 | + ->with('errors') |
|
| 45 | + ->with('messages') |
|
| 46 | + ->with('validator') |
|
| 47 | + ->compose($builder); |
|
| 48 | 48 | |
| 49 | - if( $input ) { |
|
| 50 | - wfv_validate( $form ); |
|
| 49 | + if ($input) { |
|
| 50 | + wfv_validate($form); |
|
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
@@ -59,21 +59,21 @@ discard block |
||
| 59 | 59 | * @param FormComposite $form |
| 60 | 60 | * @return bool |
| 61 | 61 | */ |
| 62 | -function wfv_validate( FormComposite $form ) { |
|
| 62 | +function wfv_validate(FormComposite $form) { |
|
| 63 | 63 | $rules = $form->rules()->get_array(); |
| 64 | 64 | $messages = $form->messages()->get_array(); |
| 65 | - $factory = new ValidatorFactory( $messages ); |
|
| 65 | + $factory = new ValidatorFactory($messages); |
|
| 66 | 66 | |
| 67 | - foreach( $rules as $field => $ruleset ) { |
|
| 67 | + foreach ($rules as $field => $ruleset) { |
|
| 68 | 68 | $optional = in_array('optional', $ruleset); |
| 69 | - if( $optional ) { |
|
| 70 | - array_shift( $ruleset ); |
|
| 69 | + if ($optional) { |
|
| 70 | + array_shift($ruleset); |
|
| 71 | 71 | } |
| 72 | - foreach( $ruleset as $rule ) { |
|
| 73 | - $params = ( is_array( $rule ) ) ? $rule['params'] : null; |
|
| 74 | - $rule_name = ( is_string( $rule ) ) ? $rule : $rule['rule']; |
|
| 75 | - $validator = $factory->create( $rule_name, $field, $params, $optional ); |
|
| 76 | - $form->validate( $validator, $field ); |
|
| 72 | + foreach ($ruleset as $rule) { |
|
| 73 | + $params = (is_array($rule)) ? $rule['params'] : null; |
|
| 74 | + $rule_name = (is_string($rule)) ? $rule : $rule['rule']; |
|
| 75 | + $validator = $factory->create($rule_name, $field, $params, $optional); |
|
| 76 | + $form->validate($validator, $field); |
|
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | return $form->is_valid(); |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -31,13 +31,13 @@ discard block |
||
| 31 | 31 | * @param bool (optional) $optional |
| 32 | 32 | * @return self |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 35 | - $format = ( isset( $this->params[0] ) ) ? $this->params[0] : null; |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | + $format = (isset($this->params[0])) ? $this->params[0] : null; |
|
| 36 | 36 | |
| 37 | 37 | $v = $this->validator; |
| 38 | - $v = ( $optional ) |
|
| 39 | - ? $v->optional( $v->create()->date( $format ) ) |
|
| 40 | - : $v->date( $format ); |
|
| 38 | + $v = ($optional) |
|
| 39 | + ? $v->optional($v->create()->date($format)) |
|
| 40 | + : $v->date($format); |
|
| 41 | 41 | return $this; |
| 42 | 42 | } |
| 43 | 43 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * @param bool (optional) $optional |
| 33 | 33 | * @return self |
| 34 | 34 | */ |
| 35 | - public function set_policy( $optional = false ) { |
|
| 35 | + public function set_policy($optional = false) { |
|
| 36 | 36 | $this->validator->notEmpty(); |
| 37 | 37 | return $this; |
| 38 | 38 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | * @param bool (optional) $optional |
| 32 | 32 | * @return self |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | 35 | $v = $this->validator; |
| 36 | - $v = ( $optional ) |
|
| 37 | - ? $v->optional( $v->create()->boolVal() ) |
|
| 36 | + $v = ($optional) |
|
| 37 | + ? $v->optional($v->create()->boolVal()) |
|
| 38 | 38 | : $v->boolVal(); |
| 39 | 39 | return $this; |
| 40 | 40 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | * @param bool (optional) $optional |
| 32 | 32 | * @return self |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | 35 | $v = $this->validator; |
| 36 | - $v = ( $optional ) |
|
| 37 | - ? $v->optional( $v->create()->alpha('-_') ) |
|
| 36 | + $v = ($optional) |
|
| 37 | + ? $v->optional($v->create()->alpha('-_')) |
|
| 38 | 38 | : $v->alpha('-_'); |
| 39 | 39 | return $this; |
| 40 | 40 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | * @param bool (optional) $optional |
| 32 | 32 | * @return self |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | 35 | $v = $this->validator; |
| 36 | - $v = ( $optional ) |
|
| 37 | - ? $v->optional( $v->create()->callback( $this->params[0] ) ) |
|
| 38 | - : $v->callback( $this->params[0] ); |
|
| 36 | + $v = ($optional) |
|
| 37 | + ? $v->optional($v->create()->callback($this->params[0])) |
|
| 38 | + : $v->callback($this->params[0]); |
|
| 39 | 39 | return $this; |
| 40 | 40 | } |
| 41 | 41 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -31,14 +31,14 @@ discard block |
||
| 31 | 31 | * @param bool (optional) $optional |
| 32 | 32 | * @return self |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | 35 | $start = $this->params[0]; |
| 36 | 36 | $end = $this->params[1]; |
| 37 | 37 | |
| 38 | 38 | $v = $this->validator; |
| 39 | - $v = ( $optional ) |
|
| 40 | - ? $v->optional( $v->create()->between( $start, $end ) ) |
|
| 41 | - : $v->between( $start, $end ); |
|
| 39 | + $v = ($optional) |
|
| 40 | + ? $v->optional($v->create()->between($start, $end)) |
|
| 41 | + : $v->between($start, $end); |
|
| 42 | 42 | return $this; |
| 43 | 43 | } |
| 44 | 44 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace WFV\Validators; |
| 3 | -defined( 'ABSPATH' ) || die(); |
|
| 3 | +defined('ABSPATH') || die(); |
|
| 4 | 4 | |
| 5 | 5 | use WFV\Validators\AbstractValidator; |
| 6 | 6 | |
@@ -31,14 +31,14 @@ discard block |
||
| 31 | 31 | * @since 0.11.0 |
| 32 | 32 | * |
| 33 | 33 | */ |
| 34 | - public function set_policy( $optional = false ) { |
|
| 34 | + public function set_policy($optional = false) { |
|
| 35 | 35 | $other_field = $this->params[0]; |
| 36 | 36 | $v = $this->validator; |
| 37 | 37 | |
| 38 | 38 | $v->when( |
| 39 | - $v->create()->key( $other_field, $v->create()->notEmpty() ), |
|
| 40 | - $v->create()->key( $this->field, $v->create()->notEmpty() ), |
|
| 41 | - $v->create()->key( $this->field, $v->create()->alwaysValid() ) |
|
| 39 | + $v->create()->key($other_field, $v->create()->notEmpty()), |
|
| 40 | + $v->create()->key($this->field, $v->create()->notEmpty()), |
|
| 41 | + $v->create()->key($this->field, $v->create()->alwaysValid()) |
|
| 42 | 42 | ); |
| 43 | 43 | return $this; |
| 44 | 44 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @param string|array $input |
| 52 | 52 | * @return bool |
| 53 | 53 | */ |
| 54 | - public function validate( $input ){ |
|
| 55 | - return $this->validator->validate( $_POST ); |
|
| 54 | + public function validate($input) { |
|
| 55 | + return $this->validator->validate($_POST); |
|
| 56 | 56 | } |
| 57 | 57 | } |