dpt-export.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | // Check setting |
| 7 | if(!acf_get_setting('acfe/modules/dynamic_post_types')) |
| 8 | return; |
| 9 | |
| 10 | if(!class_exists('ACFE_Admin_Tool_Export_DPT')): |
| 11 | |
| 12 | class ACFE_Admin_Tool_Export_DPT 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_dpt_export'; |
| 21 | $this->title = __('Export Post Types'); |
| 22 | $this->icon = 'dashicons-upload'; |
| 23 | |
| 24 | } |
| 25 | |
| 26 | function html(){ |
| 27 | |
| 28 | // Single |
| 29 | if($this->is_active()){ |
| 30 | |
| 31 | $this->html_single(); |
| 32 | |
| 33 | |
| 34 | // Archive |
| 35 | }else{ |
| 36 | |
| 37 | $this->html_archive(); |
| 38 | |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | function html_archive(){ |
| 44 | |
| 45 | // vars |
| 46 | $choices = array(); |
| 47 | |
| 48 | $dynamic_post_types = acfe_settings('modules.dynamic_post_type.data'); |
| 49 | |
| 50 | if($dynamic_post_types){ |
| 51 | foreach($dynamic_post_types as $post_type_name => $args){ |
| 52 | |
| 53 | $choices[$post_type_name] = esc_html($args['label']); |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | ?> |
| 59 | <p><?php _e('Export Post Types', '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 Post Types', '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 post type 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 | <button type="submit" name="action" class="button" value="php" <?php echo $disabled; ?>><?php _e('Generate PHP'); ?></button> |
| 101 | </p> |
| 102 | <?php |
| 103 | |
| 104 | } |
| 105 | |
| 106 | function html_single(){ |
| 107 | |
| 108 | ?> |
| 109 | <div class="acf-postbox-columns"> |
| 110 | <div class="acf-postbox-main"> |
| 111 | |
| 112 | <?php |
| 113 | // prevent default translation and fake __() within string |
| 114 | acf_update_setting('l10n_var_export', true); |
| 115 | |
| 116 | $str_replace = array( |
| 117 | " " => "\t", |
| 118 | "'!!__(!!\'" => "__('", |
| 119 | "!!\', !!\'" => "', '", |
| 120 | "!!\')!!'" => "')", |
| 121 | "array (" => "array(" |
| 122 | ); |
| 123 | |
| 124 | $preg_replace = array( |
| 125 | '/([\t\r\n]+?)array/' => 'array', |
| 126 | '/[0-9]+ => array/' => 'array' |
| 127 | ); |
| 128 | |
| 129 | // Get settings. |
| 130 | $l10n = acf_get_setting('l10n'); |
| 131 | $l10n_textdomain = acf_get_setting('l10n_textdomain'); |
| 132 | |
| 133 | ?> |
| 134 | <p><?php _e("The following code can be used to register a post type. Simply copy and paste the following code to your theme's functions.php file or include it within an external file.", 'acf'); ?></p> |
| 135 | |
| 136 | <div id="acf-admin-tool-export"> |
| 137 | |
| 138 | <textarea id="acf-export-textarea" readonly="true"><?php |
| 139 | |
| 140 | foreach($this->data as $post_type => $args){ |
| 141 | |
| 142 | // Translate settings if textdomain is set. |
| 143 | if($l10n && $l10n_textdomain){ |
| 144 | |
| 145 | $args['label'] = acf_translate($args['label']); |
| 146 | $args['description'] = acf_translate($args['description']); |
| 147 | |
| 148 | if(!empty($args['labels'])){ |
| 149 | |
| 150 | foreach($args['labels'] as $key => &$label){ |
| 151 | |
| 152 | $args['labels'][$key] = acf_translate($label); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | // code |
| 161 | $code = var_export($args, true); |
| 162 | |
| 163 | |
| 164 | // change double spaces to tabs |
| 165 | $code = str_replace( array_keys($str_replace), array_values($str_replace), $code ); |
| 166 | |
| 167 | |
| 168 | // correctly formats "=> array(" |
| 169 | $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code ); |
| 170 | |
| 171 | |
| 172 | // esc_textarea |
| 173 | $code = esc_textarea( $code ); |
| 174 | |
| 175 | |
| 176 | // echo |
| 177 | echo "register_post_type('{$post_type}', {$code});" . "\r\n" . "\r\n"; |
| 178 | |
| 179 | } |
| 180 | |
| 181 | ?></textarea> |
| 182 | |
| 183 | </div> |
| 184 | |
| 185 | <p class="acf-submit"> |
| 186 | <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a> |
| 187 | </p> |
| 188 | <script type="text/javascript"> |
| 189 | (function($){ |
| 190 | |
| 191 | // vars |
| 192 | var $a = $('#acf-export-copy'); |
| 193 | var $textarea = $('#acf-export-textarea'); |
| 194 | |
| 195 | |
| 196 | // remove $a if 'copy' is not supported |
| 197 | if( !document.queryCommandSupported('copy') ) { |
| 198 | return $a.remove(); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | // event |
| 203 | $a.on('click', function( e ){ |
| 204 | |
| 205 | // prevent default |
| 206 | e.preventDefault(); |
| 207 | |
| 208 | |
| 209 | // select |
| 210 | $textarea.get(0).select(); |
| 211 | |
| 212 | |
| 213 | // try |
| 214 | try { |
| 215 | |
| 216 | // copy |
| 217 | var copy = document.execCommand('copy'); |
| 218 | if( !copy ) return; |
| 219 | |
| 220 | |
| 221 | // tooltip |
| 222 | acf.newTooltip({ |
| 223 | text: "<?php _e('Copied', 'acf' ); ?>", |
| 224 | timeout: 250, |
| 225 | target: $(this), |
| 226 | }); |
| 227 | |
| 228 | } catch (err) { |
| 229 | |
| 230 | // do nothing |
| 231 | |
| 232 | } |
| 233 | |
| 234 | }); |
| 235 | |
| 236 | })(jQuery); |
| 237 | </script> |
| 238 | </div> |
| 239 | </div> |
| 240 | <?php |
| 241 | |
| 242 | } |
| 243 | |
| 244 | function load(){ |
| 245 | |
| 246 | if($this->is_active()){ |
| 247 | |
| 248 | $this->action = $this->get_action(); |
| 249 | $this->data = $this->get_selected(); |
| 250 | |
| 251 | // Json submit |
| 252 | if($this->action === 'json') |
| 253 | $this->submit(); |
| 254 | |
| 255 | // add notice |
| 256 | if(!empty($this->data)){ |
| 257 | |
| 258 | $count = count($this->data); |
| 259 | $text = sprintf(_n( 'Exported 1 post type.', 'Exported %s post types.', $count, 'acf' ), $count); |
| 260 | |
| 261 | acf_add_admin_notice($text, 'success'); |
| 262 | |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | |
| 267 | } |
| 268 | |
| 269 | function submit(){ |
| 270 | |
| 271 | $this->action = $this->get_action(); |
| 272 | $this->data = $this->get_selected(); |
| 273 | |
| 274 | // validate |
| 275 | if($this->data === false) |
| 276 | return acf_add_admin_notice(__('No post types selected'), 'warning'); |
| 277 | |
| 278 | $keys = array(); |
| 279 | foreach($this->data as $key => $args){ |
| 280 | |
| 281 | $keys[] = $key; |
| 282 | |
| 283 | } |
| 284 | |
| 285 | if($this->action === 'json'){ |
| 286 | |
| 287 | // Prefix |
| 288 | $prefix = (count($keys) > 1) ? 'post-types' : 'post-type'; |
| 289 | |
| 290 | // Slugs |
| 291 | $slugs = implode('-', $keys); |
| 292 | |
| 293 | // Date |
| 294 | $date = date('Y-m-d'); |
| 295 | |
| 296 | // file |
| 297 | $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json'; |
| 298 | |
| 299 | // headers |
| 300 | header("Content-Description: File Transfer"); |
| 301 | header("Content-Disposition: attachment; filename={$file_name}"); |
| 302 | header("Content-Type: application/json; charset=utf-8"); |
| 303 | |
| 304 | // return |
| 305 | echo acf_json_encode($this->data); |
| 306 | die; |
| 307 | |
| 308 | } |
| 309 | |
| 310 | elseif($this->action === 'php'){ |
| 311 | |
| 312 | // url |
| 313 | $url = add_query_arg(array( |
| 314 | 'keys' => implode('+', $keys), |
| 315 | 'action' => 'php' |
| 316 | ), $this->get_url()); |
| 317 | |
| 318 | // redirect |
| 319 | wp_redirect($url); |
| 320 | exit; |
| 321 | |
| 322 | } |
| 323 | |
| 324 | } |
| 325 | |
| 326 | function get_selected(){ |
| 327 | |
| 328 | // vars |
| 329 | $selected = $this->get_selected_keys(); |
| 330 | |
| 331 | if(!$selected) |
| 332 | return false; |
| 333 | |
| 334 | $dynamic_post_types = acfe_settings('modules.dynamic_post_type.data'); |
| 335 | |
| 336 | if(empty($dynamic_post_types)) |
| 337 | return false; |
| 338 | |
| 339 | $data = array(); |
| 340 | |
| 341 | // construct Data |
| 342 | foreach($selected as $key){ |
| 343 | |
| 344 | if(!isset($dynamic_post_types[$key])) |
| 345 | continue; |
| 346 | |
| 347 | // add to data array |
| 348 | $data[$key] = $dynamic_post_types[$key]; |
| 349 | |
| 350 | } |
| 351 | |
| 352 | // return |
| 353 | return $data; |
| 354 | |
| 355 | } |
| 356 | |
| 357 | function get_selected_keys(){ |
| 358 | |
| 359 | // check $_POST |
| 360 | if($keys = acf_maybe_get_POST('keys')){ |
| 361 | |
| 362 | return (array) $keys; |
| 363 | |
| 364 | } |
| 365 | |
| 366 | // check $_GET |
| 367 | if($keys = acf_maybe_get_GET('keys')){ |
| 368 | |
| 369 | $keys = str_replace(' ', '+', $keys); |
| 370 | return explode('+', $keys); |
| 371 | |
| 372 | } |
| 373 | |
| 374 | // return |
| 375 | return false; |
| 376 | |
| 377 | } |
| 378 | |
| 379 | function get_action(){ |
| 380 | |
| 381 | // init |
| 382 | $type = 'json'; |
| 383 | |
| 384 | // check GET / POST |
| 385 | if(($action = acf_maybe_get_GET('action')) || ($action = acf_maybe_get_POST('action'))){ |
| 386 | |
| 387 | if(in_array($action, array('json', 'php'))) |
| 388 | $type = $action; |
| 389 | |
| 390 | } |
| 391 | |
| 392 | // return |
| 393 | return $type; |
| 394 | |
| 395 | } |
| 396 | |
| 397 | } |
| 398 | |
| 399 | acf_register_admin_tool('ACFE_Admin_Tool_Export_DPT'); |
| 400 | |
| 401 | endif; |