/** * Validates a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @return string|WP_Error the validated field value or an error object */ public function validate($val = null) { if (!$val) { return ''; } $input = sanitize_email($val); $val = is_email($input); if (!$val) { return new WPError('invalid_email', sprintf(__('%s is not a valid email address.', 'wpdlib'), \WPDLib\FieldTypes\Manager::format($input, 'string', 'output'))); } return $val; }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return string the correctly parsed value */ public function parse($val, $formatted = false) { if ($formatted) { if (!is_array($formatted)) { $formatted = array(); } $formatted = wp_parse_args($formatted, array('mode' => 'text')); switch ($formatted['mode']) { case 'link': return '<a href="' . esc_url($val) . '" target="_blank">' . esc_url($val) . '</a>'; case 'text': default: return FieldManager::format($val, 'url', 'output'); } } return FieldManager::format($val, 'url', 'input'); }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return string the correctly parsed value */ public function parse($val, $formatted = false) { if ($formatted) { $parsed = FieldManager::format($val, 'html', 'input'); if (!is_array($formatted)) { $formatted = array(); } $formatted = wp_parse_args($formatted, array('wpautop' => true, 'shortcode' => false)); if ($formatted['wpautop']) { $parsed = wpautop($parsed); } if ($formatted['shortcode']) { $parsed = do_shortcode($parsed); } return $parsed; } return FieldManager::format($val, 'html', 'input'); }
/** * Formats a timestamp into a date format string ready for saving to the database. * * @since 0.5.0 * @param integer $val the timestamp to format * @return string|null the date format string or null if formatting not possible */ protected function format_timestamp($val) { return $val !== null ? FieldManager::format($val, $this->type, 'input') : null; }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return integer|string the correctly parsed value (string if $formatted is true) */ public function parse($val, $formatted = false) { if ('url' === $this->args['store']) { $val = FieldManager::format($val, 'url', 'input'); } else { $val = absint($val); } if ($formatted) { if (!is_array($formatted)) { $formatted = array(); } $formatted = wp_parse_args($formatted, array('mode' => 'field', 'field' => 'url', 'template' => '')); $attachment_id = $val; if ('url' === $this->args['store']) { if ('field' === $formatted['mode'] && 'url' === $formatted['field']) { return $val; } $attachment_id = attachment_url_to_postid($val); } return $this->format_attachment($attachment_id, $formatted); } return $val; }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return integer|float|string the correctly parsed value (string if $formatted is true) */ public function parse($val, $formatted = false) { $format = 'float'; if (is_int($this->args['step'])) { $format = 'int'; } if ($formatted) { return FieldManager::format($val, $format, 'output'); } return FieldManager::format($val, $format, 'input'); }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return string the correctly parsed value */ public function parse($val, $formatted = false) { if ($formatted) { return FieldManager::format($val, 'string', 'output'); } return FieldManager::format($val, 'string', 'input'); }
/** * Formats a single item for output. * * The argument passed as $val is the value of a specific field option. * * The 'mode' key in the $args array specifies in which format it should be returned: * - html (if option has an image, it will be printed inside an IMG tag; if it has a color, it will be printed as a div with that color as background) * - text (if option has an image, it will be printed as the plain URL; if it has a color, it will be printed as a hex color string) * * @since 0.5.0 * @param string|array $val the field option value to format * @param array $args arguments on how to format * @return string the correctly parsed value */ protected function format_item($val, $args = array()) { $skip_formatting = false; if (isset($this->args['options'][$val])) { if (is_array($this->args['options'][$val])) { if (isset($this->args['options'][$val]['label']) && !empty($this->args['options'][$val]['label'])) { $val = $this->args['options'][$val]['label']; } elseif (isset($this->args['options'][$val]['image'])) { if ('html' == $args['mode']) { $val = '<img src="' . esc_url($this->args['options'][$val]['image']) . '" style="display: inline-block;width:64px;height:auto;">'; $skip_formatting = true; } else { $val = esc_url($this->args['options'][$val]['image']); } } elseif (isset($this->args['options'][$val]['color'])) { if ('html' == $args['mode']) { $val = '<div style="display:inline-block;width:64px;height:48px;background-color:' . $this->args['options'][$val]['color'] . ';"></div>'; $skip_formatting = true; } else { $val = $this->args['options'][$val]['color']; } } } else { $val = $this->args['options'][$val]; } } if ($skip_formatting) { return $val; } return FieldManager::format($val, 'string', 'output'); }
/** * Parses a value for the field. * * @since 0.6.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return string the correctly parsed value */ public function parse($val, $formatted = false) { if ('coords' === $this->args['store']) { $val = explode('|', $val); if (2 !== count($val)) { return ''; } $mode = 'input'; if ($formatted) { $mode = 'output'; } for ($i = 0; $i < 2; $i++) { $val[$i] = FieldManager::format($val[$i], 'float', $mode, array('decimals' => 10)); } return $val[0] . '|' . $val[1]; } if ($formatted) { return FieldManager::format($val, 'string', 'output'); } return FieldManager::format($val, 'string', 'input'); }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return bool|string the correctly parsed value (a string if $formatted is true) */ public function parse($val, $formatted = false) { if ($formatted) { if (!is_array($formatted)) { $formatted = array(); } $formatted = wp_parse_args(array('mode' => 'text')); switch ($formatted['mode']) { case 'tick': if ($val) { return '✔'; } return ''; case 'text': default: return FieldManager::format($val, 'boolean', 'output'); } } return FieldManager::format($val, 'boolean', 'input'); }
/** * Parses a value for the field. * * @since 0.5.0 * @param mixed $val the current value of the field * @param bool|array $formatted whether to also format the value (default is false) * @return string the correctly parsed value */ public function parse($val, $formatted = false) { if (!$val) { return ''; } if ($formatted) { if (!is_array($formatted)) { $formatted = array(); } $formatted = wp_parse_args(array('mode' => 'text')); switch ($formatted['mode']) { case 'color': return '<div style="display:inline-block;width:64px;height:48px;background-color:' . $val . ';"></div>'; case 'color-text': return '<div style="display:inline-block;padding:5px 10px;background-color:' . $val . ';">' . FieldManager::format($val, 'string', 'output') . '</div>'; case 'text': default: return FieldManager::format($val, 'string', 'output'); } } return FieldManager::format($val, 'string', 'input'); }