PluginProbe
Advanced Custom Fields: Extended / 0.8.6.9
Advanced Custom Fields: Extended v0.8.6.9
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 All 92 releases
acf-extended / includes / admin / tools / dt-export.php
dt-export.php
411 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH'))
4 exit;
5
6 // Check setting
7 if(!acf_get_setting('acfe/modules/dynamic_taxonomies'))
8 return;
9
10 if(!class_exists('ACFE_Admin_Tool_Export_DT')):
11
12 class ACFE_Admin_Tool_Export_DT 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_dt_export';
21 $this->title = __('Export Taxonomies');
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_taxonomies = acfe_settings('modules.dynamic_taxonomy.data');
49
50 if($dynamic_taxonomies){
51 foreach($dynamic_taxonomies as $taxonomy_name => $args){
52
53 $choices[$taxonomy_name] = esc_html($args['label']);
54
55 }
56 }
57
58 ?>
59 <p><?php _e('Export Taxonomies', '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 Taxonomies', '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 taxonomy 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 taxonomy. 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 $taxonomy => $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 $post_types = array();
161 if(acf_maybe_get($args, 'post_types')){
162
163 $post_types = $args['post_types'];
164
165 }
166
167 $post_types = var_export($post_types, true);
168 $post_types = str_replace( array_keys($str_replace), array_values($str_replace), $post_types );
169 $post_types = preg_replace( array_keys($preg_replace), array_values($preg_replace), $post_types );
170
171
172 // code
173 $code = var_export($args, true);
174
175
176 // change double spaces to tabs
177 $code = str_replace( array_keys($str_replace), array_values($str_replace), $code );
178
179
180 // correctly formats "=> array("
181 $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code );
182
183
184 // esc_textarea
185 $code = esc_textarea( $code );
186
187 // echo
188 echo "register_taxonomy('{$taxonomy}', {$post_types}, {$code});" . "\r\n" . "\r\n";
189
190 }
191
192 ?></textarea>
193
194 </div>
195
196 <p class="acf-submit">
197 <a class="button" id="acf-export-copy"><?php _e( 'Copy to clipboard', 'acf' ); ?></a>
198 </p>
199 <script type="text/javascript">
200 (function($){
201
202 // vars
203 var $a = $('#acf-export-copy');
204 var $textarea = $('#acf-export-textarea');
205
206
207 // remove $a if 'copy' is not supported
208 if( !document.queryCommandSupported('copy') ) {
209 return $a.remove();
210 }
211
212
213 // event
214 $a.on('click', function( e ){
215
216 // prevent default
217 e.preventDefault();
218
219
220 // select
221 $textarea.get(0).select();
222
223
224 // try
225 try {
226
227 // copy
228 var copy = document.execCommand('copy');
229 if( !copy ) return;
230
231
232 // tooltip
233 acf.newTooltip({
234 text: "<?php _e('Copied', 'acf' ); ?>",
235 timeout: 250,
236 target: $(this),
237 });
238
239 } catch (err) {
240
241 // do nothing
242
243 }
244
245 });
246
247 })(jQuery);
248 </script>
249 </div>
250 </div>
251 <?php
252
253 }
254
255 function load(){
256
257 if($this->is_active()){
258
259 $this->action = $this->get_action();
260 $this->data = $this->get_selected();
261
262 // Json submit
263 if($this->action === 'json')
264 $this->submit();
265
266 // add notice
267 if(!empty($this->data)){
268
269 $count = count($this->data);
270 $text = sprintf(_n( 'Exported 1 taxonomy.', 'Exported %s taxonomies.', $count, 'acf' ), $count);
271
272 acf_add_admin_notice($text, 'success');
273
274 }
275
276 }
277
278 }
279
280 function submit(){
281
282 $this->action = $this->get_action();
283 $this->data = $this->get_selected();
284
285 // validate
286 if($this->data === false)
287 return acf_add_admin_notice(__('No taxonomies selected'), 'warning');
288
289 $keys = array();
290 foreach($this->data as $key => $args){
291
292 $keys[] = $key;
293
294 }
295
296 if($this->action === 'json'){
297
298 // Prefix
299 $prefix = (count($keys) > 1) ? 'taxonomies' : 'taxonomy';
300
301 // Slugs
302 $slugs = implode('-', $keys);
303
304 // Date
305 $date = date('Y-m-d');
306
307 // file
308 $file_name = 'acfe-export-' . $prefix . '-' . $slugs . '-' . $date . '.json';
309
310 // headers
311 header("Content-Description: File Transfer");
312 header("Content-Disposition: attachment; filename={$file_name}");
313 header("Content-Type: application/json; charset=utf-8");
314
315 // return
316 echo acf_json_encode($this->data);
317 die;
318
319 }
320
321 elseif($this->action === 'php'){
322
323 // url
324 $url = add_query_arg(array(
325 'keys' => implode('+', $keys),
326 'action' => 'php'
327 ), $this->get_url());
328
329 // redirect
330 wp_redirect($url);
331 exit;
332
333 }
334
335 }
336
337 function get_selected(){
338
339 // vars
340 $selected = $this->get_selected_keys();
341 $json = array();
342
343 if(!$selected)
344 return false;
345
346 $dynamic_taxonomies = acfe_settings('modules.dynamic_taxonomy.data');
347
348 if(empty($dynamic_taxonomies))
349 return false;
350
351 // construct JSON
352 foreach($selected as $key){
353
354 if(!isset($dynamic_taxonomies[$key]))
355 continue;
356
357 // add to json array
358 $json[$key] = $dynamic_taxonomies[$key];
359
360 }
361
362 // return
363 return $json;
364
365 }
366
367 function get_selected_keys(){
368
369 // check $_POST
370 if($keys = acf_maybe_get_POST('keys')){
371
372 return (array) $keys;
373
374 }
375
376 // check $_GET
377 if($keys = acf_maybe_get_GET('keys')){
378
379 $keys = str_replace(' ', '+', $keys);
380 return explode('+', $keys);
381
382 }
383
384 // return
385 return false;
386
387 }
388
389 function get_action(){
390
391 // init
392 $type = 'json';
393
394 // check GET / POST
395 if(($action = acf_maybe_get_GET('action')) || ($action = acf_maybe_get_POST('action'))){
396
397 if(in_array($action, array('json', 'php')))
398 $type = $action;
399
400 }
401
402 // return
403 return $type;
404
405 }
406
407 }
408
409 acf_register_admin_tool('ACFE_Admin_Tool_Export_DT');
410
411 endif;