|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace WFV; |
|
3
|
|
|
defined( 'ABSPATH' ) || die(); |
|
4
|
|
|
|
|
5
|
|
|
use WFV\FormComposite; |
|
6
|
|
|
use WFV\Contract\FormInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* |
|
11
|
|
|
* @since 0.11.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
class Client extends FormComposite implements FormInterface { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* |
|
18
|
|
|
* @since 0.11.0 |
|
19
|
|
|
* @access protected |
|
20
|
|
|
* @var FormComposite |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $form; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* |
|
27
|
|
|
* @since 0.11.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @param FormComposite $form |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( FormComposite $form ) { |
|
32
|
|
|
$this->form = $form; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Convenience method to repopulate checkbox input |
|
37
|
|
|
* |
|
38
|
|
|
* @since 0.10.0 |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $field Field name. |
|
41
|
|
|
* @param string $value Value to compare against. |
|
42
|
|
|
* @return string|null |
|
43
|
|
|
*/ |
|
44
|
|
|
public function checked_if( $field = null, $value = null ) { |
|
45
|
|
|
echo $this->form->string_or_null( 'checked', $field, $value ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Echo the encoded value of given field from a callback |
|
50
|
|
|
* Default callback is esc_html() |
|
51
|
|
|
* Also returns the encoded string for assignment |
|
52
|
|
|
* |
|
53
|
|
|
* @since 0.10.1 |
|
54
|
|
|
* |
|
55
|
|
|
* @param string (optional) $field |
|
56
|
|
|
* @param callable (optional) $callback |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function display( $field = null, callable $callback = null ) { |
|
60
|
|
|
echo $input = $this->form->utilize('input')->escape( $field ); |
|
61
|
|
|
return $input; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Use error collection |
|
66
|
|
|
* |
|
67
|
|
|
* |
|
68
|
|
|
* @since 0.10.0 |
|
69
|
|
|
* |
|
70
|
|
|
* @return WFV\Collection\ErrorCollection |
|
71
|
|
|
*/ |
|
72
|
|
|
public function errors() { |
|
73
|
|
|
return $this->form->utilize('errors'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Use input collection |
|
78
|
|
|
* |
|
79
|
|
|
* @since 0.10.0 |
|
80
|
|
|
* |
|
81
|
|
|
* @return WFV\Collection\InputCollection |
|
82
|
|
|
*/ |
|
83
|
|
|
public function input() { |
|
84
|
|
|
return $this->form->utilize('input'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Use rules collection |
|
89
|
|
|
* |
|
90
|
|
|
* @since 0.11.0 |
|
91
|
|
|
* |
|
92
|
|
|
* @return WFV\Collection\RuleCollection |
|
93
|
|
|
*/ |
|
94
|
|
|
public function rules() { |
|
95
|
|
|
return $this->form->utilize('rules'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Convenience method to repopulate select input |
|
100
|
|
|
* |
|
101
|
|
|
* @since 0.10.0 |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $field Field name. |
|
104
|
|
|
* @param string $value Value to compare against. |
|
105
|
|
|
* @return string|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function selected_if( $field = null, $value = null ) { |
|
108
|
|
|
return $this->form->string_or_null( 'selected', $field, $value ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Convienience method to print the hidden fields |
|
113
|
|
|
* for token and action |
|
114
|
|
|
* |
|
115
|
|
|
* @since 0.10.0 |
|
116
|
|
|
* |
|
117
|
|
|
*/ |
|
118
|
|
|
public function token_fields() { |
|
119
|
|
|
// TODO - Move markup into something - perhaps a renderable interface? |
|
120
|
|
|
$token_name = $this->form->alias . '_token'; |
|
121
|
|
|
echo $nonce_field = wp_nonce_field( $this->form->alias, $token_name, false, false ); |
|
122
|
|
|
echo $action_field = '<input type="hidden" name="action" value="'. $this->form->alias .'">'; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* |
|
127
|
|
|
* |
|
128
|
|
|
* @since 0.11.0 |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
public function is_valid( $factory ) { |
|
132
|
|
|
return $this->form->validate( $factory ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.