Esempio n. 1
0
 /**
  * Handle indexing the various date/time fields.
  *
  * This method also serves as an example of the 'facetwp_cmb2_default_index' filter, although it should be noted
  * that since it is part of the class, it does not use the $obj class object, but makes method calls directly.
  * For use outside of this class, be sure to use $obj->index_row() or $obj->index_multiple().
  *
  * @since 1.0.0
  *
  * @param bool       $filter   Continue with normal indexing.
  * @param CMB2_Field $field    The field object.
  * @param array      $defaults Array of default data.
  *
  * @return bool Whether to continue with the normal indexing.
  */
 public function time_date_indexing($filter, $field, $defaults)
 {
     $date_format = 'Y-m-d';
     $extended_format = "{$date_format} H:i:s";
     $index = array('facet_display_value' => $field->args('name'));
     // Check for special field types
     if ('text_data' == $field->type()) {
         $index['facet_value'] = date($date_format, strtotime($field->value()));
         $this->index_row($index, $defaults);
         return false;
     } elseif ('text_date_timestamp' == $field->type()) {
         $index['facet_value'] = date($date_format, $field->value());
         $this->index_row($index, $defaults);
         return false;
     } elseif ('text_datetime_timestamp' == $field->type()) {
         $index['facet_value'] = date($extended_format, $field->value());
         $this->index_row($index, $defaults);
         return false;
     } elseif ('text_datetime_timestamp_timezone' == $field->type()) {
         $value = maybe_unserialize($field->value());
         if ($value instanceof DateTime) {
             $index['facet_value'] = $value->format($extended_format);
             $this->index_row($index, $defaults);
             return false;
         }
     }
     return $filter;
 }
Esempio n. 2
0
 /**
  * Save a repeatable group
  */
 public function save_group($args)
 {
     if (!isset($args['id'], $args['fields'], $this->data_to_save[$args['id']]) || !is_array($args['fields'])) {
         return;
     }
     $field_group = new CMB2_Field(array('field_args' => $args, 'object_type' => $this->object_type(), 'object_id' => $this->object_id()));
     $base_id = $field_group->id();
     $old = $field_group->get_data();
     $group_vals = $this->data_to_save[$base_id];
     $saved = array();
     $field_group->index = 0;
     foreach (array_values($field_group->fields()) as $field_args) {
         $field = new CMB2_Field(array('field_args' => $field_args, 'group_field' => $field_group));
         $sub_id = $field->id(true);
         foreach ((array) $group_vals as $field_group->index => $post_vals) {
             // Get value
             $new_val = isset($group_vals[$field_group->index][$sub_id]) ? $group_vals[$field_group->index][$sub_id] : false;
             // Sanitize
             $new_val = $field->sanitization_cb($new_val);
             if ('file' == $field->type() && is_array($new_val)) {
                 // Add image ID to the array stack
                 $saved[$field_group->index][$new_val['field_id']] = $new_val['attach_id'];
                 // Reset var to url string
                 $new_val = $new_val['url'];
             }
             // Get old value
             $old_val = is_array($old) && isset($old[$field_group->index][$sub_id]) ? $old[$field_group->index][$sub_id] : false;
             $is_updated = !empty($new_val) && $new_val != $old_val;
             $is_removed = empty($new_val) && !empty($old_val);
             // Compare values and add to `$updated` array
             if ($is_updated || $is_removed) {
                 $this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id;
             }
             // Add to `$saved` array
             $saved[$field_group->index][$sub_id] = $new_val;
         }
         $saved[$field_group->index] = array_filter($saved[$field_group->index]);
     }
     $saved = array_filter($saved);
     $field_group->update_data($saved, true);
 }
Esempio n. 3
0
 /**
  * Get the corresponding display class for the field type.
  * @since  2.2.2
  * @param  CMB2_Field $field
  * @return CMB2_Field_Display
  */
 public static function get(CMB2_Field $field)
 {
     switch ($field->type()) {
         case 'text_url':
             $type = new CMB2_Display_Text_Url($field);
             break;
         case 'text_money':
             $type = new CMB2_Display_Text_Money($field);
             break;
         case 'colorpicker':
             $type = new CMB2_Display_Colorpicker($field);
             break;
         case 'checkbox':
             $type = new CMB2_Display_Checkbox($field);
             break;
         case 'wysiwyg':
         case 'textarea_small':
             $type = new CMB2_Display_Textarea($field);
             break;
         case 'textarea_code':
             $type = new CMB2_Display_Textarea_Code($field);
             break;
         case 'text_time':
             $type = new CMB2_Display_Text_Time($field);
             break;
         case 'text_date':
         case 'text_date_timestamp':
         case 'text_datetime_timestamp':
             $type = new CMB2_Display_Text_Date($field);
             break;
         case 'text_datetime_timestamp_timezone':
             $type = new CMB2_Display_Text_Date_Timezone($field);
             break;
         case 'select':
         case 'radio':
         case 'radio_inline':
             $type = new CMB2_Display_Select($field);
             break;
         case 'multicheck':
         case 'multicheck_inline':
             $type = new CMB2_Display_Multicheck($field);
             break;
         case 'taxonomy_radio':
         case 'taxonomy_radio_inline':
         case 'taxonomy_select':
             $type = new CMB2_Display_Taxonomy_Radio($field);
             break;
         case 'taxonomy_multicheck':
         case 'taxonomy_multicheck_inline':
             $type = new CMB2_Display_Taxonomy_Multicheck($field);
             break;
         case 'file':
             $type = new CMB2_Display_File($field);
             break;
         case 'file_list':
             $type = new CMB2_Display_File_List($field);
             break;
         case 'oembed':
             $type = new CMB2_Display_oEmbed($field);
             break;
         default:
             $type = new self($field);
             break;
     }
     return $type;
 }