/** * Validates a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @return integer|float|WP_Error the validated field value or an error object */ public function validate($val = null) { $format = 'float'; $zero = 0.0; if (is_int($this->args['step'])) { $format = 'int'; $zero = 0; } if (!$val) { if ('int' === $format) { if ($this->args['min'] > 0) { return intval($this->args['min']); } return 0; } else { if ($this->args['min'] > 0) { return floatval($this->args['min']); } return 0.0; } } if ('int' === $format) { $val = intval($val); } else { $val = floatval($val); } if (!empty($this->args['step']) && !Util::is_rest_zero($val, $this->args['step'])) { return new WPError('invalid_number_step', sprintf(__('The number %1$s is invalid since it is not divisible by %2$s.', 'wpdlib'), FieldManager::format($val, $format, 'output'), FieldManager::format($this->args['step'], $format, 'output'))); } if (!empty($this->args['min']) && $val < FieldManager::format($this->args['min'], $format, 'input')) { return new WPError('invalid_number_too_small', sprintf(__('The number %1$s is invalid. It must be greater than or equal to %2$s.', 'wpdlib'), FieldManager::format($val, $format, 'output'), FieldManager::format($this->args['min'], $format, 'output'))); } if (!empty($this->args['max']) && $val > FieldManager::format($this->args['max'], $format, 'input')) { return new WPError('invalid_number_too_big', sprintf(__('The number %1$s is invalid. It must be lower than or equal to %2$s.', 'wpdlib'), FieldManager::format($val, $format, 'output'), FieldManager::format($this->args['max'], $format, 'output'))); } return $val; }
/** * Returns the child components of the component. * * If a class is specified, only children of that class are returned. * * @since 0.5.0 * @param string $class the class the children should have (default is an empty string for no restrictions) * @return array the array of child components, or an empty array if nothing found */ public function get_children($class = '') { $children = array(); if ($class && '*' !== $class) { if (isset($this->children[$class])) { $children = $this->children[$class]; } } else { $children = Util::object_array_merge($this->children, 'slug', 'position'); } return $children; }
/** * Formats a numeric value as a byte value. * * Possible $args: * - decimals (integer) * - base_unit (string, either 'B', 'kB', 'MB', 'GB' or 'TB') * * @since 0.5.0 * @see WPDLib\Util\Util::format_unit() * @param integer|float $value the value to format * @param string $mode the formatting mode; either 'input' (default) or 'output' * @param array $args additional formatting args (optional) * @return float|string the formatted value (if $mode == 'output', the float is formatted as a string) */ private static function format_byte($value, $mode = 'input', $args = array()) { if ('output' === $mode) { $units = array('B', 'kB', 'MB', 'GB', 'TB'); $decimals = isset($args['decimals']) ? absint($args['decimals']) : 2; $base_unit = isset($args['base_unit']) && in_array($args['base_unit'], $units) ? $args['base_unit'] : 'B'; return Util::format_unit($value, $units, 1024, $base_unit, $decimals); } $formatted = self::parse_float($value); $decimals = isset($args['decimals']) ? absint($args['decimals']) : false; if ($decimals !== false) { $formatted = number_format($formatted, $decimals); } return $formatted; }
/** * Transforms options of a specific type into actual options. * * Instead of specifying an array of options in the `$value => $label` format, * you may also specify an array that only contains one of the following key value pairs: * - `'posts' => $post_type` (will be transformed into a dropdown of posts of the post type $post_type) * - `'terms' => $taxonomy` (will be transformed into a dropdown of terms of the taxonomy $taxonomy) * - `'users' => $user_role (will be transformed into a dropdown of users of the role $user_role) * * Note that instead of providing a value of the above, you could also simply specify a boolean true * to get a dropdown of ALL posts / terms / users respectively. * * @since 0.5.0 */ public function parse_options() { if (isset($this->args['options']['posts'])) { $this->args['options'] = Util::get_posts_options($this->args['options']['posts']); } elseif (isset($this->args['options']['terms'])) { $this->args['options'] = Util::get_terms_options($this->args['options']['terms']); } elseif (isset($this->args['options']['users'])) { $this->args['options'] = Util::get_users_options($this->args['options']['users']); } }