Skip to content

Commit 9aeda06

Browse files
committed
Merge pull request #7 from crowdfavorite/develop
Release 2.0.5 from develop to master
2 parents 4cc56e2 + d975952 commit 9aeda06

File tree

5 files changed

+67
-25
lines changed

5 files changed

+67
-25
lines changed

CHANGELOG.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
## 2.0.4
99
- Add filter in cf_meta_js_wysiwyg_scripts() to be able to bypass "cf-post-meta" folder if installed somewhere else
10+
- jQuery 10 compatibility for checkbox/radio button comparisons
11+
- An entered value of 0 no longer gets replaced with a default value (an empty string still does)
12+
- Adds "date" input type with jQuery UI Datepicker. Validates format for consistency, stores as yyyy-mm-dd in database, allowing easy comparison.
1013

1114
## 2.0.3
1215
- Add ability for custom JS in the head of a meta box via filter

README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Adding a meta box to the wordpress page/post edit screen requires little code.
4040
'type' => 'text', // required, input type
4141
'before' => '<div class="special">', // optional, html to put before the field
4242
'after' => '</div>', // optional, html to put after the field
43+
'default_value' => 'value' // optional, an entered empty string will be replaced by this value
4344
),
4445
// textarea
4546
array(

cf-post-meta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,4 @@ function cf_meta_css() {
247247
echo $css;
248248
exit;
249249
}
250-
?>
250+
?>

classes/cf-input.class.php

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function add() {
5858
}
5959

6060
/**
61-
* Creates a block repeater item
61+
* Creates a block repeater items
6262
* Similar to a set, but each block can expand to hold many items
6363
*/
6464
class cf_input_block {
@@ -217,8 +217,10 @@ function save_group($post) {
217217
foreach ($post[$this->config['prefix'].'blocks'][$this->config['name']] as $value) {
218218
// keep items where all values are empty from being saved
219219
$control = '';
220-
foreach($value as $item) { $control .= $item; }
221-
if(strlen(trim($control)) > 0) {
220+
foreach ($value as $item) {
221+
$control .= $item;
222+
}
223+
if (strlen(trim($control)) > 0) {
222224
$save_array[] = $value;
223225
}
224226
}
@@ -274,9 +276,9 @@ function cf_input($conf) {
274276
* verify presence of data and throw any necessary errors
275277
*/
276278
function save() {
277-
if(isset($_POST[$this->get_name()])) {
279+
if (isset($_POST[$this->get_name()])) {
278280
// check required
279-
if($_POST[$this->get_name()] == '' && $this->required) {
281+
if ($_POST[$this->get_name()] === '' && $this->required) {
280282
$this->error = 'required';
281283
return false;
282284
}
@@ -285,7 +287,8 @@ function save() {
285287

286288
// write to db
287289
return $this->save_data($_POST[$this->get_name()]);
288-
} else {
290+
}
291+
else {
289292
delete_post_meta($this->post_id,$this->get_name());
290293
}
291294
return false;
@@ -297,7 +300,7 @@ function save() {
297300
function save_data($value) {
298301
$value = apply_filters('cfinput_save_data', $value, $this->config['name'], $this->config);
299302
// delete meta entry on empty value
300-
if($value == '') {
303+
if ($value === '') {
301304
return delete_post_meta($this->post_id,$this->config['name']);
302305
}
303306
else {
@@ -339,7 +342,7 @@ function get_input( $value = false ) {
339342
$value = ($value) ? $value : $this->get_value();
340343
$class = isset($this->config['label']) ? null : 'class="full" ';
341344

342-
return '<input type="'.$this->config['type'].'" name="'.$this->get_id().'" id="'.$this->get_id().'" value="'.htmlspecialchars($value).'" '.$this->attributes().$class.'/>';
345+
return '<input type="'.$this->config['type'].'" name="'.$this->get_id().'" id="'.$this->get_id().'" value="'.esc_attr($value).'" '.$this->attributes().$class.'/>';
343346
}
344347

345348
function attributes() {
@@ -407,7 +410,7 @@ function get_value() {
407410
}
408411
}
409412

410-
if(!empty($value)) {
413+
if ($value !== '') {
411414
$this->value = $value;
412415
}
413416
else if (!empty($this->config['default_value']) && !in_array($this->config['type'], array('checkbox'))) {
@@ -601,9 +604,45 @@ function get_input( $value = false ) {
601604
}
602605

603606
class cf_input_file extends cf_input {
604-
function cf_input_text($conf) {
607+
function cf_input_file($conf) {
605608
return cf_input::cf_input($conf);
606609
}
607610
}
611+
612+
class cf_input_date extends cf_input {
613+
function cf_input_date($conf) {
614+
// We want to enqueue the jQuery datepicker for this.
615+
wp_enqueue_script('jquery-ui-datepicker');
616+
// May need to review how we want the jquery ui styles enqueued
617+
wp_enqueue_style('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
618+
return cf_input::cf_input($conf);
619+
}
620+
621+
function get_input() {
622+
$output = parent::get_input();
623+
$output .= '
624+
<script type="text/javascript">
625+
jQuery(document).ready(function($) {
626+
$("#'.$this->get_id().'").datepicker({
627+
"dateFormat": "yy-mm-dd", // This should not be changed, it is set for compatibility with §5.6 of RFC3339 regarding date inputs
628+
}).prop("placeholder", "yyyy-mm-dd");
629+
});
630+
</script>
631+
';
632+
return $output;
633+
}
634+
635+
// This clears the meta then resaves if it was passed in. This will also normalize the date format in case, by some method, it was not submitted in yyyy-mm-dd format.
636+
function save() {
637+
if (empty($_POST[$this->get_name()])) {
638+
return delete_post_meta($this->config['post_id'], $this->get_name());
639+
}
640+
else {
641+
// Because JS may not have been enabled on the user's system, let's do our best to ensure the submitted date is in the proper format.
642+
$meta_value = date('Y-m-d', strtotime($_POST[$this->get_name()]));
643+
return update_post_meta($this->config['post_id'], $this->config['name'], $meta_value);
644+
}
645+
}
646+
}
608647

609648
?>

classes/cf-meta-js.class.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ function generic_func($identifier) {
394394
* @return string
395395
*/
396396
function attribute($identifier, $attribute) {
397-
return 'jQuery("'.$identifier.'").attr("'.$attribute.'")';
397+
return 'jQuery("'.$identifier.'").prop("'.$attribute.'")';
398398
}
399399

400400
/**
@@ -551,20 +551,19 @@ function add_wysiwyg() {
551551
function cf_meta_ckeditor_toolbar_config() {
552552
if(!empty($_GET['cf_meta_action']) && $_GET['cf_meta_action'] == 'ckeditor_toolbar_config') {
553553
header('content-type: text/javascript');
554-
echo 'CKEDITOR.editorConfig = function( config )
555-
{
556-
config.toolbar = "CFMetaToolbar";
557-
558-
config.toolbar_CFMetaToolbar =
559-
[
560-
["Format"],
561-
["Bold","Italic","Strike"],
562-
["NumberedList","BulletedList","-","Outdent","Indent"],
563-
["Link","Unlink","Image","HorizontalRule","SpecialChar"],
564-
["PasteText","PasteFromWord"],
565-
["Undo","Redo","-","SelectAll","RemoveFormat"],
554+
echo 'CKEDITOR.editorConfig = function( config ) {
555+
config.toolbar = "CFMetaToolbar";
556+
557+
config.toolbar_CFMetaToolbar =
558+
[
559+
["Format"],
560+
["Bold","Italic","Strike"],
561+
["NumberedList","BulletedList","-","Outdent","Indent"],
562+
["Link","Unlink","Image","HorizontalRule","SpecialChar"],
563+
["PasteText","PasteFromWord"],
564+
["Undo","Redo","-","SelectAll","RemoveFormat"],
566565
["Source"]
567-
];
566+
];
568567
};
569568
';
570569
exit;

0 commit comments

Comments
 (0)