public function set($args) { if (isset($args['id'])) { $this->id = $args['id']; } $this->name = $args['name']; $this->type = $args['type']; $this->required = WJ_Utils::to_bool($args['required']); $this->settings = maybe_unserialize($args['settings']); if (!is_array($this->settings)) { $this->settings = array(); } if (isset(self::$defaults[$this->type]) && is_array(self::$defaults[$this->type])) { $this->settings = wp_parse_args((array) $this->settings, (array) self::$defaults[$this->type]); } }
function wysija_form_manage_field() { $response = array('result' => true, 'error' => null); // get data $data = $this->_wysija_form_get_data(); $form_id = (int) $_REQUEST['form_id']; // check for required fields if (!isset($data['type']) || isset($data['type']) && strlen(trim($data['type'])) === 0) { $response['error'] = __('You need to select a type for this field', WYSIJA); $response['result'] = false; } if (!isset($data['name']) || isset($data['name']) && strlen(trim($data['name'])) === 0) { $response['error'] = __('You need to specify a name for this field', WYSIJA); $response['result'] = false; } // only proceed if there is no error if ($response['error'] === null) { $is_required = isset($data['params']['required']) ? WJ_Utils::to_bool($data['params']['required']) : false; if (isset($data['field_id']) && (int) $data['field_id'] > 0) { // it's an update $custom_field = WJ_Field::get($data['field_id']); if ($custom_field !== NULL) { $data['params'] = array_merge($custom_field->settings, $data['params']); // update fields $custom_field->name = $data['name']; $custom_field->type = $data['type']; $custom_field->required = $is_required; $custom_field->settings = $data['params']; $custom_field->save(); } else { // throw error if field does not exist $response['error'] = __('This field does not exist', WYSIJA); $response['result'] = false; } } else { // create new custom field $custom_field = new WJ_Field(); $custom_field->set(array('name' => $data['name'], 'type' => $data['type'], 'required' => $is_required, 'settings' => $data['params'])); $custom_field->save(); } if ($response['error'] === null) { $helper_form_engine = WYSIJA::get('form_engine', 'helper'); // need to update each block instance of this custom field $block = $helper_form_engine->refresh_custom_field($form_id, array('name' => $data['name'], 'field' => $custom_field->user_column_name(), 'type' => $data['type'], 'required' => $is_required, 'settings' => $data['params'])); // render editor toolbar & templates $response['data'] = array('toolbar' => base64_encode($helper_form_engine->render_editor_toolbar()), 'templates' => base64_encode($helper_form_engine->render_editor_templates())); if ($block !== null) { // refresh block using this custom field in the current form $block_template = $helper_form_engine->render_editor_template($block); if ($block_template !== null) { $response['data']['block'] = base64_encode($block_template); } } } } return $response; }
public static function get_all($options = array()) { $object = new self(); global $wpdb; // default order by $order_by = 'id ASC'; if (isset($options['order_by'])) { $order_by = $options['order_by']; } // fetch rows from db $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$object->table} ORDER BY %s", $order_by), ARRAY_A); if ($results != null) { $collection = array(); foreach ($results as $result) { $result['settings'] = unserialize($result['settings']); $result['required'] = WJ_Utils::to_bool($result['required']); $field = new self(); $field->set($result); $collection[] = $field; } return $collection; } else { return null; } }