Completed
Push — develop ( bf654c...059c7c )
by David
02:47
created

Wordlift_Metabox_Field_Duration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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_date {
20
21
	/**
22
	 * {@inheritdoc}
23
	 */
24
	public function __construct( $args ) {
25
		parent::__construct( $args );
26
27
		$this->date_format = 'H:i';
28
		$this->timepicker  = true;
29
		$this->no_calendar = true;
30
31
	}
32
33
	/**
34
	 * Sanitize a single value. Called from $this->sanitize_data. Default sanitization excludes empty values.
35
	 * make sure the value is either empty, an integer representing valid number of minutes
36
	 * or an HH:MM time format.
37
	 *
38
	 * @param mixed $value The value being sanitized.
39
	 *
40
	 * @return mixed Returns sanitized value, or null.
41
	 */
42
	public function sanitize_data_filter( $value ) {
43
44
		if ( ! is_null( $value ) && '' !== $value ) {         // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ .
45
			preg_match( '#((([01]?[0-9]{1}|2[0-3]{1}):)?[0-5]{1})?[0-9]{1}#',
46
				trim( $value ),
47
				$matches
48
			);
49
50
			if ( count( $matches ) > 0 ) {
51
				return $matches[0];
52
			}
53
		}
54
55
		return null;
56
	}
57
58
}
59