|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Metaxboxes: Duration Field. |
|
4
|
|
|
* |
|
5
|
|
|
* This file defines the Wordlift_Metabox_Field_Duration class which displays a time duration field |
|
6
|
|
|
* in WordPress' entity posts pages. |
|
7
|
|
|
* |
|
8
|
|
|
* @since 3.14.0 |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The Wordlift_Metabox_Field_Duration class extends {@link WL_Metabox_Field} and provides |
|
14
|
|
|
* support for time duration fields. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 3.14.0 |
|
17
|
|
|
* @package Wordlift |
|
18
|
|
|
*/ |
|
19
|
|
|
class Wordlift_Metabox_Field_Duration extends WL_Metabox_Field { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param mixed $duration |
|
23
|
|
|
* |
|
24
|
|
|
* @return string HTML for the duration input element |
|
25
|
|
|
*/ |
|
26
|
|
|
public function html_input( $duration ) { |
|
27
|
|
|
|
|
28
|
|
|
$esc_duration = esc_attr( $duration ); |
|
29
|
|
|
$esc_meta_name = esc_attr( $this->meta_name ); |
|
30
|
|
|
$html = <<<EOF |
|
31
|
|
|
<div class="wl-input-wrapper"> |
|
32
|
|
|
<input type="text" pattern="\s*((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}\s*" id="$esc_meta_name" class="$esc_meta_name" name="wl_metaboxes[$esc_meta_name][]" value="$esc_duration" style="width:88%" /> |
|
33
|
|
|
<button class="button wl-remove-input wl-button" type="button" style="width:10 % ">Remove</button> |
|
34
|
|
|
</div> |
|
35
|
|
|
EOF; |
|
36
|
|
|
|
|
37
|
|
|
return $html; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function html_wrapper_close() { |
|
41
|
|
|
|
|
42
|
|
|
$invalid_message = esc_html__( 'Invalid format, should be time in HH:MM format or just MM', 'wordlift' ); |
|
43
|
|
|
$html = <<<EOF |
|
44
|
|
|
<script type='text/javascript'> |
|
45
|
|
|
( function( $ ) { |
|
46
|
|
|
|
|
47
|
|
|
$( function() { |
|
48
|
|
|
|
|
49
|
|
|
$( '.$this->meta_name' ).each(function() { |
|
50
|
|
|
const invalid = function (e) { |
|
51
|
|
|
if ( e.target.validity.patternMismatch ) { |
|
52
|
|
|
e.target.setCustomValidity('$invalid_message'); |
|
53
|
|
|
} |
|
54
|
|
|
}; |
|
55
|
|
|
this.oninvalid = invalid; |
|
56
|
|
|
this.onchange = function (e) { |
|
57
|
|
|
e.target.setCustomValidity(''); |
|
58
|
|
|
} |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
} ); |
|
62
|
|
|
} ) ( jQuery ); |
|
63
|
|
|
</script> |
|
64
|
|
|
EOF; |
|
65
|
|
|
|
|
66
|
|
|
$html .= parent::html_wrapper_close(); |
|
67
|
|
|
|
|
68
|
|
|
return $html; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sanitize a single value. Called from $this->sanitize_data. Default sanitization excludes empty values. |
|
73
|
|
|
* make sure the value is either empty, an integer representing valid number of minutes |
|
74
|
|
|
* or an HH:MM time format. |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $value The value being sanitized. |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed Returns sanitized value, or null. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function sanitize_data_filter( $value ) { |
|
81
|
|
|
|
|
82
|
|
|
if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
83
|
|
|
preg_match( '#((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}#', |
|
84
|
|
|
trim( $value ), |
|
85
|
|
|
$matches |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
if ( count( $matches ) > 0 ) { |
|
89
|
|
|
return $matches[0]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|