Skip to content

Commit

Permalink
validate fields
Browse files Browse the repository at this point in the history
  • Loading branch information
davidperezgar committed Sep 14, 2020
1 parent c1d1195 commit aec4ef7
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://www.close.marketing
Tags: events, blog, posts
Requires at least: 3.0.1
Tested up to: 3.4
Stable tag: 4.3
Stable tag: 0.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down
67 changes: 41 additions & 26 deletions admin/class-simply-event-blog-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ public function __construct( $plugin_name, $version ) {
*/
public function enqueue_styles() {

/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Simply_Event_Blog_Loader as all of the hooks are defined
* in that particular class.
*
* The Simply_Event_Blog_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/

wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/simply-event-blog-admin.css', array(), $this->version, 'all' );

}
Expand All @@ -87,8 +75,23 @@ public function enqueue_styles() {
*/
public function enqueue_scripts() {

wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/simply-event-blog-admin.js', array( 'jquery' ), $this->version, false );
wp_enqueue_script(
'jquery.timepicker',
plugin_dir_url( __FILE__ ) . 'js/jquery.timepicker.min.js',
array( 'jquery' ),
$this->version,
true
);

wp_enqueue_script(
$this->plugin_name,
plugin_dir_url( __FILE__ ) . 'js/simply-event-blog-admin.js',
array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker' ),
$this->version,
false
);

wp_enqueue_style( 'jquery-ui-datepicker' );
}

/**
Expand Down Expand Up @@ -123,24 +126,36 @@ public function metabox_date_content( $post ) {
esc_html_e( 'Adds the information for the event in this post.', 'simply-event-blog' );
?>
<p>
<label for="seb_date_start"><?php esc_html_e( 'Date start (YYYY-MM-DD)', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_date_start" id="seb_date_start" value="<?php echo esc_html( $seb_date_start ); ?>" />
<label for="seb_date_start"><?php esc_html_e( 'Date start', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_date_start" id="seb_date_start" class="event-date" value="<?php echo esc_html( $seb_date_start ); ?>" />
</p>
<p>
<label for="seb_time_start"><?php esc_html_e( 'Time start (HH:MM)', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_time_start" id="seb_time_start" value="<?php echo esc_html( $seb_time_start ); ?>" />
<label for="seb_time_start"><?php esc_html_e( 'Time start', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_time_start" id="seb_time_start" class="event-hour" value="<?php echo esc_html( $seb_time_start ); ?>" />
</p>
<p>
<label for="seb_date_finish"><?php esc_html_e( 'Date finish (YYYY-MM-DD)', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_date_fin" id="seb_date_fin" value="<?php echo esc_html( $seb_date_fin ); ?>" />
<label for="seb_date_finish"><?php esc_html_e( 'Date finish', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_date_fin" id="seb_date_fin" class="event-date" value="<?php echo esc_html( $seb_date_fin ); ?>" />
</p>
<p>
<label for="seb_time_fin"><?php esc_html_e( 'Time finish (HH:MM)', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_time_fin" id="seb_time_fin" value="<?php echo esc_html( $seb_time_fin ); ?>" />
<label for="seb_time_fin"><?php esc_html_e( 'Time finish', 'simply-event-blog' ); ?></label>
<input type="text" name="seb_time_fin" id="seb_time_fin" class="event-hour" value="<?php echo esc_html( $seb_time_fin ); ?>" />
</p>
<?php
}

/**
* Validate the date
*
* @param straing $date Date to validate.
* @param string $format Format of date.
* @return boolean
*/
private function validate_date( $date, $format = 'Y-m-d' ) {
$date_to_validate = DateTime::createFromFormat( $format, $date );
return $date_to_validate && $date_to_validate->format( $format ) == $date;
}

/**
* Metabox save
*
Expand All @@ -158,17 +173,17 @@ public function metabox_date_save( $post_id ) {
return;
}
$allowed = array();
if ( isset( $_POST['seb_date_start'] ) ) {
update_post_meta( $post_id, 'seb_date_start', wp_kses( $_POST['seb_date_start'], $allowed ) );
if ( isset( $_POST['seb_date_start'] ) && $this->validate_date( $_POST['seb_date_start'] ) ) {
update_post_meta( $post_id, 'seb_date_start', sanitize_text_field( $_POST['seb_date_start'] ) );
}
if ( isset( $_POST['seb_time_start'] ) ) {
update_post_meta( $post_id, 'seb_time_start', wp_kses( $_POST['seb_time_start'], $allowed ) );
update_post_meta( $post_id, 'seb_time_start', sanitize_text_field( $_POST['seb_time_start'] ) );
}
if ( isset( $_POST['seb_date_fin'] ) ) {
update_post_meta( $post_id, 'seb_date_fin', wp_kses( $_POST['seb_date_fin'], $allowed ) );
update_post_meta( $post_id, 'seb_date_fin', sanitize_text_field( $_POST['seb_date_fin'] ) );
}
if ( isset( $_POST['seb_time_fin'] ) ) {
update_post_meta( $post_id, 'seb_time_fin', wp_kses( $_POST['seb_time_fin'], $allowed ) );
update_post_meta( $post_id, 'seb_time_fin', sanitize_text_field( $_POST['seb_time_fin'] ) );
}
}
}
103 changes: 99 additions & 4 deletions admin/css/simply-event-blog-admin.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,99 @@
/**
* All of the CSS for your admin-specific functionality should be
* included in this file.
*/
.wp-admin.post-type-post .ui-datepicker {
background-color: white;
}

.wp-admin.post-type-post .ui-datepicker .ui-datepicker-header a {
margin-right: 15px;
margin-bottom: 15px;
}
.wp-admin.post-type-post .ui-datepicker .ui-datepicker-header .ui-datepicker-title {
margin: 15px 0;
text-align: center;
font-weight: 700;
}

.wp-admin.post-type-post .event-date {
width: 98px;
}

.wp-admin.post-type-post .event-hour {
width: 98px;
}


.ui-timepicker-container {
position: absolute;
overflow: hidden;
box-sizing: border-box;
}

.ui-timepicker {
box-sizing: content-box;
display: block;
height: 205px;
list-style: none outside none;
margin: 0;
padding: 0 1px;
text-align: center;
}

.ui-timepicker-viewport {
box-sizing: content-box;
display: block;
height: 205px;
margin: 0;
padding: 0;
overflow: auto;
overflow-x: hidden; /* IE */
}

.ui-timepicker-standard {
/* overwrites .ui-widget */
font-family: Verdana,Arial,sans-serif;
font-size: 1.1em;
/* overwrites .ui-widget-content */
background-color: #FFF;
border: 1px solid #AAA;
color: #222;
/* overwrites .ui-menu */
margin: 0;
padding: 2px;
}
.ui-timepicker-standard a {
border: 1px solid transparent;
color: #222;
display: block;
padding: 0.2em 0.4em;
text-decoration: none;
}
.ui-timepicker-standard .ui-state-hover {
/* overwrites .ui-state-hover */
background-color: #DADADA;
border: 1px solid #999;
font-weight: normal;
color: #212121;
}
.ui-timepicker-standard .ui-menu-item {
/* overwrites .ui-menu and .ui-menu-item */
/*clear: left;
float: left;*/
margin: 0;
padding: 0;
}

.ui-timepicker-corners,
.ui-timepicker-corners .ui-corner-all {
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
}

.ui-timepicker-hidden {
/* overwrites .ui-helper-hidden */
display: none;
}

.ui-timepicker-no-scrollbar .ui-timepicker {
border: none;
}

10 changes: 10 additions & 0 deletions admin/js/jquery.timepicker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aec4ef7

Please sign in to comment.