first commit
This commit is contained in:
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Setting class for storing a single meta value as an array.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Array setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting_Array extends ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'array';
|
||||
|
||||
/**
|
||||
* Sanitizes the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param array $value
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize( $values ) {
|
||||
|
||||
$multi_values = $values && ! is_array( $values ) ? explode( ',', $values ) : $values;
|
||||
|
||||
return $multi_values ? array_map( array( $this, 'map' ), $multi_values ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for sanitizing each value of the array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function map( $value ) {
|
||||
|
||||
return apply_filters( "butterbean_{$this->manager->name}_sanitize_{$this->name}", $value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if ( ! $this->check_capabilities() )
|
||||
return;
|
||||
|
||||
$old_values = $this->get_value();
|
||||
$new_values = $this->get_posted_value();
|
||||
|
||||
// If there's an array of posted values, set them.
|
||||
if ( $new_values && is_array( $new_values ) && $new_values !== $old_values )
|
||||
return update_post_meta( $this->manager->post_id, $this->name, $new_values );
|
||||
|
||||
// If no array of posted values but we have old values, delete them.
|
||||
else if ( $old_values && ! $new_values )
|
||||
return delete_post_meta( $this->manager->post_id, $this->name );
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Datetime setting class. This is meant to be used in conjunction with the built-in
|
||||
* `ButterBean_Datetime_Control` or a sub-class that passes the appropriate values.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Date setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting_Datetime extends ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'datetime';
|
||||
|
||||
/**
|
||||
* Gets the posted value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_posted_value() {
|
||||
|
||||
$field_name = $this->get_field_name();
|
||||
|
||||
// Get the posted date.
|
||||
$year = ! empty( $_POST[ "{$field_name}_year" ] ) ? zeroise( absint( $_POST[ "{$field_name}_year" ] ), 4 ) : '';
|
||||
$month = ! empty( $_POST[ "{$field_name}_month" ] ) ? zeroise( absint( $_POST[ "{$field_name}_month" ] ), 2 ) : '';
|
||||
$day = ! empty( $_POST[ "{$field_name}_day" ] ) ? zeroise( absint( $_POST[ "{$field_name}_day" ] ), 2 ) : '';
|
||||
|
||||
// Get the posted time.
|
||||
$hour = ! empty( $_POST[ "{$field_name}_hour" ] ) ? $this->validate_hour( $_POST[ "{$field_name}_hour" ] ) : '00';
|
||||
$minute = ! empty( $_POST[ "{$field_name}_minute" ] ) ? $this->validate_minute( $_POST[ "{$field_name}_minute" ] ) : '00';
|
||||
$second = ! empty( $_POST[ "{$field_name}_second" ] ) ? $this->validate_second( $_POST[ "{$field_name}_second" ] ) : '00';
|
||||
|
||||
$date = "{$year}-{$month}-{$day}";
|
||||
$time = "{$hour}:{$minute}:{$second}";
|
||||
|
||||
if ( $year && $month && $day && wp_checkdate( absint( $month ), absint( $day ), absint( $year ), $date ) )
|
||||
return "{$date} {$time}";
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the hour.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param int|string $hour
|
||||
* @return string
|
||||
*/
|
||||
public function validate_hour( $hour ) {
|
||||
|
||||
$hour = absint( $hour );
|
||||
|
||||
return $hour < 0 || $hour > 23 ? zeroise( $hour, 2 ) : '00';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the minute.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param int|string $minute
|
||||
* @return string
|
||||
*/
|
||||
public function validate_minute( $minute ) {
|
||||
|
||||
$minute = absint( $minute );
|
||||
|
||||
return $minute < 0 || $minute > 59 ? zeroise( $minute, 2 ) : '00';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the second.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param int|string $second
|
||||
* @return string
|
||||
*/
|
||||
public function validate_second( $second ) {
|
||||
|
||||
$second = absint( $second );
|
||||
|
||||
return $second < 0 || $second > 59 ? zeroise( $second, 2 ) : '00';
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/**
|
||||
* Setting class for storing multiple post meta values for a single key.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Multiple setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting_Multiple extends ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'multiple';
|
||||
|
||||
/**
|
||||
* Gets the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value() {
|
||||
|
||||
return get_post_meta( $this->manager->post_id, $this->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param array $value
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize( $values ) {
|
||||
|
||||
$multi_values = $values && ! is_array( $values ) ? explode( ',', $values ) : $values;
|
||||
|
||||
return $multi_values ? array_map( array( $this, 'map' ), $multi_values ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for sanitizing each value of the array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function map( $value ) {
|
||||
|
||||
return apply_filters( "butterbean_{$this->manager->name}_sanitize_{$this->name}", $value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if ( ! $this->check_capabilities() )
|
||||
return;
|
||||
|
||||
$old_values = $this->get_value();
|
||||
$new_values = $this->get_posted_value();
|
||||
|
||||
// If there's an array of posted values, set them.
|
||||
if ( is_array( $new_values ) )
|
||||
$this->set_values( $new_values, $old_values );
|
||||
|
||||
// If no array of posted values but we have old values, delete them.
|
||||
else if ( $old_values )
|
||||
$this->delete_values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops through new and old meta values and updates.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param array $new_values
|
||||
* @param array $old_values
|
||||
* @return void
|
||||
*/
|
||||
public function set_values( $new_values, $old_values ) {
|
||||
|
||||
foreach ( $new_values as $new ) {
|
||||
|
||||
if ( ! in_array( $new, $old_values ) )
|
||||
$this->add_value( $new );
|
||||
}
|
||||
|
||||
foreach ( $old_values as $old ) {
|
||||
|
||||
if ( ! in_array( $old, $new_values ) )
|
||||
$this->remove_value( $old );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes old meta values.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function delete_values() {
|
||||
|
||||
return delete_post_meta( $this->manager->post_id, $this->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a single meta value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function add_value( $value ) {
|
||||
|
||||
return add_post_meta( $this->manager->post_id, $this->name, $value, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a single meta value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function remove_value( $value ) {
|
||||
|
||||
return delete_post_meta( $this->manager->post_id, $this->name, $value );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user