Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

WL_Metabox_Field_date   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
B html_input() 0 24 1
B html_wrapper_close() 0 34 1
1
<?php
2
/**
3
 * Metaxboxes: Date Field.
4
 *
5
 * This file defines the WL_Metabox_Field_date class which displays a date field
6
 * in WordPress' entity posts pages.
7
 *
8
 * @since   3.2.0
9
 * @package Wordlift
10
 */
11
12
/**
13
 * The WL_Metabox_Field_date class extends {@link WL_Metabox_Field} and provides
14
 * support for date fields.
15
 *
16
 * @since   3.2.0
17
 * @package Wordlift
18
 */
19
class WL_Metabox_Field_date extends WL_Metabox_Field {
20
21
	/**
22
	 * Attribute to distinguish between date formats, inferred from the schema property export type
23
	 *
24
	 * @since  3.2.0
25
	 * @access protected
26
	 * @var string $date_format The date format.
27
	 */
28
	protected $date_format;
29
30
	/**
31
	 * Boolean flag to decide if the calendar should include time or not
32
	 *
33
	 * @since  3.2.0
34
	 * @access protected
35
	 * @var boolean $timepicker A boolean flag.
36
	 */
37
	protected $timepicker;
38
39
	/**
40
	 * Whether the calendar should be displayed or not.
41
	 *
42
	 * @since  3.14.0
43
	 * @access protected
44
	 * @var boolean $no_calendar Whether the calendar should be displayed or not.
45
	 */
46
	protected $no_calendar;
47
48
	/**
49
	 * {@inheritdoc}
50
	 */
51
	public function __construct( $args ) {
52
		parent::__construct( $args );
53
54
		$this->no_calendar = false;
55
56
		// Distinguish between date and datetime
57
		if ( isset( $this->raw_custom_field['export_type'] ) && 'xsd:dateTime' === $this->raw_custom_field['export_type'] ) {
58
			$this->date_format = 'Y/m/d H:i';
59
			$this->timepicker  = true;
60
		} else {
61
			$this->date_format = 'Y/m/d';
62
			$this->timepicker  = false;
63
		}
64
65
	}
66
67
	/**
68
	 * @param mixed $date
69
	 *
70
	 * @return string
71
	 */
72
	public function html_input( $date ) {
73
74
		$this->log->debug("Creating date input with date value $date...");
75
76
		ob_start();
77
		?>
78
			<div class="wl-input-wrapper">
79
				<input
80
					type="text"
81
					class="<?php echo esc_attr( $this->meta_name ); ?>"
82
					name="wl_metaboxes[<?php echo $this->meta_name ?>][]"
83
					value="<?php echo $date ?>"
84
					style="width:88%"
85
				/>
86
87
				<button class="button wl-remove-input wl-button" type="button">
88
					<?php esc_html_e( 'Remove', 'wordlift' ); ?>
89
				</button>
90
			</div>
91
		<?php
92
		$html = ob_get_clean();
93
94
		return $html;
95
	}
96
97
	public function html_wrapper_close() {
98
99
		// Should the widget include time picker?
100
		$timepicker  = json_encode( $this->timepicker );
101
		$date_format = json_encode( $this->date_format );
102
		$no_calendar = json_encode( $this->no_calendar );
103
104
		// Set up the datetimepicker.
105
		//
106
		// See https://github.com/trentrichardson/jQuery-Timepicker-Addon
107
		// See in http://trentrichardson.com/examples/timepicker.
108
		@ob_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
109
		?>
110
			<script type='text/javascript'>
111
				( function( $ ) {
112
113
					$( function() {
114
115
						$( '.<?php echo $this->meta_name; ?>[type=text]' ).flatpickr( {
116
							enableTime: <?php echo $timepicker; ?>,
117
							noCalendar: <?php echo $no_calendar; ?>,
118
							time_24hr: true,
119
							dateFormat: <?php echo $date_format; ?>
120
						 } );
121
					} );
122
				} ) ( jQuery );
123
			</script>
124
		<?php
125
		$html = ob_get_clean();
126
127
		$html .= parent::html_wrapper_close();
128
129
		return $html;
130
	}
131
132
}
133