request = $application->make('request'); } /** * Export forms as JSON. */ public function export() { // Get the IDs of the forms the user wants to be exported. $formIds = $this->request->get('forms'); // The associated form settings the user wants to be exported as well. // Firing an event so that others can hook into it and add their // choice of the settings to be exported with the form. $metaKeys = apply_filters('fluentform_export_settings', [ 'formSettings', 'notifications', 'mailchimp_feeds', 'slack' ]); // Load the forms for a given form IDs and the settings from the DB. $result = wpFluent()->table('fluentform_forms') ->select(['fluentform_forms.*']) ->whereIn('fluentform_forms.id', $formIds) ->when($metaKeys, function ($query) use ($metaKeys) { $query->select([ 'fluentform_form_meta.meta_key', 'fluentform_form_meta.value' ])->join( 'fluentform_form_meta', 'fluentform_form_meta.form_id', '=', 'fluentform_forms.id' )->whereIn('fluentform_form_meta.meta_key', $metaKeys); }) ->get(); // Prepare the loaded query results to form and it's settings objects. $forms = []; foreach ($result as $item) { $form = isset($forms[$item->id]) ? $forms[$item->id] : [ 'id' => $item->id, 'title' => $item->title, 'form' => json_decode($item->form_fields) ]; $settings = [ $item->meta_key => Helper::isJson($item->value) ? json_decode($item->value) : $item->value ]; $forms[$item->id] = array_merge($form, $settings); } $fileName = 'fluentform-export-forms-'.date('d-m-Y').'.json'; header('Content-disposition: attachment; filename='.$fileName); header('Content-type: application/json'); echo json_encode(array_values($forms)); die(); } /** * Import forms from a previously exported JSON file. */ public function import() { $file = $this->request->get('file'); if ($file instanceof File) { $forms = json_decode($file->getContents(), true); if ($forms && is_array($forms)) { foreach ($forms as $formItem) { // First of all make the form object. $form = [ 'title' => ArrayHelper::get($formItem, 'title'), 'form_fields' => json_encode(ArrayHelper::get($formItem, 'form', '')), 'created_by' => get_current_user_id() ]; // Insert the form to the DB. $formId = wpFluent()->table('fluentform_forms')->insert($form); // Remove the form object specific keys. unset($formItem['title']); unset($formItem['id']); unset($formItem['form']); // At this point all we have are the // settings associated with the form. $settings = []; foreach ($formItem as $settingsName => $settingsValue) { $settings[] = [ 'form_id' => $formId, 'meta_key' => $settingsName, 'value' => json_encode($settingsValue) ]; } // Fire an event letting others know which settings we are importing for this form. $settings = apply_filters('fluentform_import_form_settings', $settings, $formId); if (count($settings)) { wpFluent()->table('fluentform_form_meta')->insert($settings); } } wp_send_json([ 'message' => __('You form has been successfully imported.', 'fluentform') ], 200); } } wp_send_json([ 'message' => __('You have a faulty JSON file, please export the Fluent Forms again.', 'fluentform') ], 423); } }