form-export.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/dynamic_forms')) |
| 8 | return; |
| 9 | |
| 10 | if(!class_exists('ACFE_Admin_Tool_Export_Form')): |
| 11 | |
| 12 | class ACFE_Admin_Tool_Export_Form extends ACF_Admin_Tool{ |
| 13 | |
| 14 | public $action = false; |
| 15 | public $data = array(); |
| 16 | |
| 17 | function initialize(){ |
| 18 | |
| 19 | // vars |
| 20 | $this->name = 'acfe_tool_form_export'; |
| 21 | $this->title = __('Export Forms'); |
| 22 | $this->icon = 'dashicons-upload'; |
| 23 | |
| 24 | } |
| 25 | |
| 26 | function html(){ |
| 27 | |
| 28 | // Archive |
| 29 | if(!$this->is_active()){ |
| 30 | |
| 31 | $this->html_archive(); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | |
| 37 | function html_archive(){ |
| 38 | |
| 39 | // vars |
| 40 | $choices = array(); |
| 41 | |
| 42 | $get_forms = get_posts(array( |
| 43 | 'post_type' => 'acfe-form', |
| 44 | 'posts_per_page' => -1, |
| 45 | 'fields' => 'ids' |
| 46 | )); |
| 47 | |
| 48 | if($get_forms){ |
| 49 | foreach($get_forms as $form_id){ |
| 50 | |
| 51 | $name = get_field('acfe_form_name', $form_id); |
| 52 | |
| 53 | $choices[$name] = esc_html(get_the_title($form_id)); |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | ?> |
| 59 | <p><?php _e('Export Forms', 'acf'); ?></p> |
| 60 | |
| 61 | <div class="acf-fields"> |
| 62 | <?php |
| 63 | |
| 64 | if(!empty($choices)){ |
| 65 | |
| 66 | // render |
| 67 | acf_render_field_wrap(array( |
| 68 | 'label' => __('Select Forms', 'acf'), |
| 69 | 'type' => 'checkbox', |
| 70 | 'name' => 'keys', |
| 71 | 'prefix' => false, |
| 72 | 'value' => false, |
| 73 | 'toggle' => true, |
| 74 | 'choices' => $choices, |
| 75 | )); |
| 76 | |
| 77 | } |
| 78 | |
| 79 | else{ |
| 80 | |
| 81 | echo '<div style="padding:15px 12px;">'; |
| 82 | _e('No dynamic form available.'); |
| 83 | echo '</div>'; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | ?> |
| 88 | </div> |
| 89 | |
| 90 | <?php |
| 91 | |
| 92 | $disabled = ''; |
| 93 | if(empty($choices)) |
| 94 | $disabled = 'disabled="disabled"'; |
| 95 | |
| 96 | ?> |
| 97 | |
| 98 | <p class="acf-submit"> |
| 99 | <button type="submit" name="action" class="button button-primary" value="json" <?php echo $disabled; ?>><?php _e('Export File'); ?></button> |
| 100 | </p> |
| 101 | <?php |
| 102 | |
| 103 | } |
| 104 | |
| 105 | function load(){ |
| 106 | |
| 107 | if($this->is_active()){ |
| 108 | |
| 109 | $this->action = $this->get_action(); |
| 110 | $this->data = $this->get_selected(); |
| 111 | |
| 112 | // Json submit |
| 113 | if($this->action === 'json') |
| 114 | $this->submit(); |
| 115 | |
| 116 | // add notice |
| 117 | if(!empty($this->data)){ |
| 118 | |
| 119 | $count = count($this->data); |
| 120 | $text = sprintf(_n( 'Exported 1 form.', 'Exported %s forms.', $count, 'acf' ), $count); |
| 121 | |
| 122 | acf_add_admin_notice($text, 'success'); |
| 123 | |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | function submit(){ |
| 131 | |
| 132 | $this->action = $this->get_action(); |
| 133 | $this->data = $this->get_selected(); |
| 134 | |
| 135 | // validate |
| 136 | if($this->data === false) |
| 137 | return acf_add_admin_notice(__('No forms selected'), 'warning'); |
| 138 | |
| 139 | $keys = array(); |
| 140 | foreach($this->data as $args){ |
| 141 | |
| 142 | $keys[] = $args['acfe_form_name']; |
| 143 | |
| 144 | } |
| 145 | |
| 146 | if($this->action === 'json'){ |
| 147 | |
| 148 | // Prefix |
| 149 | $prefix = (count($keys) > 1) ? 'forms' : 'forms'; |
| 150 | |
| 151 | // Slugs |
| 152 | $slugs = implode('-', $keys); |
| 153 | |
| 154 | // Date |
| 155 | $date = date('Y-m-d'); |
| 156 | |
| 157 | // file |
| 158 | $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json'; |
| 159 | |
| 160 | // headers |
| 161 | header("Content-Description: File Transfer"); |
| 162 | header("Content-Disposition: attachment; filename={$file_name}"); |
| 163 | header("Content-Type: application/json; charset=utf-8"); |
| 164 | |
| 165 | // return |
| 166 | echo acf_json_encode($this->data); |
| 167 | die; |
| 168 | |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | function get_selected(){ |
| 174 | |
| 175 | // vars |
| 176 | $selected = $this->get_selected_keys(); |
| 177 | |
| 178 | if(!$selected) |
| 179 | return false; |
| 180 | |
| 181 | $data = array(); |
| 182 | |
| 183 | acf_enable_filter('local'); |
| 184 | |
| 185 | // construct Data |
| 186 | foreach($selected as $key){ |
| 187 | |
| 188 | if(!$form = get_page_by_path($key, OBJECT, 'acfe-form')) |
| 189 | continue; |
| 190 | |
| 191 | // add to data array |
| 192 | $data[] = array_merge(array('title' => get_the_title($form->ID)), get_fields($form->ID, false)); |
| 193 | |
| 194 | } |
| 195 | |
| 196 | acf_disable_filter('local'); |
| 197 | |
| 198 | // return |
| 199 | return $data; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | function get_selected_keys(){ |
| 204 | |
| 205 | // check $_POST |
| 206 | if($keys = acf_maybe_get_POST('keys')){ |
| 207 | |
| 208 | return (array) $keys; |
| 209 | |
| 210 | } |
| 211 | |
| 212 | // check $_GET |
| 213 | if($keys = acf_maybe_get_GET('keys')){ |
| 214 | |
| 215 | $keys = str_replace(' ', '+', $keys); |
| 216 | return explode('+', $keys); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | // return |
| 221 | return false; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | function get_action(){ |
| 226 | |
| 227 | // init |
| 228 | $type = 'json'; |
| 229 | |
| 230 | // return |
| 231 | return $type; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | acf_register_admin_tool('ACFE_Admin_Tool_Export_Form'); |
| 238 | |
| 239 | endif; |