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