module-export.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_module_export')): |
| 7 | |
| 8 | class acfe_module_export extends ACF_Admin_Tool{ |
| 9 | |
| 10 | public $instance; |
| 11 | public $action; |
| 12 | public $data = array(); |
| 13 | |
| 14 | public $description; |
| 15 | public $select; |
| 16 | public $default_action; |
| 17 | public $allowed_actions = array(); |
| 18 | public $file; |
| 19 | public $files; |
| 20 | public $messages = array(); |
| 21 | |
| 22 | function html(){ |
| 23 | |
| 24 | // Single |
| 25 | if($this->is_active()){ |
| 26 | |
| 27 | $this->html_single(); |
| 28 | |
| 29 | // Archive |
| 30 | }else{ |
| 31 | |
| 32 | $this->html_archive(); |
| 33 | |
| 34 | } |
| 35 | |
| 36 | } |
| 37 | |
| 38 | function html_archive(){ |
| 39 | |
| 40 | // vars |
| 41 | $choices = $this->instance->export_choices(); |
| 42 | |
| 43 | ?> |
| 44 | <p><?php echo $this->description; ?></p> |
| 45 | |
| 46 | <div class="acf-fields"> |
| 47 | <?php |
| 48 | |
| 49 | if(!empty($choices)){ |
| 50 | |
| 51 | // render |
| 52 | acf_render_field_wrap(array( |
| 53 | 'label' => $this->select, |
| 54 | 'type' => 'checkbox', |
| 55 | 'name' => 'keys', |
| 56 | 'prefix' => false, |
| 57 | 'value' => false, |
| 58 | 'toggle' => true, |
| 59 | 'choices' => $choices, |
| 60 | )); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | else{ |
| 65 | |
| 66 | echo '<div style="padding:15px 12px;">'; |
| 67 | echo $this->messages['not_found']; |
| 68 | echo '</div>'; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | ?> |
| 73 | </div> |
| 74 | |
| 75 | <?php $disabled = empty($choices) ? 'disabled="disabled"' : ''; ?> |
| 76 | |
| 77 | <p class="acf-submit"> |
| 78 | |
| 79 | <?php if(in_array('json', $this->allowed_actions)){ ?> |
| 80 | <button type="submit" name="action" class="button button-primary" value="json" <?php echo $disabled; ?>><?php _e('Export File'); ?></button> |
| 81 | <?php } ?> |
| 82 | |
| 83 | <?php if(in_array('php', $this->allowed_actions)){ ?> |
| 84 | <button type="submit" name="action" class="button" value="php" <?php echo $disabled; ?>><?php _e('Generate PHP'); ?></button> |
| 85 | <?php } ?> |
| 86 | |
| 87 | </p> |
| 88 | <?php |
| 89 | |
| 90 | } |
| 91 | |
| 92 | function html_single(){ |
| 93 | |
| 94 | ?> |
| 95 | <div class="acf-postbox-columns"> |
| 96 | <div class="acf-postbox-main"> |
| 97 | |
| 98 | <p><?php _e("You can copy and paste the following code to your theme's functions.php file or include it within an external file.", 'acf'); ?></p> |
| 99 | |
| 100 | <div id="acf-admin-tool-export"> |
| 101 | <textarea id="acf-export-textarea" readonly="true"><?php $this->instance->export_php($this->data); ?></textarea> |
| 102 | </div> |
| 103 | |
| 104 | <p class="acf-submit"> |
| 105 | <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a> |
| 106 | </p> |
| 107 | |
| 108 | <script type="text/javascript"> |
| 109 | (function($){ |
| 110 | |
| 111 | var $a = $('#acf-export-copy'); |
| 112 | var $textarea = $('#acf-export-textarea'); |
| 113 | |
| 114 | if(!document.queryCommandSupported('copy')){ |
| 115 | return $a.remove(); |
| 116 | } |
| 117 | |
| 118 | $a.on('click', function(e){ |
| 119 | |
| 120 | e.preventDefault(); |
| 121 | |
| 122 | $textarea.get(0).select(); |
| 123 | |
| 124 | try{ |
| 125 | |
| 126 | // copy |
| 127 | var copy = document.execCommand('copy'); |
| 128 | if(!copy) |
| 129 | return; |
| 130 | |
| 131 | // tooltip |
| 132 | acf.newTooltip({ |
| 133 | text: "<?php _e('Copied', 'acf' ); ?>", |
| 134 | timeout: 250, |
| 135 | target: $(this), |
| 136 | }); |
| 137 | |
| 138 | }catch(err){ |
| 139 | // do nothing |
| 140 | } |
| 141 | |
| 142 | }); |
| 143 | |
| 144 | })(jQuery); |
| 145 | </script> |
| 146 | </div> |
| 147 | </div> |
| 148 | <?php |
| 149 | |
| 150 | } |
| 151 | |
| 152 | function load(){ |
| 153 | |
| 154 | if(!$this->is_active()) |
| 155 | return; |
| 156 | |
| 157 | $this->action = $this->get_action(); |
| 158 | $this->data = $this->get_data(); |
| 159 | |
| 160 | // Json |
| 161 | if($this->action === 'json'){ |
| 162 | |
| 163 | $this->submit(); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | // PHP |
| 168 | elseif($this->action === 'php'){ |
| 169 | |
| 170 | // add notice |
| 171 | if(!empty($this->data)){ |
| 172 | |
| 173 | $count = count($this->data); |
| 174 | $text = sprintf(_n($this->messages['success_single'], $this->messages['success_multiple'], $count, 'acf' ), $count); |
| 175 | |
| 176 | acf_add_admin_notice($text, 'success'); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
| 184 | function submit(){ |
| 185 | |
| 186 | $this->action = $this->get_action(); |
| 187 | $this->data = $this->get_data(); |
| 188 | $keys = array_keys($this->data); |
| 189 | |
| 190 | // validate |
| 191 | if(!$this->data){ |
| 192 | return acf_add_admin_notice($this->messages['not_selected'], 'warning'); |
| 193 | } |
| 194 | |
| 195 | // Json |
| 196 | if($this->action === 'json'){ |
| 197 | |
| 198 | // Prefix |
| 199 | $prefix = (count($keys) > 1) ? $this->file : $this->files; |
| 200 | |
| 201 | // Slugs |
| 202 | $slugs = implode('-', $keys); |
| 203 | |
| 204 | // Date |
| 205 | $date = date('Y-m-d'); |
| 206 | |
| 207 | // file |
| 208 | $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json'; |
| 209 | |
| 210 | // headers |
| 211 | header("Content-Description: File Transfer"); |
| 212 | header("Content-Disposition: attachment; filename={$file_name}"); |
| 213 | header("Content-Type: application/json; charset=utf-8"); |
| 214 | |
| 215 | // return |
| 216 | echo acf_json_encode($this->data); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | // PHP |
| 221 | elseif($this->action === 'php'){ |
| 222 | |
| 223 | // url |
| 224 | $url = add_query_arg(array( |
| 225 | 'keys' => implode('+', $keys), |
| 226 | 'action' => 'php' |
| 227 | ), $this->get_url()); |
| 228 | |
| 229 | // redirect |
| 230 | wp_redirect($url); |
| 231 | |
| 232 | } |
| 233 | |
| 234 | exit; |
| 235 | |
| 236 | } |
| 237 | |
| 238 | function get_data(){ |
| 239 | |
| 240 | // vars |
| 241 | $keys = $this->get_keys(); |
| 242 | $data = array(); |
| 243 | |
| 244 | foreach($keys as $key){ |
| 245 | |
| 246 | // export |
| 247 | $args = $this->instance->export_data($key); |
| 248 | |
| 249 | if(!$args) |
| 250 | continue; |
| 251 | |
| 252 | $data[$key] = $args; |
| 253 | |
| 254 | } |
| 255 | |
| 256 | return $data; |
| 257 | |
| 258 | } |
| 259 | |
| 260 | function get_keys(){ |
| 261 | |
| 262 | // vars |
| 263 | $keys_post = acf_maybe_get_POST('keys'); |
| 264 | $keys_get = acf_maybe_get_GET('keys'); |
| 265 | $keys = array(); |
| 266 | |
| 267 | // $_POST |
| 268 | if($keys_post){ |
| 269 | |
| 270 | $keys = (array) $keys_post; |
| 271 | |
| 272 | } |
| 273 | |
| 274 | // $_GET |
| 275 | elseif($keys_get){ |
| 276 | |
| 277 | $keys_get = str_replace(' ', '+', $keys_get); |
| 278 | $keys = explode('+', $keys_get); |
| 279 | |
| 280 | } |
| 281 | |
| 282 | return $keys; |
| 283 | |
| 284 | } |
| 285 | |
| 286 | function get_action(){ |
| 287 | |
| 288 | // vars |
| 289 | $default = $this->default_action; |
| 290 | $action = acfe_maybe_get_REQUEST('action', $default); |
| 291 | |
| 292 | // check allowed |
| 293 | if(!in_array($action, $this->allowed_actions)) |
| 294 | $action = $default; |
| 295 | |
| 296 | // return |
| 297 | return $action; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | |
| 303 | endif; |