@@ -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 | } |
@@ -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 | |
@@ -30,14 +30,14 @@ discard block |
||
| 30 | 30 | * @since 0.11.0 |
| 31 | 31 | * |
| 32 | 32 | */ |
| 33 | - public function set_policy( $optional = false ) { |
|
| 33 | + public function set_policy($optional = false) { |
|
| 34 | 34 | $other_field = $this->params[0]; |
| 35 | 35 | $other_value = $this->params[1]; |
| 36 | 36 | $v = $this->validator; |
| 37 | 37 | $v->when( |
| 38 | - $v->create()->key( $other_field, $v->create()->equals( $other_value ) ), |
|
| 39 | - $v->create()->key( $this->field, $v->create()->notEmpty() ), |
|
| 40 | - $v->create()->key( $this->field, $v->create()->alwaysValid() ) |
|
| 38 | + $v->create()->key($other_field, $v->create()->equals($other_value)), |
|
| 39 | + $v->create()->key($this->field, $v->create()->notEmpty()), |
|
| 40 | + $v->create()->key($this->field, $v->create()->alwaysValid()) |
|
| 41 | 41 | ); |
| 42 | 42 | return $this; |
| 43 | 43 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * @param string|array $input |
| 51 | 51 | * @return bool |
| 52 | 52 | */ |
| 53 | - public function validate( $input ){ |
|
| 54 | - return $this->validator->validate( $_POST ); |
|
| 53 | + public function validate($input) { |
|
| 54 | + return $this->validator->validate($_POST); |
|
| 55 | 55 | } |
| 56 | 56 | } |