Ejemplo n.º 1
0
 /**
  * Sete repetitive form for single field.
  *
  * @param type $meta
  * @return string
  */
 function get_field_form($meta_value = null, $meta_id = null)
 {
     $form = array();
     if (is_null($meta_value)) {
         $key = 'wpcf_field_' . wpcf_unique_id(md5($this->index) . $meta_id);
     } else {
         $key = 'wpcf_field_' . md5(maybe_serialize($meta_value) . $meta_id);
     }
     /*
      *
      *
      * TODO We prevented array because of some fails we had before.
      * Now it should work fine
      * Add debug log if meta_value['custom_order'] passed.
      * That means setting meta_value did not went well.
      */
     //        if ( is_null( $meta_value ) || is_array( $meta_value ) ) {
     if (is_null($meta_value) || is_array($meta_value) && isset($meta_value['custom_order'])) {
         $meta_value = $this->meta['single'];
     }
     // Open drag div
     $form[$key . '_drag_open'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-repetitive-drag-and-drop">');
     // Use WPCF_Field::get_field_form()
     $field_form = parent::_get_meta_form($meta_value, $meta_id, false);
     /*
      *
      * Apply filters to each form element.
      * Here we add specific properties
      * e.g. Skype alters fields.
      */
     $_loop = false;
     foreach ($field_form as $k => $field) {
         /*
          *
          * IMPORTANT
          * We change name to hold array
          */
         if (isset($field['#name'])) {
             $temp = explode('[' . $this->cf['slug'] . ']', $field['#name']);
             // Assign new name
             $field['#name'] = $temp[0] . '[' . $this->cf['slug'] . ']' . '[' . $key . ']';
             // Append rest if any
             if (isset($temp[1])) {
                 $field['#name'] .= $temp[1];
             }
         }
         // Apply filters
         $field_form[$k] = apply_filters('wpcf_repetitive_field', $field, $this->post, $this->cf, $k);
         // BREAKPOINT
         /*
          * This is place where we clean display.
          * First item is displayed as it is, each after is reduced.
          * If called via AJAX - that means it added and should be reduced.
          */
         //            if ( $_loop == true || defined( 'DOING_AJAX' ) ) {
         /*
          * See if field has Repeater pattern defined
          */
         if (isset($field['__repeater_restricted']) && is_array($field['__repeater_restricted'])) {
             foreach ($field['__repeater_restricted'] as $_e => $_v) {
                 if (isset($field[$_e])) {
                     unset($field[$_e]);
                 }
             }
         } else {
             unset($field['#title'], $field['#description']);
         }
         // Set main
         $field_form[$k] = $field;
         //            }
         //            $_loop = true;
     }
     // Just append form
     $form = $form + $field_form;
     // Open control div
     $form[$key . '_control_open'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-repetitive-control">');
     // Drag button
     $form[$key . '_drag_button'] = array('#type' => 'markup', '#markup' => wpcf_repetitive_drag_button($this->cf, $this->post));
     // 'Delete' button
     $form[$key . '_delete_button'] = array('#type' => 'markup', '#markup' => wpcf_repetitive_delete_button($this->cf, $this->post, $meta_id));
     // Close control div
     $form[$key . '_control_close'] = array('#type' => 'markup', '#markup' => '</div>');
     // Close drag div
     $form[$key . '_drag_close'] = array('#type' => 'markup', '#markup' => '</div>');
     // Count it and set JS var
     $this->_set_form_count();
     wpcf_admin_add_js_settings('wpcf_repetitive_count_' . md5($this->cf['id']), $this->index);
     return $form;
 }