コード例 #1
0
 /**
  * This function correctly parses (and optionally formats) a meta value.
  *
  * @see wpptd_get_post_meta_values()
  * @see wpptd_get_post_meta_value()
  * @see wpptd_get_term_meta_values()
  * @see wpptd_get_term_meta_value()
  * @since 0.5.0
  * @param mixed $meta_value the meta value to parse (or format)
  * @param WPPTD\Components\Field|WPPTD\Components\TermField $field the field component the meta value belongs to
  * @param null|boolean $single whether to force arrays or no arrays being returned (default is to not force anything)
  * @param boolean $formatted whether to return automatically formatted values, ready for output (default is false)
  * @return mixed the parsed (or formatted) meta value
  */
 public static function parse_meta_value($meta_value, $field, $single = null, $formatted = false)
 {
     $_meta_value = $meta_value;
     $meta_value = null;
     $type_hint = $field->validate_meta_value(null, true);
     if (is_array($type_hint)) {
         $meta_value = $field->_field->parse($_meta_value, $formatted);
         if ($single !== null && $single) {
             if (count($meta_value > 0)) {
                 $meta_value = $meta_value[0];
             } else {
                 $meta_value = null;
             }
         }
     } else {
         if (count($_meta_value) > 0) {
             $meta_value = $field->_field->parse($_meta_value[0], $formatted);
         } else {
             $meta_value = $field->_field->parse($field->default, $formatted);
         }
         if ($single !== null && !$single) {
             $meta_value = array($meta_value);
         }
     }
     return $meta_value;
 }
コード例 #2
0
 /**
  * Validates a meta value.
  *
  * @since 0.5.0
  * @param WPPTD\Components\Field $field field object to validate the meta value for
  * @param mixed $meta_value the meta value to validate
  * @param mixed $meta_value_old the previous meta value
  * @return array an array containing the validated value, a variable possibly containing a WP_Error object and a boolean value whether the meta value has changed
  */
 protected function validate_meta_value($field, $meta_value, $meta_value_old)
 {
     $meta_value = $field->validate_meta_value($meta_value);
     $error = false;
     $changed = false;
     if (is_wp_error($meta_value)) {
         $error = $meta_value;
         $meta_value = $meta_value_old;
     } elseif ($meta_value != $meta_value_old) {
         if (has_action('wpptd_update_meta_value_' . $this->slug . '_' . $field->slug)) {
             App::deprecated_action('wpptd_update_meta_value_' . $this->slug . '_' . $field->slug, '0.6.0', 'wpptd_update_post_meta_value_' . $this->slug . '_' . $field->slug);
             /**
              * This action can be used to perform additional steps when the meta value for a specific field of this post type has been updated.
              *
              * @since 0.5.0
              * @deprecated 0.6.0
              * @param mixed the updated meta value
              * @param mixed the previous meta value
              */
             do_action('wpptd_update_meta_value_' . $this->slug . '_' . $field->slug, $meta_value, $meta_value_old);
         }
         /**
          * This action can be used to perform additional steps when the meta value for a specific field of this post type has been updated.
          *
          * @since 0.6.0
          * @param mixed the updated meta value
          * @param mixed the previous meta value
          */
         do_action('wpptd_update_post_meta_value_' . $this->slug . '_' . $field->slug, $meta_value, $meta_value_old);
         $changed = true;
     }
     return array($meta_value, $error, $changed);
 }