/
builder.php
295 lines (250 loc) · 10.2 KB
/
builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
namespace Podlove\Form\Input;
class Builder {
/**
* Model record.
* @var object
*/
public $object;
/**
* Form field name prefix.
* @var string
*/
public $context;
public $object_key;
public $arguments;
public function __construct( $object, $context ) {
$this->object = $object;
$this->context = $context;
}
public function get_field_name() {
return ( $this->context ) ? "{$this->context}[{$this->object_key}]" : $this->object_key;
}
public function get_field_id() {
if ( $this->context ) {
$id = "{$this->context}_{$this->object_key}";
} else {
$id = $this->object_key;
}
$id = str_replace( array( '[', ']' ), '_', $id );
$id = str_replace( '__', '_', $id );
return $id;
}
public function get_extra_html_attributes() {
if ( ! isset( $this->arguments['html'] ) || ! is_array( $this->arguments['html'] ) )
return '';
$compiled_html = '';
foreach ( $this->arguments['html'] as $key => $value )
$compiled_html .= "$key=\"$value\" ";
return $compiled_html;
}
/**
* Generate values required to build input fields.
*
* @param string $object_key name of the model attribute
* @param array $arguments input field options
* @return void
*/
private function build_input_values( $object_key, $arguments ) {
$this->object_key = $object_key;
$this->arguments = $arguments;
$this->field_name = $this->get_field_name();
// multiselect takes care of its values
if ( ! isset( $arguments['ignore_values'] ) || $arguments['ignore_values'] === false ) {
$this->field_value = $this->object->{$object_key};
if ( $this->field_value === NULL && isset( $arguments['default'] ) && $arguments['default'] ) {
$this->field_value = $arguments['default'];
}
}
$this->field_id = $this->get_field_id();
$this->html_attributes = $this->get_extra_html_attributes();
}
public function string( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<div>
<input type="text" name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" value="<?php echo esc_attr( $this->field_value ); ?>" <?php echo $this->html_attributes; ?>><span class="podlove-input-status" data-podlove-input-status-for="<?php echo $this->field_id; ?>"></span>
</div>
<?php
}
public function color( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<div>
<input type="color" name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" value="<?php echo esc_attr( $this->field_value ); ?>" <?php echo $this->html_attributes; ?>><span class="podlove-input-status" data-podlove-input-status-for="<?php echo $this->field_id; ?>"></span>
</div>
<?php
}
public function hidden( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<div>
<input type="hidden" name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" value="<?php echo esc_attr( $this->field_value ); ?>" <?php echo $this->html_attributes; ?>>
</div>
<?php
}
public function text( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<div>
<textarea name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" <?php echo $this->html_attributes; ?>><?php echo $this->field_value; ?></textarea><span class="podlove-input-status" data-podlove-input-status-for="<?php echo $this->field_id; ?>"></span>
</div>
<?php
}
public function checkbox( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<input type="checkbox" name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" <?php if ( in_array( $this->field_value, array( true, 1, 'on' ) ) ): ?>checked="checked"<?php endif; ?> <?php echo $this->html_attributes; ?>>
<input type="hidden" name="checkboxes[]" value="<?php echo esc_attr( $this->object_key ) ?>">
<?php
}
public function select( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<select name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" <?php echo $this->html_attributes; ?>>
<?php if ( ! isset( $this->arguments['please_choose'] ) || $this->arguments['please_choose'] ): ?>
<option value=""><?php
if (isset($this->arguments['please_choose_text'])) {
echo $this->arguments['please_choose_text'];
} else {
echo __( 'Please choose ...', 'podlove-podcasting-plugin-for-wordpress' );
}
?></option>
<?php endif; ?>
<?php foreach ( $this->arguments['options'] as $key => $value ): ?>
<?php
if ( is_array( $value ) ) {
$attributes = $value['attributes'];
$value = $value['value'];
} else {
$attributes = '';
}
?>
<option value="<?php echo esc_attr( $key ); ?>" <?php echo $attributes ?> <?php if ( $key == $this->field_value ): ?> selected="selected"<?php endif; ?>><?php echo $value; ?></option>
<?php endforeach; ?>
</select>
<?php
}
public function multiselect( $object_key, $arguments ) {
$arguments['ignore_values'] = true;
$this->build_input_values( $object_key, $arguments );
foreach ( $this->arguments['options'] as $key => $value ) {
if ( isset( $this->arguments['multi_values'][ $key ] ) ) {
$checked = $this->arguments['multi_values'][ $key ];
} else {
$checked = $this->arguments['default'];
}
$name = $this->field_name . '[' . $key . ']';
// generate an id without braces by turning braces into underscores
$id = $this->field_id . '_' . $key;
$id = str_replace( array( '[', ']' ), '_', $id );
$id = str_replace( '__', '_', $id );
if ( isset( $this->arguments['multiselect_callback'] ) ) {
$callback = call_user_func( $this->arguments['multiselect_callback'], $key );
} else {
$callback = '';
}
$html = function() use ( $id, $name, $checked, $callback, $value ) {
?>
<div>
<label for="<?php echo $id; ?>">
<input type="checkbox" name="<?php echo $name; ?>" id="<?php echo $id; ?>" <?php if ( $checked ): ?>checked="checked"<?php endif; ?> <?php echo $callback; ?>> <?php echo $value; ?>
</label>
</div>
<?php
};
if ( isset( $this->arguments['around_each'] ) && is_callable( $this->arguments['around_each'] ) ) {
$this->arguments['around_each']( $html );
} else {
call_user_func( $html );
}
}
}
public function radio( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
?>
<?php foreach ( $this->arguments['options'] as $key => $value ): ?>
<input type="radio" id="<?php echo $this->field_id . '_' . $key; ?>" name="<?php echo $this->field_name; ?>" value="<?php echo esc_attr( $key ); ?>"<?php if ( $key == $this->field_value ): ?> checked="checked"<?php endif; ?>>
<label for="<?php echo $this->field_id . '_' . $key; ?>"><?php echo $value; ?></label>
<?php endforeach; ?>
<?php
}
public function image( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
// determine image dimensions
$img_html_attributes = '';
if ( isset( $arguments['image_width'] ) )
$img_html_attributes .= ' width="' . $arguments['image_width'] . '"';
if ( isset( $arguments['image_height'] ) )
$img_html_attributes .= ' height="' . $arguments['image_height'] . '"';
?>
<div>
<input type="text" name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>" value="<?php echo esc_attr( $this->field_value ); ?>" <?php echo $this->html_attributes; ?>><span class="podlove-input-status" data-podlove-input-status-for="<?php echo $this->field_id; ?>"></span>
<br>
<img src="<?php echo $this->field_value; ?>" <?php echo $img_html_attributes ?> />
</div>
<script type="text/javascript">
(function($) {
$("#<?php echo $this->field_id ?>").on( 'change', function() {
url = $(this).val();
$(this).parent().find("img").attr("src", url);
} );
})(jQuery);
</script>
<?php
}
public function upload( $object_key, $arguments ) {
$this->build_input_values( $object_key, $arguments );
wp_enqueue_media();
$defaults = [
'form_button_text' => __('Select', 'podlove-podcasting-plugin-for-wordpress'),
'media_button_text' => __('Use Image', 'podlove-podcasting-plugin-for-wordpress'),
'media_title' => __('Image', 'podlove-podcasting-plugin-for-wordpress'),
'allow_gravatar' => false
];
$arguments = wp_parse_args($arguments, $defaults);
?>
<div class="podlove-media-upload-wrap">
<span>
<input type="text" <?php echo $this->html_attributes; ?> value="<?php echo esc_attr( $this->field_value ); ?>" name="<?php echo $this->field_name; ?>" id="<?php echo $this->field_id; ?>">
<a href="#" class="podlove-media-upload button"
data-target="<?php echo $this->field_id; ?>"
data-title="<?php echo $arguments['media_title'] ?>"
data-type="image"
data-button="<?php echo $arguments['media_button_text'] ?>"
data-class="media-frame"
data-frame="select"
data-size="full"
data-state="podlove_select_single_image"
data-preview=".podlove_preview_pic"
data-allow-gravatar="<?php echo $arguments['allow_gravatar'] ?>"
data-fetch="url"><?php echo $arguments['form_button_text'] ?></a>
</span>
<?php if (!isset($arguments['description']) || !$arguments['description']): ?>
<p>
<span class="description"><?php echo __('Enter URL or select image from media library.', 'podlove-podcasting-plugin-for-wordpress') ?></span>
</p>
<?php endif ?>
<div class="podlove_preview_pic"></div>
</div>
<?php
}
public function callback( $object_key, $arguments ) {
call_user_func( $arguments['callback'] );
}
/**
* Build nested form.
*
* @param object $object object that shall be modified via the form
* @param array $args list of options, all optional
* - hidden dictionary with hidden values
* @param function $callback inner form
* @return void
*/
function fields_for( $object, $args, $callback ) {
// determine context
$context = isset( $args['context'] ) ? $this->context . '[' . $args['context'] . ']' . "[{$object->id}]" : $this->context;
// build input elements
call_user_func( $callback, new \Podlove\Form\Input\Builder( $object, $context ) );
}
}