Completed
Push — master ( 6b2386...2bf093 )
by Ahmad
04:44
created

TitanFrameworkOptionDateControl::render_content()   D

Complexity

Conditions 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 4
cc 8
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Date Option Class
5
 *
6
 * @author Ardalan Naghshineh (www.ardalan.me)
7
 * @package Titan Framework Core
8
 **/
9
10
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly
11
}
12
/**
13
 * Date Option Class
14
 *
15
 * @since	1.0
16
 **/
17
class TitanFrameworkOptionDate extends TitanFrameworkOption {
18
19
	// Default settings specific to this option
20
	public $defaultSecondarySettings = array(
21
		'date' => true,
22
		'time' => false,
23
	);
24
25
	private static $date_epoch;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @since	1.4
31
	 */
32
	function __construct( $settings, $owner ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
		parent::__construct( $settings, $owner );
34
35
		tf_add_action_once( 'admin_enqueue_scripts', array( $this, 'enqueueDatepicker' ) );
36
		tf_add_action_once( 'customize_controls_enqueue_scripts', array( $this, 'enqueueDatepicker' ) );
37
		add_action( 'admin_head', array( __CLASS__, 'createCalendarScript' ) );
38
39
		if ( empty( self::$date_epoch ) ) {
40
			self::$date_epoch = date( 'Y-m-d', 0 );
41
		}
42
	}
43
44
45
	/**
46
	 * Cleans up the serialized value before saving
47
	 *
48
	 * @param	string $value The serialized value
49
	 * @return	string The cleaned value
50
	 * @since	1.4
51
	 */
52
	public function cleanValueForSaving( $value ) {
53
		if ( $value == '' ) {
54
			return 0;
55
		}
56
		if ( ! $this->settings['date'] && $this->settings['time'] ) {
57
			$value = self::$date_epoch . ' ' . $value;
58
		}
59
		return strtotime( $value );
60
	}
61
62
63
	/**
64
	 * Cleans the value for getOption
65
	 *
66
	 * @param	string $value The raw value of the option
67
	 * @return	mixes The cleaned value
68
	 * @since	1.4
69
	 */
70
	public function cleanValueForGetting( $value ) {
71
		if ( $value == 0 ) {
72
			return '';
73
		}
74
		return $value;
75
	}
76
77
78
	/**
79
	 * Enqueues the jQuery UI scripts
80
	 *
81
	 * @return	void
82
	 * @since	1.4
83
	 */
84
	public function enqueueDatepicker() {
85
		wp_enqueue_script( 'jquery-ui-core' );
86
		wp_enqueue_script( 'jquery-ui-slider' );
87
		wp_enqueue_script( 'jquery-ui-datepicker' );
88
		wp_enqueue_script( 'tf-jquery-ui-timepicker-addon', TitanFramework::getURL( '../js/min/jquery-ui-timepicker-addon-min.js', __FILE__ ), array( 'jquery-ui-datepicker', 'jquery-ui-slider' ) );
89
	}
90
91
92
	/**
93
	 * Prints out the script the initializes the jQuery Datepicker
94
	 *
95
	 * @return	void
96
	 * @since	1.4
97
	 */
98
	public static function createCalendarScript() {
99
		?>
100
		<script>
101
		jQuery(document).ready(function($) {
102
			"use strict";
103
104
			var datepickerSettings = {
105
					dateFormat: 'yy-mm-dd',
106
107
					beforeShow: function(input, inst) {
108
						$('#ui-datepicker-div').addClass('tf-date-datepicker');
109
110
						// Fix the button styles
111
						setTimeout( function() {
112
							jQuery('#ui-datepicker-div')
113
							.find('[type=button]').addClass('button').end()
114
							.find('.ui-datepicker-close[type=button]').addClass('button-primary');
115
						}, 0);
116
					},
117
118
					// Fix the button styles
119
					onChangeMonthYear: function() {
120
						setTimeout( function() {
121
							jQuery('#ui-datepicker-div')
122
							.find('[type=button]').addClass('button').end()
123
							.find('.ui-datepicker-close[type=button]').addClass('button-primary');
124
						}, 0);
125
					}
126
				};
127
			$('.tf-date input[type=text]').each(function() {
128
				var $this = $(this);
129
				if ( $this.hasClass('date') && ! $this.hasClass('time') ) {
130
					$this.datepicker( datepickerSettings );
131
				} else if ( ! $this.hasClass('date') && $this.hasClass('time') ) {
132
					$this.timepicker( datepickerSettings );
133
				} else {
134
					$this.datetimepicker( datepickerSettings );
135
				}
136
			});
137
		});
138
		</script>
139
		<?php
140
	}
141
142
143
	/**
144
	 * Displays the option for admin pages and meta boxes
145
	 *
146
	 * @return	void
147
	 * @since	1.0
148
	 */
149
	public function display() {
150
		$this->echoOptionHeader();
151
		$dateFormat = 'Y-m-d H:i';
152
		$placeholder = 'YYYY-MM-DD HH:MM';
153
		if ( $this->settings['date'] && ! $this->settings['time'] ) {
154
			$dateFormat = 'Y-m-d';
155
			$placeholder = 'YYYY-MM-DD';
156
		} else if ( ! $this->settings['date'] && $this->settings['time'] ) {
157
			$dateFormat = 'H:i';
158
			$placeholder = 'HH:MM';
159
		}
160
161
		printf('<input class="input-date%s%s" name="%s" placeholder="%s" id="%s" type="text" value="%s" /> <p class="description">%s</p>',
162
			( $this->settings['date'] ? ' date' : '' ),
163
			( $this->settings['time'] ? ' time' : '' ),
164
			$this->getID(),
165
			$placeholder,
166
			$this->getID(),
167
			esc_attr( ($this->getValue() > 0) ? date( $dateFormat, $this->getValue() ) : '' ),
168
			$this->settings['desc']
169
		);
170
		$this->echoOptionFooter( false );
171
	}
172
173
174
	/**
175
	 * Registers the theme customizer control, for displaying the option
176
	 *
177
	 * @param	WP_Customize                    $wp_enqueue_script The customize object
0 ignored issues
show
Bug introduced by
There is no parameter named $wp_enqueue_script. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
	 * @param	TitanFrameworkCustomizerSection $section The section where this option will be placed
179
	 * @param	int                             $priority The order of this control in the section
180
	 * @return	void
181
	 * @since	1.7
182
	 */
183
	public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
184
		$wp_customize->add_control( new TitanFrameworkOptionDateControl( $wp_customize, $this->getID(), array(
185
			'label' => $this->settings['name'],
186
			'section' => $section->settings['id'],
187
			'settings' => $this->getID(),
188
			'description' => $this->settings['desc'],
189
			'priority' => $priority,
190
			'date' => $this->settings['date'],
191
			'time' => $this->settings['time'],
192
		) ) );
193
	}
194
}
195
196
197
/*
198
 * We create a new control for the theme customizer
199
 */
200
add_action( 'customize_register', 'registerTitanFrameworkOptionDateControl', 1 );
201
202
203
/**
204
 * Creates the option for the theme customizer
205
 *
206
 * @return	void
207
 * @since	1.3
208
 */
209
function registerTitanFrameworkOptionDateControl() {
210
	class TitanFrameworkOptionDateControl extends WP_Customize_Control {
211
		public $description;
212
		public $date;
213
		public $time;
214
215
		public function render_content() {
216
217
			TitanFrameworkOptionDate::createCalendarScript();
0 ignored issues
show
Unused Code introduced by
The call to the method TitanFrameworkOptionDate::createCalendarScript() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
218
219
			$dateFormat = 'Y-m-d H:i';
220
			$placeholder = 'YYYY-MM-DD HH:MM';
221
			if ( $this->date && ! $this->time ) {
222
				$dateFormat = 'Y-m-d';
223
				$placeholder = 'YYYY-MM-DD';
224
			} else if ( ! $this->date && $this->time ) {
225
				$dateFormat = 'H:i';
226
				$placeholder = 'HH:MM';
227
			}
228
229
			$class = $this->date ? ' date' : '';
230
			$class .= $this->time ? ' time' : ''
231
			?>
232
			<label class='tf-date'>
233
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
234
				<input class="input-date<?php echo $class ?>" <?php $this->link(); ?> placeholder="<?php echo $placeholder ?>" type="text" value="<?php echo $this->value() ?>" />
235
236
				<?php
237
				if ( ! empty( $this->description ) ) {
238
					echo "<p class='description'>{$this->description}</p>";
239
				}
240
				?>
241
			</label>
242
			<?php
243
		}
244
	}
245
}
246