Completed
Push — milestone/2_0/container-condit... ( 446b23...9b81fb )
by
unknown
04:50
created

Time_Field   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 144
rs 10
wmc 12
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A admin_enqueue_scripts() 0 7 1
A get_interval_step() 0 3 1
A set_restraints() 0 5 1
A get_restraints() 0 3 1
A to_json() 0 13 1
A set_interval_step() 0 12 2
A set_picker_options() 0 4 1
A get_picker_options() 0 3 1
A set_time_format() 0 4 1
A get_time_format() 0 3 2
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Time picker field class.
7
 */
8
class Time_Field extends Field {
9
	
10
	/**
11
	 * Picker type.
12
	 *
13
	 * @var string
14
	 */
15
	protected $picker_type = 'timepicker';
16
17
	/**
18
	 * Picker options.
19
	 *
20
	 * @var array
21
	 */
22
	public $picker_options = array(
23
		'dateFormat' => 'yy-mm-dd',
24
		'timeFormat' => 'hh:mm tt',
25
		'altFormat' => 'yy-mm-dd',
26
		'altTimeFormat' => 'HH:mm:ss',
27
		'altFieldTimeOnly' => false,
28
	);
29
30
	/**
31
	 * Interval step for hours, minutes and seconds.
32
	 *
33
	 * @var array
34
	 */
35
	public $interval_step = array();
36
37
	/**
38
	 * Restraints for hours, minutes, seconds and dates.
39
	 *
40
	 * @var array
41
	 */
42
	public $restraints = array();
43
44
	/**
45
	 * The storage format in variant that can be used by JavaScript.
46
	 *
47
	 * @var string
48
	 */
49
	protected $storage_format = 'HH:mm:ss';
50
51
	/**
52
	 * You can use this method to modify the field properties that are added to the JSON object.
53
	 *
54
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
55
	 * @return array
56
	 */
57
	public function to_json( $load ) {
58
		$field_data = parent::to_json( $load );
59
60
		$field_data = array_merge( $field_data, array(
61
			'storage_format' => $this->storage_format,
62
			'interval_step' => $this->get_interval_step(),
63
			'restraints' => $this->get_restraints(),
64
			'picker_type' => $this->picker_type,
65
			'picker_options' => $this->get_picker_options(),
66
		) );
67
68
		return $field_data;
69
	}
70
71
	/**
72
	 * This method is called in the admin_enqueue_scripts action. It is called once per field type.
73
	 * Enqueues field scripts and styles.
74
	 */
75
	public static function admin_enqueue_scripts() {
76
		# Enqueue CSS
77
		wp_enqueue_style( 'jquery-ui', '//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.min.css', array(), \Carbon_Fields\VERSION );
78
79
		# Enqueue JS
80
		wp_enqueue_script( 'carbon-jquery-timepicker', \Carbon_Fields\URL . '/assets/js/lib/jquery-ui-timepicker.js', array( 'jquery-ui-datepicker', 'jquery-ui-slider' ), \Carbon_Fields\VERSION );
81
	}
82
83
	/**
84
	 * Sets the time format.
85
	 */
86
	public function set_time_format( $time_format ) {
87
		$this->picker_options['timeFormat'] = $time_format;
88
		return $this;
89
	}
90
91
	/**
92
	 * Returns the time format.
93
	 */
94
	public function get_time_format() {
95
		return isset( $this->picker_options['timeFormat'] ) ? $this->picker_options['timeFormat'] : null;
96
	}
97
98
	/**
99
	 * Sets the interval step.
100
	 */
101
	public function set_interval_step( $interval_steps ) {
102
		$output = array();
103
104
		foreach ( $interval_steps as $step_name => $step_value ) {
105
			$name = 'step' . ucwords( $step_name );
106
			$output[ $name ] = (int) $step_value;
107
		}
108
109
		$this->interval_step = $output;
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Returns the interval step.
116
	 */
117
	public function get_interval_step() {
118
		return $this->interval_step;
119
	}
120
121
	/**
122
	 * Sets the restraints.
123
	 */
124
	public function set_restraints( $restraints ) {
125
		$this->restraints = array_map( 'intval', $restraints );
126
127
		return $this;
128
	}
129
130
	/**
131
	 * Returns the restraints.
132
	 */
133
	public function get_restraints() {
134
		return $this->restraints;
135
	}
136
137
	/**
138
	 * Sets other picker options.
139
	 */
140
	public function set_picker_options( $options ) {
141
		$this->picker_options = array_replace( $this->picker_options, $options );
142
		return $this;
143
	}
144
145
	/**
146
	 * Returns the picker options.
147
	 */
148
	public function get_picker_options() {
149
		return $this->picker_options;
150
	}
151
}
152